Permalink
Browse files

Update to Paging 2.1.0-alpha01

  • Loading branch information...
ChrisCraik committed Oct 12, 2018
1 parent 9cad538 commit 779787f193386a077ab7506955f696c0cbad9d4e
@@ -47,7 +47,7 @@ versions.atsl_runner = "1.1.0-alpha4"
versions.atsl_rules = "1.1.0-alpha4"
versions.hamcrest = "1.3"
versions.kotlin = "1.2.71"
versions.paging = "2.0.0"
versions.paging = "2.1.0-alpha01"
versions.navigation = "1.0.0-alpha06"
versions.work = "1.0.0-alpha10"
def deps = [:]
@@ -120,7 +120,8 @@ kotlin.plugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
kotlin.allopen = "org.jetbrains.kotlin:kotlin-allopen:$versions.kotlin"
deps.kotlin = kotlin
deps.paging = "androidx.paging:paging-runtime:$versions.paging"
deps.paging_ktx = "androidx.paging:paging-runtime-ktx:$versions.paging"
def glide = [:]
glide.runtime = "com.github.bumptech.glide:glide:$versions.glide"
@@ -47,7 +47,7 @@ versions.atsl_runner = "1.1.0-alpha4"
versions.atsl_rules = "1.1.0-alpha4"
versions.hamcrest = "1.3"
versions.kotlin = "1.2.71"
versions.paging = "2.0.0"
versions.paging = "2.1.0-alpha01"
versions.navigation = "1.0.0-alpha06"
versions.work = "1.0.0-alpha10"
def deps = [:]
@@ -120,7 +120,8 @@ kotlin.plugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
kotlin.allopen = "org.jetbrains.kotlin:kotlin-allopen:$versions.kotlin"
deps.kotlin = kotlin
deps.paging = "androidx.paging:paging-runtime:$versions.paging"
deps.paging_ktx = "androidx.paging:paging-runtime-ktx:$versions.paging"
def glide = [:]
glide.runtime = "com.github.bumptech.glide:glide:$versions.glide"
@@ -47,7 +47,7 @@ versions.atsl_runner = "1.1.0-alpha4"
versions.atsl_rules = "1.1.0-alpha4"
versions.hamcrest = "1.3"
versions.kotlin = "1.2.71"
versions.paging = "2.0.0"
versions.paging = "2.1.0-alpha01"
versions.navigation = "1.0.0-alpha06"
versions.work = "1.0.0-alpha10"
def deps = [:]
@@ -120,7 +120,8 @@ kotlin.plugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
kotlin.allopen = "org.jetbrains.kotlin:kotlin-allopen:$versions.kotlin"
deps.kotlin = kotlin
deps.paging = "androidx.paging:paging-runtime:$versions.paging"
deps.paging_ktx = "androidx.paging:paging-runtime-ktx:$versions.paging"
def glide = [:]
glide.runtime = "com.github.bumptech.glide:glide:$versions.glide"
@@ -47,7 +47,7 @@ versions.atsl_runner = "1.1.0-alpha4"
versions.atsl_rules = "1.1.0-alpha4"
versions.hamcrest = "1.3"
versions.kotlin = "1.2.71"
versions.paging = "2.0.0"
versions.paging = "2.1.0-alpha01"
versions.navigation = "1.0.0-alpha06"
versions.work = "1.0.0-alpha10"
def deps = [:]
@@ -120,7 +120,8 @@ kotlin.plugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
kotlin.allopen = "org.jetbrains.kotlin:kotlin-allopen:$versions.kotlin"
deps.kotlin = kotlin
deps.paging = "androidx.paging:paging-runtime:$versions.paging"
deps.paging_ktx = "androidx.paging:paging-runtime-ktx:$versions.paging"
def glide = [:]
glide.runtime = "com.github.bumptech.glide:glide:$versions.glide"
@@ -47,7 +47,7 @@ versions.atsl_runner = "1.1.0-alpha4"
versions.atsl_rules = "1.1.0-alpha4"
versions.hamcrest = "1.3"
versions.kotlin = "1.2.71"
versions.paging = "2.0.0"
versions.paging = "2.1.0-alpha01"
versions.navigation = "1.0.0-alpha06"
versions.work = "1.0.0-alpha10"
def deps = [:]
@@ -120,7 +120,8 @@ kotlin.plugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
kotlin.allopen = "org.jetbrains.kotlin:kotlin-allopen:$versions.kotlin"
deps.kotlin = kotlin
deps.paging = "androidx.paging:paging-runtime:$versions.paging"
deps.paging_ktx = "androidx.paging:paging-runtime-ktx:$versions.paging"
def glide = [:]
glide.runtime = "com.github.bumptech.glide:glide:$versions.glide"
@@ -47,7 +47,7 @@ dependencies {
implementation deps.room.runtime
implementation deps.lifecycle.runtime
implementation deps.lifecycle.extensions
implementation deps.paging
implementation deps.paging_ktx
implementation deps.kotlin.stdlib
// Android Testing Support Library's runner and rules
@@ -57,4 +57,4 @@ dependencies {
androidTestImplementation deps.arch_core.testing
kapt deps.room.compiler
}
}
@@ -18,42 +18,47 @@ package paging.android.example.com.pagingsample
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import androidx.paging.Config
import androidx.paging.toLiveData
/**
* A simple ViewModel that provides a paged list of delicious Cheeses.
*/
class CheeseViewModel(app: Application) : AndroidViewModel(app) {
val dao = CheeseDb.get(app).cheeseDao()
companion object {
/**
* A good page size is a value that fills at least a screen worth of content on a large
* device so the User is unlikely to see a null item.
* You can play with this constant to observe the paging behavior.
* <p>
* It's possible to vary this with list device size, but often unnecessary, unless a user
* scrolling on a large device is expected to scroll through items more quickly than a small
* device, such as when the large device uses a grid layout of items.
*/
private const val PAGE_SIZE = 30
/**
* We use -ktx Kotlin extension functions here, otherwise you would use LivePagedListBuilder(),
* and PagedList.Config.Builder()
*/
val allCheeses = dao.allCheesesByName().toLiveData(Config(
/**
* A good page size is a value that fills at least a screen worth of content on a large
* device so the User is unlikely to see a null item.
* You can play with this constant to observe the paging behavior.
* <p>
* It's possible to vary this with list device size, but often unnecessary, unless a
* user scrolling on a large device is expected to scroll through items more quickly
* than a small device, such as when the large device uses a grid layout of items.
*/
pageSize = 30,
/**
* If placeholders are enabled, PagedList will report the full size but some items might
* be null in onBind method (PagedListAdapter triggers a rebind when data is loaded).
* <p>
* If placeholders are disabled, onBind will never receive null but as more pages are
* loaded, the scrollbars will jitter as new pages are loaded. You should probably disable
* scrollbars if you disable placeholders.
*/
private const val ENABLE_PLACEHOLDERS = true
}
/**
* If placeholders are enabled, PagedList will report the full size but some items might
* be null in onBind method (PagedListAdapter triggers a rebind when data is loaded).
* <p>
* If placeholders are disabled, onBind will never receive null but as more pages are
* loaded, the scrollbars will jitter as new pages are loaded. You should probably
* disable scrollbars if you disable placeholders.
*/
enablePlaceholders = true,
val allCheeses = LivePagedListBuilder(dao.allCheesesByName(), PagedList.Config.Builder()
.setPageSize(PAGE_SIZE)
.setEnablePlaceholders(ENABLE_PLACEHOLDERS)
.build()).build()
/**
* Maximum number of items a PagedList should hold in memory at once.
* <p>
* This number triggers the PagedList to start dropping distant pages as more are loaded.
*/
maxSize = 200))
fun insert(text: CharSequence) = ioThread {
dao.insert(Cheese(id = 0, name = text.toString()))
@@ -62,4 +67,4 @@ class CheeseViewModel(app: Application) : AndroidViewModel(app) {
fun remove(cheese: Cheese) = ioThread {
dao.delete(cheese)
}
}
}
@@ -47,7 +47,7 @@ versions.atsl_runner = "1.1.0-alpha4"
versions.atsl_rules = "1.1.0-alpha4"
versions.hamcrest = "1.3"
versions.kotlin = "1.2.71"
versions.paging = "2.0.0"
versions.paging = "2.1.0-alpha01"
versions.navigation = "1.0.0-alpha06"
versions.work = "1.0.0-alpha10"
def deps = [:]
@@ -120,7 +120,8 @@ kotlin.plugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin"
kotlin.allopen = "org.jetbrains.kotlin:kotlin-allopen:$versions.kotlin"
deps.kotlin = kotlin
deps.paging = "androidx.paging:paging-runtime:$versions.paging"
deps.paging_ktx = "androidx.paging:paging-runtime-ktx:$versions.paging"
def glide = [:]
glide.runtime = "com.github.bumptech.glide:glide:$versions.glide"
@@ -58,7 +58,7 @@ dependencies {
implementation deps.room.runtime
implementation deps.lifecycle.runtime
implementation deps.lifecycle.extensions
implementation deps.paging
implementation deps.paging_ktx
implementation deps.kotlin.stdlib
implementation deps.retrofit.runtime
implementation deps.retrofit.gson
@@ -21,6 +21,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.paging.LivePagedListBuilder
import androidx.annotation.MainThread
import androidx.paging.toLiveData
import com.android.example.paging.pagingwithnetwork.reddit.api.RedditApi
import com.android.example.paging.pagingwithnetwork.reddit.db.RedditDb
import com.android.example.paging.pagingwithnetwork.reddit.repository.Listing
@@ -109,11 +110,6 @@ class DbRedditPostRepository(
handleResponse = this::insertResultIntoDb,
ioExecutor = ioExecutor,
networkPageSize = networkPageSize)
// create a data source factory from Room
val dataSourceFactory = db.posts().postsBySubreddit(subReddit)
val builder = LivePagedListBuilder(dataSourceFactory, pageSize)
.setBoundaryCallback(boundaryCallback)
// we are using a mutable live data to trigger refresh requests which eventually calls
// refresh method and gets a new live data. Each refresh request by the user becomes a newly
// dispatched data in refreshTrigger
@@ -122,8 +118,13 @@ class DbRedditPostRepository(
refresh(subReddit)
}
// We use toLiveData Kotlin extension function here, you could also use LivePagedListBuilder
val livePagedList = db.posts().postsBySubreddit(subReddit).toLiveData(
pageSize = pageSize,
boundaryCallback = boundaryCallback)
return Listing(
pagedList = builder.build(),
pagedList = livePagedList,
networkState = boundaryCallback.networkState,
retry = {
boundaryCallback.helper.retryAllFailed()
@@ -20,6 +20,8 @@ import androidx.lifecycle.Transformations
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import androidx.annotation.MainThread
import androidx.paging.Config
import androidx.paging.toLiveData
import com.android.example.paging.pagingwithnetwork.reddit.api.RedditApi
import com.android.example.paging.pagingwithnetwork.reddit.repository.Listing
import com.android.example.paging.pagingwithnetwork.reddit.repository.RedditPostRepository
@@ -36,22 +38,23 @@ class InMemoryByItemRepository(
@MainThread
override fun postsOfSubreddit(subReddit: String, pageSize: Int): Listing<RedditPost> {
val sourceFactory = SubRedditDataSourceFactory(redditApi, subReddit, networkExecutor)
val pagedListConfig = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setInitialLoadSizeHint(pageSize * 2)
.setPageSize(pageSize)
.build()
val pagedList = LivePagedListBuilder(sourceFactory, pagedListConfig)
// We use toLiveData Kotlin ext. function here, you could also use LivePagedListBuilder
val livePagedList = sourceFactory.toLiveData(
// we use Config Kotlin ext. function here, could also use PagedList.Config.Builder
config = Config(
pageSize = pageSize,
enablePlaceholders = false,
initialLoadSizeHint = pageSize * 2),
// provide custom executor for network requests, otherwise it will default to
// Arch Components' IO pool which is also used for disk access
.setFetchExecutor(networkExecutor)
.build()
fetchExecutor = networkExecutor)
val refreshState = Transformations.switchMap(sourceFactory.sourceLiveData) {
it.initialLoad
}
return Listing(
pagedList = pagedList,
pagedList = livePagedList,
networkState = Transformations.switchMap(sourceFactory.sourceLiveData) {
it.networkState
},
@@ -19,6 +19,7 @@ package com.android.example.paging.pagingwithnetwork.reddit.repository.inMemory.
import androidx.lifecycle.Transformations
import androidx.paging.LivePagedListBuilder
import androidx.annotation.MainThread
import androidx.paging.toLiveData
import com.android.example.paging.pagingwithnetwork.reddit.api.RedditApi
import com.android.example.paging.pagingwithnetwork.reddit.repository.Listing
import com.android.example.paging.pagingwithnetwork.reddit.repository.RedditPostRepository
@@ -35,11 +36,12 @@ class InMemoryByPageKeyRepository(private val redditApi: RedditApi,
override fun postsOfSubreddit(subReddit: String, pageSize: Int): Listing<RedditPost> {
val sourceFactory = SubRedditDataSourceFactory(redditApi, subReddit, networkExecutor)
val livePagedList = LivePagedListBuilder(sourceFactory, pageSize)
// We use toLiveData Kotlin extension function here, you could also use LivePagedListBuilder
val livePagedList = sourceFactory.toLiveData(
pageSize = pageSize,
// provide custom executor for network requests, otherwise it will default to
// Arch Components' IO pool which is also used for disk access
.setFetchExecutor(networkExecutor)
.build()
fetchExecutor = networkExecutor)
val refreshState = Transformations.switchMap(sourceFactory.sourceLiveData) {
it.initialLoad
@@ -61,6 +61,15 @@ class InMemoryRepositoryTest(type : RedditPostRepository.Type) {
else -> throw IllegalArgumentException()
}
private val postFactory = PostFactory()
private fun <T> PagedList<T>.loadAllData() {
do {
val oldSize = this.loadedCount
this.loadAround(this.size - 1)
} while (this.size != oldSize)
}
/**
* asserts that empty list works fine
*/
@@ -92,7 +101,7 @@ class InMemoryRepositoryTest(type : RedditPostRepository.Type) {
val listing = repository.postsOfSubreddit(subReddit = "bar", pageSize = 3)
// trigger loading of the whole list
val pagedList = getPagedList(listing)
pagedList.loadAround(posts.size - 1)
pagedList.loadAllData()
assertThat(pagedList, `is`(posts))
}
@@ -147,10 +156,11 @@ class InMemoryRepositoryTest(type : RedditPostRepository.Type) {
list.size < posts.size, `is`(true))
assertThat(getNetworkState(listing), `is`(NetworkState.LOADED))
fakeApi.failureMsg = "fail"
list.loadAround(posts.size - 1)
list.loadAllData()
assertThat(getNetworkState(listing), `is`(NetworkState.error("fail")))
fakeApi.failureMsg = null
listing.retry()
list.loadAllData()
assertThat(getNetworkState(listing), `is`(NetworkState.LOADED))
assertThat(list, `is`(posts))
}
@@ -164,7 +174,7 @@ class InMemoryRepositoryTest(type : RedditPostRepository.Type) {
postsV1.forEach(fakeApi::addPost)
val listing = repository.postsOfSubreddit(subReddit = "bar", pageSize = 5)
val list = getPagedList(listing)
list.loadAround(10)
list.loadAround(5)
val postsV2 = (0..10).map { postFactory.createRedditPost("bar") }
fakeApi.clear()
postsV2.forEach(fakeApi::addPost)
@@ -175,7 +185,7 @@ class InMemoryRepositoryTest(type : RedditPostRepository.Type) {
listing.refresh()
val list2 = getPagedList(listing)
list2.loadAround(10)
list2.loadAround(5)
assertThat(list2, `is`(postsV2))
val inOrder = Mockito.inOrder(refreshObserver)
inOrder.verify(refreshObserver).onChanged(NetworkState.LOADED) // initial state
@@ -229,4 +239,4 @@ class InMemoryRepositoryTest(type : RedditPostRepository.Type) {
this.value = t
}
}
}
}
Oops, something went wrong.

0 comments on commit 779787f

Please sign in to comment.