- Only saves draft after the first change in the UI.

- Adjusts coroutine scopes to make sure it cannot be cancelled while posting
This commit is contained in:
Vitor Pamplona
2025-05-20 18:21:19 -04:00
parent de881c23ec
commit e10332c957
7 changed files with 39 additions and 48 deletions
@@ -118,7 +118,10 @@ open class CommentPostViewModel :
init {
viewModelScope.launch(Dispatchers.IO) {
draftTag.versions.collectLatest {
sendDraftSync()
// don't save the first
if (it > 0) {
sendDraftSync()
}
}
}
}
@@ -108,7 +108,10 @@ class ChatNewMessageViewModel :
init {
viewModelScope.launch(Dispatchers.IO) {
draftTag.versions.collectLatest {
sendDraft()
// don't save the first
if (it > 0) {
sendDraftSync()
}
}
}
}
@@ -359,12 +362,6 @@ class ChatNewMessageViewModel :
cancel()
}
fun sendDraft() {
viewModelScope.launch(Dispatchers.IO) {
sendDraftSync()
}
}
suspend fun sendDraftSync() {
if (message.text.isBlank()) {
account?.deleteDraft(draftTag.current)
@@ -75,6 +75,7 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.core.net.toUri
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.vitorpamplona.amethyst.Amethyst
import com.vitorpamplona.amethyst.R
@@ -133,7 +134,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@OptIn(ExperimentalMaterial3Api::class, FlowPreview::class)
@Composable
@@ -195,13 +195,13 @@ fun NewGroupDMScreen(
CloseButton(
modifier = HalfStartPadding,
onPress = {
scope.launch {
withContext(Dispatchers.IO) {
postViewModel.sendDraftSync()
postViewModel.cancel()
}
// uses the accountViewModel scope to avoid cancelling this
// function when the postViewModel is released
accountViewModel.viewModelScope.launch(Dispatchers.IO) {
postViewModel.sendDraftSync()
delay(100)
nav.popBack()
postViewModel.cancel()
}
},
)
@@ -234,10 +234,14 @@ fun ActionButton(
PostButton(
modifier = HalfEndPadding,
onPost = {
postViewModel.sendPost {
// uses the accountViewModel scope to avoid cancelling this
// function when the postViewModel is released
accountViewModel.viewModelScope.launch(Dispatchers.IO) {
postViewModel.sendPostSync()
postViewModel.room?.let {
nav.nav(routeToMessage(it, null, null, null, accountViewModel))
}
postViewModel.cancel()
}
nav.popBack()
},
@@ -104,7 +104,10 @@ open class ChannelNewMessageViewModel :
init {
viewModelScope.launch(Dispatchers.IO) {
draftTag.versions.collectLatest {
sendDraft()
// don't save the first
if (it > 0) {
sendDraftSync()
}
}
}
}
@@ -279,12 +282,6 @@ open class ChannelNewMessageViewModel :
cancel()
}
fun sendDraft() {
viewModelScope.launch(Dispatchers.IO) {
sendDraftSync()
}
}
suspend fun sendDraftSync() {
val accountViewModel = accountViewModel ?: return
@@ -49,7 +49,6 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Alignment.Companion.CenterVertically
@@ -57,6 +56,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Note
@@ -99,7 +99,6 @@ import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@@ -153,7 +152,6 @@ fun NewProductScreen(
) {
WatchAndLoadMyEmojiList(accountViewModel)
val scope = rememberCoroutineScope()
var showRelaysDialog by remember { mutableStateOf(false) }
Scaffold(
@@ -182,10 +180,10 @@ fun NewProductScreen(
}
PostButton(
onPost = {
postViewModel.sendPost()
scope.launch {
delay(100)
accountViewModel.viewModelScope.launch(Dispatchers.IO) {
postViewModel.sendPostSync()
nav.popBack()
postViewModel.cancel()
}
},
isActive = postViewModel.canPost(),
@@ -197,13 +195,11 @@ fun NewProductScreen(
Spacer(modifier = StdHorzSpacer)
CloseButton(
onPress = {
scope.launch {
withContext(Dispatchers.IO) {
postViewModel.sendDraftSync()
postViewModel.cancel()
}
accountViewModel.viewModelScope.launch(Dispatchers.IO) {
postViewModel.sendDraftSync()
nav.popBack()
postViewModel.cancel()
}
nav.popBack()
},
)
}
@@ -106,7 +106,10 @@ open class NewProductViewModel :
init {
viewModelScope.launch(Dispatchers.IO) {
draftTag.versions.collectLatest {
sendDraft()
// don't save the first
if (it > 0) {
sendDraftSync()
}
}
}
}
@@ -285,12 +288,6 @@ open class NewProductViewModel :
urlPreviews.update(message)
}
fun sendPost() {
viewModelScope.launch(Dispatchers.IO) {
sendPostSync()
}
}
suspend fun sendPostSync() {
val template = createTemplate() ?: return
@@ -304,12 +301,6 @@ open class NewProductViewModel :
cancel()
}
fun sendDraft() {
viewModelScope.launch(Dispatchers.IO) {
sendDraftSync()
}
}
suspend fun sendDraftSync() {
val accountViewModel = accountViewModel ?: return
@@ -139,7 +139,10 @@ open class ShortNotePostViewModel :
init {
viewModelScope.launch(Dispatchers.IO) {
draftTag.versions.collectLatest {
sendDraftSync()
// don't save the first
if (it > 0) {
sendDraftSync()
}
}
}
}