Skip to content

Commit f9bcf3f

Browse files
committed
fix wrong dispatch mode causing frontend hangs
1 parent e755574 commit f9bcf3f

4 files changed

Lines changed: 14 additions & 6 deletions

File tree

composeApp/src/commonMain/kotlin/me/lkl/dalvikus/ui/editor/EditorView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ fun EditorView(tabElement: TabElement) {
119119
Scaffold(
120120
containerColor = Color.Transparent,
121121
floatingActionButton = {
122-
if (viewModel.hasUnsavedChanges())
122+
if (viewModel.hasUnsavedChanges() && !viewModel.isSaving)
123123
ExtendedFloatingActionButton(
124124
modifier = Modifier.padding(bottom = 8.dp, end = 8.dp),
125125
onClick = { viewModel.saveCode(coroutine) },

composeApp/src/commonMain/kotlin/me/lkl/dalvikus/ui/editor/EditorViewModel.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class EditorViewModel(private val tab: TabElement) {
4646

4747
val editable by mutableStateOf(tab.contentProvider.isEditable())
4848

49+
var isSaving by mutableStateOf(false)
50+
private set
51+
4952
var openable by
5053
mutableStateOf(tab.contentProvider.getSizeEstimate() < maxEditorFileSize && tab.contentProvider.isDisplayable())
5154
private set
@@ -89,7 +92,7 @@ class EditorViewModel(private val tab: TabElement) {
8992
}
9093

9194
suspend fun loadCode() {
92-
withContext(Dispatchers.IO) {
95+
withContext(Dispatchers.Default) {
9396
tab.contentProvider.loadContent()
9497
internalContent = internalContent.copy(text = tab.contentProvider.contentFlow.value.decodeToString())
9598

@@ -203,9 +206,13 @@ class EditorViewModel(private val tab: TabElement) {
203206

204207
fun saveCode(coroutineScope: CoroutineScope) {
205208
if (!isLoaded) throw IllegalArgumentException("code not initialized.")
206-
coroutineScope.launch(crtExHandler) {
209+
isSaving = true
210+
coroutineScope.launch(Dispatchers.Default + crtExHandler) {
207211
tab.contentProvider.updateContent(internalContent.text.toByteArray())
208212
tab.hasUnsavedChanges.value = false
213+
withContext(Dispatchers.Main) {
214+
isSaving = false
215+
}
209216
}
210217
}
211218

composeApp/src/commonMain/kotlin/me/lkl/dalvikus/ui/packaging/PackagingViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class PackagingViewModel() {
5757
apk: File,
5858
onError: (Throwable) -> Unit,
5959
onSuccess: (ApkVerifier.Result) -> Unit,
60-
) = withContext(Dispatchers.IO) {
60+
) = withContext(Dispatchers.Default) {
6161
try {
6262
val keystoreFile = keystoreInfo.keystoreFile
6363
val keyAlias = keystoreInfo.keyAlias
@@ -150,7 +150,7 @@ class PackagingViewModel() {
150150
onError: (Throwable) -> Unit,
151151
onSuccess: () -> Unit,
152152
packageName: String? = null
153-
) = withContext(Dispatchers.IO) {
153+
) = withContext(Dispatchers.Default) {
154154
// TODO add string resources for messages here.
155155
try {
156156
AndroidDebugBridge.init(false)

composeApp/src/commonMain/kotlin/me/lkl/dalvikus/ui/tree/TreeView.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import androidx.compose.ui.graphics.Color
1919
import androidx.compose.ui.text.style.TextOverflow
2020
import androidx.compose.ui.unit.dp
2121
import co.touchlab.kermit.Logger
22+
import kotlinx.coroutines.Dispatchers
2223
import kotlinx.coroutines.launch
2324
import me.lkl.dalvikus.snackbarManager
2425
import me.lkl.dalvikus.tree.ContainerNode
@@ -94,7 +95,7 @@ fun TreeView(
9495
indent = indent,
9596
isExpanded = expandedState[node] == true,
9697
onToggleExpand = { shouldExpand ->
97-
coroutineScope.launch(crtExHandler) {
98+
coroutineScope.launch(Dispatchers.IO + crtExHandler) {
9899
if (node is ContainerNode) {
99100
if (shouldExpand) {
100101
try {

0 commit comments

Comments
 (0)