diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt index f16586858..514556be7 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt @@ -77,6 +77,7 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import java.util.UUID enum class UserSuggestionAnchor { @@ -200,12 +201,16 @@ open class NewPostViewModel() : ViewModel() { val noteAuthor = draft?.author if (draft != null && noteEvent is DraftEvent && noteAuthor != null) { - accountViewModel.createTempDraftNote(noteEvent, noteAuthor) { innerNote -> - val oldTag = (draft.event as? AddressableEvent)?.dTag() - if (oldTag != null) { - draftTag = oldTag + viewModelScope.launch(Dispatchers.IO) { + accountViewModel.createTempDraftNote(noteEvent) { innerNote -> + if (innerNote != null) { + val oldTag = (draft.event as? AddressableEvent)?.dTag() + if (oldTag != null) { + draftTag = oldTag + } + loadFromDraft(innerNote, accountViewModel) + } } - loadFromDraft(innerNote, accountViewModel) } } else { originalNote = replyingTo @@ -442,18 +447,22 @@ open class NewPostViewModel() : ViewModel() { } fun sendDraft(relayList: List? = null) { - viewModelScope.launch(Dispatchers.IO) { - innerSendPost(relayList, draftTag) + viewModelScope.launch { + sendDraftSync(relayList) } } + suspend fun sendDraftSync(relayList: List? = null) { + innerSendPost(relayList, draftTag) + } + private suspend fun innerSendPost( relayList: List? = null, localDraft: String?, - ) { + ) = withContext(Dispatchers.IO) { if (accountViewModel == null) { cancel() - return + return@withContext } val tagger = NewMessageTagger(message.text, pTags, eTags, originalNote?.channelHex(), accountViewModel!!) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index 427bc3a91..0edba9bb3 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -1323,20 +1323,11 @@ class AccountViewModel(val account: Account, val settings: SettingsState) : View account.deleteDraft(draftTag) } - fun createTempCachedDraftNote( + suspend fun createTempDraftNote( noteEvent: DraftEvent, - author: User, - ): Note? { - return noteEvent.preCachedDraft(account.signer)?.let { createTempDraftNote(it, author) } - } - - fun createTempDraftNote( - noteEvent: DraftEvent, - author: User, - onReady: (Note) -> Unit, + onReady: (Note?) -> Unit, ) { - viewModelScope.launch(Dispatchers.IO) { - } + draftNoteCache.update(noteEvent, onReady) } fun createTempDraftNote(