From 704a08e7bb8434f9996912c0b13bb74778fe8658 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Thu, 21 Aug 2025 13:51:10 -0400 Subject: [PATCH] Fixes follow/unfollow from hashtags and geohashes --- .../reqCommand/user/UserObservers.kt | 41 ++++++++----------- .../screen/loggedIn/geohash/GeoHashScreen.kt | 2 +- .../screen/loggedIn/hashtag/HashtagScreen.kt | 2 +- 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/user/UserObservers.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/user/UserObservers.kt index 6b354e41d..812fc9e84 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/user/UserObservers.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/reqCommand/user/UserObservers.kt @@ -429,56 +429,47 @@ fun observeUserIsFollowing( return flow.collectAsStateWithLifecycle(user1.isFollowing(user2)) } +@SuppressLint("StateFlowValueCalledInComposition") @OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class) @Composable fun observeUserIsFollowingHashtag( - user: User, hashtag: String, accountViewModel: AccountViewModel, ): State { - // Subscribe in the relay for changes in the metadata of this user. - UserFinderFilterAssemblerSubscription(user, accountViewModel) - // Subscribe in the LocalCache for changes that arrive in the device val flow = - remember(user) { - user - .flow() - .follows.stateFlow - .sample(1000) - .mapLatest { userState -> - userState.user.isFollowingHashtag(hashtag) + remember(accountViewModel) { + accountViewModel.account.hashtagList.flow + .mapLatest { hashtags -> + hashtag in hashtags + }.onStart { + emit(hashtag in accountViewModel.account.hashtagList.flow.value) }.distinctUntilChanged() .flowOn(Dispatchers.Default) } - return flow.collectAsStateWithLifecycle(user.isFollowingHashtag(hashtag)) + return flow.collectAsStateWithLifecycle(hashtag in accountViewModel.account.hashtagList.flow.value) } +@SuppressLint("StateFlowValueCalledInComposition") @OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class) @Composable fun observeUserIsFollowingGeohash( - user: User, geohash: String, accountViewModel: AccountViewModel, ): State { - // Subscribe in the relay for changes in the metadata of this user. - UserFinderFilterAssemblerSubscription(user, accountViewModel) - - // Subscribe in the LocalCache for changes that arrive in the device val flow = - remember(user) { - user - .flow() - .follows.stateFlow - .sample(1000) - .mapLatest { userState -> - userState.user.isFollowingGeohash(geohash) + remember(accountViewModel) { + accountViewModel.account.geohashList.flow + .mapLatest { geohashes -> + geohash in geohashes + }.onStart { + emit(geohash in accountViewModel.account.geohashList.flow.value) }.distinctUntilChanged() .flowOn(Dispatchers.Default) } - return flow.collectAsStateWithLifecycle(user.isFollowingGeohash(geohash)) + return flow.collectAsStateWithLifecycle(geohash in accountViewModel.account.geohashList.flow.value) } @OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/geohash/GeoHashScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/geohash/GeoHashScreen.kt index 443f99376..cb623710b 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/geohash/GeoHashScreen.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/geohash/GeoHashScreen.kt @@ -132,7 +132,7 @@ fun GeoHashActionOptions( tag: String, accountViewModel: AccountViewModel, ) { - val isFollowingTag by observeUserIsFollowingGeohash(accountViewModel.userProfile(), tag, accountViewModel) + val isFollowingTag by observeUserIsFollowingGeohash(tag, accountViewModel) if (isFollowingTag) { UnfollowButton { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/hashtag/HashtagScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/hashtag/HashtagScreen.kt index d8fbb9dc2..f6c07d857 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/hashtag/HashtagScreen.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/hashtag/HashtagScreen.kt @@ -145,7 +145,7 @@ fun HashtagActionOptions( tag: String, accountViewModel: AccountViewModel, ) { - val isFollowingTag by observeUserIsFollowingHashtag(accountViewModel.userProfile(), tag, accountViewModel) + val isFollowingTag by observeUserIsFollowingHashtag(tag, accountViewModel) if (isFollowingTag) { UnfollowButton {