Skip to content

Commit

Permalink
Revert "Invalidate custom history data source when data changes. God …
Browse files Browse the repository at this point in the history
…wish I use room or sqldelight soon"

This reverts commit f0ed22f.
  • Loading branch information
arunkumar9t2 committed Sep 14, 2019
1 parent 864f5b5 commit 110a050
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,14 @@ import androidx.paging.DataSource
import androidx.paging.PositionalDataSource
import arun.com.chromer.data.history.HistoryStore
import arun.com.chromer.data.website.model.Website
import dev.arunkumar.android.rxschedulers.SchedulerProvider
import io.reactivex.disposables.CompositeDisposable
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Provider
import javax.inject.Singleton

@Singleton
class PagedHistoryDataSource
@Inject
constructor(
private val historyStore: HistoryStore,
private val schedulerProvider: SchedulerProvider
) : PositionalDataSource<Website>() {

private val compositeDisposable = CompositeDisposable()

init {
addInvalidatedCallback { compositeDisposable.clear() }
compositeDisposable.add(
historyStore.changes()
.debounce(200, TimeUnit.MILLISECONDS)
.subscribe { invalidate() }
)
}

constructor(private val historyStore: HistoryStore) : PositionalDataSource<Website>() {

override fun loadRange(
params: LoadRangeParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class HistoryFragment : BaseFragment(), Snackable, FabHandler {
loadingLiveData.observe(owner, Observer { loading(it!!) })
historyPagedListLiveData.observe(owner, Observer { historyAdapter.submitList(it) })
}
loadHistory()
}

private fun setupIncognitoSwitch() {
Expand Down Expand Up @@ -196,10 +197,7 @@ class HistoryFragment : BaseFragment(), Snackable, FabHandler {

swipeRefreshLayout.apply {
setColorSchemeColors(ContextCompat.getColor(context!!, R.color.colorPrimary), ContextCompat.getColor(context!!, R.color.accent))
setOnRefreshListener {
viewModel.invalidate()
isRefreshing = false
}
setOnRefreshListener { loadHistory() }
}

ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, LEFT or RIGHT) {
Expand All @@ -216,10 +214,15 @@ class HistoryFragment : BaseFragment(), Snackable, FabHandler {
}).attachToRecyclerView(historyList)
}

private fun loadHistory() {
viewModel.loadHistory()
}

override fun inject(fragmentComponent: FragmentComponent) = fragmentComponent.inject(this)

override fun onResume() {
super.onResume()
loadHistory()
historySwitch.isChecked = !preferences.historyDisabled()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import arun.com.chromer.data.history.HistoryRepository
import arun.com.chromer.data.website.model.Website
import arun.com.chromer.util.SchedulerProvider
import rx.Observable
import rx.subjects.PublishSubject
import rx.subscriptions.CompositeSubscription
import timber.log.Timber
import javax.inject.Inject
Expand All @@ -41,28 +42,42 @@ constructor(
val loadingLiveData = MutableLiveData<Boolean>()
var historyPagedListLiveData = historyRepository.pagedHistory()

private val loaderSubject: PublishSubject<Int> = PublishSubject.create()
val subs = CompositeSubscription()

fun invalidate() {
historyPagedListLiveData.value?.dataSource?.invalidate()
init {
/* subs.add(loaderSubject
.doOnNext { loadingLiveData.postValue(true) }
.switchMap {
historyRepository
.allItemsCursor
.compose(SchedulerProvider.applyIoSchedulers())
}.doOnNext { loadingLiveData.postValue(false) }
.doOnNext(historyCursorLiveData::postValue)
.subscribe())*/
}

fun loadHistory() {
loaderSubject.onNext(0)
}

fun deleteAll(onSuccess: (rows: Int) -> Unit) {
subs.add(historyRepository
.deleteAll()
.compose(SchedulerProvider.applyIoSchedulers())
.doOnNext { rows ->
invalidate()
loadHistory()
onSuccess(rows)
}.subscribe())
}

fun deleteHistory(website: Website?) {
subs.add(Observable.just(website)
.filter { webSite -> webSite?.url != null }
.flatMap { historyRepository.delete(it!!) }
.flatMap<Website> { historyRepository.delete(it!!) }
.compose(SchedulerProvider.applyIoSchedulers())
.doOnError(Timber::e)
.doOnNext { loadHistory() }
.subscribe())
}

Expand Down

0 comments on commit 110a050

Please sign in to comment.