From cd521e57b749049e1dff172542f13bc09db29bed Mon Sep 17 00:00:00 2001 From: "de.andrianov" Date: Wed, 20 Nov 2024 13:06:05 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=BC=D0=B5=D0=BD=D1=8F?= =?UTF-8?q?=D0=B5=D0=BC=20=D0=B4=D0=B6=D0=B5=D0=BD=D0=B5=D1=80=D0=B8=D0=BA?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/BaseListDiffCallback.java | 24 +++++++++++++++++++ .../presentation/base/ListDiffInterface.java | 8 +++++++ .../filter/FilterContactTypeAdapter.java | 22 ++--------------- .../filter/model/FilterContactTypeUi.java | 9 ++++++- .../presentation/main/ContactAdapter.java | 21 ++-------------- .../contacts/presentation/main/ContactUi.java | 9 ++++++- .../presentation/sort/SortTypeAdapter.java | 22 ++--------------- .../presentation/sort/SortTypeUI.java | 8 ++++++- 8 files changed, 61 insertions(+), 62 deletions(-) create mode 100644 app/src/main/java/ru/yandex/practicum/contacts/presentation/base/BaseListDiffCallback.java create mode 100644 app/src/main/java/ru/yandex/practicum/contacts/presentation/base/ListDiffInterface.java diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/base/BaseListDiffCallback.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/base/BaseListDiffCallback.java new file mode 100644 index 00000000..e6a64aaa --- /dev/null +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/base/BaseListDiffCallback.java @@ -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> extends DiffUtil.ItemCallback { + + @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; + } +} diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/base/ListDiffInterface.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/base/ListDiffInterface.java new file mode 100644 index 00000000..5af9e994 --- /dev/null +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/base/ListDiffInterface.java @@ -0,0 +1,8 @@ +package ru.yandex.practicum.contacts.presentation.base; + +public interface ListDiffInterface { + boolean theSameAs(T other); + + @Override + boolean equals(Object obj); +} \ No newline at end of file diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeAdapter.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeAdapter.java index 4203bc29..41f72d16 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeAdapter.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeAdapter.java @@ -17,6 +17,7 @@ import ru.yandex.practicum.contacts.databinding.ItemFilterBinding; import ru.yandex.practicum.contacts.model.ContactType; +import ru.yandex.practicum.contacts.presentation.base.BaseListDiffCallback; 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; @@ -26,7 +27,7 @@ public class FilterContactTypeAdapter extends RecyclerView.Adapter differ = new AsyncListDiffer<>( new AdapterListUpdateCallback(this), - new AsyncDifferConfig.Builder<>(new ListDiffCallback()).build() + new AsyncDifferConfig.Builder(new BaseListDiffCallback()).build() ); private final Consumer clickListener; @@ -85,23 +86,4 @@ public void bind(FilterContactTypeUi data) { } } } - - static class ListDiffCallback extends DiffUtil.ItemCallback { - - @Override - public boolean areItemsTheSame(@NonNull FilterContactTypeUi oldItem, @NonNull FilterContactTypeUi newItem) { - return oldItem.getContactType() == newItem.getContactType(); - } - - @Override - public boolean areContentsTheSame(@NonNull FilterContactTypeUi oldItem, @NonNull FilterContactTypeUi newItem) { - return oldItem.equals(newItem); - } - - @Nullable - @Override - public Object getChangePayload(@NonNull FilterContactTypeUi oldItem, @NonNull FilterContactTypeUi newItem) { - return newItem; - } - } } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/model/FilterContactTypeUi.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/model/FilterContactTypeUi.java index fbb187fd..e3946f0a 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/model/FilterContactTypeUi.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/model/FilterContactTypeUi.java @@ -2,7 +2,9 @@ import androidx.annotation.NonNull; -public class FilterContactTypeUi { +import ru.yandex.practicum.contacts.presentation.base.ListDiffInterface; + +public class FilterContactTypeUi implements ListDiffInterface { private final FilterContactType contactType; private final boolean selected; @@ -20,6 +22,11 @@ public boolean isSelected() { return selected; } + @Override + public boolean theSameAs(FilterContactTypeUi other) { + return this.getContactType() == other.getContactType(); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactAdapter.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactAdapter.java index 9c231724..b0907dc5 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactAdapter.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactAdapter.java @@ -23,12 +23,13 @@ import ru.yandex.practicum.contacts.R; import ru.yandex.practicum.contacts.databinding.ItemContactBinding; +import ru.yandex.practicum.contacts.presentation.base.BaseListDiffCallback; public class ContactAdapter extends RecyclerView.Adapter { private final AsyncListDiffer differ = new AsyncListDiffer<>( new AdapterListUpdateCallback(this), - new AsyncDifferConfig.Builder<>(new ListDiffCallback()).build() + new AsyncDifferConfig.Builder(new BaseListDiffCallback()).build() ); @NonNull @@ -93,22 +94,4 @@ private void loadAvatar(ContactUi contact) { } } - static class ListDiffCallback extends DiffUtil.ItemCallback { - - @Override - public boolean areItemsTheSame(@NonNull ContactUi oldItem, @NonNull ContactUi newItem) { - return oldItem.hashCode() == newItem.hashCode(); - } - - @Override - public boolean areContentsTheSame(@NonNull ContactUi oldItem, @NonNull ContactUi newItem) { - return oldItem.equals(newItem); - } - - @Nullable - @Override - public Object getChangePayload(@NonNull ContactUi oldItem, @NonNull ContactUi newItem) { - return newItem; - } - } } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactUi.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactUi.java index 4b2f216e..d1ee6e72 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactUi.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactUi.java @@ -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 { private final String name; private final String phone; @@ -41,6 +42,12 @@ public List getTypes() { return types; } + @Override + public boolean theSameAs(ContactUi other) { + return this.hashCode() == other.hashCode(); + } + + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeAdapter.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeAdapter.java index fde5f59c..a2b3faa5 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeAdapter.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeAdapter.java @@ -17,13 +17,14 @@ import ru.yandex.practicum.contacts.R; import ru.yandex.practicum.contacts.databinding.ItemSortBinding; +import ru.yandex.practicum.contacts.presentation.base.BaseListDiffCallback; import ru.yandex.practicum.contacts.presentation.sort.model.SortType; public class SortTypeAdapter extends RecyclerView.Adapter { private final AsyncListDiffer differ = new AsyncListDiffer<>( new AdapterListUpdateCallback(this), - new AsyncDifferConfig.Builder<>(new ListDiffCallback()).build() + new AsyncDifferConfig.Builder(new BaseListDiffCallback()).build() ); private final Consumer clickListener; @@ -88,23 +89,4 @@ private int resource(SortType sortType) { } } } - - static class ListDiffCallback extends DiffUtil.ItemCallback { - - @Override - public boolean areItemsTheSame(@NonNull SortTypeUI oldItem, @NonNull SortTypeUI newItem) { - return oldItem.getSortType() == newItem.getSortType(); - } - - @Override - public boolean areContentsTheSame(@NonNull SortTypeUI oldItem, @NonNull SortTypeUI newItem) { - return oldItem.equals(newItem); - } - - @Nullable - @Override - public Object getChangePayload(@NonNull SortTypeUI oldItem, @NonNull SortTypeUI newItem) { - return newItem; - } - } } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeUI.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeUI.java index eb71f27c..74d48432 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeUI.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeUI.java @@ -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 { private final SortType sortType; private final boolean selected; @@ -22,6 +23,11 @@ public boolean isSelected() { return selected; } + @Override + public boolean theSameAs(SortTypeUI other) { + return this.getSortType() == other.getSortType(); + } + @Override public boolean equals(Object o) { if (this == o) return true;