toggle between 'chats' and 'all chats'

This commit is contained in:
Daniel Gultsch 2023-02-19 18:25:38 +01:00
parent 87e33a779f
commit 867db9d54c
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
3 changed files with 40 additions and 4 deletions

View file

@ -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<String> groups) {
final var menu = this.binding.navigationView.getMenu();
final var menuItemSpaces = menu.findItem(R.id.spaces);

View file

@ -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<List<AccountIdentifier>> accounts;
private final LiveData<List<String>> groups;
private final MediatorLiveData<Boolean> 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<AccountIdentifier> accounts, final List<String> groups) {
this.chatFilterAvailable.setValue(
(accounts != null && accounts.size() > 1) || (groups != null && groups.size() > 0));
}
public LiveData<List<AccountIdentifier>> getAccounts() {
@ -28,4 +48,8 @@ public class OverviewViewModel extends AndroidViewModel {
public LiveData<List<String>> getGroups() {
return Transformations.distinctUntilChanged(this.chatRepository.getGroups());
}
public LiveData<Boolean> getChatFilterAvailable() {
return Transformations.distinctUntilChanged(this.chatFilterAvailable);
}
}

View file

@ -1016,5 +1016,6 @@
<string name="all_chats">All chats</string>
<string name="accounts">Accounts</string>
<string name="spaces">Spaces</string>
<string name="chats">Chats</string>
</resources>