From c5751cb2afdd8d29d152f4ddc6375fe2056cde5d Mon Sep 17 00:00:00 2001 From: Mazen Rashed Date: Wed, 18 Mar 2020 13:21:56 +0200 Subject: [PATCH] Preparing to release 1.0 --- .../rxpaginationlib/DataChanges.kt | 8 ---- .../PaginationListManagerImpl.kt | 16 +++----- .../rxpaginationlib/RxPagination.kt | 11 +----- .../repository/GithubRepositoryRepository.kt | 1 - .../ui/GithubRepositoriesViewModel.kt | 4 +- .../rxpagination/ui/MainActivity.kt | 38 +++---------------- app/src/main/res/layout/activity_main.xml | 12 ++++++ 7 files changed, 27 insertions(+), 63 deletions(-) delete mode 100644 RxPaginationLib/src/main/java/com/mazenrashed/rxpaginationlib/DataChanges.kt diff --git a/RxPaginationLib/src/main/java/com/mazenrashed/rxpaginationlib/DataChanges.kt b/RxPaginationLib/src/main/java/com/mazenrashed/rxpaginationlib/DataChanges.kt deleted file mode 100644 index ca3d24b..0000000 --- a/RxPaginationLib/src/main/java/com/mazenrashed/rxpaginationlib/DataChanges.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.mazenrashed.rxpaginationlib - - -data class DataChanges( - var list: ArrayList, - var effectedItem: Int, - var transactionType: RxPagination.TransactionTypes -) \ No newline at end of file diff --git a/RxPaginationLib/src/main/java/com/mazenrashed/rxpaginationlib/PaginationListManagerImpl.kt b/RxPaginationLib/src/main/java/com/mazenrashed/rxpaginationlib/PaginationListManagerImpl.kt index caa16d2..94b1e47 100644 --- a/RxPaginationLib/src/main/java/com/mazenrashed/rxpaginationlib/PaginationListManagerImpl.kt +++ b/RxPaginationLib/src/main/java/com/mazenrashed/rxpaginationlib/PaginationListManagerImpl.kt @@ -4,7 +4,7 @@ import com.jakewharton.rxrelay2.BehaviorRelay import com.mazenrashed.rxpaginationlib.RxPagination.Companion.ALL_ITEMS_EFFECTED class PaginationListManagerImpl( - private val dataChanges: BehaviorRelay>, + private val dataList: BehaviorRelay>, private var page: BehaviorRelay, private val pageSize : Int, private var isLastPage: BehaviorRelay @@ -13,24 +13,20 @@ class PaginationListManagerImpl( override fun addToList(newMembers: ArrayList) { if (isLastPage.value == true) return - dataChanges.accept( - dataChanges.value.apply { + dataList.accept( + dataList.value.apply { if (page.value == 0) - this?.list?.clear() + this?.clear() if (newMembers.isEmpty()) { isLastPage.accept(true) return@apply } - this?.list?.addAll(newMembers.apply { + this?.addAll(newMembers.apply { if (this.size < pageSize) isLastPage.accept(true) }) - } ?: DataChanges( - ArrayList(), - ALL_ITEMS_EFFECTED, - RxPagination.TransactionTypes.REPLACE_ALL - ) + } ?: ArrayList() ) page.accept(page.value?.plus(1) ?: 0) } diff --git a/RxPaginationLib/src/main/java/com/mazenrashed/rxpaginationlib/RxPagination.kt b/RxPaginationLib/src/main/java/com/mazenrashed/rxpaginationlib/RxPagination.kt index 8ca96f4..ed4af62 100644 --- a/RxPaginationLib/src/main/java/com/mazenrashed/rxpaginationlib/RxPagination.kt +++ b/RxPaginationLib/src/main/java/com/mazenrashed/rxpaginationlib/RxPagination.kt @@ -16,14 +16,7 @@ abstract class RxPagination( private val bag = CompositeDisposable() - val dataList = BehaviorRelay.createDefault( - DataChanges( - ArrayList(), - ALL_ITEMS_EFFECTED, - TransactionTypes.REPLACE_ALL - ) - ) - + val dataList = BehaviorRelay.createDefault(ArrayList()) var page = BehaviorRelay.createDefault(firstPage) var isLastPage = BehaviorRelay.createDefault(false) @@ -49,7 +42,7 @@ abstract class RxPagination( abstract fun onFetchDataListSubscribed() - abstract fun onFetchDataListSuccess(dataList: ArrayList) + abstract fun onFetchDataListSuccess(lastLoadedList: ArrayList) abstract fun onFetchDataListError(throwable: Throwable) diff --git a/app/src/main/java/com/mazenrashed/rxpagination/data/repository/GithubRepositoryRepository.kt b/app/src/main/java/com/mazenrashed/rxpagination/data/repository/GithubRepositoryRepository.kt index a40b669..1156440 100644 --- a/app/src/main/java/com/mazenrashed/rxpagination/data/repository/GithubRepositoryRepository.kt +++ b/app/src/main/java/com/mazenrashed/rxpagination/data/repository/GithubRepositoryRepository.kt @@ -14,5 +14,4 @@ class GithubRepositoryRepository : ListRepository { return endPoints.getRepositories(pageSize, page) } - } \ No newline at end of file diff --git a/app/src/main/java/com/mazenrashed/rxpagination/ui/GithubRepositoriesViewModel.kt b/app/src/main/java/com/mazenrashed/rxpagination/ui/GithubRepositoriesViewModel.kt index 2db0520..78d4969 100644 --- a/app/src/main/java/com/mazenrashed/rxpagination/ui/GithubRepositoriesViewModel.kt +++ b/app/src/main/java/com/mazenrashed/rxpagination/ui/GithubRepositoriesViewModel.kt @@ -10,7 +10,6 @@ class GithubRepositoriesViewModel : val isLoading = BehaviorRelay.createDefault(false) - init { loadDataList() } @@ -21,9 +20,10 @@ class GithubRepositoriesViewModel : override fun onFetchDataListError(throwable: Throwable) { isLoading.accept(false) + throwable.printStackTrace() } - override fun onFetchDataListSuccess(dataList: ArrayList) { + override fun onFetchDataListSuccess(lastLoadedList: ArrayList) { isLoading.accept(false) } diff --git a/app/src/main/java/com/mazenrashed/rxpagination/ui/MainActivity.kt b/app/src/main/java/com/mazenrashed/rxpagination/ui/MainActivity.kt index 1bdeed4..fdd528d 100644 --- a/app/src/main/java/com/mazenrashed/rxpagination/ui/MainActivity.kt +++ b/app/src/main/java/com/mazenrashed/rxpagination/ui/MainActivity.kt @@ -1,13 +1,11 @@ package com.mazenrashed.rxpagination.ui import android.os.Bundle -import android.util.Log +import android.view.View import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity -import com.google.gson.Gson import com.mazenrashed.rxpagination.R import com.mazenrashed.rxpagination.data.model.GithubRepository -import com.mazenrashed.rxpaginationlib.RxPagination import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import io.reactivex.rxkotlin.addTo @@ -35,41 +33,15 @@ class MainActivity : AppCompatActivity() { .observeOn(AndroidSchedulers.mainThread()) .subscribe { refresh_layout.isRefreshing = it + progress_bar.visibility = if (it) View.VISIBLE else View.GONE }.addTo(bag) -// Observable.interval(7, TimeUnit.SECONDS) -// .subscribe({ -// if (viewModel.isLastPage.value == false) -// viewModel.loadDataList() -// }, { -// it.printStackTrace() -// }) -// .addTo(bag) viewModel .dataList .observeOn(AndroidSchedulers.mainThread()) - .subscribe { - it.effectedItem.let { - Log.d( - "Effected item", - if (it == RxPagination.ALL_ITEMS_EFFECTED) "All Items" else it.toString() - ) - } - - Log.d( - "Transaction type", - when (it.transactionType) { - RxPagination.TransactionTypes.ADD -> "Add" - RxPagination.TransactionTypes.DELETE -> "Delete" - RxPagination.TransactionTypes.MODIFY -> "Modify" - RxPagination.TransactionTypes.REPLACE_ALL -> "Replace All" - } - ) - - Log.d("list", Gson().toJson(it.list.map { it.name })) - - dataList.clear() - dataList.addAll(it.list) + .subscribe { dataList -> + this.dataList.clear() + this.dataList.addAll(dataList) adapter.notifyDataSetChanged() } .addTo(bag) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c9ccd07..3b4e687 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,6 +6,17 @@ android:layout_height="match_parent" tools:context=".ui.MainActivity"> + + + \ No newline at end of file