remember chat filter selection across rotations

This commit is contained in:
Daniel Gultsch 2023-02-26 19:06:04 +01:00
parent d54978f593
commit b8f3472af0
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
6 changed files with 43 additions and 6 deletions

View file

@ -4,12 +4,19 @@ import com.google.common.base.Preconditions;
import com.google.common.io.BaseEncoding;
import com.google.common.io.ByteSource;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;
public class IDs {
private static final Random RANDOM = new Random();
private static final long UUID_VERSION_MASK = 4 << 12;
public static int quickInt() {
return RANDOM.nextInt();
}
public static String huge() {
final var random = new byte[96];
Conversations.SECURE_RANDOM.nextBytes(random);

View file

@ -50,6 +50,7 @@ public abstract class RosterDao extends GroupDao {
deleteEmptyGroups();
}
@Transaction
public void update(
final Account account, final String version, final Collection<Item> updates) {
for (final Item item : updates) {

View file

@ -31,6 +31,7 @@ public class AccountIdentifier implements ChatFilter {
}
@Override
@NonNull
public String toString() {
return MoreObjects.toStringHelper(this).add("id", id).add("address", address).toString();
}

View file

@ -1,6 +1,8 @@
package im.conversations.android.database.model;
import androidx.annotation.NonNull;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
public class GroupIdentifier implements ChatFilter {
@ -8,6 +10,7 @@ public class GroupIdentifier implements ChatFilter {
public final String name;
@Override
@NonNull
public String toString() {
return MoreObjects.toStringHelper(this).add("id", id).add("name", name).toString();
}
@ -16,4 +19,17 @@ public class GroupIdentifier implements ChatFilter {
this.id = id;
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GroupIdentifier that = (GroupIdentifier) o;
return id == that.id && Objects.equal(name, that.name);
}
@Override
public int hashCode() {
return Objects.hashCode(id, name);
}
}

View file

@ -3,6 +3,7 @@ package im.conversations.android.ui.fragment.main;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@ -13,6 +14,7 @@ import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.elevation.SurfaceColors;
import com.google.android.material.search.SearchView;
import im.conversations.android.IDs;
import im.conversations.android.R;
import im.conversations.android.database.model.AccountIdentifier;
import im.conversations.android.database.model.ChatFilter;
@ -59,7 +61,9 @@ public class OverviewFragment extends Fragment {
}
});
binding.navigationView.setNavigationItemSelectedListener(this::onNavigationItemSelected);
binding.navigationView.setCheckedItem(R.id.chats);
if (this.overviewViewModel.getChatFilter() == null) {
binding.navigationView.setCheckedItem(R.id.chats);
}
this.overviewViewModel
.getAccounts()
.observe(getViewLifecycleOwner(), this::onAccountsUpdated);
@ -117,14 +121,18 @@ public class OverviewFragment extends Fragment {
menuItemSpaces.setVisible(false);
return;
}
final var chatFilter = this.overviewViewModel.getChatFilter();
menuItemSpaces.setVisible(true);
final var subMenu = menuItemSpaces.getSubMenu();
subMenu.clear();
for (final GroupIdentifier group : groups) {
final var menuItemSpace = subMenu.add(group.name);
final var menuItemSpace = subMenu.add(Menu.NONE, IDs.quickInt(), Menu.NONE, group.name);
menuItemSpace.setCheckable(true);
menuItemSpace.setIcon(R.drawable.ic_workspaces_24dp);
menuItemSpace.setIntent(Intents.of(group));
if (group.equals(chatFilter)) {
this.binding.navigationView.setCheckedItem(menuItemSpace);
}
}
}
@ -135,14 +143,19 @@ public class OverviewFragment extends Fragment {
menuItemAccounts.setVisible(false);
return;
}
final var chatFilter = this.overviewViewModel.getChatFilter();
menuItemAccounts.setVisible(true);
final var subMenu = menuItemAccounts.getSubMenu();
subMenu.clear();
for (final AccountIdentifier account : accounts) {
final var menuItemAccount = subMenu.add(account.address);
final var menuItemAccount =
subMenu.add(Menu.NONE, IDs.quickInt(), Menu.NONE, account.address);
menuItemAccount.setCheckable(true);
menuItemAccount.setIcon(R.drawable.ic_person_24dp);
menuItemAccount.setIntent(Intents.of(account));
if (account.equals(chatFilter)) {
this.binding.navigationView.setCheckedItem(menuItemAccount);
}
}
}
}

View file

@ -46,12 +46,11 @@ public class OverviewViewModel extends AndroidViewModel {
}
public LiveData<List<AccountIdentifier>> getAccounts() {
return Transformations.distinctUntilChanged(this.accountRepository.getAccounts());
return Transformations.distinctUntilChanged(this.accounts);
}
// TODO change into GroupIdentifier
public LiveData<List<GroupIdentifier>> getGroups() {
return Transformations.distinctUntilChanged(this.chatRepository.getGroups());
return Transformations.distinctUntilChanged(this.groups);
}
public LiveData<Boolean> getChatFilterAvailable() {