Skip to content

Commit

Permalink
add paging
Browse files Browse the repository at this point in the history
  • Loading branch information
dlwls5201 committed Jul 12, 2020
1 parent 5b4e941 commit 329efb7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.studyfork.sfoide.presentation.main

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.studyfork.sfoide.R
import com.studyfork.sfoide.base.BaseActivity
Expand Down Expand Up @@ -29,16 +31,45 @@ class MainActivity : BaseActivity<ActivityMainBinding>(R.layout.activity_main),
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding.model = mainViewModel
binding.rvRandomUser.adapter = userAdapter
binding.srlMainActivity.setOnRefreshListener(this)

initRecyclerView()

mainViewModel.randomUsers.observe(this, Observer {
userAdapter.replaceAll(it)
})

mainViewModel.addRandomUsers.observe(this, Observer {
userAdapter.addItems(it)
})

mainViewModel.loadRandomUsers()
}

private fun initRecyclerView() {
with(binding.rvRandomUser) {
adapter = userAdapter

addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)

if (dy > 0) {
val lm = recyclerView.layoutManager
if (lm is LinearLayoutManager) {
if (lm.findLastCompletelyVisibleItemPosition() ==
recyclerView.adapter?.itemCount?.minus(1)
) {
mainViewModel.addRandomUsers()
}
}
}

}
})
}
}

override fun onRefresh() {
mainViewModel.loadRandomUsers(true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class MainViewModel(
private val _randomUsers = MutableLiveData<List<UserItem>>()
val randomUsers: LiveData<List<UserItem>> get() = _randomUsers

private val _addRandomUsers = MutableLiveData<List<UserItem>>()
val addRandomUsers: LiveData<List<UserItem>> get() = _addRandomUsers

private val _isLoading = MutableLiveData<Boolean>()
val isLoading: LiveData<Boolean> get() = _isLoading

Expand Down Expand Up @@ -41,6 +44,32 @@ class MainViewModel(
}

private var page = 1
private var isAdding = false

fun addRandomUsers() {
if (isAdding) {
return
}

page++
isAdding = true

compositeDisposable.add(
userRepository.getRandomUsers(page)
.doOnSubscribe {
showLoading()
}
.doOnTerminate {
hideLoadingAndRefresh()
}
.subscribe({
isAdding = false
_addRandomUsers.postValue(it)
}) {
Dlog.e(it.message)
}
)
}

private fun showLoading() {
_isLoading.value = true
Expand All @@ -50,5 +79,4 @@ class MainViewModel(
_isLoading.value = false
_isRefresh.value = false
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ class RandomUserAdapter : RecyclerView.Adapter<BaseViewHolder<ViewDataBinding, U
notifyDataSetChanged()
}

fun addItems(items: List<UserItem>) {
val preItemSize = this.items.size

this.items.run {
addAll(items)
}

notifyItemRangeChanged(preItemSize - 1, this.items.size)
}

class SampleViewHolder(parent: ViewGroup) :
BaseViewHolder<ItemUserBinding, UserItem>(parent, R.layout.item_user) {

Expand Down

0 comments on commit 329efb7

Please # to comment.