Skip to content

Instantly share code, notes, and snippets.

@brainail
Created September 17, 2017 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainail/8f61d99934664faf8ac2afef76b0ff07 to your computer and use it in GitHub Desktop.
Save brainail/8f61d99934664faf8ac2afef76b0ff07 to your computer and use it in GitHub Desktop.
Our ViewModel for Kitsu Screen
class KitsuViewModel(app: Application) : AndroidViewModel(app) {
private var allKitsuLiveData: LiveData<PagedList<KitsuItem>>? = null
val allKitsu: LiveData<PagedList<KitsuItem>>
get() {
if (null == allKitsuLiveData) {
allKitsuLiveData = KitsuMediaPagedListProvider.allKitsu().create(0,
PagedList.Config.Builder()
.setPageSize(PAGED_LIST_PAGE_SIZE)
.setInitialLoadSizeHint(PAGED_LIST_PAGE_SIZE)
.setEnablePlaceholders(PAGED_LIST_ENABLE_PLACEHOLDERS)
.build())!!
}
return allKitsuLiveData ?: throw AssertionError("Check your threads ...")
}
fun setQueryFilter(queryFilter: String) {
KitsuMediaPagedListProvider.setQueryFilter(queryFilter)
allKitsuLiveData = null // invalidate
}
companion object {
private const val PAGED_LIST_PAGE_SIZE = 20
private const val PAGED_LIST_ENABLE_PLACEHOLDERS = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment