-
Notifications
You must be signed in to change notification settings - Fork 727
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* EpoxyController that works with PagedList This CL introduces a new class in epoxy-paging called CachingPagingEpoxyController. This controller works with a PagedList and caches models for each item. It still allows modifying the final model list via an addModels API. Fixes: 524 Test: PagedListModelCacheTest * More docs and style fixes * more docs & cleanup based on comments * Rename caching paging to PagedListEpoxyController * update sample not to add items randomly, it is confusing
- Loading branch information
Showing
13 changed files
with
797 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,26 @@ | ||
apply plugin: 'com.android.library' | ||
|
||
apply plugin: 'kotlin-android' | ||
android { | ||
compileSdkVersion rootProject.COMPILE_SDK_VERSION | ||
|
||
defaultConfig { | ||
minSdkVersion rootProject.MIN_SDK_VERSION | ||
targetSdkVersion rootProject.TARGET_SDK_VERSION | ||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation rootProject.deps.kotlin | ||
api rootProject.deps.androidPagingComponent | ||
api project(':epoxy-annotations') | ||
api project(':epoxy-adapter') | ||
|
||
compile rootProject.deps.androidPagingComponent | ||
compile project(':epoxy-annotations') | ||
compile project(':epoxy-adapter') | ||
androidTestImplementation rootProject.deps.junit | ||
androidTestImplementation rootProject.deps.androidArchCoreTesting | ||
androidTestImplementation rootProject.deps.androidTestRunner | ||
|
||
testCompile rootProject.deps.junit | ||
testCompile rootProject.deps.robolectric | ||
testCompile rootProject.deps.mockito | ||
kaptAndroidTest project(":epoxy-processor") | ||
} | ||
|
||
apply from: rootProject.file('gradle/gradle-maven-push.gradle') |
31 changes: 31 additions & 0 deletions
31
epoxy-paging/src/androidTest/java/com/airbnb/epoxy/paging/Item.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2018 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.airbnb.epoxy.paging | ||
|
||
import android.support.v7.util.DiffUtil | ||
|
||
/** | ||
* Dummy item for testing. | ||
*/ | ||
data class Item(val id: Int, val value: String) { | ||
companion object { | ||
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Item>() { | ||
override fun areItemsTheSame(oldItem: Item, newItem: Item) = oldItem.id == newItem.id | ||
|
||
override fun areContentsTheSame(oldItem: Item, newItem: Item) = oldItem == newItem | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
epoxy-paging/src/androidTest/java/com/airbnb/epoxy/paging/ListDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2018 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.airbnb.epoxy.paging | ||
|
||
import android.arch.paging.PositionalDataSource | ||
|
||
/** | ||
* Simple data source that works with a given list and its loading can be stopped / started. | ||
*/ | ||
class ListDataSource<T>( | ||
private val data: List<T> | ||
) : PositionalDataSource<T>() { | ||
private var pendingActions = arrayListOf<() -> Unit>() | ||
private var running = true | ||
|
||
private fun compute(f: () -> Unit) { | ||
if (running) { | ||
f() | ||
} else { | ||
pendingActions.add(f) | ||
} | ||
} | ||
|
||
fun start() { | ||
running = true | ||
val pending = pendingActions | ||
pendingActions = arrayListOf() | ||
pending.forEach(this::compute) | ||
} | ||
|
||
fun stop() { | ||
running = false | ||
} | ||
|
||
override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<T>) { | ||
compute { | ||
callback.onResult( | ||
data.subList(params.startPosition, Math.min(data.size, params.startPosition + params.loadSize)) | ||
) | ||
} | ||
} | ||
|
||
override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<T>) { | ||
val start = computeInitialLoadPosition(params, data.size) | ||
val itemCnt = computeInitialLoadSize(params, start, data.size) | ||
callback.onResult( | ||
data.subList(start, start + itemCnt), | ||
start, | ||
data.size | ||
) | ||
} | ||
} |
Oops, something went wrong.