From 0e9cc5424508c868dd50099cb61c5ff2345c978c Mon Sep 17 00:00:00 2001 From: KotlinGeekDev Date: Thu, 31 Oct 2024 16:45:12 +0100 Subject: [PATCH] Refactor FollowSetFeedFilter and Account. --- .../vitorpamplona/amethyst/model/Account.kt | 33 ++++++++++--------- .../amethyst/ui/dal/FollowSetFeedFilter.kt | 19 ++++++----- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt index 63cc4b6a5..3e30e3809 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -23,7 +23,6 @@ package com.vitorpamplona.amethyst.model import android.util.Log import androidx.compose.runtime.Immutable import androidx.compose.runtime.Stable -import androidx.compose.ui.util.fastFilter import androidx.lifecycle.LiveData import androidx.lifecycle.asLiveData import androidx.lifecycle.liveData @@ -33,6 +32,7 @@ import com.vitorpamplona.amethyst.BuildConfig import com.vitorpamplona.amethyst.service.FileHeader import com.vitorpamplona.amethyst.service.NostrLnZapPaymentResponseDataSource import com.vitorpamplona.amethyst.service.checkNotInMainThread +import com.vitorpamplona.amethyst.ui.screen.loggedIn.lists.FollowSet import com.vitorpamplona.amethyst.ui.tor.TorType import com.vitorpamplona.ammolite.relays.Client import com.vitorpamplona.ammolite.relays.Constants @@ -108,7 +108,6 @@ import com.vitorpamplona.quartz.events.ZapSplitSetup import com.vitorpamplona.quartz.signers.NostrSigner import com.vitorpamplona.quartz.signers.NostrSignerInternal import com.vitorpamplona.quartz.utils.DualCase -import kotlinx.collections.immutable.toImmutableList import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.Dispatchers @@ -131,6 +130,7 @@ import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.coroutines.suspendCancellableCoroutine +import kotlinx.coroutines.withContext import kotlinx.coroutines.withTimeoutOrNull import java.math.BigDecimal import java.util.Locale @@ -2897,18 +2897,26 @@ class Account( return LocalCache.getOrCreateAddressableNote(aTag) } - fun getFollowSetNotes(): List = LocalCache.getFollowSetsFor(userProfile()) + suspend fun getFollowSetNotes() = + withContext(Dispatchers.Default) { + val followSetNotes = LocalCache.getFollowSetsFor(userProfile()) + userProfile().followSets.addAll(followSetNotes) + println("Number of follow sets: ${followSetNotes.size}") + } - fun loadUserFollowSets() { - val sets = - getFollowSetNotes() - .fastFilter { !it.dTag().isNullOrBlank() } + fun mapNoteToFollowSet(note: Note): FollowSet = + FollowSet + .mapEventToSet( + event = note.event as PeopleListEvent, + signer, + ) - userProfile().followSets = sets.toImmutableList() + fun followSetNotesFlow(): MutableStateFlow> { + val setStateFlow = MutableStateFlow(userProfile().followSets.toList()) + println("Follow set flow value: ${setStateFlow.value}") + return setStateFlow } - fun followSetNotesFlow() = userProfile().flow().followSets.stateFlow - fun getMuteListFlow(): StateFlow = getMuteListNote().flow().metadata.stateFlow fun getBlockList(): PeopleListEvent? = getBlockListNote().event as? PeopleListEvent @@ -3702,11 +3710,6 @@ class Account( } } - scope.launch(Dispatchers.IO) { - Log.d("AccountFollowSetsWorker", "Loading Follow Sets for Account") - loadUserFollowSets() - } - scope.launch(Dispatchers.Default) { Log.d("AccountRegisterObservers", "Mute List Collector Start") getMuteListFlow().collect { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/dal/FollowSetFeedFilter.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/dal/FollowSetFeedFilter.kt index 34537aa80..466d851b8 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/dal/FollowSetFeedFilter.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/dal/FollowSetFeedFilter.kt @@ -21,17 +21,20 @@ package com.vitorpamplona.amethyst.ui.dal import com.vitorpamplona.amethyst.model.Account -import com.vitorpamplona.amethyst.model.Note +import com.vitorpamplona.amethyst.ui.screen.loggedIn.lists.FollowSet +import kotlinx.coroutines.launch class FollowSetFeedFilter( val account: Account, -) : FeedFilter() { +) : FeedFilter() { override fun feedKey(): String = account.userProfile().pubkeyHex - override fun feed(): List = - account - .followSetNotesFlow() - .value - .user - .followSets + override fun feed(): List { + if (account.userProfile().followSets.isEmpty()) { + account.scope.launch { + account.getFollowSetNotes() + } + } + return account.followSetNotesFlow().value.map { account.mapNoteToFollowSet(it) } + } }