Skip to content

Commit ca29cad

Browse files
committedAug 22, 2017
Initial project
1 parent 3195aed commit ca29cad

File tree

3 files changed

+120
-14
lines changed

3 files changed

+120
-14
lines changed
 

‎app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ dependencies {
4141
implementation 'com.facebook.stetho:stetho:1.5.0'
4242
implementation 'com.android.support:appcompat-v7:26.0.1'
4343
implementation 'com.android.support:recyclerview-v7:26.0.1'
44+
implementation 'com.android.support:design:26.0.1'
4445

4546
kapt "android.arch.persistence.room:compiler:1.0.0-alpha9"
4647
kapt 'com.google.dagger:dagger-compiler:2.11'
Lines changed: 93 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,108 @@
11
package com.example.tamaskozmer.kotlinrxexample.view.activities
22

33
import android.os.Bundle
4+
import android.support.design.widget.Snackbar
45
import android.support.v7.app.AppCompatActivity
6+
import android.support.v7.widget.LinearLayoutManager
7+
import android.support.v7.widget.RecyclerView
8+
import android.view.View
9+
import android.widget.Toast
510
import com.example.tamaskozmer.kotlinrxexample.R
6-
import com.example.tamaskozmer.kotlinrxexample.view.fragments.UserListFragment
11+
import com.example.tamaskozmer.kotlinrxexample.di.modules.UserListFragmentModule
12+
import com.example.tamaskozmer.kotlinrxexample.presentation.presenters.UserListPresenter
13+
import com.example.tamaskozmer.kotlinrxexample.presentation.view.UserListView
14+
import com.example.tamaskozmer.kotlinrxexample.presentation.view.viewmodels.UserViewModel
15+
import com.example.tamaskozmer.kotlinrxexample.util.customApplication
16+
import com.example.tamaskozmer.kotlinrxexample.view.adapters.UserListAdapter
17+
import kotlinx.android.synthetic.main.activity_main.*
718

8-
class MainActivity : AppCompatActivity() {
19+
class MainActivity : AppCompatActivity(), UserListView {
20+
21+
private val presenter: UserListPresenter by lazy { component.presenter() }
22+
private val component by lazy { customApplication.component.plus(UserListFragmentModule()) }
23+
private val adapter by lazy {
24+
val userList = mutableListOf<UserViewModel>()
25+
UserListAdapter(userList) {
26+
user -> showUserClickedSnackbar(user)
27+
}
28+
}
29+
30+
private lateinit var layoutManager: LinearLayoutManager
931

1032
override fun onCreate(savedInstanceState: Bundle?) {
1133
super.onCreate(savedInstanceState)
1234
setContentView(R.layout.activity_main)
1335

14-
addUserListFragment()
36+
initViews()
37+
initAdapter()
38+
39+
presenter.attachView(this)
40+
41+
showLoading()
42+
presenter.getUsers()
43+
}
44+
45+
override fun onDestroy() {
46+
presenter.detachView()
47+
super.onDestroy()
48+
}
49+
50+
private fun initViews() {
51+
swipeRefreshLayout.setOnRefreshListener {
52+
presenter.getUsers(forced = true)
53+
}
54+
}
55+
56+
// region View interface methods
57+
override fun showLoading() {
58+
swipeRefreshLayout.isRefreshing = true
59+
}
60+
61+
override fun hideLoading() {
62+
swipeRefreshLayout.isRefreshing = false
63+
}
64+
65+
override fun addUsersToList(users: List<UserViewModel>) {
66+
val adapter = recyclerView.adapter as UserListAdapter
67+
adapter.addUsers(users)
68+
}
69+
70+
override fun showEmptyListError() {
71+
errorView.visibility = View.VISIBLE
72+
}
73+
74+
override fun hideEmptyListError() {
75+
errorView.visibility = View.GONE
76+
}
77+
78+
override fun showToastError() {
79+
Toast.makeText(this, "Error loading data", Toast.LENGTH_SHORT).show()
80+
}
81+
82+
override fun clearList() {
83+
adapter.clearUsers()
84+
}
85+
// endregion
86+
87+
private fun initAdapter() {
88+
layoutManager = LinearLayoutManager(customApplication)
89+
recyclerView.layoutManager = layoutManager
90+
91+
recyclerView.adapter = adapter
92+
93+
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
94+
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
95+
96+
val lastVisibleItemPosition = layoutManager.findFirstVisibleItemPosition() + layoutManager.childCount
97+
val totalItemCount = layoutManager.itemCount
98+
99+
presenter.onScrollChanged(lastVisibleItemPosition, totalItemCount)
100+
}
101+
})
15102
}
16103

17-
private fun addUserListFragment() {
18-
supportFragmentManager
19-
.beginTransaction()
20-
.replace(R.id.fragmentContainer, UserListFragment())
21-
.commit()
104+
private fun showUserClickedSnackbar(user: UserViewModel) {
105+
Snackbar.make(recyclerView, "${user.displayName}: ${user.reputation} pts", Snackbar.LENGTH_SHORT)
106+
.show()
22107
}
23108
}
Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
34
android:layout_width="match_parent"
45
android:layout_height="match_parent"
5-
android:orientation="vertical">
6+
tools:context="com.example.tamaskozmer.kotlinrxexample.view.activities.MainActivity">
67

7-
<FrameLayout
8-
android:id="@+id/fragmentContainer"
8+
<TextView
9+
android:id="@+id/errorView"
10+
android:layout_width="wrap_content"
11+
android:layout_height="wrap_content"
12+
android:layout_gravity="center"
13+
android:drawablePadding="16dp"
14+
android:drawableTop="@drawable/ic_error"
15+
android:padding="16dp"
16+
android:text="@string/user_list_error"
17+
android:visibility="gone"/>
18+
19+
<android.support.v4.widget.SwipeRefreshLayout
20+
android:id="@+id/swipeRefreshLayout"
921
android:layout_width="match_parent"
10-
android:layout_height="match_parent" />
22+
android:layout_height="match_parent">
23+
24+
<android.support.v7.widget.RecyclerView
25+
android:id="@+id/recyclerView"
26+
android:layout_width="match_parent"
27+
android:layout_height="match_parent"
28+
android:scrollbars="vertical" />
29+
30+
</android.support.v4.widget.SwipeRefreshLayout>
1131

12-
</LinearLayout>
32+
</FrameLayout>

0 commit comments

Comments
 (0)
Please sign in to comment.