From a1568467f08f048cb28d5e0076358d42234c01cd Mon Sep 17 00:00:00 2001 From: GG Bond Date: Mon, 19 Feb 2024 14:14:35 +0800 Subject: [PATCH] Cleanup showTip usage --- .../com/hippo/ehviewer/ui/MainActivity.kt | 11 +-- .../ehviewer/ui/screen/FavoritesScreen.kt | 7 +- .../ehviewer/ui/screen/GalleryListScreen.kt | 91 ++++++++++--------- 3 files changed, 55 insertions(+), 54 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/MainActivity.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/MainActivity.kt index 503c3e47b..dc1899246 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/MainActivity.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/MainActivity.kt @@ -215,6 +215,7 @@ class MainActivity : EhActivity() { val dialogState = LocalDialogState.current val navDrawerState = rememberDrawerState(DrawerValue.Closed) val sideSheetState = rememberDrawerState(DrawerValue.Closed) + val snackbarState = remember { SnackbarHostState() } val scope = rememberCoroutineScope() val navController = rememberNavController() fun closeDrawer(callback: () -> Unit = {}) = scope.launch { @@ -249,7 +250,7 @@ class MainActivity : EhActivity() { } } }.onFailure { - showTip(getString(R.string.update_failed, ExceptionUtils.getReadableString(it))) + snackbarState.showSnackbar(getString(R.string.update_failed, ExceptionUtils.getReadableString(it))) } } @@ -295,7 +296,6 @@ class MainActivity : EhActivity() { } } - val snackbarState = remember { SnackbarHostState() } LaunchedEffect(Unit) { tipFlow.collectLatest { snackbarState.showSnackbar(it) @@ -495,11 +495,8 @@ class MainActivity : EhActivity() { } fun showTip(@StringRes id: Int, useToast: Boolean = false) { - showTip(getString(id), useToast) - } - - fun showTip(message: CharSequence, useToast: Boolean = false) { - if (useToast || !tipFlow.tryEmit(message.toString())) { + val message = getString(id) + if (useToast || !tipFlow.tryEmit(message)) { Toast.makeText(this, message, Toast.LENGTH_SHORT).show() } } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/FavoritesScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/FavoritesScreen.kt index 5c5799ef8..e19278adf 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/FavoritesScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/FavoritesScreen.kt @@ -66,6 +66,7 @@ import com.hippo.ehviewer.collectAsState import com.hippo.ehviewer.icons.EhIcons import com.hippo.ehviewer.icons.filled.GoTo import com.hippo.ehviewer.ui.LocalSideSheetState +import com.hippo.ehviewer.ui.LocalSnackbarHostState import com.hippo.ehviewer.ui.LockDrawer import com.hippo.ehviewer.ui.MainActivity import com.hippo.ehviewer.ui.main.FAB_ANIMATE_TIME @@ -121,6 +122,7 @@ fun FavouritesScreen(navigator: DestinationsNavigator) { } val context = LocalContext.current val density = LocalDensity.current + val snackbarState = LocalSnackbarHostState.current val dialogState = LocalDialogState.current val activity = remember(context) { context.findActivity() } val coroutineScope = rememberCoroutineScope() @@ -413,10 +415,11 @@ fun FavouritesScreen(navigator: DestinationsNavigator) { val gidArray = info.mapToLongArray(BaseGalleryInfo::gid) EhEngine.modifyFavorites(gidArray, srcCat, dstCat) } + }.onSuccess { + data.refresh() }.onFailure { - activity.showTip(ExceptionUtils.getReadableString(it)) + snackbarState.showSnackbar(ExceptionUtils.getReadableString(it)) } - data.refresh() } } } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryListScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryListScreen.kt index bce888e81..a3b408649 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryListScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryListScreen.kt @@ -112,6 +112,7 @@ import com.hippo.ehviewer.icons.EhIcons import com.hippo.ehviewer.icons.filled.GoTo import com.hippo.ehviewer.image.Image.Companion.decodeBitmap import com.hippo.ehviewer.ui.LocalSideSheetState +import com.hippo.ehviewer.ui.LocalSnackbarHostState import com.hippo.ehviewer.ui.MainActivity import com.hippo.ehviewer.ui.destinations.ProgressScreenDestination import com.hippo.ehviewer.ui.doGalleryInfoAction @@ -215,6 +216,7 @@ fun GalleryListScreen(lub: ListUrlBuilder, navigator: DestinationsNavigator) { animationSpec = tween(FAB_ANIMATE_TIME * 2), ) { showSearchLayout = false } val context = LocalContext.current + val snackbarState = LocalSnackbarHostState.current val activity = remember { context.findActivity() } val density = LocalDensity.current val dialogState = LocalDialogState.current @@ -338,55 +340,54 @@ fun GalleryListScreen(lub: ListUrlBuilder, navigator: DestinationsNavigator) { contentDescription = stringResource(id = R.string.readme), ) } + val invalidImageQuickSearch = stringResource(R.string.image_search_not_quick_search) IconButton(onClick = { if (data.itemCount == 0) return@IconButton - if (urlBuilder.mode == MODE_IMAGE_SEARCH) { - activity.showTip(R.string.image_search_not_quick_search, true) - return@IconButton - } - - val firstItem = data.itemSnapshotList.items[getFirstVisibleItemIndex()] - val next = firstItem.gid + 1 - quickSearchList.fastForEach { q -> - if (urlBuilder.equalsQuickSearch(q)) { - val nextStr = q.name.substringAfterLast('@', "") - if (nextStr.toLongOrNull() == next) { - activity.showTip(context.getString(R.string.duplicate_quick_search, q.name), true) - return@IconButton - } - } - } - coroutineScope.launch { - dialogState.awaitInputTextWithCheckBox( - initial = quickSearchName ?: urlBuilder.keyword.orEmpty(), - title = R.string.add_quick_search_dialog_title, - hint = R.string.quick_search, - checked = saveProgress, - checkBoxText = R.string.save_progress, - ) { input, checked -> - var text = input.trim() - if (text.isEmpty()) { - return@awaitInputTextWithCheckBox context.getString(R.string.name_is_empty) + if (urlBuilder.mode == MODE_IMAGE_SEARCH) { + snackbarState.showSnackbar(invalidImageQuickSearch) + } else { + val firstItem = data.itemSnapshotList.items[getFirstVisibleItemIndex()] + val next = firstItem.gid + 1 + quickSearchList.fastForEach { q -> + if (urlBuilder.equalsQuickSearch(q)) { + val nextStr = q.name.substringAfterLast('@', "") + if (nextStr.toLongOrNull() == next) { + snackbarState.showSnackbar(context.getString(R.string.duplicate_quick_search, q.name)) + return@launch + } + } } + dialogState.awaitInputTextWithCheckBox( + initial = quickSearchName ?: urlBuilder.keyword.orEmpty(), + title = R.string.add_quick_search_dialog_title, + hint = R.string.quick_search, + checked = saveProgress, + checkBoxText = R.string.save_progress, + ) { input, checked -> + var text = input.trim() + if (text.isEmpty()) { + return@awaitInputTextWithCheckBox context.getString(R.string.name_is_empty) + } - if (checked) { - text += "@$next" - } - if (quickSearchList.fastAny { it.name == text }) { - return@awaitInputTextWithCheckBox context.getString(R.string.duplicate_name) - } + if (checked) { + text += "@$next" + } + if (quickSearchList.fastAny { it.name == text }) { + return@awaitInputTextWithCheckBox context.getString(R.string.duplicate_name) + } - val quickSearch = urlBuilder.toQuickSearch(text) - quickSearch.position = quickSearchList.size - // Insert to DB first to update the id - withIOContext { - EhDB.insertQuickSearch(quickSearch) + val quickSearch = urlBuilder.toQuickSearch(text) + quickSearch.position = quickSearchList.size + // Insert to DB first to update the id + withIOContext { + EhDB.insertQuickSearch(quickSearch) + } + quickSearchList.add(quickSearch) + saveProgress = checked + return@awaitInputTextWithCheckBox null } - quickSearchList.add(quickSearch) - saveProgress = checked - return@awaitInputTextWithCheckBox null } } }) { @@ -530,7 +531,7 @@ fun GalleryListScreen(lub: ListUrlBuilder, navigator: DestinationsNavigator) { searchFieldState = searchFieldState, searchFieldHint = searchBarHint, showSearchFab = showSearchLayout, - onApplySearch = { query -> + onApplySearch = ret@{ query -> val builder = ListUrlBuilder() val oldMode = urlBuilder.mode if (!showSearchLayout) { @@ -546,11 +547,11 @@ fun GalleryListScreen(lub: ListUrlBuilder, navigator: DestinationsNavigator) { } else { builder.mode = MODE_IMAGE_SEARCH if (imagePath.isBlank()) { - activity.showTip(selectImageFirst) - return@SearchBarScreen + snackbarState.showSnackbar(selectImageFirst) + return@ret } val uri = Uri.parse(imagePath) - val temp = AppConfig.createTempFile() ?: return@SearchBarScreen + val temp = AppConfig.createTempFile() ?: return@ret val bitmap = context.decodeBitmap(uri) temp.outputStream().use { bitmap.compress(Bitmap.CompressFormat.JPEG, 90, it) } builder.imagePath = temp.path