Skip to content

Commit

Permalink
Preparing to release 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Mazen Rashed committed Mar 18, 2020
1 parent 5de74d1 commit c5751cb
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 63 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.jakewharton.rxrelay2.BehaviorRelay
import com.mazenrashed.rxpaginationlib.RxPagination.Companion.ALL_ITEMS_EFFECTED

class PaginationListManagerImpl<T>(
private val dataChanges: BehaviorRelay<DataChanges<T>>,
private val dataList: BehaviorRelay<ArrayList<T>>,
private var page: BehaviorRelay<Int>,
private val pageSize : Int,
private var isLastPage: BehaviorRelay<Boolean>
Expand All @@ -13,24 +13,20 @@ class PaginationListManagerImpl<T>(

override fun addToList(newMembers: ArrayList<T>) {
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@ abstract class RxPagination<T>(

private val bag = CompositeDisposable()

val dataList = BehaviorRelay.createDefault(
DataChanges<T>(
ArrayList(),
ALL_ITEMS_EFFECTED,
TransactionTypes.REPLACE_ALL
)
)

val dataList = BehaviorRelay.createDefault(ArrayList<T>())
var page = BehaviorRelay.createDefault(firstPage)
var isLastPage = BehaviorRelay.createDefault(false)

Expand All @@ -49,7 +42,7 @@ abstract class RxPagination<T>(

abstract fun onFetchDataListSubscribed()

abstract fun onFetchDataListSuccess(dataList: ArrayList<T>)
abstract fun onFetchDataListSuccess(lastLoadedList: ArrayList<T>)

abstract fun onFetchDataListError(throwable: Throwable)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ class GithubRepositoryRepository : ListRepository<GithubRepository> {
return endPoints.getRepositories(pageSize, page)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class GithubRepositoriesViewModel :

val isLoading = BehaviorRelay.createDefault(false)


init {
loadDataList()
}
Expand All @@ -21,9 +20,10 @@ class GithubRepositoriesViewModel :

override fun onFetchDataListError(throwable: Throwable) {
isLoading.accept(false)
throwable.printStackTrace()
}

override fun onFetchDataListSuccess(dataList: ArrayList<GithubRepository>) {
override fun onFetchDataListSuccess(lastLoadedList: ArrayList<GithubRepository>) {
isLoading.accept(false)
}

Expand Down
38 changes: 5 additions & 33 deletions app/src/main/java/com/mazenrashed/rxpagination/ui/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
android:layout_height="match_parent"
tools:context=".ui.MainActivity">

<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:elevation="2dp"/>

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/refresh_layout"
android:layout_width="0dp"
Expand All @@ -21,6 +32,7 @@
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="4"
tools:listitem="@layout/repo_item"/>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit c5751cb

Please sign in to comment.