Skip to content

Commit

Permalink
handle case without diff util. disable placeholders in sample to show…
Browse files Browse the repository at this point in the history
… off loadstate
  • Loading branch information
kedzie committed Aug 6, 2020
1 parent b594ea3 commit 251b304
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,59 @@ class ImmutableViewModel : ViewModel(), ImmutableListeners {
value = (0 until 3).map { i -> ImmutableItem(index = i, checked = false) }
}
private val headerFooterList =
Transformations.map<List<ImmutableItem>, List<Any>>(mutList) { input ->
val list = ArrayList<Any>(input.size + 2)
list.add("Header")
list.addAll(input)
list.add("Footer")
list
}
Transformations.map<List<ImmutableItem>, List<Any>>(mutList) { input ->
val list = ArrayList<Any>(input.size + 2)
list.add("Header")
list.addAll(input)
list.add("Footer")
list
}
val list: LiveData<List<Any>> = headerFooterList

val pagedList: LiveData<PagedList<Any>> =
LivePagedListBuilder(object : DataSource.Factory<Int, Any>() {
override fun create(): DataSource<Int, Any> =
object : PositionalDataSource<Any>() {

override fun loadInitial(
params: LoadInitialParams,
callback: LoadInitialCallback<Any>
) {
val list =
(0 until params.requestedLoadSize).map {
ImmutableItem(
index = it + params.requestedStartPosition,
checked = false
)
LivePagedListBuilder(object : DataSource.Factory<Int, Any>() {
override fun create(): DataSource<Int, Any> =
object : PositionalDataSource<Any>() {

override fun loadInitial(
params: LoadInitialParams,
callback: LoadInitialCallback<Any>
) {
val list =
(0 until params.requestedLoadSize).map {
ImmutableItem(
index = it + params.requestedStartPosition,
checked = false
)
}
// Pretend we are slow
Thread.sleep(1000)
callback.onResult(list, params.requestedStartPosition, TOTAL_COUNT)
}
// Pretend we are slow
Thread.sleep(1000)
callback.onResult(list, params.requestedStartPosition, 200)
}

override fun loadRange(
params: LoadRangeParams,
callback: LoadRangeCallback<Any>
) {
val list =
(0 until params.loadSize).map {
ImmutableItem(
index = it + params.startPosition,
checked = false
)

override fun loadRange(
params: LoadRangeParams,
callback: LoadRangeCallback<Any>
) {
val list =
(0 until params.loadSize).map {
ImmutableItem(
index = it + params.startPosition,
checked = false
)
}
// Pretend we are slow
Thread.sleep(1000)
callback.onResult(list)
}
// Pretend we are slow
Thread.sleep(1000)
callback.onResult(list)
}
}
}, 20).build()

val pagedListV3: LiveData<PagingData<Any>> = Pager<Int, Any>(PagingConfig(pageSize = 20, maxSize = 100)) {
}
}, PAGE_SIZE).build()

val pagedListV3: LiveData<PagingData<Any>> = Pager<Int, Any>(PagingConfig(
pageSize = PAGE_SIZE,
maxSize = 100,
prefetchDistance = PAGE_SIZE * 2,
enablePlaceholders = false)) {
object : PagingSource<Int, Any>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Any> {
val safeKey = params.key ?: 0
Expand All @@ -81,7 +85,9 @@ class ImmutableViewModel : ViewModel(), ImmutableListeners {
return LoadResult.Page(
data = list,
prevKey = if (safeKey == 0) null else (safeKey - params.loadSize),
nextKey = if (safeKey >= 200 - params.loadSize) null else (safeKey + params.loadSize)
nextKey = if (safeKey >= 200 - params.loadSize) null else (safeKey + params.loadSize),
itemsBefore = safeKey,
itemsAfter = TOTAL_COUNT - params.loadSize - safeKey
)
}
}
Expand Down Expand Up @@ -134,4 +140,9 @@ class ImmutableViewModel : ViewModel(), ImmutableListeners {
}
}
}

companion object {
private val TOTAL_COUNT = 200
private val PAGE_SIZE = 20
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.tatarka.bindingcollectionadapter2;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.paging.AsyncPagingDataDiffer;
import androidx.paging.CombinedLoadStates;
Expand All @@ -9,6 +10,7 @@
import androidx.recyclerview.widget.AdapterListUpdateCallback;
import androidx.recyclerview.widget.AsyncDifferConfig;
import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Collections;
Expand Down Expand Up @@ -80,18 +82,18 @@ public static <T> void setAdapter(RecyclerView recyclerView,
if (itemBinding == null) {
throw new IllegalArgumentException("itemBinding must not be null");
}
RecyclerView.Adapter bla = recyclerView.getAdapter();
RecyclerView.Adapter rootAdapter = recyclerView.getAdapter();

BindingRecyclerViewAdapter oldAdapter = null;
if (bla instanceof ConcatAdapter) {
ConcatAdapter concatAdapter = (ConcatAdapter) bla;
if (rootAdapter instanceof ConcatAdapter) {
ConcatAdapter concatAdapter = (ConcatAdapter) rootAdapter;
for (RecyclerView.Adapter childAdapter : concatAdapter.getAdapters()) {
if (childAdapter instanceof BindingRecyclerViewAdapter) {
oldAdapter = (BindingRecyclerViewAdapter<T>) childAdapter;
}
}
} else if (bla instanceof BindingRecyclerViewAdapter) {
oldAdapter = (BindingRecyclerViewAdapter<T>) bla;
} else if (rootAdapter instanceof BindingRecyclerViewAdapter) {
oldAdapter = (BindingRecyclerViewAdapter<T>) rootAdapter;
}

if (adapter == null) {
Expand All @@ -104,9 +106,22 @@ public static <T> void setAdapter(RecyclerView recyclerView,

adapter.setItemBinding(itemBinding);

if (diffConfig != null && items != null) {
if (items != null) {
AsyncDiffPagedObservableListV3<T> list = (AsyncDiffPagedObservableListV3<T>) recyclerView.getTag(R.id.bindingcollectiondapter_list_id);
if (list == null) {
if (diffConfig == null) {
diffConfig = new AsyncDifferConfig.Builder(new DiffUtil.ItemCallback() {
@Override
public boolean areItemsTheSame(@NonNull Object oldItem, @NonNull Object newItem) {
return false;
}

@Override
public boolean areContentsTheSame(@NonNull Object oldItem, @NonNull Object newItem) {
return false;
}
}).build();
}
list = new AsyncDiffPagedObservableListV3<>(diffConfig);
if (headerLoadStateAdapter != null || footerLoadStateAdapter != null) {
list.addLoadStateListener(new Function1<CombinedLoadStates, Unit>() {
Expand All @@ -126,8 +141,6 @@ public Unit invoke(CombinedLoadStates combinedLoadStates) {
adapter.setItems(list);
}
list.update(Utils.findLifecycleOwner(recyclerView).getLifecycle(), items);
} else {
// adapter.setItems(list);
}

adapter.setItemIds(itemIds);
Expand Down

0 comments on commit 251b304

Please sign in to comment.