Migrates to use suspending routines for OTS, statuses and edits

This commit is contained in:
Vitor Pamplona
2025-09-02 19:20:44 -04:00
parent c46cf79501
commit c64e65ddb4
3 changed files with 28 additions and 46 deletions

View File

@@ -129,14 +129,13 @@ fun LoadOts(
val noteStatus by observeNoteOts(note, accountViewModel) val noteStatus by observeNoteOts(note, accountViewModel)
LaunchedEffect(key1 = noteStatus) { LaunchedEffect(key1 = noteStatus) {
accountViewModel.findOtsEventsForNote(noteStatus?.note ?: note) { newOts -> val newOts = accountViewModel.findOtsEventsForNote(noteStatus?.note ?: note)
earliestDate = earliestDate =
if (newOts == null) { if (newOts == null) {
GenericLoadable.Empty() GenericLoadable.Empty()
} else { } else {
GenericLoadable.Loaded(newOts) GenericLoadable.Loaded(newOts)
} }
}
} }
(earliestDate as? GenericLoadable.Loaded)?.let { (earliestDate as? GenericLoadable.Loaded)?.let {

View File

@@ -1230,19 +1230,18 @@ fun observeEdits(
LaunchedEffect(key1 = updatedNote) { LaunchedEffect(key1 = updatedNote) {
updatedNote?.note?.let { updatedNote?.note?.let {
accountViewModel.findModificationEventsForNote(it) { newModifications -> val newModifications = accountViewModel.findModificationEventsForNote(it)
if (newModifications.isEmpty()) { if (newModifications.isEmpty()) {
if (editState.value !is GenericLoadable.Empty) { if (editState.value !is GenericLoadable.Empty) {
editState.value = GenericLoadable.Empty() editState.value = GenericLoadable.Empty()
} }
} else {
if (editState.value is GenericLoadable.Loaded) {
(editState.value as? GenericLoadable.Loaded<EditState>)?.loaded?.updateModifications(newModifications)
} else { } else {
if (editState.value is GenericLoadable.Loaded) { val state = EditState()
(editState.value as? GenericLoadable.Loaded<EditState>)?.loaded?.updateModifications(newModifications) state.updateModifications(newModifications)
} else { editState.value = GenericLoadable.Loaded(state)
val state = EditState()
state.updateModifications(newModifications)
editState.value = GenericLoadable.Loaded(state)
}
} }
} }
} }

View File

@@ -1026,38 +1026,22 @@ class AccountViewModel(
fun getAddressableNoteIfExists(key: Address): AddressableNote? = LocalCache.getAddressableNoteIfExists(key) fun getAddressableNoteIfExists(key: Address): AddressableNote? = LocalCache.getAddressableNoteIfExists(key)
suspend fun findStatusesForUser( suspend fun findStatusesForUser(myUser: User) =
myUser: User,
onResult: (ImmutableList<AddressableNote>) -> Unit,
) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
onResult(LocalCache.findStatusesForUser(myUser)) LocalCache.findStatusesForUser(myUser)
} }
}
suspend fun findOtsEventsForNote( suspend fun findOtsEventsForNote(note: Note) =
note: Note, withContext(Dispatchers.Default) {
onResult: (Long?) -> Unit, LocalCache.findEarliestOtsForNote(note, account.otsResolverBuilder)
) { }
onResult(
withContext(Dispatchers.Default) {
LocalCache.findEarliestOtsForNote(note, account.otsResolverBuilder)
},
)
}
fun cachedModificationEventsForNote(note: Note) = LocalCache.cachedModificationEventsForNote(note) fun cachedModificationEventsForNote(note: Note) = LocalCache.cachedModificationEventsForNote(note)
suspend fun findModificationEventsForNote( suspend fun findModificationEventsForNote(note: Note): List<Note> =
note: Note, withContext(Dispatchers.Default) {
onResult: (List<Note>) -> Unit, LocalCache.findLatestModificationForNote(note)
) { }
onResult(
withContext(Dispatchers.Default) {
LocalCache.findLatestModificationForNote(note)
},
)
}
fun checkGetOrCreatePublicChatChannel(key: HexKey): PublicChatChannel? = LocalCache.getOrCreatePublicChatChannel(key) fun checkGetOrCreatePublicChatChannel(key: HexKey): PublicChatChannel? = LocalCache.getOrCreatePublicChatChannel(key)