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,25 @@
// ListDiffCallback.java
package ru.yandex.practicum.contacts.presentation.base;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;

public class ListDiffCallback<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 other);
boolean equals(Object o);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
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.Objects;
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.ListDiffCallback;
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 +26,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 ListDiffCallback<FilterContactTypeUi>()).build()
);

private final Consumer<FilterContactTypeUi> clickListener;
Expand Down Expand Up @@ -60,7 +60,6 @@ public void setItems(List<FilterContactTypeUi> items) {
static class ViewHolder extends RecyclerView.ViewHolder {

private final ItemFilterBinding binding;

private FilterContactTypeUi data;

public ViewHolder(@NonNull ItemFilterBinding binding, Consumer<FilterContactTypeUi> clickListener) {
Expand All @@ -72,36 +71,19 @@ public ViewHolder(@NonNull ItemFilterBinding binding, Consumer<FilterContactType

public void bind(FilterContactTypeUi data) {
this.data = data;
final int sortResId = FilterContactTypeUtils.getStringRes(data.getContactType());

final int sortResId = FilterContactTypeUtils.getStringRes(data.getType());
binding.text.setText(sortResId);
binding.selected.setChecked(data.isSelected());
if (data.getContactType() == FilterContactType.ALL){

if (Objects.equals(data.getType(), FilterContactType.ALL)) {
binding.logo.setVisibility(View.GONE);
} else {
final ContactType contactType = FilterContactTypeUtils.toContactType(data.getContactType());
final ContactType contactType = FilterContactTypeUtils.toContactType(data.getType());
final int iconRes = ContactTypeUtils.getIconRes(contactType);
binding.logo.setVisibility(View.VISIBLE);
binding.logo.setImageResource(iconRes);
}
}
}

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 @@ -3,9 +3,6 @@
import android.os.Bundle;
import android.view.View;

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

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand All @@ -17,6 +14,9 @@
import ru.yandex.practicum.contacts.presentation.filter.model.FilterContactTypeUi;
import ru.yandex.practicum.contacts.ui.widget.DividerItemDecoration;

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

public class FilterContactTypeDialogFragment extends BaseBottomSheetDialogFragment<FilterContactTypeViewModel> {

public static final String REQUEST_KEY = "REQUEST_KEY_FILTER";
Expand All @@ -32,7 +32,10 @@ private FilterContactTypeDialogFragment() {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
iniViewModel();
adapter = new FilterContactTypeAdapter(viewModel::onFilterTypeItemClick);
adapter = new FilterContactTypeAdapter((filterContactType) -> {
viewModel.onFilterTypeItemClick(filterContactType);
viewModel.log(filterContactType.createLogMessage());
});
binding.recycler.setAdapter(adapter);

final DividerItemDecoration decoration = new DividerItemDecoration(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ru.yandex.practicum.contacts.presentation.filter;

import androidx.lifecycle.MutableLiveData;
import android.util.Log;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -17,6 +18,8 @@
import ru.yandex.practicum.contacts.utils.model.ContactTypeUtils;
import ru.yandex.practicum.contacts.utils.model.FilterContactTypeUtils;

import androidx.lifecycle.MutableLiveData;

public class FilterContactTypeViewModel extends BaseBottomSheetViewModel {

private final UiState uiState = new UiState();
Expand All @@ -34,7 +37,7 @@ public void init(Set<ContactType> defaultFilterContactTypes) {
}

public void onFilterTypeItemClick(FilterContactTypeUi filterContactType) {
updateSelectedContactTypes(filterContactType.getContactType());
updateSelectedContactTypes(filterContactType.getType());
updateFilterContactTypes();
updateUiState();
}
Expand All @@ -60,6 +63,10 @@ public MutableLiveData<UiState> getUiStateLiveDate() {
return uiStateLiveDate;
}

public void log(String message) {
Log.d("FilterContactTypeViewModel", message);
}

private void updateFilterContactTypes() {
final List<FilterContactTypeUi> filterContactTypesUi = new ArrayList<>();
final boolean allSelected = selectedFilterContactTypes.size() == ContactType.values().length;
Expand All @@ -79,8 +86,8 @@ private void updateUiState() {
uiStateLiveDate.setValue(uiState);
}

private void updateSelectedContactTypes(FilterContactType type) {
if (type == FilterContactType.ALL) {
private void updateSelectedContactTypes(String type) {
if (Objects.equals(type, FilterContactType.ALL)) {
if (selectedFilterContactTypes.size() == ContactType.values().length) {
selectedFilterContactTypes.clear();
} else {
Expand All @@ -97,6 +104,7 @@ private void updateSelectedContactTypes(FilterContactType type) {
}

static class UiState {

public boolean isApplyEnable = false;
public Set<ContactType> newSelectedContactTypes = Collections.emptySet();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ru.yandex.practicum.contacts.presentation.filter.model;

public enum FilterContactType {
ALL,
TELEGRAM,
WHATS_APP,
VIBER,
SIGNAL,
THREEMA,
PHONE,
EMAIL
public class FilterContactType {
public final static String ALL = "all";
public final static String TELEGRAM = "telegram";
public final static String WHATS_APP = "whats_app";
public final static String VIBER = "viber";
public final static String SIGNAL = "signal";
public final static String THREEMA = "threema";
public final static String PHONE = "phone";
public final static String EMAIL = "email";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,49 @@

import androidx.annotation.NonNull;

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

private final FilterContactType contactType;
private final boolean selected;
public class FilterContactTypeUi implements ListDiffInterface<FilterContactTypeUi> {

public FilterContactTypeUi(@NonNull FilterContactType contactType, boolean selected) {
this.contactType = contactType;
this.selected = selected;
private final String type;
private final boolean isSelected;

public FilterContactTypeUi(@NonNull String type, boolean isSelected) {
this.type = type;
this.isSelected = isSelected;
}

public FilterContactType getContactType() {
return contactType;
public String getType() {
return type;
}

public boolean isSelected() {
return selected;
return isSelected;
}

public String createLogMessage() {
return "Выбран фильтр: " + type;
}

@Override
public boolean theSameAs(FilterContactTypeUi other) {
return this.type.equals(other.type);
}

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

FilterContactTypeUi that = (FilterContactTypeUi) o;

if (selected != that.selected) return false;
return contactType == that.contactType;
return isSelected == that.isSelected && type.equals(that.type);
}

@Override
public int hashCode() {
int result = contactType.hashCode();
result = 31 * result + (selected ? 1 : 0);
int result = type.hashCode();
result = 31 * result + (isSelected ? 1 : 0);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
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;
Expand All @@ -23,12 +21,13 @@

import ru.yandex.practicum.contacts.R;
import ru.yandex.practicum.contacts.databinding.ItemContactBinding;
import ru.yandex.practicum.contacts.presentation.base.ListDiffCallback;

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<ContactUi>(new ListDiffCallback<>()).build()
);

@NonNull
Expand Down Expand Up @@ -65,6 +64,7 @@ public ViewHolder(@NonNull ItemContactBinding binding) {
super(binding.getRoot());
this.binding = binding;
binding.getRoot().setOnClickListener(view -> {
// По клику можно добавить действие
});
}

Expand Down Expand Up @@ -92,23 +92,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;
}
}
}
Loading