Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.yandex.practicum.contacts.presentation.base;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;

public class BaseListDiffCallback<T extends ListDiffInterface<T>> extends DiffUtil.ItemCallback<T> {

@Override
public boolean areItemsTheSame(@NonNull T oldItem, @NonNull T newItem) {
return oldItem.theSameAs(newItem);
}

@Override
public boolean areContentsTheSame(@NonNull T oldItem, @NonNull T newItem) {
return oldItem.equals(newItem);
}

@Nullable
@Override
public Object getChangePayload(@NonNull T oldItem, @NonNull T newItem) {
return newItem;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.yandex.practicum.contacts.presentation.base;

public interface ListDiffInterface<T> {

boolean theSameAs(T other);
boolean equals(Object obj);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@
import java.util.List;
import java.util.function.Consumer;

import ru.yandex.practicum.contacts.presentation.base.BaseListDiffCallback;
import ru.yandex.practicum.contacts.databinding.ItemFilterBinding;
import ru.yandex.practicum.contacts.model.ContactType;
import ru.yandex.practicum.contacts.presentation.base.ListDiffInterface;
import ru.yandex.practicum.contacts.presentation.filter.model.FilterContactType;
import ru.yandex.practicum.contacts.presentation.filter.model.FilterContactTypeUi;
import ru.yandex.practicum.contacts.utils.model.ContactTypeUtils;
import ru.yandex.practicum.contacts.utils.model.FilterContactTypeUtils;

public class FilterContactTypeAdapter extends RecyclerView.Adapter<FilterContactTypeAdapter.ViewHolder> {
public class FilterContactTypeAdapter extends RecyclerView.Adapter<FilterContactTypeAdapter.ViewHolder> {

private final AsyncListDiffer<FilterContactTypeUi> differ = new AsyncListDiffer<>(
new AdapterListUpdateCallback(this),
new AsyncDifferConfig.Builder<>(new ListDiffCallback()).build()
new AsyncDifferConfig.Builder<>(new BaseListDiffCallback<FilterContactTypeUi>()).build()
);


private final Consumer<FilterContactTypeUi> clickListener;

public FilterContactTypeAdapter(Consumer<FilterContactTypeUi> clickListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import androidx.annotation.NonNull;

public class FilterContactTypeUi {
import ru.yandex.practicum.contacts.presentation.base.ListDiffInterface;

public class FilterContactTypeUi implements ListDiffInterface<FilterContactTypeUi> {

private final FilterContactType contactType;
private final boolean selected;
Expand All @@ -20,6 +22,11 @@ public boolean isSelected() {
return selected;
}

@Override
public boolean theSameAs(FilterContactTypeUi other) {
return getContactType() == other.getContactType();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package ru.yandex.practicum.contacts.presentation.main;
import ru.yandex.practicum.contacts.presentation.base.BaseListDiffCallback;

import android.content.Context;
import android.graphics.drawable.Drawable;
Expand Down Expand Up @@ -28,9 +29,10 @@ public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ViewHold

private final AsyncListDiffer<ContactUi> differ = new AsyncListDiffer<>(
new AdapterListUpdateCallback(this),
new AsyncDifferConfig.Builder<>(new ListDiffCallback()).build()
new AsyncDifferConfig.Builder<>(new BaseListDiffCallback<ContactUi>()).build()
);


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import java.util.List;

import ru.yandex.practicum.contacts.model.ContactType;
import ru.yandex.practicum.contacts.presentation.base.ListDiffInterface;

public class ContactUi {
public class ContactUi implements ListDiffInterface<ContactUi> {

private final String name;
private final String phone;
Expand Down Expand Up @@ -41,6 +42,11 @@ public List<ContactType> getTypes() {
return types;
}

@Override
public boolean theSameAs(ContactUi other) {
return hashCode() == other.hashCode();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.view.View;
import android.view.ViewGroup;

import ru.yandex.practicum.contacts.presentation.base.BaseListDiffCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.AdapterListUpdateCallback;
Expand All @@ -23,9 +24,10 @@ public class SortTypeAdapter extends RecyclerView.Adapter<SortTypeAdapter.ViewHo

private final AsyncListDiffer<SortTypeUI> differ = new AsyncListDiffer<>(
new AdapterListUpdateCallback(this),
new AsyncDifferConfig.Builder<>(new ListDiffCallback()).build()
new AsyncDifferConfig.Builder<>(new BaseListDiffCallback<SortTypeUI>()).build()
);


private final Consumer<SortTypeUI> clickListener;

public SortTypeAdapter(Consumer<SortTypeUI> clickListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import androidx.annotation.NonNull;

import ru.yandex.practicum.contacts.presentation.base.ListDiffInterface;
import ru.yandex.practicum.contacts.presentation.sort.model.SortType;

public class SortTypeUI {
public class SortTypeUI implements ListDiffInterface<SortTypeUI> {

private final SortType sortType;
private final boolean selected;
Expand All @@ -22,6 +23,11 @@ public boolean isSelected() {
return selected;
}

@Override
public boolean theSameAs(SortTypeUI other) {
return getSortType() == other.getSortType();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down