From 1adb2b2caa1d2c76016cccddf2c83f8c283df607 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 21 Apr 2023 11:00:06 -0400 Subject: [PATCH] Fix crashing when changing users. --- .../amethyst/ui/navigation/Routes.kt | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/Routes.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/Routes.kt index 216a5531f..f0266f41a 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/Routes.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/Routes.kt @@ -11,6 +11,7 @@ import com.vitorpamplona.amethyst.NotificationCache import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Note +import com.vitorpamplona.amethyst.ui.dal.AdditiveFeedFilter import com.vitorpamplona.amethyst.ui.dal.ChatroomListKnownFeedFilter import com.vitorpamplona.amethyst.ui.dal.HomeNewThreadFeedFilter import com.vitorpamplona.amethyst.ui.dal.NotificationFeedFilter @@ -109,9 +110,29 @@ fun currentRoute(navController: NavHostController): String? { return navBackStackEntry?.destination?.route } -object HomeLatestItem { - private var newestItem: Note? = null +open class LatestItem { + var newestItemPerAccount: Map = mapOf() + fun updateNewestItem(newNotes: Set, account: Account, filter: AdditiveFeedFilter): Note? { + val newestItem = newestItemPerAccount[account.userProfile().pubkeyHex] + + if (newestItem == null) { + newestItemPerAccount = newestItemPerAccount + Pair( + account.userProfile().pubkeyHex, + filter.feed().firstOrNull { it.createdAt() != null } + ) + } else { + newestItemPerAccount = newestItemPerAccount + Pair( + account.userProfile().pubkeyHex, + filter.sort(filter.applyFilter(newNotes) + newestItem).first() + ) + } + + return newestItemPerAccount[account.userProfile().pubkeyHex] + } +} + +object HomeLatestItem : LatestItem() { fun hasNewItems( account: Account, cache: NotificationCache, @@ -120,22 +141,13 @@ object HomeLatestItem { val lastTime = cache.load("HomeFollows") HomeNewThreadFeedFilter.account = account - if (newestItem == null) { - newestItem = HomeNewThreadFeedFilter.feed().firstOrNull { it.createdAt() != null } - } else { - newestItem = - HomeNewThreadFeedFilter.sort( - HomeNewThreadFeedFilter.applyFilter(newNotes + newestItem!!) - ).first() - } + val newestItem = updateNewestItem(newNotes, account, HomeNewThreadFeedFilter) return (newestItem?.createdAt() ?: 0) > lastTime } } -object NotificationLatestItem { - private var newestItem: Note? = null - +object NotificationLatestItem : LatestItem() { fun hasNewItems( account: Account, cache: NotificationCache, @@ -144,21 +156,13 @@ object NotificationLatestItem { val lastTime = cache.load("Notification") NotificationFeedFilter.account = account - if (newestItem == null) { - newestItem = NotificationFeedFilter.feed().firstOrNull { it.createdAt() != null } - } else { - newestItem = HomeNewThreadFeedFilter.sort( - NotificationFeedFilter.applyFilter(newNotes) + newestItem!! - ).first() - } + val newestItem = updateNewestItem(newNotes, account, NotificationFeedFilter) return (newestItem?.createdAt() ?: 0) > lastTime } } object MessagesLatestItem { - private var newestItem: Note? = null - fun hasNewItems( account: Account, cache: NotificationCache,