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
Expand Up @@ -11,6 +11,7 @@ public class ContactType {
public static final String EMAIL = "email";

public static String[] getContactTypes() {
// метод должен возвращать массив строк, перечисленных выше
String[] contacts = {TELEGRAM, WHATS_APP, VIBER, SIGNAL, THREEMA, PHONE, EMAIL};
return contacts;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏫ Можно сразу вернуть return new String[]{TELEGRAM, WHATS_APP, VIBER, SIGNAL, THREEMA, PHONE, EMAIL};

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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> {
public BaseListDiffCallback() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏫ Так как конструктор пустой, можно убрать его описание отсюда

super();
}
@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 false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ По аналогии с локальными реализациями ListDiffCallback, тут нужно сравнивать oldItem.equals(newItem);

}

@Nullable
@Override
public Object getChangePayload(@NonNull T oldItem, @NonNull T newItem) {
return super.getChangePayload(oldItem, newItem);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Необходимо возвращать новый айтем: return newItem

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ru.yandex.practicum.contacts.presentation.base;

public interface ListDiffInterface<T> {
boolean theSameAs(ListDiffInterface<T> listDiffInterface);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Здесь типизированный интерфейс для того чтобы сравнивать элементы тип T. Поэтому необходимо правильно описать метод: boolean theSameAs(T listDiffInterface);

boolean equals(Object o);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,27 @@
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.AdapterListUpdateCallback;
import androidx.recyclerview.widget.AsyncDifferConfig;
import androidx.recyclerview.widget.AsyncListDiffer;
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.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;
import ru.yandex.practicum.contacts.utils.model.FilterContactTypeUtils;

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;

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;
Expand Down Expand Up @@ -84,23 +83,4 @@ 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
@@ -1,5 +1,6 @@
package ru.yandex.practicum.contacts.presentation.filter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -60,13 +61,16 @@ public MutableLiveData<UiState> getUiStateLiveDate() {
}

private void updateFilterContactTypes() {
// создайте массив строк. Для инициализации используйте метод ContactType.getContactTypes(),
// который должен возвращать список всех доступных источников контактов
String[] types = ContactType.getContactTypes();

// создайте список типа FilterContactTypeUi и заполните его с помощью цикла forEach
// forEach должен бежать по строковому массиву, который вы создали ранее
ArrayList<FilterContactTypeUi> filterContactTypesUi = new ArrayList<>();
filterContactTypesUi.add(createAllSelectedItem(types));

// вызовите меотод setValue() у переменной filterContactTypesLiveDate и передайте в качестве аргументы ваш список типа FilterContactTypeUi
for (String type : types) {
filterContactTypesUi.add(createFilterContactType(type));
}

filterContactTypesLiveDate.setValue(filterContactTypesUi);
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

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 @@ -21,6 +26,11 @@ public boolean isSelected() {
}

@Override
public boolean theSameAs(ListDiffInterface<FilterContactTypeUi> listDiffInterface) {
return this.getContactType() == ((FilterContactTypeUi) listDiffInterface).getContactType();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Когда будет правильно описан интерфейс, реализация здесь станет корректной и красивой: return this.getContactType() == listDiffInterface.getContactType();

}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import androidx.core.content.ContextCompat;
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 com.bumptech.glide.Glide;

import java.util.List;
import java.util.Objects;

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

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

@NonNull
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
Expand Down Expand Up @@ -93,22 +93,5 @@ private void loadAvatar(ContactUi contact) {
}
}

static class ListDiffCallback extends DiffUtil.ItemCallback<ContactUi> {

@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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,31 @@
import androidx.annotation.NonNull;

import java.util.List;

import java.lang.String;
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;
private final String photo;
private final List<String> types;
private final List<ContactType> types;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ContactType содержит в себе существующие типы в строковом виде. Так что здесь по смыслу должен быть список строк, как и было изначально.


public ContactUi(
@NonNull String name,
@NonNull String phone,
@NonNull String photo,
@NonNull List<String> types
@NonNull List<ContactType> types
) {
this.name = name;
this.phone = phone;
this.photo = photo;
this.types = types;
}



public String getName() {
return name;
}
Expand All @@ -37,8 +40,15 @@ public String getPhoto() {
return photo;
}

public List<String> getTypes() {
return types;
public List<ContactType> getTypes() {
return types;
}



@Override
public boolean theSameAs(ListDiffInterface<ContactUi> listDiffInterface) {
return this.hashCode() == ((ContactUi)listDiffInterface).hashCode();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Когда будет правильно описан интерфейс, реализация здесь станет корректной и красивой: return this.hashCode() == listDiffInterface.hashCode();

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@
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.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<>(
new AdapterListUpdateCallback(this),
new AsyncDifferConfig.Builder<>(new ListDiffCallback()).build()
new AsyncDifferConfig.Builder<>(new BaseListDiffCallback<SortTypeUI>()).build()
);

private final Consumer<SortTypeUI> clickListener;
Expand Down Expand Up @@ -88,23 +87,4 @@ private int resource(SortType sortType) {
}
}
}

static class ListDiffCallback extends DiffUtil.ItemCallback<SortTypeUI> {

@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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

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 @@ -14,15 +18,20 @@ public SortTypeUI(@NonNull SortType sortType, boolean selected) {
this.selected = selected;
}

public SortType getSortType() {
return sortType;
}
public SortType getSortType() {
return sortType;
}

public boolean isSelected() {
return selected;
}

@Override
@Override
public boolean theSameAs(ListDiffInterface<SortTypeUI> listDiffInterface) {
return this.getSortType() == ((SortTypeUI)listDiffInterface).getSortType();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Когда будет правильно описан интерфейс, реализация здесь станет корректной и красивой: return this.getSortType() == listDiffInterface.getSortType();

}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Expand Down