composing with CoroutineScope
This commit is contained in:
1 parent
f082766f06
commit
9040cb54d4
4 files changed
+24
-48
No files matched your search
@@ -3,14 +3,16 @@ package com.hippo.ehviewer.ui
|
||||
import android.content.Context
|
||||
import androidx.compose.material3.SnackbarHostState
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import com.hippo.ehviewer.ui.tools.DialogState
|
||||
import com.hippo.ehviewer.ui.tools.LocalDialogState
|
||||
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
||||
@Composable
|
||||
inline fun <R> composing(
|
||||
navigator: DestinationsNavigator,
|
||||
block: @Composable context(Context, SnackbarHostState, DialogState, DestinationsNavigator)
|
||||
block: @Composable context(Context, SnackbarHostState, DialogState, DestinationsNavigator, CoroutineScope)
|
||||
() -> R,
|
||||
) = block(LocalContext.current, LocalSnackBarHostState.current, LocalDialogState.current, navigator)
|
||||
) = block(LocalContext.current, LocalSnackBarHostState.current, LocalDialogState.current, navigator, rememberCoroutineScope())
|
||||
@@ -17,7 +17,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.Modifier
|
||||
@@ -44,7 +43,6 @@ import kotlinx.coroutines.launch
|
||||
@Destination
|
||||
@Composable
|
||||
fun ReaderScreen(info: BaseGalleryInfo, page: Int = -1, navigator: DestinationsNavigator) = composing(navigator) {
|
||||
val scope = rememberCoroutineScope()
|
||||
LockDrawer(true)
|
||||
val pageLoader = remember {
|
||||
val archive = DownloadManager.getDownloadInfo(info.gid)?.archiveFile
|
||||
@@ -93,7 +91,7 @@ fun ReaderScreen(info: BaseGalleryInfo, page: Int = -1, navigator: DestinationsN
|
||||
lazyListState = lazyListState,
|
||||
pageLoader = pageLoader,
|
||||
onSelectPage = { page ->
|
||||
scope.launch {
|
||||
launch {
|
||||
dialog { cont ->
|
||||
fun dispose() = cont.cancel()
|
||||
val state = rememberModalBottomSheetState()
|
||||
@@ -105,11 +103,11 @@ fun ReaderScreen(info: BaseGalleryInfo, page: Int = -1, navigator: DestinationsN
|
||||
ReaderPageSheetMeta(
|
||||
retry = { pageLoader.retryPage(page.index) },
|
||||
retryOrigin = { pageLoader.retryPage(page.index, true) },
|
||||
share = { scope.launchIO { with(pageLoader) { shareImage(page, info) } } },
|
||||
copy = { scope.launchIO { with(pageLoader) { copy(page) } } },
|
||||
share = { launchIO { with(pageLoader) { shareImage(page, info) } } },
|
||||
copy = { launchIO { with(pageLoader) { copy(page) } } },
|
||||
save = {},
|
||||
saveTo = {},
|
||||
dismiss = { scope.launch { state.hide().also { dispose() } } },
|
||||
dismiss = { launch { state.hide().also { dispose() } } },
|
||||
)
|
||||
Spacer(modifier = Modifier.navigationBarsPadding())
|
||||
}
|
||||
|
||||
@@ -59,7 +59,6 @@ import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.mutableStateMapOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.rememberUpdatedState
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
@@ -142,7 +141,6 @@ fun DownloadsScreen(navigator: DestinationsNavigator) = composing(navigator) {
|
||||
|
||||
val context = LocalContext.current
|
||||
val activity = remember { findActivity<MainActivity>() }
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val density = LocalDensity.current
|
||||
val view = LocalView.current
|
||||
val allName = stringResource(R.string.download_all)
|
||||
@@ -176,14 +174,14 @@ fun DownloadsScreen(navigator: DestinationsNavigator) = composing(navigator) {
|
||||
|
||||
with(activity) {
|
||||
ProvideSideSheetContent { drawerState ->
|
||||
fun closeSheet() = coroutineScope.launch { drawerState.close() }
|
||||
fun closeSheet() = launch { drawerState.close() }
|
||||
TopAppBar(
|
||||
title = { Text(text = labelsStr) },
|
||||
windowInsets = WindowInsets(0, 0, 0, 0),
|
||||
actions = {
|
||||
IconButton(
|
||||
onClick = {
|
||||
coroutineScope.launch {
|
||||
launch {
|
||||
val text = awaitInputText(title = newLabel, hint = labelsStr) { text ->
|
||||
when {
|
||||
text.isBlank() -> labelEmpty
|
||||
@@ -201,7 +199,7 @@ fun DownloadsScreen(navigator: DestinationsNavigator) = composing(navigator) {
|
||||
val letMeSelect = stringResource(R.string.let_me_select)
|
||||
IconButton(
|
||||
onClick = {
|
||||
coroutineScope.launch {
|
||||
launch {
|
||||
showSelectActions(R.string.default_download_label) {
|
||||
onSelect(letMeSelect) {
|
||||
Settings.hasDefaultDownloadLabel = false
|
||||
@@ -268,7 +266,7 @@ fun DownloadsScreen(navigator: DestinationsNavigator) = composing(navigator) {
|
||||
val dismissState = rememberSwipeToDismissBoxState(
|
||||
confirmValueChange = {
|
||||
if (it == SwipeToDismissBoxValue.EndToStart) {
|
||||
coroutineScope.launch {
|
||||
launch {
|
||||
DownloadManager.deleteLabel(item)
|
||||
when (filterState.label) {
|
||||
item -> switchLabel("")
|
||||
@@ -306,7 +304,7 @@ fun DownloadsScreen(navigator: DestinationsNavigator) = composing(navigator) {
|
||||
Row {
|
||||
IconButton(
|
||||
onClick = {
|
||||
coroutineScope.launch {
|
||||
launch {
|
||||
val new = awaitInputText(initial = item, title = renameLabel, hint = labelsStr) { text ->
|
||||
when {
|
||||
text.isBlank() -> labelEmpty
|
||||
@@ -334,9 +332,7 @@ fun DownloadsScreen(navigator: DestinationsNavigator) = composing(navigator) {
|
||||
val range = if (fromIndex < index) fromIndex..index else index..fromIndex
|
||||
val toUpdate = labelList.slice(range)
|
||||
toUpdate.zip(range).forEach { it.first.position = it.second }
|
||||
coroutineScope.launchIO {
|
||||
EhDB.updateDownloadLabel(toUpdate)
|
||||
}
|
||||
launchIO { EhDB.updateDownloadLabel(toUpdate) }
|
||||
}
|
||||
fromIndex = -1
|
||||
}
|
||||
@@ -378,9 +374,7 @@ fun DownloadsScreen(navigator: DestinationsNavigator) = composing(navigator) {
|
||||
text = { Text(text = stringResource(id = R.string.download_labels)) },
|
||||
onClick = {
|
||||
expanded = false
|
||||
coroutineScope.launch {
|
||||
sideSheetState.open()
|
||||
}
|
||||
launch { sideSheetState.open() }
|
||||
},
|
||||
)
|
||||
DropdownMenuItem(
|
||||
@@ -396,16 +390,14 @@ fun DownloadsScreen(navigator: DestinationsNavigator) = composing(navigator) {
|
||||
text = { Text(text = stringResource(id = R.string.download_stop_all)) },
|
||||
onClick = {
|
||||
expanded = false
|
||||
coroutineScope.launchIO {
|
||||
DownloadManager.stopAllDownload()
|
||||
}
|
||||
launchIO { DownloadManager.stopAllDownload() }
|
||||
},
|
||||
)
|
||||
DropdownMenuItem(
|
||||
text = { Text(text = stringResource(id = R.string.download_reset_reading_progress)) },
|
||||
onClick = {
|
||||
expanded = false
|
||||
coroutineScope.launchIO {
|
||||
launchIO {
|
||||
awaitPermissionOrCancel(
|
||||
confirmText = android.R.string.ok,
|
||||
dismissText = android.R.string.cancel,
|
||||
@@ -460,9 +452,7 @@ fun DownloadsScreen(navigator: DestinationsNavigator) = composing(navigator) {
|
||||
}
|
||||
|
||||
fun onItemClick(info: DownloadInfo) {
|
||||
coroutineScope.launchIO {
|
||||
EhDB.putHistoryInfo(info.galleryInfo)
|
||||
}
|
||||
launchIO { EhDB.putHistoryInfo(info.galleryInfo) }
|
||||
navToReader(info.galleryInfo)
|
||||
}
|
||||
Crossfade(targetState = gridView, label = "Downloads") { showGridView ->
|
||||
@@ -519,11 +509,7 @@ fun DownloadsScreen(navigator: DestinationsNavigator) = composing(navigator) {
|
||||
intent.putExtra(DownloadService.KEY_GALLERY_INFO, info.galleryInfo)
|
||||
ContextCompat.startForegroundService(activity, intent)
|
||||
},
|
||||
onStop = {
|
||||
coroutineScope.launchIO {
|
||||
DownloadManager.stopDownload(info.gid)
|
||||
}
|
||||
},
|
||||
onStop = { launchIO { DownloadManager.stopDownload(info.gid) } },
|
||||
info = info,
|
||||
modifier = Modifier.height(height),
|
||||
interactionSource = interactionSource,
|
||||
|
||||
@@ -21,7 +21,6 @@ import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.material3.rememberSwipeToDismissBoxState
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||
@@ -59,7 +58,6 @@ import kotlinx.coroutines.launch
|
||||
@Destination
|
||||
@Composable
|
||||
fun HistoryScreen(navigator: DestinationsNavigator) = composing(navigator) {
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
|
||||
val historyData = rememberInVM {
|
||||
Pager(config = PagingConfig(pageSize = 20, jumpThreshold = 40)) {
|
||||
@@ -74,13 +72,13 @@ fun HistoryScreen(navigator: DestinationsNavigator) = composing(navigator) {
|
||||
title = { Text(text = stringResource(id = R.string.history)) },
|
||||
navigationIcon = {
|
||||
val drawerState = LocalNavDrawerState.current
|
||||
IconButton(onClick = { coroutineScope.launch { drawerState.open() } }) {
|
||||
IconButton(onClick = { launch { drawerState.open() } }) {
|
||||
Icon(imageVector = Icons.Default.Menu, contentDescription = null)
|
||||
}
|
||||
},
|
||||
actions = {
|
||||
IconButton(onClick = {
|
||||
coroutineScope.launchIO {
|
||||
launchIO {
|
||||
awaitPermissionOrCancel(
|
||||
confirmText = R.string.clear_all,
|
||||
text = { Text(text = stringResource(id = R.string.clear_all_history)) },
|
||||
@@ -113,9 +111,7 @@ fun HistoryScreen(navigator: DestinationsNavigator) = composing(navigator) {
|
||||
val dismissState = rememberSwipeToDismissBoxState(
|
||||
confirmValueChange = {
|
||||
if (it == SwipeToDismissBoxValue.EndToStart) {
|
||||
coroutineScope.launchIO {
|
||||
EhDB.deleteHistoryInfo(info)
|
||||
}
|
||||
launchIO { EhDB.deleteHistoryInfo(info) }
|
||||
}
|
||||
true
|
||||
},
|
||||
@@ -126,14 +122,8 @@ fun HistoryScreen(navigator: DestinationsNavigator) = composing(navigator) {
|
||||
modifier = Modifier.animateItemPlacement(),
|
||||
) {
|
||||
GalleryInfoListItem(
|
||||
onClick = {
|
||||
navigate(info.asDst())
|
||||
},
|
||||
onLongClick = {
|
||||
coroutineScope.launchIO {
|
||||
doGalleryInfoAction(info)
|
||||
}
|
||||
},
|
||||
onClick = { navigate(info.asDst()) },
|
||||
onLongClick = { launchIO { doGalleryInfoAction(info) } },
|
||||
info = info,
|
||||
showPages = showPages,
|
||||
modifier = Modifier.height(cardHeight),
|
||||
|
||||
Reference in new issue
Block a user