Moves the creation of Draft Notes to the IO Thread

This commit is contained in:
Vitor Pamplona
2024-04-02 17:49:52 -04:00
parent 4d7de6bc19
commit 638dba770d
3 changed files with 106 additions and 13 deletions
@@ -39,7 +39,6 @@ import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
@@ -54,6 +53,7 @@ import androidx.compose.ui.unit.dp
import androidx.lifecycle.distinctUntilChanged
import androidx.lifecycle.map
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.commons.compose.produceCachedStateAsync
import com.vitorpamplona.amethyst.model.Channel
import com.vitorpamplona.amethyst.model.FeatureSetType
import com.vitorpamplona.amethyst.model.Note
@@ -724,16 +724,8 @@ fun ObserveDraftEvent(
val noteState by note.live().metadata.observeAsState()
val noteEvent = noteState?.note?.event as? DraftEvent ?: return
val noteAuthor = noteState?.note?.author ?: return
val innerNote =
produceState(initialValue = accountViewModel.createTempCachedDraftNote(noteEvent, noteAuthor), noteEvent.id) {
if (value == null || value?.event?.id() != noteEvent.id) {
accountViewModel.createTempDraftNote(noteEvent, noteAuthor) {
value = it
}
}
}
val innerNote = produceCachedStateAsync(cache = accountViewModel.draftNoteCache, key = noteEvent)
innerNote.value?.let {
render(it)
@@ -38,6 +38,7 @@ import coil.request.ImageRequest
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ServiceManager
import com.vitorpamplona.amethyst.commons.compose.GenericBaseCache
import com.vitorpamplona.amethyst.commons.compose.GenericBaseCacheAsync
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.AccountState
import com.vitorpamplona.amethyst.model.AddressableNote
@@ -1335,9 +1336,6 @@ class AccountViewModel(val account: Account, val settings: SettingsState) : View
onReady: (Note) -> Unit,
) {
viewModelScope.launch(Dispatchers.IO) {
noteEvent.cachedDraft(account.signer) {
onReady(createTempDraftNote(it, author))
}
}
}
@@ -1355,6 +1353,21 @@ class AccountViewModel(val account: Account, val settings: SettingsState) : View
return note
}
val draftNoteCache = CachedDraftNotes(this)
class CachedDraftNotes(val accountViewModel: AccountViewModel) : GenericBaseCacheAsync<DraftEvent, Note>(20) {
override suspend fun compute(
key: DraftEvent,
onReady: (Note?) -> Unit,
) = withContext(Dispatchers.IO) {
key.cachedDraft(accountViewModel.account.signer) {
val author = LocalCache.getOrCreateUser(key.pubKey)
val note = accountViewModel.createTempDraftNote(it, author)
onReady(note)
}
}
}
val bechLinkCache = CachedLoadedBechLink(this)
class CachedLoadedBechLink(val accountViewModel: AccountViewModel) : GenericBaseCache<String, LoadedBechLink>(20) {