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 @@ -7,5 +7,18 @@ public enum ContactType {
SIGNAL,
THREEMA,
PHONE,
EMAIL
EMAIL;

public static String[] getContactTypes() {
ContactType[] types = values();
String[] result = new String[types.length];

for (int i = 0; i < types.length; i++) {
result[i] = types[i].name();
}

return result;
}
}


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.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);
}

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

public interface ListDiffInterface<T> {
boolean theSameAs(T other);
boolean equals(Object object);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,8 +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
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,34 @@ public MutableLiveData<UiState> getUiStateLiveDate() {
return uiStateLiveDate;
}

private FilterContactTypeUi createFilterContactType(String type) {
ContactType contactType = ContactType.valueOf(type);

return new FilterContactTypeUi(
ContactTypeUtils.toFilterContactType(contactType),
selectedFilterContactTypes.contains(contactType)
);
}

private FilterContactTypeUi createAllSelectedItem(String[] types) {
boolean allSelected = selectedFilterContactTypes.size() == types.length;

return new FilterContactTypeUi(
FilterContactType.ALL,
allSelected
);
}

private void updateFilterContactTypes() {
final List<FilterContactTypeUi> filterContactTypesUi = new ArrayList<>();
final boolean allSelected = selectedFilterContactTypes.size() == ContactType.values().length;
filterContactTypesUi.add(new FilterContactTypeUi(FilterContactType.ALL, allSelected));
final List<FilterContactTypeUi> collect = Arrays.stream(ContactType.values())
.map(contactType -> new FilterContactTypeUi(
ContactTypeUtils.toFilterContactType(contactType),
selectedFilterContactTypes.contains(contactType)
))
.collect(Collectors.toList());
filterContactTypesUi.addAll(collect);
String[] types = ContactType.getContactTypes();

ArrayList<FilterContactTypeUi> filterContactTypesUi = new ArrayList<>();
filterContactTypesUi.add(createAllSelectedItem(types));

for (String type : types) {
filterContactTypesUi.add(createFilterContactType(type));
}

filterContactTypesLiveDate.setValue(filterContactTypesUi);
}

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 this.getContactType() == other.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.

Лучше other также проверить на null, иначе может быть NPE

}

@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 @@ -23,13 +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<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
@Override
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,10 @@ public List<ContactType> getTypes() {
return types;
}

public boolean theSameAs(ContactUi other) {
return this.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 @@ -17,14 +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<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
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 this.getSortType() == other.getSortType();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.6.1' apply false
id 'com.android.library' version '8.6.1' apply false
id 'com.android.application' version '8.13.2' apply false
id 'com.android.library' version '8.13.2' apply false
id 'org.jetbrains.kotlin.android' version '2.0.20' apply false
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Oct 17 01:49:43 MSK 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists