From a594d741b1def530069e66c0be2c2baeef253673 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 29 Aug 2025 07:37:24 -0400 Subject: [PATCH] Creates an additional pointer for Account lists to make sure the GC doesn't clear instances that are only stored in the LocalCache's WeakReference DB. --- .../com/vitorpamplona/amethyst/model/Account.kt | 12 ------------ .../model/edits/PrivateStorageRelayListState.kt | 12 ++++++------ .../model/emphChat/EphemeralChatListState.kt | 12 ++++++------ .../model/nip02FollowLists/FollowListState.kt | 9 ++++----- .../amethyst/model/nip17Dms/DmRelayListState.kt | 12 ++++++------ .../nip28PublicChats/PublicChatListState.kt | 12 ++++++------ .../model/nip30CustomEmojis/EmojiPackState.kt | 10 +++++----- .../model/nip51Lists/BookmarkListState.kt | 13 +++++++------ .../blockPeopleList/BlockPeopleListState.kt | 11 ++++++----- .../blockedRelays/BlockedRelayListState.kt | 12 ++++++------ .../broadcastRelays/BroadcastRelayListState.kt | 12 ++++++------ .../nip51Lists/geohashLists/GeohashListState.kt | 12 ++++++------ .../nip51Lists/hashtagLists/HashtagListState.kt | 12 ++++++------ .../indexerRelays/IndexerRelayListState.kt | 12 ++++++------ .../model/nip51Lists/muteList/MuteListState.kt | 11 ++++++----- .../proxyRelays/ProxyRelayListState.kt | 12 ++++++------ .../searchRelays/SearchRelayListState.kt | 14 +++++++------- .../trustedRelays/TrustedRelayListState.kt | 12 ++++++------ .../model/nip65RelayList/Nip65RelayListState.kt | 16 ++++++++-------- .../model/nip72Communities/CommunityListState.kt | 11 ++++++----- .../model/nip78AppSpecific/AppSpecificState.kt | 7 ++++--- .../FileStorageServerListState.kt | 12 ++++++------ .../model/nipB7Blossom/BlossomServerListState.kt | 12 ++++++------ 23 files changed, 131 insertions(+), 139 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 2737a74b9..7ce629bb9 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -399,18 +399,6 @@ class Account( val liveNotificationFollowListsPerRelay = OutboxLoaderState(liveNotificationFollowLists, cache, scope).flow - /* - val mergedTopFeedAuthorLists = - MergedTopFeedAuthorListsState( - liveHomeFollowListsPerRelay, - liveStoriesFollowListsPerRelay, - liveDiscoveryFollowListsPerRelay, - liveNotificationFollowListsPerRelay, - scope, - ).flow - - */ - fun isWriteable(): Boolean = settings.isWriteable() suspend fun updateWarnReports(warnReports: Boolean): Boolean { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/edits/PrivateStorageRelayListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/edits/PrivateStorageRelayListState.kt index e4f28c081..89d3eaa0a 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/edits/PrivateStorageRelayListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/edits/PrivateStorageRelayListState.kt @@ -22,7 +22,6 @@ package com.vitorpamplona.amethyst.model.edits import android.util.Log import com.vitorpamplona.amethyst.model.AccountSettings -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.NoteState @@ -48,13 +47,14 @@ class PrivateStorageRelayListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val privateOutboxListNote = cache.getOrCreateAddressableNote(getPrivateOutboxRelayListAddress()) + fun getPrivateOutboxRelayListAddress() = PrivateOutboxRelayListEvent.createAddress(signer.pubKey) - fun getPrivateOutboxRelayListNote(): AddressableNote = LocalCache.getOrCreateAddressableNote(getPrivateOutboxRelayListAddress()) + fun getPrivateOutboxRelayListFlow(): StateFlow = privateOutboxListNote.flow().metadata.stateFlow - fun getPrivateOutboxRelayListFlow(): StateFlow = getPrivateOutboxRelayListNote().flow().metadata.stateFlow - - fun getPrivateOutboxRelayList(): PrivateOutboxRelayListEvent? = getPrivateOutboxRelayListNote().event as? PrivateOutboxRelayListEvent + fun getPrivateOutboxRelayList(): PrivateOutboxRelayListEvent? = privateOutboxListNote.event as? PrivateOutboxRelayListEvent suspend fun normalizePrivateOutboxRelayListWithBackup(note: Note): Set { val event = note.event as? PrivateOutboxRelayListEvent ?: settings.backupPrivateHomeRelayList @@ -65,7 +65,7 @@ class PrivateStorageRelayListState( getPrivateOutboxRelayListFlow() .map { normalizePrivateOutboxRelayListWithBackup(it.note) } .onStart { - emit(normalizePrivateOutboxRelayListWithBackup(getPrivateOutboxRelayListNote())) + emit(normalizePrivateOutboxRelayListWithBackup(privateOutboxListNote)) }.flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/emphChat/EphemeralChatListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/emphChat/EphemeralChatListState.kt index 0a9378ebb..6e8769570 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/emphChat/EphemeralChatListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/emphChat/EphemeralChatListState.kt @@ -22,7 +22,6 @@ package com.vitorpamplona.amethyst.model.emphChat import android.util.Log import com.vitorpamplona.amethyst.model.AccountSettings -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.NoteState @@ -49,13 +48,14 @@ class EphemeralChatListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val ephemeralChatListNote = cache.getOrCreateAddressableNote(getEphemeralChatListAddress()) + fun getEphemeralChatListAddress() = EphemeralChatListEvent.createAddress(signer.pubKey) - fun getEphemeralChatListNote(): AddressableNote = cache.getOrCreateAddressableNote(getEphemeralChatListAddress()) + fun getEphemeralChatListFlow(): StateFlow = ephemeralChatListNote.flow().metadata.stateFlow - fun getEphemeralChatListFlow(): StateFlow = getEphemeralChatListNote().flow().metadata.stateFlow - - fun getEphemeralChatList(): EphemeralChatListEvent? = getEphemeralChatListNote().event as? EphemeralChatListEvent + fun getEphemeralChatList(): EphemeralChatListEvent? = ephemeralChatListNote.event as? EphemeralChatListEvent suspend fun ephemeralChatListWithBackup(note: Note): Set { val event = note.event as? EphemeralChatListEvent ?: settings.backupEphemeralChatList @@ -68,7 +68,7 @@ class EphemeralChatListState( .transformLatest { noteState -> emit(ephemeralChatListWithBackup(noteState.note)) }.onStart { - emit(ephemeralChatListWithBackup(getEphemeralChatListNote())) + emit(ephemeralChatListWithBackup(ephemeralChatListNote)) }.flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip02FollowLists/FollowListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip02FollowLists/FollowListState.kt index 409f31e15..d98b6b377 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip02FollowLists/FollowListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip02FollowLists/FollowListState.kt @@ -51,13 +51,12 @@ class FollowListState( val scope: CoroutineScope, val settings: AccountSettings, ) { - // fun getEphemeralChatListAddress() = cache.getOrCreateUser(signer.pubKey) + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val user = cache.getOrCreateUser(signer.pubKey) - fun getFollowListUser(): User = cache.getOrCreateUser(signer.pubKey) + fun getFollowListFlow(): StateFlow = user.flow().follows.stateFlow - fun getFollowListFlow(): StateFlow = getFollowListUser().flow().follows.stateFlow - - fun getFollowListEvent(): ContactListEvent? = getFollowListUser().latestContactList + fun getFollowListEvent(): ContactListEvent? = user.latestContactList @OptIn(ExperimentalCoroutinesApi::class) private val innerFlow: Flow = diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip17Dms/DmRelayListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip17Dms/DmRelayListState.kt index 08e3b4111..842072115 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip17Dms/DmRelayListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip17Dms/DmRelayListState.kt @@ -22,7 +22,6 @@ package com.vitorpamplona.amethyst.model.nip17Dms import android.util.Log import com.vitorpamplona.amethyst.model.AccountSettings -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.NoteState @@ -47,13 +46,14 @@ class DmRelayListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val dmListNote = cache.getOrCreateAddressableNote(getDMRelayListAddress()) + fun getDMRelayListAddress() = ChatMessageRelayListEvent.createAddress(signer.pubKey) - fun getDMRelayListNote(): AddressableNote = LocalCache.getOrCreateAddressableNote(getDMRelayListAddress()) + fun getDMRelayListFlow(): StateFlow = dmListNote.flow().metadata.stateFlow - fun getDMRelayListFlow(): StateFlow = getDMRelayListNote().flow().metadata.stateFlow - - fun getDMRelayList(): ChatMessageRelayListEvent? = getDMRelayListNote().event as? ChatMessageRelayListEvent + fun getDMRelayList(): ChatMessageRelayListEvent? = dmListNote.event as? ChatMessageRelayListEvent fun normalizeDMRelayListWithBackup(note: Note): Set { val event = note.event as? ChatMessageRelayListEvent ?: settings.backupDMRelayList @@ -63,7 +63,7 @@ class DmRelayListState( val flow = getDMRelayListFlow() .map { normalizeDMRelayListWithBackup(it.note) } - .onStart { emit(normalizeDMRelayListWithBackup(getDMRelayListNote())) } + .onStart { emit(normalizeDMRelayListWithBackup(dmListNote)) } .flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip28PublicChats/PublicChatListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip28PublicChats/PublicChatListState.kt index 8ee8452d5..70b4505ce 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip28PublicChats/PublicChatListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip28PublicChats/PublicChatListState.kt @@ -22,7 +22,6 @@ package com.vitorpamplona.amethyst.model.nip28PublicChats import android.util.Log import com.vitorpamplona.amethyst.model.AccountSettings -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.NoteState @@ -51,13 +50,14 @@ class PublicChatListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val publicChatListNote = cache.getOrCreateAddressableNote(getChannelListAddress()) + fun getChannelListAddress() = ChannelListEvent.createAddress(signer.pubKey) - fun getChannelListNote(): AddressableNote = cache.getOrCreateAddressableNote(getChannelListAddress()) + fun getChannelListFlow(): StateFlow = publicChatListNote.flow().metadata.stateFlow - fun getChannelListFlow(): StateFlow = getChannelListNote().flow().metadata.stateFlow - - fun getChannelList(): ChannelListEvent? = getChannelListNote().event as? ChannelListEvent + fun getChannelList(): ChannelListEvent? = publicChatListNote.event as? ChannelListEvent suspend fun publicChatListWithBackup(note: Note): Set { val event = note.event as? ChannelListEvent ?: settings.backupChannelList @@ -70,7 +70,7 @@ class PublicChatListState( .transformLatest { noteState -> emit(publicChatListWithBackup(noteState.note)) }.onStart { - emit(publicChatListWithBackup(getChannelListNote())) + emit(publicChatListWithBackup(publicChatListNote)) }.flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip30CustomEmojis/EmojiPackState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip30CustomEmojis/EmojiPackState.kt index b40392a63..38cf0aedd 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip30CustomEmojis/EmojiPackState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip30CustomEmojis/EmojiPackState.kt @@ -21,7 +21,6 @@ package com.vitorpamplona.amethyst.model.nip30CustomEmojis import com.vitorpamplona.amethyst.commons.richtext.MediaUrlImage -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.NoteState @@ -52,13 +51,14 @@ class EmojiPackState( val link: MediaUrlImage, ) + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val emojiPackListNote = cache.getOrCreateAddressableNote(getEmojiPackSelectionAddress()) + fun getEmojiPackSelectionAddress() = EmojiPackSelectionEvent.createAddress(signer.pubKey) - fun getEmojiPackSelection(): EmojiPackSelectionEvent? = getEmojiPackSelectionNote().event as? EmojiPackSelectionEvent + fun getEmojiPackSelection(): EmojiPackSelectionEvent? = emojiPackListNote.event as? EmojiPackSelectionEvent - fun getEmojiPackSelectionFlow(): StateFlow = getEmojiPackSelectionNote().flow().metadata.stateFlow - - fun getEmojiPackSelectionNote(): AddressableNote = cache.getOrCreateAddressableNote(getEmojiPackSelectionAddress()) + fun getEmojiPackSelectionFlow(): StateFlow = emojiPackListNote.flow().metadata.stateFlow fun convertEmojiSelectionPack(selection: EmojiPackSelectionEvent?): List>? = selection?.taggedAddresses()?.map { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/BookmarkListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/BookmarkListState.kt index 57069ef61..a75852253 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/BookmarkListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/BookmarkListState.kt @@ -51,13 +51,14 @@ class BookmarkListState( val private: List = emptyList(), ) + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val bookmarkList = cache.getOrCreateAddressableNote(getBookmarkListAddress()) + fun getBookmarkListAddress() = BookmarkListEvent.createBookmarkAddress(signer.pubKey) - fun getBookmarkListNote() = cache.getOrCreateAddressableNote(getBookmarkListAddress()) + fun getBookmarkListFlow(): StateFlow = bookmarkList.flow().metadata.stateFlow - fun getBookmarkListFlow(): StateFlow = getBookmarkListNote().flow().metadata.stateFlow - - fun getBookmarkList(): BookmarkListEvent? = getBookmarkListNote().event as? BookmarkListEvent + fun getBookmarkList(): BookmarkListEvent? = bookmarkList.event as? BookmarkListEvent suspend fun publicBookmarks(note: Note): List { val noteEvent = note.event as? BookmarkListEvent @@ -75,7 +76,7 @@ class BookmarkListState( .map { noteState -> publicBookmarks(noteState.note) }.onStart { - emit(publicBookmarks(getBookmarkListNote())) + emit(publicBookmarks(bookmarkList)) }.debounce(100) .flowOn(Dispatchers.Default) .stateIn( @@ -90,7 +91,7 @@ class BookmarkListState( .map { noteState -> privateBookmarks(noteState.note) }.onStart { - emit(privateBookmarks(getBookmarkListNote())) + emit(privateBookmarks(bookmarkList)) }.debounce(100) .flowOn(Dispatchers.Default) .stateIn( diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/blockPeopleList/BlockPeopleListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/blockPeopleList/BlockPeopleListState.kt index 2bf5a11cb..cfc6a20cd 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/blockPeopleList/BlockPeopleListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/blockPeopleList/BlockPeopleListState.kt @@ -43,13 +43,14 @@ class BlockPeopleListState( val decryptionCache: PeopleListDecryptionCache, val scope: CoroutineScope, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val blockListNote = cache.getOrCreateAddressableNote(getBlockListAddress()) + fun getBlockListAddress() = PeopleListEvent.createBlockAddress(signer.pubKey) - fun getBlockListNote() = LocalCache.getOrCreateAddressableNote(getBlockListAddress()) + fun getBlockListFlow(): StateFlow = blockListNote.flow().metadata.stateFlow - fun getBlockListFlow(): StateFlow = getBlockListNote().flow().metadata.stateFlow - - fun getBlockList(): PeopleListEvent? = getBlockListNote().event as? PeopleListEvent + fun getBlockList(): PeopleListEvent? = blockListNote.event as? PeopleListEvent suspend fun blockListWithBackup(note: Note): List { val event = note.event as? PeopleListEvent @@ -59,7 +60,7 @@ class BlockPeopleListState( val flow = getBlockListFlow() .map { blockListWithBackup(it.note) } - .onStart { emit(blockListWithBackup(getBlockListNote())) } + .onStart { emit(blockListWithBackup(blockListNote)) } .flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/blockedRelays/BlockedRelayListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/blockedRelays/BlockedRelayListState.kt index 394bfd1ba..965e58bfc 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/blockedRelays/BlockedRelayListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/blockedRelays/BlockedRelayListState.kt @@ -22,7 +22,6 @@ package com.vitorpamplona.amethyst.model.nip51Lists.blockedRelays import android.util.Log import com.vitorpamplona.amethyst.model.AccountSettings -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.NoteState @@ -49,13 +48,14 @@ class BlockedRelayListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val blockedListNote = cache.getOrCreateAddressableNote(getBlockedRelayListAddress()) + fun getBlockedRelayListAddress() = BlockedRelayListEvent.createAddress(signer.pubKey) - fun getBlockedRelayListNote(): AddressableNote = LocalCache.getOrCreateAddressableNote(getBlockedRelayListAddress()) + fun getBlockedRelayListFlow(): StateFlow = blockedListNote.flow().metadata.stateFlow - fun getBlockedRelayListFlow(): StateFlow = getBlockedRelayListNote().flow().metadata.stateFlow - - fun getBlockedRelayList(): BlockedRelayListEvent? = getBlockedRelayListNote().event as? BlockedRelayListEvent + fun getBlockedRelayList(): BlockedRelayListEvent? = blockedListNote.event as? BlockedRelayListEvent suspend fun normalizeBlockedRelayListWithBackup(note: Note): Set { val event = note.event as? BlockedRelayListEvent ?: settings.backupBlockedRelayList @@ -66,7 +66,7 @@ class BlockedRelayListState( getBlockedRelayListFlow() .map { normalizeBlockedRelayListWithBackup(it.note) - }.onStart { emit(normalizeBlockedRelayListWithBackup(getBlockedRelayListNote())) } + }.onStart { emit(normalizeBlockedRelayListWithBackup(blockedListNote)) } .flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/broadcastRelays/BroadcastRelayListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/broadcastRelays/BroadcastRelayListState.kt index 8cada24fe..8e6b88e8b 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/broadcastRelays/BroadcastRelayListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/broadcastRelays/BroadcastRelayListState.kt @@ -21,7 +21,6 @@ package com.vitorpamplona.amethyst.model.nip51Lists.broadcastRelays import com.vitorpamplona.amethyst.model.AccountSettings -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.NoteState @@ -44,13 +43,14 @@ class BroadcastRelayListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val broadcastListNote = cache.getOrCreateAddressableNote(getBroadcastRelayListAddress()) + fun getBroadcastRelayListAddress() = BroadcastRelayListEvent.createAddress(signer.pubKey) - fun getBroadcastRelayListNote(): AddressableNote = cache.getOrCreateAddressableNote(getBroadcastRelayListAddress()) + fun getBroadcastRelayListFlow(): StateFlow = broadcastListNote.flow().metadata.stateFlow - fun getBroadcastRelayListFlow(): StateFlow = getBroadcastRelayListNote().flow().metadata.stateFlow - - fun getBroadcastRelayList(): BroadcastRelayListEvent? = getBroadcastRelayListNote().event as? BroadcastRelayListEvent + fun getBroadcastRelayList(): BroadcastRelayListEvent? = broadcastListNote.event as? BroadcastRelayListEvent suspend fun normalizeBroadcastRelayListWithBackup(note: Note): Set { val event = note.event as? BroadcastRelayListEvent @@ -60,7 +60,7 @@ class BroadcastRelayListState( val flow = getBroadcastRelayListFlow() .map { normalizeBroadcastRelayListWithBackup(it.note) } - .onStart { emit(normalizeBroadcastRelayListWithBackup(getBroadcastRelayListNote())) } + .onStart { emit(normalizeBroadcastRelayListWithBackup(broadcastListNote)) } .flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/geohashLists/GeohashListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/geohashLists/GeohashListState.kt index a11c07130..2bebd3094 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/geohashLists/GeohashListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/geohashLists/GeohashListState.kt @@ -22,7 +22,6 @@ package com.vitorpamplona.amethyst.model.nip51Lists.geohashLists import android.util.Log import com.vitorpamplona.amethyst.model.AccountSettings -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.NoteState @@ -48,13 +47,14 @@ class GeohashListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val geohashListNote = cache.getOrCreateAddressableNote(getGeohashListAddress()) + fun getGeohashListAddress() = GeohashListEvent.createAddress(signer.pubKey) - fun getGeohashListNote(): AddressableNote = cache.getOrCreateAddressableNote(getGeohashListAddress()) + fun getGeohashListFlow(): StateFlow = geohashListNote.flow().metadata.stateFlow - fun getGeohashListFlow(): StateFlow = getGeohashListNote().flow().metadata.stateFlow - - fun getGeohashList(): GeohashListEvent? = getGeohashListNote().event as? GeohashListEvent + fun getGeohashList(): GeohashListEvent? = geohashListNote.event as? GeohashListEvent suspend fun geohashListWithBackup(note: Note): Set { val event = note.event as? GeohashListEvent ?: settings.backupGeohashList @@ -67,7 +67,7 @@ class GeohashListState( .transformLatest { noteState -> emit(geohashListWithBackup(noteState.note)) }.onStart { - emit(geohashListWithBackup(getGeohashListNote())) + emit(geohashListWithBackup(geohashListNote)) }.flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/hashtagLists/HashtagListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/hashtagLists/HashtagListState.kt index 6a73325c5..73e5906d8 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/hashtagLists/HashtagListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/hashtagLists/HashtagListState.kt @@ -22,7 +22,6 @@ package com.vitorpamplona.amethyst.model.nip51Lists.hashtagLists import android.util.Log import com.vitorpamplona.amethyst.model.AccountSettings -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.NoteState @@ -48,13 +47,14 @@ class HashtagListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val hashtagListNote = cache.getOrCreateAddressableNote(getHashtagListAddress()) + fun getHashtagListAddress() = HashtagListEvent.createAddress(signer.pubKey) - fun getHashtagListNote(): AddressableNote = cache.getOrCreateAddressableNote(getHashtagListAddress()) + fun getHashtagListFlow(): StateFlow = hashtagListNote.flow().metadata.stateFlow - fun getHashtagListFlow(): StateFlow = getHashtagListNote().flow().metadata.stateFlow - - fun getHashtagList(): HashtagListEvent? = getHashtagListNote().event as? HashtagListEvent + fun getHashtagList(): HashtagListEvent? = hashtagListNote.event as? HashtagListEvent suspend fun hashtagListWithBackup(note: Note): Set { val event = note.event as? HashtagListEvent ?: settings.backupHashtagList @@ -67,7 +67,7 @@ class HashtagListState( .transformLatest { noteState -> emit(hashtagListWithBackup(noteState.note)) }.onStart { - emit(hashtagListWithBackup(getHashtagListNote())) + emit(hashtagListWithBackup(hashtagListNote)) }.flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/indexerRelays/IndexerRelayListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/indexerRelays/IndexerRelayListState.kt index 5fa08599a..257e6b25a 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/indexerRelays/IndexerRelayListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/indexerRelays/IndexerRelayListState.kt @@ -21,7 +21,6 @@ package com.vitorpamplona.amethyst.model.nip51Lists.indexerRelays import com.vitorpamplona.amethyst.model.AccountSettings -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.NoteState @@ -45,13 +44,14 @@ class IndexerRelayListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val indexerListNote = cache.getOrCreateAddressableNote(getIndexerRelayListAddress()) + fun getIndexerRelayListAddress() = IndexerRelayListEvent.createAddress(signer.pubKey) - fun getIndexerRelayListNote(): AddressableNote = cache.getOrCreateAddressableNote(getIndexerRelayListAddress()) + fun getIndexerRelayListFlow(): StateFlow = indexerListNote.flow().metadata.stateFlow - fun getIndexerRelayListFlow(): StateFlow = getIndexerRelayListNote().flow().metadata.stateFlow - - fun getIndexerRelayList(): IndexerRelayListEvent? = getIndexerRelayListNote().event as? IndexerRelayListEvent + fun getIndexerRelayList(): IndexerRelayListEvent? = indexerListNote.event as? IndexerRelayListEvent suspend fun normalizeIndexerRelayListWithBackup(note: Note): Set { val event = note.event as? IndexerRelayListEvent @@ -61,7 +61,7 @@ class IndexerRelayListState( val flow = getIndexerRelayListFlow() .map { normalizeIndexerRelayListWithBackup(it.note) } - .onStart { emit(normalizeIndexerRelayListWithBackup(getIndexerRelayListNote())) } + .onStart { emit(normalizeIndexerRelayListWithBackup(indexerListNote)) } .flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/muteList/MuteListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/muteList/MuteListState.kt index e284cc024..f78ba1f2c 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/muteList/MuteListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/muteList/MuteListState.kt @@ -49,13 +49,14 @@ class MuteListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val muteListNote = cache.getOrCreateAddressableNote(getMuteListAddress()) + fun getMuteListAddress() = MuteListEvent.createAddress(signer.pubKey) - fun getMuteListNote() = cache.getOrCreateAddressableNote(getMuteListAddress()) + fun getMuteListFlow(): StateFlow = muteListNote.flow().metadata.stateFlow - fun getMuteListFlow(): StateFlow = getMuteListNote().flow().metadata.stateFlow - - fun getMuteList(): MuteListEvent? = getMuteListNote().event as? MuteListEvent + fun getMuteList(): MuteListEvent? = muteListNote.event as? MuteListEvent suspend fun muteListWithBackup(note: Note): List { val event = note.event as? MuteListEvent ?: settings.backupMuteList @@ -65,7 +66,7 @@ class MuteListState( val flow = getMuteListFlow() .map { muteListWithBackup(it.note) } - .onStart { emit(muteListWithBackup(getMuteListNote())) } + .onStart { emit(muteListWithBackup(muteListNote)) } .flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/proxyRelays/ProxyRelayListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/proxyRelays/ProxyRelayListState.kt index 2946990a9..13fe7f0bf 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/proxyRelays/ProxyRelayListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/proxyRelays/ProxyRelayListState.kt @@ -21,7 +21,6 @@ package com.vitorpamplona.amethyst.model.nip51Lists.proxyRelays import com.vitorpamplona.amethyst.model.AccountSettings -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.NoteState @@ -44,13 +43,14 @@ class ProxyRelayListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val proxyListNote = cache.getOrCreateAddressableNote(getProxyRelayListAddress()) + fun getProxyRelayListAddress() = ProxyRelayListEvent.createAddress(signer.pubKey) - fun getProxyRelayListNote(): AddressableNote = cache.getOrCreateAddressableNote(getProxyRelayListAddress()) + fun getProxyRelayListFlow(): StateFlow = proxyListNote.flow().metadata.stateFlow - fun getProxyRelayListFlow(): StateFlow = getProxyRelayListNote().flow().metadata.stateFlow - - fun getProxyRelayList(): ProxyRelayListEvent? = getProxyRelayListNote().event as? ProxyRelayListEvent + fun getProxyRelayList(): ProxyRelayListEvent? = proxyListNote.event as? ProxyRelayListEvent suspend fun normalizeProxyRelayListWithBackup(note: Note): Set { val event = note.event as? ProxyRelayListEvent @@ -60,7 +60,7 @@ class ProxyRelayListState( val flow = getProxyRelayListFlow() .map { normalizeProxyRelayListWithBackup(it.note) } - .onStart { emit(normalizeProxyRelayListWithBackup(getProxyRelayListNote())) } + .onStart { emit(normalizeProxyRelayListWithBackup(proxyListNote)) } .flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/searchRelays/SearchRelayListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/searchRelays/SearchRelayListState.kt index 2a62ca3d1..590c409d4 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/searchRelays/SearchRelayListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/searchRelays/SearchRelayListState.kt @@ -22,7 +22,6 @@ package com.vitorpamplona.amethyst.model.nip51Lists.searchRelays import android.util.Log import com.vitorpamplona.amethyst.model.AccountSettings -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.DefaultSearchRelayList import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note @@ -49,13 +48,14 @@ class SearchRelayListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val searchListNote = cache.getOrCreateAddressableNote(getSearchRelayListAddress()) + fun getSearchRelayListAddress() = SearchRelayListEvent.Companion.createAddress(signer.pubKey) - fun getSearchRelayListNote(): AddressableNote = LocalCache.getOrCreateAddressableNote(getSearchRelayListAddress()) + fun getSearchRelayListFlow(): StateFlow = searchListNote.flow().metadata.stateFlow - fun getSearchRelayListFlow(): StateFlow = getSearchRelayListNote().flow().metadata.stateFlow - - fun getSearchRelayList(): SearchRelayListEvent? = getSearchRelayListNote().event as? SearchRelayListEvent + fun getSearchRelayList(): SearchRelayListEvent? = searchListNote.event as? SearchRelayListEvent fun searchListEvent(note: Note) = note.event as? SearchRelayListEvent ?: settings.backupSearchRelayList @@ -66,7 +66,7 @@ class SearchRelayListState( val flow = getSearchRelayListFlow() .map { normalizeSearchRelayListWithBackup(it.note) } - .onStart { emit(normalizeSearchRelayListWithBackup(getSearchRelayListNote())) } + .onStart { emit(normalizeSearchRelayListWithBackup(searchListNote)) } .flowOn(Dispatchers.Default) .stateIn( scope, @@ -77,7 +77,7 @@ class SearchRelayListState( val flowNoDefaults = getSearchRelayListFlow() .map { normalizeSearchRelayListWithBackupNoDefaults(it.note) } - .onStart { emit(normalizeSearchRelayListWithBackupNoDefaults(getSearchRelayListNote())) } + .onStart { emit(normalizeSearchRelayListWithBackupNoDefaults(searchListNote)) } .flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/trustedRelays/TrustedRelayListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/trustedRelays/TrustedRelayListState.kt index d5bd69235..41aff1c06 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/trustedRelays/TrustedRelayListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip51Lists/trustedRelays/TrustedRelayListState.kt @@ -22,7 +22,6 @@ package com.vitorpamplona.amethyst.model.nip51Lists.trustedRelays import android.util.Log import com.vitorpamplona.amethyst.model.AccountSettings -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.NoteState @@ -48,13 +47,14 @@ class TrustedRelayListState( val scope: CoroutineScope, val settings: AccountSettings, ) { - fun getTrustedRelayListAddress() = TrustedRelayListEvent.Companion.createAddress(signer.pubKey) + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val trustedListNote = cache.getOrCreateAddressableNote(getTrustedRelayListAddress()) - fun getTrustedRelayListNote(): AddressableNote = cache.getOrCreateAddressableNote(getTrustedRelayListAddress()) + fun getTrustedRelayListAddress() = TrustedRelayListEvent.createAddress(signer.pubKey) - fun getTrustedRelayListFlow(): StateFlow = getTrustedRelayListNote().flow().metadata.stateFlow + fun getTrustedRelayListFlow(): StateFlow = trustedListNote.flow().metadata.stateFlow - fun getTrustedRelayList(): TrustedRelayListEvent? = getTrustedRelayListNote().event as? TrustedRelayListEvent + fun getTrustedRelayList(): TrustedRelayListEvent? = trustedListNote.event as? TrustedRelayListEvent suspend fun normalizeTrustedRelayListWithBackup(note: Note): Set { val event = note.event as? TrustedRelayListEvent ?: settings.backupTrustedRelayList @@ -64,7 +64,7 @@ class TrustedRelayListState( val flow = getTrustedRelayListFlow() .map { normalizeTrustedRelayListWithBackup(it.note) } - .onStart { emit(normalizeTrustedRelayListWithBackup(getTrustedRelayListNote())) } + .onStart { emit(normalizeTrustedRelayListWithBackup(trustedListNote)) } .flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip65RelayList/Nip65RelayListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip65RelayList/Nip65RelayListState.kt index 4d70792fd..8773f12cd 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip65RelayList/Nip65RelayListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip65RelayList/Nip65RelayListState.kt @@ -22,7 +22,6 @@ package com.vitorpamplona.amethyst.model.nip65RelayList import android.util.Log import com.vitorpamplona.amethyst.model.AccountSettings -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.Constants import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note @@ -49,13 +48,14 @@ class Nip65RelayListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val nip65ListNote = cache.getOrCreateAddressableNote(getNIP65RelayListAddress()) + fun getNIP65RelayListAddress() = AdvertisedRelayListEvent.createAddress(signer.pubKey) - fun getNIP65RelayListNote(): AddressableNote = cache.getOrCreateAddressableNote(getNIP65RelayListAddress()) + fun getNIP65RelayListFlow(): StateFlow = nip65ListNote.flow().metadata.stateFlow - fun getNIP65RelayListFlow(): StateFlow = getNIP65RelayListNote().flow().metadata.stateFlow - - fun getNIP65RelayList(): AdvertisedRelayListEvent? = getNIP65RelayListNote().event as? AdvertisedRelayListEvent + fun getNIP65RelayList(): AdvertisedRelayListEvent? = nip65ListNote.event as? AdvertisedRelayListEvent fun nip65Event(note: Note) = note.event as? AdvertisedRelayListEvent ?: settings.backupNIP65RelayList @@ -70,7 +70,7 @@ class Nip65RelayListState( val outboxFlow = getNIP65RelayListFlow() .map { normalizeNIP65WriteRelayListWithBackup(it.note) } - .onStart { emit(normalizeNIP65ReadRelayListWithBackup(getNIP65RelayListNote())) } + .onStart { emit(normalizeNIP65ReadRelayListWithBackup(nip65ListNote)) } .flowOn(Dispatchers.Default) .stateIn( scope, @@ -81,7 +81,7 @@ class Nip65RelayListState( val inboxFlow = getNIP65RelayListFlow() .map { normalizeNIP65ReadRelayListWithBackup(it.note) } - .onStart { emit(normalizeNIP65ReadRelayListWithBackup(getNIP65RelayListNote())) } + .onStart { emit(normalizeNIP65ReadRelayListWithBackup(nip65ListNote)) } .flowOn(Dispatchers.Default) .stateIn( scope, @@ -92,7 +92,7 @@ class Nip65RelayListState( val allFlowNoDefaults = getNIP65RelayListFlow() .map { normalizeNIP65AllRelayListWithBackupNoDefaults(it.note) } - .onStart { emit(normalizeNIP65AllRelayListWithBackupNoDefaults(getNIP65RelayListNote())) } + .onStart { emit(normalizeNIP65AllRelayListWithBackupNoDefaults(nip65ListNote)) } .flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip72Communities/CommunityListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip72Communities/CommunityListState.kt index 9406632f6..24052d620 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip72Communities/CommunityListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip72Communities/CommunityListState.kt @@ -51,13 +51,14 @@ class CommunityListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val communityListNote = cache.getOrCreateAddressableNote(getCommunityListAddress()) + fun getCommunityListAddress() = CommunityListEvent.createAddress(signer.pubKey) - fun getCommunityListNote(): AddressableNote = cache.getOrCreateAddressableNote(getCommunityListAddress()) + fun getCommunityListFlow(): StateFlow = communityListNote.flow().metadata.stateFlow - fun getCommunityListFlow(): StateFlow = getCommunityListNote().flow().metadata.stateFlow - - fun getCommunityList(): CommunityListEvent? = getCommunityListNote().event as? CommunityListEvent + fun getCommunityList(): CommunityListEvent? = communityListNote.event as? CommunityListEvent suspend fun communityListWithBackup(note: Note): Set { val event = note.event as? CommunityListEvent ?: settings.backupCommunityList @@ -70,7 +71,7 @@ class CommunityListState( .transformLatest { noteState -> emit(communityListWithBackup(noteState.note)) }.onStart { - emit(communityListWithBackup(getCommunityListNote())) + emit(communityListWithBackup(communityListNote)) }.flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip78AppSpecific/AppSpecificState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip78AppSpecific/AppSpecificState.kt index 92f8d6236..d44cface2 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip78AppSpecific/AppSpecificState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip78AppSpecific/AppSpecificState.kt @@ -47,11 +47,12 @@ class AppSpecificState( const val APP_SPECIFIC_DATA_D_TAG = "AmethystSettings" } + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val amethystSettingsNote = cache.getOrCreateAddressableNote(getAppSpecificDataAddress()) + fun getAppSpecificDataAddress() = AppSpecificDataEvent.createAddress(signer.pubKey, APP_SPECIFIC_DATA_D_TAG) - fun getAppSpecificDataNote() = cache.getOrCreateAddressableNote(getAppSpecificDataAddress()) - - fun getAppSpecificDataFlow(): StateFlow = getAppSpecificDataNote().flow().metadata.stateFlow + fun getAppSpecificDataFlow(): StateFlow = amethystSettingsNote.flow().metadata.stateFlow suspend fun saveNewAppSpecificData(): AppSpecificDataEvent { val toInternal = settings.syncedSettings.toInternal() diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip96FileStorage/FileStorageServerListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip96FileStorage/FileStorageServerListState.kt index 05af74039..519d6cb27 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip96FileStorage/FileStorageServerListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip96FileStorage/FileStorageServerListState.kt @@ -21,7 +21,6 @@ package com.vitorpamplona.amethyst.model.nip96FileStorage import com.vitorpamplona.amethyst.model.AccountSettings -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.NoteState @@ -42,13 +41,14 @@ class FileStorageServerListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val fileStorageListNote = cache.getOrCreateAddressableNote(getFileServersAddress()) + fun getFileServersAddress() = FileServersEvent.createAddress(signer.pubKey) - fun getFileServersNote(): AddressableNote = LocalCache.getOrCreateAddressableNote(getFileServersAddress()) + fun getFileServersListFlow(): StateFlow = fileStorageListNote.flow().metadata.stateFlow - fun getFileServersListFlow(): StateFlow = getFileServersNote().flow().metadata.stateFlow - - fun getFileServersList(): FileServersEvent? = getFileServersNote().event as? FileServersEvent + fun getFileServersList(): FileServersEvent? = fileStorageListNote.event as? FileServersEvent fun normalizeServers(note: Note): List { val event = note.event as? FileServersEvent @@ -58,7 +58,7 @@ class FileStorageServerListState( val flow = getFileServersListFlow() .map { normalizeServers(it.note) } - .onStart { emit(normalizeServers(getFileServersNote())) } + .onStart { emit(normalizeServers(fileStorageListNote)) } .flowOn(Dispatchers.Default) .stateIn( scope, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nipB7Blossom/BlossomServerListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nipB7Blossom/BlossomServerListState.kt index 5d8557d35..47ee78e48 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nipB7Blossom/BlossomServerListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nipB7Blossom/BlossomServerListState.kt @@ -21,7 +21,6 @@ package com.vitorpamplona.amethyst.model.nipB7Blossom import com.vitorpamplona.amethyst.model.AccountSettings -import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.NoteState @@ -44,13 +43,14 @@ class BlossomServerListState( val scope: CoroutineScope, val settings: AccountSettings, ) { + // Creates a long-term reference for this note so that the GC doesn't collect the note it self + val blossomListNote = cache.getOrCreateAddressableNote(getBlossomServersAddress()) + fun getBlossomServersAddress() = BlossomServersEvent.createAddress(signer.pubKey) - fun getBlossomServersNote(): AddressableNote = cache.getOrCreateAddressableNote(getBlossomServersAddress()) + fun getBlossomServersListFlow(): StateFlow = blossomListNote.flow().metadata.stateFlow - fun getBlossomServersListFlow(): StateFlow = getBlossomServersNote().flow().metadata.stateFlow - - fun getBlossomServersList(): BlossomServersEvent? = getBlossomServersNote().event as? BlossomServersEvent + fun getBlossomServersList(): BlossomServersEvent? = blossomListNote.event as? BlossomServersEvent fun normalizeServers(note: Note): List { val event = note.event as? BlossomServersEvent @@ -60,7 +60,7 @@ class BlossomServerListState( val flow = getBlossomServersListFlow() .map { normalizeServers(it.note) } - .onStart { emit(normalizeServers(getBlossomServersNote())) } + .onStart { emit(normalizeServers(blossomListNote)) } .flowOn(Dispatchers.Default) .stateIn( scope,