From 867db9d54c0d1f9792146fd44525d78b5a52ac7f Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 19 Feb 2023 18:25:38 +0100 Subject: [PATCH] toggle between 'chats' and 'all chats' --- .../ui/fragment/main/OverviewFragment.java | 19 +++++++++++---- .../android/ui/model/OverviewViewModel.java | 24 +++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/im/conversations/android/ui/fragment/main/OverviewFragment.java b/app/src/main/java/im/conversations/android/ui/fragment/main/OverviewFragment.java index aaf281fbd..5fdd9ece1 100644 --- a/app/src/main/java/im/conversations/android/ui/fragment/main/OverviewFragment.java +++ b/app/src/main/java/im/conversations/android/ui/fragment/main/OverviewFragment.java @@ -34,10 +34,7 @@ public class OverviewFragment extends Fragment { new ViewModelProvider(this, getDefaultViewModelProviderFactory()); this.overviewViewModel = viewModelProvider.get(OverviewViewModel.class); binding.setLifecycleOwner(getViewLifecycleOwner()); - binding.searchBar.setNavigationOnClickListener( - view -> { - binding.drawerLayout.open(); - }); + binding.searchBar.setNavigationOnClickListener(view -> binding.drawerLayout.open()); binding.searchView.addTransitionListener( (searchView, previousState, newState) -> { final var activity = requireActivity(); @@ -55,6 +52,7 @@ public class OverviewFragment extends Fragment { item -> { if (item.getItemId() == R.id.add_account) { startActivity(new Intent(requireContext(), SetupActivity.class)); + return false; } return true; }); @@ -63,9 +61,22 @@ public class OverviewFragment extends Fragment { .getAccounts() .observe(getViewLifecycleOwner(), this::onAccountsUpdated); this.overviewViewModel.getGroups().observe(getViewLifecycleOwner(), this::onGroupsUpdated); + this.overviewViewModel + .getChatFilterAvailable() + .observe(getViewLifecycleOwner(), this::onChatFilterAvailable); return binding.getRoot(); } + private void onChatFilterAvailable(final Boolean available) { + final var menu = this.binding.navigationView.getMenu(); + final var chatsMenuItem = menu.findItem(R.id.chats); + if (Boolean.TRUE.equals(available)) { + chatsMenuItem.setTitle(R.string.all_chats); + } else { + chatsMenuItem.setTitle(R.string.chats); + } + } + private void onGroupsUpdated(final List groups) { final var menu = this.binding.navigationView.getMenu(); final var menuItemSpaces = menu.findItem(R.id.spaces); diff --git a/app/src/main/java/im/conversations/android/ui/model/OverviewViewModel.java b/app/src/main/java/im/conversations/android/ui/model/OverviewViewModel.java index 97ee35059..58d123a40 100644 --- a/app/src/main/java/im/conversations/android/ui/model/OverviewViewModel.java +++ b/app/src/main/java/im/conversations/android/ui/model/OverviewViewModel.java @@ -4,21 +4,41 @@ import android.app.Application; import androidx.annotation.NonNull; import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; +import androidx.lifecycle.MediatorLiveData; import androidx.lifecycle.Transformations; import im.conversations.android.database.model.AccountIdentifier; import im.conversations.android.repository.AccountRepository; import im.conversations.android.repository.ChatRepository; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class OverviewViewModel extends AndroidViewModel { private final AccountRepository accountRepository; private final ChatRepository chatRepository; + private final LiveData> accounts; + private final LiveData> groups; + private final MediatorLiveData chatFilterAvailable = new MediatorLiveData<>(); + + private static final Logger LOGGER = LoggerFactory.getLogger(OverviewViewModel.class); public OverviewViewModel(@NonNull Application application) { super(application); this.accountRepository = new AccountRepository(application); this.chatRepository = new ChatRepository(application); + this.accounts = this.accountRepository.getAccounts(); + this.groups = this.chatRepository.getGroups(); + this.chatFilterAvailable.addSource( + this.accounts, accounts -> setChatFilterAvailable(accounts, groups.getValue())); + this.chatFilterAvailable.addSource( + this.groups, groups -> setChatFilterAvailable(this.accounts.getValue(), groups)); + } + + private void setChatFilterAvailable( + final List accounts, final List groups) { + this.chatFilterAvailable.setValue( + (accounts != null && accounts.size() > 1) || (groups != null && groups.size() > 0)); } public LiveData> getAccounts() { @@ -28,4 +48,8 @@ public class OverviewViewModel extends AndroidViewModel { public LiveData> getGroups() { return Transformations.distinctUntilChanged(this.chatRepository.getGroups()); } + + public LiveData getChatFilterAvailable() { + return Transformations.distinctUntilChanged(this.chatFilterAvailable); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3f7d63d1c..bc62d3cf9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1016,5 +1016,6 @@ All chats Accounts Spaces + Chats