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,23 @@
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,9 @@
package ru.yandex.practicum.contacts.presentation.base;

public interface ListDiffInterface<T> {


boolean theSameAs(T other);

boolean equals(Object other);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import androidx.recyclerview.widget.AdapterListUpdateCallback;
import androidx.recyclerview.widget.AsyncDifferConfig;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
import java.util.function.Consumer;

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;
Expand All @@ -26,7 +27,7 @@ public class FilterContactTypeAdapter extends RecyclerView.Adapter<FilterContact

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;
Expand Down Expand Up @@ -86,22 +87,6 @@ public void bind(FilterContactTypeUi data) {
}
}

static class ListDiffCallback extends DiffUtil.ItemCallback<FilterContactTypeUi> {

@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;
}
}
}
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(@NonNull FilterContactTypeUi other) {
return this.contactType == other.contactType;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -37,4 +44,4 @@ public int hashCode() {
result = 31 * result + (selected ? 1 : 0);
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@

import ru.yandex.practicum.contacts.R;
import ru.yandex.practicum.contacts.databinding.ItemContactBinding;
import ru.yandex.practicum.contacts.presentation.base.BaseListDiffCallback;
import ru.yandex.practicum.contacts.presentation.main.ContactUi;

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

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

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
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 @@ -45,15 +47,15 @@ public List<ContactType> getTypes() {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ContactUi contact = (ContactUi) o;

if (!name.equals(contact.name)) return false;
if (!phone.equals(contact.phone)) return false;
if (!photo.equals(contact.photo)) return false;
return types.equals(contact.types);
}



@Override
public int hashCode() {
int result = name.hashCode();
Expand All @@ -62,4 +64,8 @@ public int hashCode() {
result = 31 * result + types.hashCode();
return result;
}
@Override
public boolean theSameAs(ContactUi other) {
return this.hashCode() == other.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private void iniViewModel() {
viewModel.init(defaultSortType);
}

private void updateSortTypes(List<SortTypeUI> sortTypes) {
private void updateSortTypes(List<SortTypeUi> sortTypes) {
adapter.setItems(sortTypes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@

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<SortTypeAdapter.ViewHolder> {

private final AsyncListDiffer<SortTypeUI> differ = new AsyncListDiffer<>(
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;
private final Consumer<SortTypeUi> clickListener;

public SortTypeAdapter(Consumer<SortTypeUI> clickListener) {
public SortTypeAdapter(Consumer<SortTypeUi> clickListener) {
this.clickListener = clickListener;
}

Expand All @@ -50,23 +51,23 @@ public int getItemCount() {
return differ.getCurrentList().size();
}

public void setItems(List<SortTypeUI> items) {
public void setItems(List<SortTypeUi> items) {
differ.submitList(items);
}

static class ViewHolder extends RecyclerView.ViewHolder {

private final ItemSortBinding binding;

private SortTypeUI data;
private SortTypeUi data;

public ViewHolder(@NonNull ItemSortBinding binding, Consumer<SortTypeUI> clickListener) {
public ViewHolder(@NonNull ItemSortBinding binding, Consumer<SortTypeUi> clickListener) {
super(binding.getRoot());
this.binding = binding;
this.binding.getRoot().setOnClickListener(v -> clickListener.accept(data));
}

public void bind(SortTypeUI data) {
public void bind(SortTypeUi data) {
this.data = data;
final int sortResId = resource(data.getSortType());
binding.text.setText(sortResId);
Expand All @@ -89,21 +90,21 @@ private int resource(SortType sortType) {
}
}

static class ListDiffCallback extends DiffUtil.ItemCallback<SortTypeUI> {
static class ListDiffCallback extends DiffUtil.ItemCallback<SortTypeUi> {

@Override
public boolean areItemsTheSame(@NonNull SortTypeUI oldItem, @NonNull SortTypeUI newItem) {
public boolean areItemsTheSame(@NonNull SortTypeUi oldItem, @NonNull SortTypeUi newItem) {
return oldItem.getSortType() == newItem.getSortType();
}

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

@Nullable
@Override
public Object getChangePayload(@NonNull SortTypeUI oldItem, @NonNull SortTypeUI newItem) {
public Object getChangePayload(@NonNull SortTypeUi oldItem, @NonNull SortTypeUi newItem) {
return newItem;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

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;

public SortTypeUI(@NonNull SortType sortType, boolean selected) {
public SortTypeUi(@NonNull SortType sortType, boolean selected) {
this.sortType = sortType;
this.selected = selected;
}
Expand All @@ -27,12 +29,19 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

SortTypeUI that = (SortTypeUI) o;
SortTypeUi that = (SortTypeUi) o;

if (selected != that.selected) return false;
return sortType == that.sortType;
}



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

@Override
public int hashCode() {
int result = sortType.hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class SortViewModel extends BaseBottomSheetViewModel {

private final UiState uiState = new UiState();
private final MutableLiveData<List<SortTypeUI>> sortTypesLiveDate = new MutableLiveData<>();
private final MutableLiveData<List<SortTypeUi>> sortTypesLiveDate = new MutableLiveData<>();
private final MutableLiveData<UiState> uiStateLiveDate = new MutableLiveData<>();

private SortType defaultSortType;
Expand All @@ -27,7 +27,7 @@ public void init(SortType defaultSortType) {
updateUiState();
}

public void onSortTypeItemClick(SortTypeUI sortType) {
public void onSortTypeItemClick(SortTypeUi sortType) {
selectedSortType = sortType.getSortType();
updateSortTypes();
updateUiState();
Expand All @@ -46,7 +46,7 @@ public void onResetClick() {
updateUiState();
}

public MutableLiveData<List<SortTypeUI>> getSortTypesLiveDate() {
public MutableLiveData<List<SortTypeUi>> getSortTypesLiveDate() {
return sortTypesLiveDate;
}

Expand All @@ -56,8 +56,8 @@ public MutableLiveData<UiState> getUiStateLiveDate() {

private void updateSortTypes() {
final SortType[] sortTypes = SortType.values();
final List<SortTypeUI> sortTypesUi = Arrays.stream(sortTypes)
.map(sortType -> new SortTypeUI(sortType, Objects.equals(sortType, selectedSortType)))
final List<SortTypeUi> sortTypesUi = Arrays.stream(sortTypes)
.map(sortType -> new SortTypeUi(sortType, Objects.equals(sortType, selectedSortType)))
.collect(Collectors.toList());
sortTypesLiveDate.setValue(sortTypesUi);
}
Expand Down