Fixes follow/unfollow from hashtags and geohashes

This commit is contained in:
Vitor Pamplona
2025-08-21 13:51:10 -04:00
parent b699b30954
commit 704a08e7bb
3 changed files with 18 additions and 27 deletions

View File

@@ -429,56 +429,47 @@ fun observeUserIsFollowing(
return flow.collectAsStateWithLifecycle(user1.isFollowing(user2)) return flow.collectAsStateWithLifecycle(user1.isFollowing(user2))
} }
@SuppressLint("StateFlowValueCalledInComposition")
@OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class) @OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class)
@Composable @Composable
fun observeUserIsFollowingHashtag( fun observeUserIsFollowingHashtag(
user: User,
hashtag: String, hashtag: String,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
): State<Boolean> { ): State<Boolean> {
// 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 // Subscribe in the LocalCache for changes that arrive in the device
val flow = val flow =
remember(user) { remember(accountViewModel) {
user accountViewModel.account.hashtagList.flow
.flow() .mapLatest { hashtags ->
.follows.stateFlow hashtag in hashtags
.sample(1000) }.onStart {
.mapLatest { userState -> emit(hashtag in accountViewModel.account.hashtagList.flow.value)
userState.user.isFollowingHashtag(hashtag)
}.distinctUntilChanged() }.distinctUntilChanged()
.flowOn(Dispatchers.Default) .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) @OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class)
@Composable @Composable
fun observeUserIsFollowingGeohash( fun observeUserIsFollowingGeohash(
user: User,
geohash: String, geohash: String,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
): State<Boolean> { ): State<Boolean> {
// 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 = val flow =
remember(user) { remember(accountViewModel) {
user accountViewModel.account.geohashList.flow
.flow() .mapLatest { geohashes ->
.follows.stateFlow geohash in geohashes
.sample(1000) }.onStart {
.mapLatest { userState -> emit(geohash in accountViewModel.account.geohashList.flow.value)
userState.user.isFollowingGeohash(geohash)
}.distinctUntilChanged() }.distinctUntilChanged()
.flowOn(Dispatchers.Default) .flowOn(Dispatchers.Default)
} }
return flow.collectAsStateWithLifecycle(user.isFollowingGeohash(geohash)) return flow.collectAsStateWithLifecycle(geohash in accountViewModel.account.geohashList.flow.value)
} }
@OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class) @OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class)

View File

@@ -132,7 +132,7 @@ fun GeoHashActionOptions(
tag: String, tag: String,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
) { ) {
val isFollowingTag by observeUserIsFollowingGeohash(accountViewModel.userProfile(), tag, accountViewModel) val isFollowingTag by observeUserIsFollowingGeohash(tag, accountViewModel)
if (isFollowingTag) { if (isFollowingTag) {
UnfollowButton { UnfollowButton {

View File

@@ -145,7 +145,7 @@ fun HashtagActionOptions(
tag: String, tag: String,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
) { ) {
val isFollowingTag by observeUserIsFollowingHashtag(accountViewModel.userProfile(), tag, accountViewModel) val isFollowingTag by observeUserIsFollowingHashtag(tag, accountViewModel)
if (isFollowingTag) { if (isFollowingTag) {
UnfollowButton { UnfollowButton {