|
1 | 1 | package com.example.tamaskozmer.kotlinrxexample.view.activities
|
2 | 2 |
|
3 | 3 | import android.os.Bundle
|
| 4 | +import android.support.design.widget.Snackbar |
4 | 5 | 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 |
5 | 10 | 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.* |
7 | 18 |
|
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 |
9 | 31 |
|
10 | 32 | override fun onCreate(savedInstanceState: Bundle?) {
|
11 | 33 | super.onCreate(savedInstanceState)
|
12 | 34 | setContentView(R.layout.activity_main)
|
13 | 35 |
|
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 | + }) |
15 | 102 | }
|
16 | 103 |
|
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() |
22 | 107 | }
|
23 | 108 | }
|
0 commit comments