From 0fe4325feda81adc1ab6d0916368c32b9dc55e3e Mon Sep 17 00:00:00 2001 From: Ealyk Date: Tue, 16 Apr 2024 20:19:39 +0300 Subject: [PATCH] make private variables in SortUiState, FilterContactUiState and MainUiState classes. SortUiState, FilterContactUiState, MainUiState, MainViewModel, SortViewModel and FilterContactTypeViewModel changed from public to private-package --- .../FilterContactTypeDialogFragment.java | 6 +-- .../filter/FilterContactTypeViewModel.java | 6 +-- .../filter/FilterContactUiState.java | 21 ++++++++-- .../presentation/main/MainActivity.java | 18 ++++----- .../presentation/main/MainUiState.java | 40 ++++++++++++++++--- .../presentation/main/MainViewModel.java | 26 ++++++------ .../presentation/sort/SortDialogFragment.java | 6 +-- .../presentation/sort/SortUiState.java | 19 +++++++-- .../presentation/sort/SortViewModel.java | 6 +-- 9 files changed, 103 insertions(+), 45 deletions(-) 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 2b4ce56..402cc53 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 @@ -54,10 +54,10 @@ private void updateFilterContactTypes(List filterTypes) { } private void updateState(FilterContactUiState state) { - binding.applyButton.setEnabled(state.isApplyEnable); + binding.applyButton.setEnabled(state.getIsApplyEnable()); - if (!state.newSelectedContactTypes.isEmpty()) { - getParentFragmentManager().setFragmentResult(REQUEST_KEY, createBundle(state.newSelectedContactTypes)); + if (!state.getNewSelectedContactTypes().isEmpty()) { + getParentFragmentManager().setFragmentResult(REQUEST_KEY, createBundle(state.getNewSelectedContactTypes())); dismiss(); } } 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 749d0ae..0ae8a7e 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 @@ -17,7 +17,7 @@ import ru.yandex.practicum.contacts.utils.model.ContactTypeUtils; import ru.yandex.practicum.contacts.utils.model.FilterContactTypeUtils; -public class FilterContactTypeViewModel extends BaseBottomSheetViewModel { +class FilterContactTypeViewModel extends BaseBottomSheetViewModel { private final FilterContactUiState uiState = new FilterContactUiState(); private final MutableLiveData> filterContactTypesLiveDate = new MutableLiveData<>(); @@ -41,7 +41,7 @@ public void onFilterTypeItemClick(FilterContactTypeUi filterContactType) { @Override public void onApplyClick() { - uiState.newSelectedContactTypes = selectedFilterContactTypes; + uiState.setNewSelectedContactTypes(selectedFilterContactTypes); updateUiState(); } @@ -75,7 +75,7 @@ private void updateFilterContactTypes() { } private void updateUiState() { - uiState.isApplyEnable = !defaultFilterContactTypes.equals(selectedFilterContactTypes) && !selectedFilterContactTypes.isEmpty(); + uiState.setApplyEnable(!defaultFilterContactTypes.equals(selectedFilterContactTypes) && !selectedFilterContactTypes.isEmpty()); uiStateLiveDate.setValue(uiState); } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactUiState.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactUiState.java index 7fc59c4..1aa0277 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactUiState.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/filter/FilterContactUiState.java @@ -5,7 +5,22 @@ import ru.yandex.practicum.contacts.model.ContactType; -public class FilterContactUiState { - public boolean isApplyEnable = false; - public Set newSelectedContactTypes = Collections.emptySet(); +class FilterContactUiState { + private boolean isApplyEnable = false; + private Set newSelectedContactTypes = Collections.emptySet(); + public boolean getIsApplyEnable(){ + return isApplyEnable; + } + + public Set getNewSelectedContactTypes() { + return newSelectedContactTypes; + } + + public void setApplyEnable(boolean applyEnable) { + isApplyEnable = applyEnable; + } + + public void setNewSelectedContactTypes(Set newSelectedContactTypes) { + this.newSelectedContactTypes = newSelectedContactTypes; + } } 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 8debd26..814ad4f 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 @@ -130,17 +130,17 @@ private void updateContacts(List contacts) { } private void updateUiState(MainUiState uiState) { - final Boolean finishActivity = uiState.actions.finishActivity.data; + final Boolean finishActivity = uiState.getActions().finishActivity.data; if (finishActivity != null && finishActivity) { finish(); return; } - binding.searchLayout.getRoot().setVisibility(uiState.searchVisibility ? View.VISIBLE : View.GONE); - binding.searchLayout.resetButton.setVisibility(uiState.resetSearchButtonVisibility ? View.VISIBLE : View.GONE); - if (uiState.actions.showSortTypeDialog.data != null) { - showSortDialog(uiState.actions.showSortTypeDialog.data); + binding.searchLayout.getRoot().setVisibility(uiState.isSearchVisibility() ? View.VISIBLE : View.GONE); + binding.searchLayout.resetButton.setVisibility(uiState.isResetSearchButtonVisibility() ? View.VISIBLE : View.GONE); + if (uiState.getActions().showSortTypeDialog.data != null) { + showSortDialog(uiState.getActions().showSortTypeDialog.data); } - final Set filterContactTypes = uiState.actions.showFilterContactTypeDialog.data; + final Set filterContactTypes = uiState.getActions().showFilterContactTypeDialog.data; if (filterContactTypes != null && filterContactTypes.size() > 0) { showFilterContactTypeDialog(filterContactTypes); } @@ -148,9 +148,9 @@ private void updateUiState(MainUiState uiState) { } private void updateBadges(MainUiState uiState) { - updateBadge(uiState.menuBadges.sort, R.id.menu_sort); - updateBadge(uiState.menuBadges.filter, R.id.menu_filter); - updateBadge(uiState.menuBadges.search, R.id.menu_search); + updateBadge(uiState.getMenuBadges().sort, R.id.menu_sort); + updateBadge(uiState.getMenuBadges().filter, R.id.menu_filter); + updateBadge(uiState.getMenuBadges().search, R.id.menu_search); } private void updateBadge(MainUiState.MenuBadge badge, @IdRes int menuItemId) { diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainUiState.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainUiState.java index b92e384..b881bb3 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainUiState.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/main/MainUiState.java @@ -8,13 +8,43 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; -public class MainUiState { +class MainUiState { + + private boolean searchVisibility = false; + private boolean resetSearchButtonVisibility = false; + private Actions actions = new Actions(); + private MenuBadges menuBadges = new MenuBadges(); + public Actions getActions() { + return actions; + } + + public boolean isResetSearchButtonVisibility() { + return resetSearchButtonVisibility; + } - public boolean searchVisibility = false; - public boolean resetSearchButtonVisibility = false; + public MenuBadges getMenuBadges() { + return menuBadges; + } - public Actions actions = new Actions(); - public MenuBadges menuBadges = new MenuBadges(); + public boolean isSearchVisibility() { + return searchVisibility; + } + + public void setActions(Actions actions) { + this.actions = actions; + } + + public void setMenuBadges(MenuBadges menuBadges) { + this.menuBadges = menuBadges; + } + + public void setResetSearchButtonVisibility(boolean resetSearchButtonVisibility) { + this.resetSearchButtonVisibility = resetSearchButtonVisibility; + } + + public void setSearchVisibility(boolean searchVisibility) { + this.searchVisibility = searchVisibility; + } @NonNull public MainUiState copy() { 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 9ebef8f..754c095 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 @@ -28,7 +28,7 @@ import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; -public class MainViewModel extends AndroidViewModel { +class MainViewModel extends AndroidViewModel { private final ContactSourceRepository contactSourceRepository; private final ContactRepository contactRepository; @@ -77,13 +77,13 @@ public void search() { public void onMenuClick(MenuClick click) { switch (click) { case SORT: - uiState.actions.showSortTypeDialog.data = state.getSortType(); + uiState.getActions().showSortTypeDialog.data = state.getSortType(); break; case FILTER: - uiState.actions.showFilterContactTypeDialog.data = new HashSet<>(state.getContactTypes()); + uiState.getActions().showFilterContactTypeDialog.data = new HashSet<>(state.getContactTypes()); break; case SEARCH: - uiState.searchVisibility = !uiState.searchVisibility; + uiState.setSearchVisibility(!uiState.isSearchVisibility()); break; } updateUiState(); @@ -102,31 +102,31 @@ public void updateFilterContactTypes(Set filterContactTypes) { } public void onBackPressed() { - if (uiState.searchVisibility) { - uiState.searchVisibility = false; + if (uiState.isSearchVisibility()) { + uiState.setSearchVisibility(false); } else { - uiState.actions.finishActivity.data = true; + uiState.getActions().finishActivity.data = true; } updateUiState(); } public void updateSearchText(String query) { state.setQuery(query); - uiState.resetSearchButtonVisibility = state.getQuery().length() != 0; + uiState.setResetSearchButtonVisibility(state.getQuery().length() != 0); updateUiState(); } private void updateBadges() { if (!state.getSortType().equals(state.getDefaultSortType())) { - uiState.menuBadges.sort = new MainUiState.MenuBadge(0); + uiState.getMenuBadges().sort = new MainUiState.MenuBadge(0); } else { - uiState.menuBadges.sort = null; + uiState.getMenuBadges().sort = null; } if (!state.getContactTypes().equals(state.getDefaultContactTypes())) { - uiState.menuBadges.filter = new MainUiState.MenuBadge(state.getContactTypes().size()); + uiState.getMenuBadges().filter = new MainUiState.MenuBadge(state.getContactTypes().size()); } else { - uiState.menuBadges.filter = null; + uiState.getMenuBadges().filter = null; } updateUiState(); @@ -207,6 +207,6 @@ private Comparator createReversedComparator(Function sortTypes) { } private void updateState(SortUiState state) { - binding.applyButton.setEnabled(state.isApplyEnable); + binding.applyButton.setEnabled(state.getIsApplyEnable()); - if (state.newSelectedSortType != null) { - getParentFragmentManager().setFragmentResult(REQUEST_KEY, createBundle(state.newSelectedSortType)); + if (state.getNewSelectedSortType() != null) { + getParentFragmentManager().setFragmentResult(REQUEST_KEY, createBundle(state.getNewSelectedSortType())); dismiss(); } } diff --git a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortUiState.java b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortUiState.java index 7ce466f..25217f0 100644 --- a/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortUiState.java +++ b/app/src/main/java/ru/yandex/practicum/contacts/presentation/sort/SortUiState.java @@ -1,11 +1,24 @@ package ru.yandex.practicum.contacts.presentation.sort; // класс UiState должен иметь package-private доступ -public class SortUiState { +class SortUiState { // сделайте поля isApplyEnable и newSelectedSortType приватными - public boolean isApplyEnable = false; - public String newSelectedSortType = null; + private boolean isApplyEnable = false; + private String newSelectedSortType = null; + public boolean getIsApplyEnable(){ + return isApplyEnable; + } + public String getNewSelectedSortType(){ + return newSelectedSortType; + } + public void setApplyEnable(boolean applyEnable) { + this.isApplyEnable = applyEnable; + } + + public void setNewSelectedSortType(String newSelectedSortType) { + this.newSelectedSortType = newSelectedSortType; + } // реализуйте get и set методы для обоих полей } 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 13247fa..02296b3 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 @@ -10,7 +10,7 @@ import androidx.lifecycle.MutableLiveData; -public class SortViewModel extends BaseBottomSheetViewModel { +class SortViewModel extends BaseBottomSheetViewModel { private final SortUiState uiState = new SortUiState(); private final MutableLiveData> sortTypesLiveDate = new MutableLiveData<>(); @@ -34,7 +34,7 @@ public void onSortTypeItemClick(SortTypeUI sortType) { @Override public void onApplyClick() { - uiState.newSelectedSortType = selectedSortType; + uiState.setNewSelectedSortType(selectedSortType); updateUiState(); } @@ -62,7 +62,7 @@ private void updateSortTypes() { } private void updateUiState() { - uiState.isApplyEnable = !defaultSortType.equals(selectedSortType); + uiState.setApplyEnable(!defaultSortType.equals(selectedSortType)); uiStateLiveDate.setValue(uiState); } }