2019-07-24 23:37:10 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.fenix.library.history
|
|
|
|
|
|
|
|
import androidx.lifecycle.LiveData
|
2019-07-31 02:16:31 +02:00
|
|
|
import androidx.lifecycle.MutableLiveData
|
2019-07-24 23:37:10 +02:00
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
|
import androidx.paging.LivePagedListBuilder
|
2020-02-06 17:14:32 +01:00
|
|
|
import androidx.paging.PagedList
|
2019-07-24 23:37:10 +02:00
|
|
|
import org.mozilla.fenix.components.history.PagedHistoryProvider
|
|
|
|
|
|
|
|
class HistoryViewModel(historyProvider: PagedHistoryProvider) : ViewModel() {
|
|
|
|
var history: LiveData<PagedList<HistoryItem>>
|
2019-07-31 02:16:31 +02:00
|
|
|
var userHasHistory = MutableLiveData(true)
|
2019-07-24 23:37:10 +02:00
|
|
|
private val datasource: LiveData<HistoryDataSource>
|
|
|
|
|
|
|
|
init {
|
|
|
|
val historyDataSourceFactory = HistoryDataSourceFactory(historyProvider)
|
2019-07-31 02:16:31 +02:00
|
|
|
datasource = historyDataSourceFactory.datasource
|
2019-07-24 23:37:10 +02:00
|
|
|
|
2019-07-31 02:16:31 +02:00
|
|
|
history = LivePagedListBuilder(historyDataSourceFactory, PAGE_SIZE)
|
|
|
|
.setBoundaryCallback(object : PagedList.BoundaryCallback<HistoryItem>() {
|
|
|
|
override fun onZeroItemsLoaded() {
|
|
|
|
userHasHistory.value = false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.build()
|
2019-07-24 23:37:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun invalidate() {
|
|
|
|
datasource.value?.invalidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val PAGE_SIZE = 25
|
|
|
|
}
|
|
|
|
}
|