diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/base/ListDiffCallback.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/base/ListDiffCallback.java new file mode 100644 index 000000000..b43a4b007 --- /dev/null +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/base/ListDiffCallback.java @@ -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> extends DiffUtil.ItemCallback { + + @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; + } +} diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/base/ListDiffInterface.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/base/ListDiffInterface.java new file mode 100644 index 000000000..24cd77d09 --- /dev/null +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/base/ListDiffInterface.java @@ -0,0 +1,6 @@ +package ru.yandex.practicum.contacts.presentation.base; + +public interface ListDiffInterface { + boolean theSameAs(T other); + boolean equals(Object o); +} \ No newline at end of file diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeAdapter.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeAdapter.java index 4203bc297..88ea532b1 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeAdapter.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeAdapter.java @@ -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; @@ -26,7 +26,7 @@ public class FilterContactTypeAdapter extends RecyclerView.Adapter differ = new AsyncListDiffer<>( new AdapterListUpdateCallback(this), - new AsyncDifferConfig.Builder<>(new ListDiffCallback()).build() + new AsyncDifferConfig.Builder<>(new ListDiffCallback()).build() ); private final Consumer clickListener; @@ -60,7 +60,6 @@ public void setItems(List items) { static class ViewHolder extends RecyclerView.ViewHolder { private final ItemFilterBinding binding; - private FilterContactTypeUi data; public ViewHolder(@NonNull ItemFilterBinding binding, Consumer clickListener) { @@ -72,36 +71,19 @@ public ViewHolder(@NonNull ItemFilterBinding binding, Consumer { - - @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; - } - } } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeDialogFragment.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeDialogFragment.java index 7e5f7599a..3c8cb1fd3 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeDialogFragment.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeDialogFragment.java @@ -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; @@ -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 { public static final String REQUEST_KEY = "REQUEST_KEY_FILTER"; @@ -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( diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeViewModel.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeViewModel.java index 48c8c805b..9b9635c86 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeViewModel.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactTypeViewModel.java @@ -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; @@ -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(); @@ -34,7 +37,7 @@ public void init(Set defaultFilterContactTypes) { } public void onFilterTypeItemClick(FilterContactTypeUi filterContactType) { - updateSelectedContactTypes(filterContactType.getContactType()); + updateSelectedContactTypes(filterContactType.getType()); updateFilterContactTypes(); updateUiState(); } @@ -60,6 +63,10 @@ public MutableLiveData getUiStateLiveDate() { return uiStateLiveDate; } + public void log(String message) { + Log.d("FilterContactTypeViewModel", message); + } + private void updateFilterContactTypes() { final List filterContactTypesUi = new ArrayList<>(); final boolean allSelected = selectedFilterContactTypes.size() == ContactType.values().length; @@ -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 { @@ -97,6 +104,7 @@ private void updateSelectedContactTypes(FilterContactType type) { } static class UiState { + public boolean isApplyEnable = false; public Set newSelectedContactTypes = Collections.emptySet(); } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/model/FilterContactType.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/model/FilterContactType.java index 6fca0ba88..7c10260ca 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/model/FilterContactType.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/model/FilterContactType.java @@ -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"; } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/model/FilterContactTypeUi.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/model/FilterContactTypeUi.java index fbb187fdf..61ebb41a8 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/model/FilterContactTypeUi.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/model/FilterContactTypeUi.java @@ -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 { - 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; } } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactAdapter.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactAdapter.java index 9c2317248..09fa56d0c 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactAdapter.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactAdapter.java @@ -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; @@ -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 { private final AsyncListDiffer differ = new AsyncListDiffer<>( new AdapterListUpdateCallback(this), - new AsyncDifferConfig.Builder<>(new ListDiffCallback()).build() + new AsyncDifferConfig.Builder(new ListDiffCallback<>()).build() ); @NonNull @@ -65,6 +64,7 @@ public ViewHolder(@NonNull ItemContactBinding binding) { super(binding.getRoot()); this.binding = binding; binding.getRoot().setOnClickListener(view -> { + // По клику можно добавить действие }); } @@ -92,23 +92,4 @@ private void loadAvatar(ContactUi contact) { .into(binding.contactPhoto); } } - - static class ListDiffCallback extends DiffUtil.ItemCallback { - - @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; - } - } } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactUi.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactUi.java index 4b2f216e8..b4554ff7c 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactUi.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/ContactUi.java @@ -3,22 +3,19 @@ import androidx.annotation.NonNull; import java.util.List; +import java.util.Objects; import ru.yandex.practicum.contacts.model.ContactType; +import ru.yandex.practicum.contacts.presentation.base.ListDiffInterface; -public class ContactUi { +public class ContactUi implements ListDiffInterface { private final String name; private final String phone; private final String photo; private final List types; - public ContactUi( - @NonNull String name, - @NonNull String phone, - @NonNull String photo, - @NonNull List types - ) { + public ContactUi(@NonNull String name, @NonNull String phone, @NonNull String photo, @NonNull List types) { this.name = name; this.phone = phone; this.photo = photo; @@ -41,25 +38,25 @@ public List getTypes() { return types; } + @Override + public boolean theSameAs(ContactUi other) { + // Можно использовать любой способ сравнения, вот — через hashCode, как в оригинале: + return this.hashCode() == other.hashCode(); + } + @Override public boolean equals(Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - + if (!(o instanceof ContactUi)) 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); + return name.equals(contact.name) && + phone.equals(contact.phone) && + photo.equals(contact.photo) && + types.equals(contact.types); } @Override public int hashCode() { - int result = name.hashCode(); - result = 31 * result + phone.hashCode(); - result = 31 * result + photo.hashCode(); - result = 31 * result + types.hashCode(); - return result; + return Objects.hash(name, phone, photo, types); } } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainActivity.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainActivity.java index 104653a53..6e825e840 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainActivity.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainActivity.java @@ -69,7 +69,7 @@ protected void onCreate(Bundle savedInstanceState) { binding.searchLayout.resetButton.setOnClickListener(view -> clearSearch()); getSupportFragmentManager().setFragmentResultListener(SortDialogFragment.REQUEST_KEY, this, (requestKey, result) -> { - final SortType newSortType = SortDialogFragment.from(result); + final String newSortType = SortDialogFragment.from(result); viewModel.updateSortType(newSortType); }); @@ -105,7 +105,7 @@ public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); } - private void showSortDialog(SortType sortType) { + private void showSortDialog(String sortType) { SortDialogFragment.newInstance(sortType).show(getSupportFragmentManager(), SORT_TAG); } @@ -194,4 +194,4 @@ private void clearSearch() { private void toast(@StringRes int res) { Toast.makeText(this, res, Toast.LENGTH_SHORT).show(); } -} \ No newline at end of file +} diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainState.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainState.java index dd1708e8a..bf215e1f2 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainState.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainState.java @@ -14,11 +14,11 @@ public class MainState { - private final SortType defaultSortType = SortType.BY_NAME; + private final String defaultSortType = SortType.BY_NAME; private final Set defaultContactTypes = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(ContactType.values()))); private List allContacts = Collections.emptyList(); - private SortType sortType = defaultSortType; + private String sortType = defaultSortType; private Set contactTypes = new HashSet<>(defaultContactTypes); private String query = ""; @@ -32,16 +32,16 @@ public void setAllContacts(@NonNull List allContacts) { } @NonNull - public SortType getDefaultSortType() { + public String getDefaultSortType() { return defaultSortType; } @NonNull - public SortType getSortType() { + public String getSortType() { return sortType; } - public void setSortType(@NonNull SortType sortType) { + public void setSortType(@NonNull String sortType) { this.sortType = sortType; } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainViewModel.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainViewModel.java index 29cfee42b..87929836a 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainViewModel.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainViewModel.java @@ -91,7 +91,7 @@ public void onMenuClick(MenuClick click) { updateUiState(); } - public void updateSortType(SortType sortType) { + public void updateSortType(String sortType) { state.setSortType(sortType); updateBadges(); mapContactsAndUpdate(); @@ -144,24 +144,24 @@ private void mapContactsAndUpdate() { contactsLiveDate.postValue(uiContacts); } - private Comparator createComparator(SortType type) { + private Comparator createComparator(String type) { switch (type) { - case BY_NAME: + case SortType.BY_NAME: return createComparator(MergedContact::getFirstName) .thenComparing(createComparator(MergedContact::getSurname)) .thenComparing(createComparator(MergedContact::getNormalizedNumber)) .thenComparing(createComparator(MergedContact::getEmail)); - case BY_NAME_REVERSED: + case SortType.BY_NAME_REVERSED: return createReversedComparator(MergedContact::getFirstName) .thenComparing(createReversedComparator(MergedContact::getSurname)) .thenComparing(createReversedComparator(MergedContact::getNormalizedNumber)) .thenComparing(createReversedComparator(MergedContact::getEmail)); - case BY_SURNAME: + case SortType.BY_SURNAME: return createComparator(MergedContact::getSurname) .thenComparing(createComparator(MergedContact::getFirstName)) .thenComparing(createComparator(MergedContact::getNormalizedNumber)) .thenComparing(createComparator(MergedContact::getEmail)); - case BY_SURNAME_REVERSED: + case SortType.BY_SURNAME_REVERSED: return createReversedComparator(MergedContact::getSurname) .thenComparing(createReversedComparator(MergedContact::getFirstName)) .thenComparing(createReversedComparator(MergedContact::getNormalizedNumber)) @@ -232,7 +232,7 @@ public UiState copy() { public static class Actions { public Action finishActivity = new Action<>(false); - public Action showSortTypeDialog = new Action<>(null); + public Action showSortTypeDialog = new Action<>(null); public Action> showFilterContactTypeDialog = new Action<>(Collections.emptySet()); @NonNull @@ -287,4 +287,4 @@ public MenuBadge(int value) { } } } -} \ No newline at end of file +} diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortDialogFragment.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortDialogFragment.java index 06ac17cb7..44ccd92a6 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortDialogFragment.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortDialogFragment.java @@ -3,11 +3,6 @@ import android.os.Bundle; import android.view.View; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.core.content.ContextCompat; -import androidx.recyclerview.widget.DividerItemDecoration; - import java.util.List; import java.util.Objects; @@ -15,6 +10,11 @@ import ru.yandex.practicum.contacts.presentation.base.BaseBottomSheetDialogFragment; import ru.yandex.practicum.contacts.presentation.sort.model.SortType; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; +import androidx.recyclerview.widget.DividerItemDecoration; + public class SortDialogFragment extends BaseBottomSheetDialogFragment { public static final String REQUEST_KEY = "REQUEST_KEY_SORT"; @@ -30,7 +30,10 @@ private SortDialogFragment() { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); iniViewModel(); - adapter = new SortTypeAdapter(viewModel::onSortTypeItemClick); + adapter = new SortTypeAdapter((sortType) -> { + viewModel.onSortTypeItemClick(sortType); + viewModel.log(sortType.createLogMessage()); + }); binding.recycler.setAdapter(adapter); final DividerItemDecoration decoration = new DividerItemDecoration(requireActivity(), DividerItemDecoration.VERTICAL); @@ -42,7 +45,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat } private void iniViewModel() { - final SortType defaultSortType = from(getArguments()); + final String defaultSortType = from(getArguments()); viewModel.init(defaultSortType); } @@ -59,22 +62,22 @@ private void updateState(SortViewModel.UiState state) { } } - public static SortDialogFragment newInstance(SortType selectedSortType) { + public static SortDialogFragment newInstance(String selectedSortType) { final SortDialogFragment fragment = new SortDialogFragment(); fragment.setArguments(createBundle(selectedSortType)); return fragment; } - public static SortType from(@Nullable Bundle bundle) { + public static String from(@Nullable Bundle bundle) { if (bundle == null) { return SortType.BY_NAME; } - return (SortType) bundle.getSerializable(ARG_SELECTED_SORT_TYPE); + return bundle.getString(ARG_SELECTED_SORT_TYPE); } - private static Bundle createBundle(SortType sortType) { + private static Bundle createBundle(String sortType) { final Bundle bundle = new Bundle(); - bundle.putSerializable(ARG_SELECTED_SORT_TYPE, sortType); + bundle.putString(ARG_SELECTED_SORT_TYPE, sortType); return bundle; } } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeAdapter.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeAdapter.java index fde5f59c8..63c3f67cc 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeAdapter.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeAdapter.java @@ -5,11 +5,9 @@ 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; @@ -17,13 +15,15 @@ import ru.yandex.practicum.contacts.R; import ru.yandex.practicum.contacts.databinding.ItemSortBinding; +import ru.yandex.practicum.contacts.presentation.base.ListDiffCallback; import ru.yandex.practicum.contacts.presentation.sort.model.SortType; +import ru.yandex.practicum.contacts.presentation.sort.SortTypeUI; public class SortTypeAdapter extends RecyclerView.Adapter { private final AsyncListDiffer differ = new AsyncListDiffer<>( new AdapterListUpdateCallback(this), - new AsyncDifferConfig.Builder<>(new ListDiffCallback()).build() + new AsyncDifferConfig.Builder(new ListDiffCallback<>()).build() ); private final Consumer clickListener; @@ -57,7 +57,6 @@ public void setItems(List items) { static class ViewHolder extends RecyclerView.ViewHolder { private final ItemSortBinding binding; - private SortTypeUI data; public ViewHolder(@NonNull ItemSortBinding binding, Consumer clickListener) { @@ -68,43 +67,24 @@ public ViewHolder(@NonNull ItemSortBinding binding, Consumer clickLi public void bind(SortTypeUI data) { this.data = data; - final int sortResId = resource(data.getSortType()); + final int sortResId = resource(data.getType()); binding.text.setText(sortResId); binding.selected.setVisibility(data.isSelected() ? View.VISIBLE : View.GONE); } - private int resource(SortType sortType) { + private int resource(String sortType) { switch (sortType) { - case BY_NAME: + case SortType.BY_NAME: return R.string.sort_by_name; - case BY_NAME_REVERSED: + case SortType.BY_NAME_REVERSED: return R.string.sort_by_name_reversed; - case BY_SURNAME: + case SortType.BY_SURNAME: return R.string.sort_by_surname; - case BY_SURNAME_REVERSED: + case SortType.BY_SURNAME_REVERSED: return R.string.sort_by_surname_reversed; default: throw new IllegalArgumentException("Not supported SortType"); } } } - - static class ListDiffCallback extends DiffUtil.ItemCallback { - - @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; - } - } } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeUI.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeUI.java index eb71f27cd..7193d57be 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeUI.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortTypeUI.java @@ -2,24 +2,33 @@ import androidx.annotation.NonNull; -import ru.yandex.practicum.contacts.presentation.sort.model.SortType; +import ru.yandex.practicum.contacts.presentation.base.ListDiffInterface; -public class SortTypeUI { +public class SortTypeUI implements ListDiffInterface { - private final SortType sortType; - private final boolean selected; + private final String type; + private final boolean isSelected; - public SortTypeUI(@NonNull SortType sortType, boolean selected) { - this.sortType = sortType; - this.selected = selected; + public SortTypeUI(@NonNull String type, boolean isSelected) { + this.type = type; + this.isSelected = isSelected; } - public SortType getSortType() { - return sortType; + public String getType() { + return type; } public boolean isSelected() { - return selected; + return isSelected; + } + + public String createLogMessage() { + return "Выбран тип сортировки: " + type; + } + + @Override + public boolean theSameAs(SortTypeUI other) { + return this.type.equals(other.type); } @Override @@ -29,14 +38,14 @@ public boolean equals(Object o) { SortTypeUI that = (SortTypeUI) o; - if (selected != that.selected) return false; - return sortType == that.sortType; + if (isSelected != that.isSelected) return false; + return type.equals(that.type); } @Override public int hashCode() { - int result = sortType.hashCode(); - result = 31 * result + (selected ? 1 : 0); + int result = type.hashCode(); + result = 31 * result + (isSelected ? 1 : 0); return result; } } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortViewModel.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortViewModel.java index 1ce55fd6f..2d7f2bc54 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortViewModel.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortViewModel.java @@ -1,7 +1,6 @@ package ru.yandex.practicum.contacts.presentation.sort; -import androidx.annotation.Nullable; -import androidx.lifecycle.MutableLiveData; +import android.util.Log; import java.util.Arrays; import java.util.List; @@ -11,16 +10,19 @@ import ru.yandex.practicum.contacts.presentation.base.BaseBottomSheetViewModel; import ru.yandex.practicum.contacts.presentation.sort.model.SortType; +import androidx.annotation.Nullable; +import androidx.lifecycle.MutableLiveData; + public class SortViewModel extends BaseBottomSheetViewModel { private final UiState uiState = new UiState(); private final MutableLiveData> sortTypesLiveDate = new MutableLiveData<>(); private final MutableLiveData uiStateLiveDate = new MutableLiveData<>(); - private SortType defaultSortType; - private SortType selectedSortType; + private String defaultSortType; + private String selectedSortType; - public void init(SortType defaultSortType) { + public void init(String defaultSortType) { this.defaultSortType = defaultSortType; this.selectedSortType = defaultSortType; updateSortTypes(); @@ -28,7 +30,7 @@ public void init(SortType defaultSortType) { } public void onSortTypeItemClick(SortTypeUI sortType) { - selectedSortType = sortType.getSortType(); + selectedSortType = sortType.getType(); updateSortTypes(); updateUiState(); } @@ -54,8 +56,12 @@ public MutableLiveData getUiStateLiveDate() { return uiStateLiveDate; } + public void log(String message) { + Log.d("SortViewModel", message); + } + private void updateSortTypes() { - final SortType[] sortTypes = SortType.values(); + final String[] sortTypes = SortType.values(); final List sortTypesUi = Arrays.stream(sortTypes) .map(sortType -> new SortTypeUI(sortType, Objects.equals(sortType, selectedSortType))) .collect(Collectors.toList()); @@ -68,7 +74,9 @@ private void updateUiState() { } static class UiState { + public boolean isApplyEnable = false; - @Nullable public SortType newSelectedSortType = null; + @Nullable + public String newSelectedSortType = null; } } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/model/SortType.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/model/SortType.java index 31844dcb6..3335e6c55 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/model/SortType.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/model/SortType.java @@ -1,8 +1,13 @@ package ru.yandex.practicum.contacts.presentation.sort.model; -public enum SortType { - BY_NAME, - BY_NAME_REVERSED, - BY_SURNAME, - BY_SURNAME_REVERSED +public class SortType { + + public static final String BY_NAME = "by_name"; + public static final String BY_NAME_REVERSED = "by_name_reversed"; + public static final String BY_SURNAME = "by_surname"; + public static final String BY_SURNAME_REVERSED = "by_surname_reversed"; + + public static String[] values() { + return new String[]{BY_NAME, BY_NAME_REVERSED, BY_SURNAME, BY_SURNAME_REVERSED}; + } } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/utils/model/ContactTypeUtils.java b/app/src/main/java/ru/yandex/practicum/contacts/utils/model/ContactTypeUtils.java index d9dc22743..5b2887c91 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/utils/model/ContactTypeUtils.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/utils/model/ContactTypeUtils.java @@ -33,7 +33,7 @@ public static int getIconRes(@NonNull ContactType type) { } } - public static FilterContactType toFilterContactType(ContactType type) { + public static String toFilterContactType(ContactType type) { switch (type) { case TELEGRAM: return FilterContactType.TELEGRAM; diff --git a/app/src/main/java/ru/yandex/practicum/contacts/utils/model/FilterContactTypeUtils.java b/app/src/main/java/ru/yandex/practicum/contacts/utils/model/FilterContactTypeUtils.java index 030af2824..be0dd69df 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/utils/model/FilterContactTypeUtils.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/utils/model/FilterContactTypeUtils.java @@ -10,23 +10,23 @@ public class FilterContactTypeUtils { @StringRes - public static int getStringRes(FilterContactType contactType) { + public static int getStringRes(String contactType) { switch (contactType) { - case ALL: + case FilterContactType.ALL: return R.string.filter_contact_type_all; - case TELEGRAM: + case FilterContactType.TELEGRAM: return R.string.filter_contact_type_telegram; - case WHATS_APP: + case FilterContactType.WHATS_APP: return R.string.filter_contact_type_whatsapp; - case VIBER: + case FilterContactType.VIBER: return R.string.filter_contact_type_viber; - case SIGNAL: + case FilterContactType.SIGNAL: return R.string.filter_contact_type_signal; - case THREEMA: + case FilterContactType.THREEMA: return R.string.filter_contact_type_threema; - case PHONE: + case FilterContactType.PHONE: return R.string.filter_contact_type_phone; - case EMAIL: + case FilterContactType.EMAIL: return R.string.filter_contact_type_email; default: throw new IllegalArgumentException("Not supported SortType"); @@ -34,21 +34,21 @@ public static int getStringRes(FilterContactType contactType) { } @NonNull - public static ContactType toContactType(FilterContactType type) { + public static ContactType toContactType(String type) { switch (type) { - case TELEGRAM: + case FilterContactType.TELEGRAM: return ContactType.TELEGRAM; - case WHATS_APP: + case FilterContactType.WHATS_APP: return ContactType.WHATS_APP; - case VIBER: + case FilterContactType.VIBER: return ContactType.VIBER; - case SIGNAL: + case FilterContactType.SIGNAL: return ContactType.SIGNAL; - case THREEMA: + case FilterContactType.THREEMA: return ContactType.THREEMA; - case PHONE: + case FilterContactType.PHONE: return ContactType.PHONE; - case EMAIL: + case FilterContactType.EMAIL: return ContactType.EMAIL; default: throw new IllegalArgumentException("Not supported FilterContactType"); diff --git a/app/src/main/res/layout/item_filter.xml b/app/src/main/res/layout/item_filter.xml index 046a27fdb..c23ae417c 100644 --- a/app/src/main/res/layout/item_filter.xml +++ b/app/src/main/res/layout/item_filter.xml @@ -48,4 +48,4 @@ app:layout_constraintTop_toTopOf="parent" tools:ignore="ContentDescription" /> - \ No newline at end of file +