From b8f3472af0ba9403d9228a48a455be9d6dadc611 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 26 Feb 2023 19:06:04 +0100 Subject: [PATCH] remember chat filter selection across rotations --- .../java/im/conversations/android/IDs.java | 7 +++++++ .../android/database/dao/RosterDao.java | 1 + .../database/model/AccountIdentifier.java | 1 + .../database/model/GroupIdentifier.java | 16 ++++++++++++++++ .../ui/fragment/main/OverviewFragment.java | 19 ++++++++++++++++--- .../android/ui/model/OverviewViewModel.java | 5 ++--- 6 files changed, 43 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/im/conversations/android/IDs.java b/app/src/main/java/im/conversations/android/IDs.java index a0945288f..23e973675 100644 --- a/app/src/main/java/im/conversations/android/IDs.java +++ b/app/src/main/java/im/conversations/android/IDs.java @@ -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); diff --git a/app/src/main/java/im/conversations/android/database/dao/RosterDao.java b/app/src/main/java/im/conversations/android/database/dao/RosterDao.java index ce59748e1..fc4829071 100644 --- a/app/src/main/java/im/conversations/android/database/dao/RosterDao.java +++ b/app/src/main/java/im/conversations/android/database/dao/RosterDao.java @@ -50,6 +50,7 @@ public abstract class RosterDao extends GroupDao { deleteEmptyGroups(); } + @Transaction public void update( final Account account, final String version, final Collection updates) { for (final Item item : updates) { diff --git a/app/src/main/java/im/conversations/android/database/model/AccountIdentifier.java b/app/src/main/java/im/conversations/android/database/model/AccountIdentifier.java index 048a321cc..39c3206ad 100644 --- a/app/src/main/java/im/conversations/android/database/model/AccountIdentifier.java +++ b/app/src/main/java/im/conversations/android/database/model/AccountIdentifier.java @@ -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(); } diff --git a/app/src/main/java/im/conversations/android/database/model/GroupIdentifier.java b/app/src/main/java/im/conversations/android/database/model/GroupIdentifier.java index f260ba6b1..11fcb8e64 100644 --- a/app/src/main/java/im/conversations/android/database/model/GroupIdentifier.java +++ b/app/src/main/java/im/conversations/android/database/model/GroupIdentifier.java @@ -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); + } } 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 368238ff6..2e5bb4811 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 @@ -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); + } } } } 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 85155b5d0..714a780c4 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 @@ -46,12 +46,11 @@ public class OverviewViewModel extends AndroidViewModel { } public LiveData> getAccounts() { - return Transformations.distinctUntilChanged(this.accountRepository.getAccounts()); + return Transformations.distinctUntilChanged(this.accounts); } - // TODO change into GroupIdentifier public LiveData> getGroups() { - return Transformations.distinctUntilChanged(this.chatRepository.getGroups()); + return Transformations.distinctUntilChanged(this.groups); } public LiveData getChatFilterAvailable() {