Skip to content

Commit f001be9

Browse files
committed
better coroutine handling
1 parent 0b9956d commit f001be9

File tree

5 files changed

+37
-39
lines changed

5 files changed

+37
-39
lines changed

app/src/main/java/com/raival/compose/file/explorer/screen/main/tab/files/ui/FilesList.kt

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import com.raival.compose.file.explorer.screen.preferences.constant.FileItemSize
7373
import com.raival.compose.file.explorer.screen.preferences.constant.FileItemSizeMap.getFileListIconSize
7474
import com.raival.compose.file.explorer.screen.preferences.constant.FileItemSizeMap.getFileListSpace
7575
import kotlinx.coroutines.Dispatchers
76+
import kotlinx.coroutines.Dispatchers.IO
7677
import kotlinx.coroutines.delay
7778
import kotlinx.coroutines.launch
7879
import kotlinx.coroutines.withContext
@@ -577,39 +578,36 @@ private fun FileDetails(
577578
fontSize: Int,
578579
isHighlighted: Boolean
579580
) {
580-
val scope = rememberCoroutineScope()
581-
Isolate {
582-
var details by remember(
583-
key1 = currentItemPath,
584-
key2 = item.lastModified
585-
) { mutableStateOf(emptyString) }
586-
587-
LaunchedEffect(
588-
key1 = currentItemPath,
589-
key2 = item.lastModified
590-
) {
591-
scope.launch(Dispatchers.IO) {
592-
if (details.isEmpty()) {
593-
val det = item.getDetails()
594-
details = det
595-
}
581+
var details by remember(
582+
key1 = currentItemPath,
583+
key2 = item.lastModified
584+
) { mutableStateOf(emptyString) }
585+
586+
LaunchedEffect(
587+
key1 = currentItemPath,
588+
key2 = item.lastModified
589+
) {
590+
if (details.isEmpty()) {
591+
val det = withContext(IO) {
592+
item.getDetails()
596593
}
594+
details = det
597595
}
598-
599-
Text(
600-
modifier = Modifier.alpha(0.7f),
601-
text = details,
602-
fontSize = (fontSize - 4).sp,
603-
maxLines = 1,
604-
lineHeight = (fontSize + 2).sp,
605-
overflow = TextOverflow.Ellipsis,
606-
color = if (isHighlighted) {
607-
colorScheme.primary
608-
} else {
609-
Color.Unspecified
610-
}
611-
)
612596
}
597+
598+
Text(
599+
modifier = Modifier.alpha(0.7f),
600+
text = details,
601+
fontSize = (fontSize - 4).sp,
602+
maxLines = 1,
603+
lineHeight = (fontSize + 2).sp,
604+
overflow = TextOverflow.Ellipsis,
605+
color = if (isHighlighted) {
606+
colorScheme.primary
607+
} else {
608+
Color.Unspecified
609+
}
610+
)
613611
}
614612

615613
@Composable

app/src/main/java/com/raival/compose/file/explorer/screen/main/tab/files/ui/dialog/BookmarksDialog.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ import com.raival.compose.file.explorer.common.ui.Space
4545
import com.raival.compose.file.explorer.screen.main.tab.files.FilesTab
4646
import com.raival.compose.file.explorer.screen.main.tab.files.holder.LocalFileHolder
4747
import com.raival.compose.file.explorer.screen.main.tab.files.ui.FileItemRow
48-
import kotlinx.coroutines.launch
48+
import kotlinx.coroutines.Dispatchers
49+
import kotlinx.coroutines.withContext
4950
import java.io.File
5051

5152
@OptIn(ExperimentalMaterialApi::class, ExperimentalFoundationApi::class)
@@ -62,7 +63,7 @@ fun BookmarksDialog(
6263
}
6364

6465
LaunchedEffect(Unit) {
65-
tab.scope.launch {
66+
withContext(Dispatchers.IO) {
6667
val originalList = globalClass.preferencesManager.bookmarks
6768
bookmarks.addAll(
6869
originalList.map { LocalFileHolder(File(it)) }.filter { it.isValid() }

app/src/main/java/com/raival/compose/file/explorer/screen/main/tab/home/ui/HomeLayoutSettingsScreen.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ import com.raival.compose.file.explorer.screen.main.tab.home.data.HomeSectionCon
6464
import com.raival.compose.file.explorer.screen.main.tab.home.data.HomeSectionType
6565
import com.raival.compose.file.explorer.screen.main.tab.home.data.getDefaultHomeLayout
6666
import kotlinx.coroutines.Dispatchers
67-
import kotlinx.coroutines.launch
67+
import kotlinx.coroutines.withContext
6868
import sh.calvin.reorderable.ReorderableCollectionItemScope
6969
import sh.calvin.reorderable.ReorderableItem
7070
import sh.calvin.reorderable.rememberReorderableLazyListState
@@ -103,7 +103,7 @@ fun HomeLayoutSettingsScreen(
103103
}
104104

105105
LaunchedEffect(Unit) {
106-
scope.launch(Dispatchers.IO) {
106+
withContext(Dispatchers.IO) {
107107
val config = try {
108108
Gson().fromJson(
109109
globalClass.preferencesManager.homeTabLayout,

app/src/main/java/com/raival/compose/file/explorer/screen/main/tab/home/ui/HomeTabContentView.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import com.raival.compose.file.explorer.screen.main.ui.StorageDeviceView
8282
import kotlinx.coroutines.Dispatchers
8383
import kotlinx.coroutines.async
8484
import kotlinx.coroutines.launch
85+
import kotlinx.coroutines.withContext
8586

8687
@OptIn(ExperimentalFoundationApi::class)
8788
@Composable
@@ -91,7 +92,7 @@ fun ColumnScope.HomeTabContentView(tab: HomeTab) {
9192
val enabledSections = remember { mutableStateListOf<HomeSectionConfig>() }
9293

9394
LaunchedEffect(tab.id) {
94-
scope.launch(Dispatchers.IO) {
95+
withContext(Dispatchers.IO) {
9596
async {
9697
tab.fetchRecentFiles()
9798
}

app/src/main/java/com/raival/compose/file/explorer/screen/viewer/pdf/ui/PdfViewerContent.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import androidx.compose.runtime.getValue
2020
import androidx.compose.runtime.mutableStateListOf
2121
import androidx.compose.runtime.mutableStateOf
2222
import androidx.compose.runtime.remember
23-
import androidx.compose.runtime.rememberCoroutineScope
2423
import androidx.compose.runtime.setValue
2524
import androidx.compose.ui.Alignment
2625
import androidx.compose.ui.Modifier
@@ -31,7 +30,7 @@ import com.raival.compose.file.explorer.common.isNot
3130
import com.raival.compose.file.explorer.screen.viewer.pdf.PdfViewerInstance
3231
import com.raival.compose.file.explorer.screen.viewer.pdf.misc.PdfPageHolder
3332
import kotlinx.coroutines.Dispatchers.IO
34-
import kotlinx.coroutines.launch
33+
import kotlinx.coroutines.withContext
3534
import my.nanihadesuka.compose.InternalLazyColumnScrollbar
3635
import my.nanihadesuka.compose.ScrollbarLayoutSide
3736
import my.nanihadesuka.compose.ScrollbarSettings
@@ -61,7 +60,6 @@ fun PdfViewerContent(instance: PdfViewerInstance, onBackPress: () -> Unit) {
6160
listState.firstVisibleItemIndex == 0
6261
}
6362
}
64-
val scope = rememberCoroutineScope()
6563

6664
val visiblePageNumbers by remember {
6765
derivedStateOf {
@@ -83,7 +81,7 @@ fun PdfViewerContent(instance: PdfViewerInstance, onBackPress: () -> Unit) {
8381
}
8482

8583
LaunchedEffect(Unit) {
86-
scope.launch(IO) {
84+
withContext(IO) {
8785
instance.prepare { success ->
8886
if (success) {
8987
defaultPageSize = Size(

0 commit comments

Comments
 (0)