Cleanup showTip usage

This commit is contained in:
GG Bond authored and Vladimir Dostoyevskiy committed 2024-02-19 15:29:33 +08:00
1 parent 32e2a03c31
commit a1568467f0
3 files changed
+55 -54

No files matched your search

@@ -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()
}
}
@@ -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<MainActivity>() }
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()
}
}
}
@@ -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<MainActivity>() }
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