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
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ android {
buildFeatures {
viewBinding true
}
namespace 'ru.yandex.practicum.contacts'
}

dependencies {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.yandex.practicum.contacts">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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,6 @@
package ru.yandex.practicum.contacts.presentation.base;

public interface ListDiffInterface<T>{
boolean theSameAs(T t);
boolean equals(Object o);
}
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,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 @@ -85,23 +86,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
Expand Up @@ -2,7 +2,10 @@

import androidx.annotation.NonNull;

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

public class FilterContactTypeUi implements ListDiffInterface<FilterContactTypeUi> {

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

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

@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,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<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 Expand Up @@ -92,23 +93,4 @@ private void loadAvatar(ContactUi contact) {
.into(binding.contactPhoto);
}
}

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 @@ -5,8 +5,10 @@
import java.util.List;

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

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

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

@Override
public boolean theSameAs(ContactUi contactUi) {
return this.hashCode() == contactUi.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,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<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 +89,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,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 sortTypeUI) {
return this.getSortType() == sortTypeUI.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,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
}

Expand Down
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 @@
#Tue Apr 26 22:12:06 CEST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME