From 329efb70d7b4fe7f949fd2342eb3a5e13509e70c Mon Sep 17 00:00:00 2001 From: dlwls5201 Date: Sun, 12 Jul 2020 17:38:41 +0900 Subject: [PATCH] add paging --- .../sfoide/presentation/main/MainActivity.kt | 33 ++++++++++++++++++- .../sfoide/presentation/main/MainViewModel.kt | 30 ++++++++++++++++- .../presentation/main/RandomUserAdapter.kt | 10 ++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/studyfork/sfoide/presentation/main/MainActivity.kt b/app/src/main/java/com/studyfork/sfoide/presentation/main/MainActivity.kt index 4c2e812..d46217a 100644 --- a/app/src/main/java/com/studyfork/sfoide/presentation/main/MainActivity.kt +++ b/app/src/main/java/com/studyfork/sfoide/presentation/main/MainActivity.kt @@ -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 @@ -29,16 +31,45 @@ class MainActivity : BaseActivity(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) } diff --git a/app/src/main/java/com/studyfork/sfoide/presentation/main/MainViewModel.kt b/app/src/main/java/com/studyfork/sfoide/presentation/main/MainViewModel.kt index 9762da7..1aba183 100644 --- a/app/src/main/java/com/studyfork/sfoide/presentation/main/MainViewModel.kt +++ b/app/src/main/java/com/studyfork/sfoide/presentation/main/MainViewModel.kt @@ -14,6 +14,9 @@ class MainViewModel( private val _randomUsers = MutableLiveData>() val randomUsers: LiveData> get() = _randomUsers + private val _addRandomUsers = MutableLiveData>() + val addRandomUsers: LiveData> get() = _addRandomUsers + private val _isLoading = MutableLiveData() val isLoading: LiveData get() = _isLoading @@ -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 @@ -50,5 +79,4 @@ class MainViewModel( _isLoading.value = false _isRefresh.value = false } - } \ No newline at end of file diff --git a/app/src/main/java/com/studyfork/sfoide/presentation/main/RandomUserAdapter.kt b/app/src/main/java/com/studyfork/sfoide/presentation/main/RandomUserAdapter.kt index 82f8342..becd509 100644 --- a/app/src/main/java/com/studyfork/sfoide/presentation/main/RandomUserAdapter.kt +++ b/app/src/main/java/com/studyfork/sfoide/presentation/main/RandomUserAdapter.kt @@ -48,6 +48,16 @@ class RandomUserAdapter : RecyclerView.Adapter) { + val preItemSize = this.items.size + + this.items.run { + addAll(items) + } + + notifyItemRangeChanged(preItemSize - 1, this.items.size) + } + class SampleViewHolder(parent: ViewGroup) : BaseViewHolder(parent, R.layout.item_user) {