mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 00:59:49 +01:00
Refactor MappingAdapter code into package.
This commit is contained in:
committed by
Greyson Parrelli
parent
4275877b47
commit
dbce4be31d
@@ -0,0 +1,9 @@
|
||||
package org.thoughtcrime.securesms.util.adapter.mapping;
|
||||
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
public interface Factory<T extends MappingModel<T>> {
|
||||
@NonNull MappingViewHolder<T> createViewHolder(@NonNull ViewGroup parent);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.thoughtcrime.securesms.util.adapter.mapping;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.LayoutRes;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.whispersystems.libsignal.util.guava.Function;
|
||||
|
||||
public class LayoutFactory<T extends MappingModel<T>> implements Factory<T> {
|
||||
private Function<View, MappingViewHolder<T>> creator;
|
||||
private final int layout;
|
||||
|
||||
public LayoutFactory(@NonNull Function<View, MappingViewHolder<T>> creator, @LayoutRes int layout) {
|
||||
this.creator = creator;
|
||||
this.layout = layout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull MappingViewHolder<T> createViewHolder(@NonNull ViewGroup parent) {
|
||||
return creator.apply(LayoutInflater.from(parent.getContext()).inflate(layout, parent, false));
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,15 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
package org.thoughtcrime.securesms.util.adapter.mapping;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.LayoutRes;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.recyclerview.widget.DefaultItemAnimator;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
import androidx.recyclerview.widget.ListAdapter;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import org.thoughtcrime.securesms.util.NoCrossfadeChangeDefaultAnimator;
|
||||
import org.whispersystems.libsignal.util.guava.Function;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -129,54 +126,4 @@ public class MappingAdapter extends ListAdapter<MappingModel<?>, MappingViewHold
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private static class MappingDiffCallback extends DiffUtil.ItemCallback<MappingModel<?>> {
|
||||
@Override
|
||||
public boolean areItemsTheSame(@NonNull MappingModel oldItem, @NonNull MappingModel newItem) {
|
||||
if (oldItem.getClass() == newItem.getClass()) {
|
||||
//noinspection unchecked
|
||||
return oldItem.areItemsTheSame(newItem);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressLint("DiffUtilEquals")
|
||||
@Override
|
||||
public boolean areContentsTheSame(@NonNull MappingModel oldItem, @NonNull MappingModel newItem) {
|
||||
if (oldItem.getClass() == newItem.getClass()) {
|
||||
//noinspection unchecked
|
||||
return oldItem.areContentsTheSame(newItem);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Object getChangePayload(@NonNull MappingModel oldItem, @NonNull MappingModel newItem) {
|
||||
if (oldItem.getClass() == newItem.getClass()) {
|
||||
//noinspection unchecked
|
||||
return oldItem.getChangePayload(newItem);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public interface Factory<T extends MappingModel<T>> {
|
||||
@NonNull MappingViewHolder<T> createViewHolder(@NonNull ViewGroup parent);
|
||||
}
|
||||
|
||||
public static class LayoutFactory<T extends MappingModel<T>> implements Factory<T> {
|
||||
private Function<View, MappingViewHolder<T>> creator;
|
||||
private final int layout;
|
||||
|
||||
public LayoutFactory(@NonNull Function<View, MappingViewHolder<T>> creator, @LayoutRes int layout) {
|
||||
this.creator = creator;
|
||||
this.layout = layout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull MappingViewHolder<T> createViewHolder(@NonNull ViewGroup parent) {
|
||||
return creator.apply(LayoutInflater.from(parent.getContext()).inflate(layout, parent, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.thoughtcrime.securesms.util.adapter.mapping;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
|
||||
class MappingDiffCallback extends DiffUtil.ItemCallback<MappingModel<?>> {
|
||||
@Override
|
||||
public boolean areItemsTheSame(@NonNull MappingModel oldItem, @NonNull MappingModel newItem) {
|
||||
if (oldItem.getClass() == newItem.getClass()) {
|
||||
//noinspection unchecked
|
||||
return oldItem.areItemsTheSame(newItem);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressLint("DiffUtilEquals")
|
||||
@Override
|
||||
public boolean areContentsTheSame(@NonNull MappingModel oldItem, @NonNull MappingModel newItem) {
|
||||
if (oldItem.getClass() == newItem.getClass()) {
|
||||
//noinspection unchecked
|
||||
return oldItem.areContentsTheSame(newItem);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Object getChangePayload(@NonNull MappingModel oldItem, @NonNull MappingModel newItem) {
|
||||
if (oldItem.getClass() == newItem.getClass()) {
|
||||
//noinspection unchecked
|
||||
return oldItem.getChangePayload(newItem);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
package org.thoughtcrime.securesms.util.adapter.mapping;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
package org.thoughtcrime.securesms.util.adapter.mapping;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
package org.thoughtcrime.securesms.util.adapter.mapping;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.View;
|
||||
@@ -7,7 +7,7 @@ import androidx.annotation.NonNull;
|
||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId;
|
||||
import org.thoughtcrime.securesms.util.MappingModel;
|
||||
import org.thoughtcrime.securesms.util.adapter.mapping.MappingModel;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
@@ -11,8 +11,9 @@ import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.badges.BadgeImageView;
|
||||
import org.thoughtcrime.securesms.components.AvatarImageView;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.util.MappingAdapter;
|
||||
import org.thoughtcrime.securesms.util.MappingViewHolder;
|
||||
import org.thoughtcrime.securesms.util.adapter.mapping.Factory;
|
||||
import org.thoughtcrime.securesms.util.adapter.mapping.LayoutFactory;
|
||||
import org.thoughtcrime.securesms.util.adapter.mapping.MappingViewHolder;
|
||||
|
||||
public class RecipientViewHolder<T extends RecipientMappingModel<T>> extends MappingViewHolder<T> {
|
||||
|
||||
@@ -57,8 +58,8 @@ public class RecipientViewHolder<T extends RecipientMappingModel<T>> extends Map
|
||||
}
|
||||
}
|
||||
|
||||
public static @NonNull <T extends RecipientMappingModel<T>> MappingAdapter.Factory<T> createFactory(@LayoutRes int layout, @Nullable EventListener<T> listener) {
|
||||
return new MappingAdapter.LayoutFactory<>(view -> new RecipientViewHolder<>(view, listener), layout);
|
||||
public static @NonNull <T extends RecipientMappingModel<T>> Factory<T> createFactory(@LayoutRes int layout, @Nullable EventListener<T> listener) {
|
||||
return new LayoutFactory<>(view -> new RecipientViewHolder<>(view, listener), layout);
|
||||
}
|
||||
|
||||
public interface EventListener<T extends RecipientMappingModel<T>> {
|
||||
|
||||
Reference in New Issue
Block a user