mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 00:59:49 +01:00
Add PagingMappingAdapter and convert GiphyMp4Adapter.
This commit is contained in:
committed by
Greyson Parrelli
parent
dd79688f48
commit
5918227bff
@@ -44,9 +44,9 @@ import kotlin.jvm.functions.Function1;
|
||||
*/
|
||||
public class MappingAdapter extends ListAdapter<MappingModel<?>, MappingViewHolder<?>> {
|
||||
|
||||
private final Map<Integer, Factory<?>> factories;
|
||||
private final Map<Class<?>, Integer> itemTypes;
|
||||
private int typeCount;
|
||||
final Map<Integer, Factory<?>> factories;
|
||||
final Map<Class<?>, Integer> itemTypes;
|
||||
int typeCount;
|
||||
|
||||
public MappingAdapter() {
|
||||
super(new MappingDiffCallback());
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package org.thoughtcrime.securesms.util.adapter.mapping;
|
||||
|
||||
import android.view.View;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.signal.paging.PagingController;
|
||||
import org.thoughtcrime.securesms.util.ViewUtil;
|
||||
|
||||
/**
|
||||
* A specialized {@link MappingAdapter} backed by a {@link PagingController}.
|
||||
*/
|
||||
public class PagingMappingAdapter<Key> extends MappingAdapter {
|
||||
|
||||
private PagingController<Key> pagingController;
|
||||
|
||||
public PagingMappingAdapter() {
|
||||
this(1, ViewUtil.dpToPx(100));
|
||||
}
|
||||
|
||||
public PagingMappingAdapter(int placeHolderWidth, int placeHolderHeight) {
|
||||
registerFactory(Placeholder.class, parent -> {
|
||||
View view = new FrameLayout(parent.getContext());
|
||||
view.setLayoutParams(new FrameLayout.LayoutParams(placeHolderWidth, placeHolderHeight));
|
||||
return new MappingViewHolder.SimpleViewHolder<>(view);
|
||||
});
|
||||
}
|
||||
|
||||
public void setPagingController(@Nullable PagingController<Key> pagingController) {
|
||||
this.pagingController = pagingController;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @Nullable MappingModel<?> getItem(int position) {
|
||||
if (pagingController != null) {
|
||||
pagingController.onDataNeededAroundIndex(position);
|
||||
}
|
||||
return super.getItem(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemViewType(int position) {
|
||||
MappingModel<?> item = getItem(position);
|
||||
if (item == null) {
|
||||
//noinspection ConstantConditions
|
||||
return itemTypes.get(Placeholder.class);
|
||||
}
|
||||
|
||||
Integer type = itemTypes.get(item.getClass());
|
||||
if (type != null) {
|
||||
return type;
|
||||
}
|
||||
throw new AssertionError("No view holder factory for type: " + item.getClass());
|
||||
}
|
||||
|
||||
private static class Placeholder implements MappingModel<Placeholder> {
|
||||
@Override
|
||||
public boolean areItemsTheSame(@NonNull Placeholder newItem) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(@NonNull Placeholder newItem) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user