From d4609593e22a8377768326bf9b9031e58ae94c64 Mon Sep 17 00:00:00 2001 From: GG Bond Date: Sun, 18 Feb 2024 18:27:27 +0800 Subject: [PATCH] Remove showTip usage in GalleryDetailScreen --- .../ehviewer/ui/screen/GalleryDetailScreen.kt | 215 ++++++++++-------- 1 file changed, 114 insertions(+), 101 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt index 55d9dbc61..0bfe612de 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/screen/GalleryDetailScreen.kt @@ -164,7 +164,6 @@ import eu.kanade.tachiyomi.util.lang.withIOContext import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.system.logcat import kotlin.math.roundToInt -import kotlinx.coroutines.CancellationException import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock @@ -290,12 +289,12 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa EhEngine.voteTag(apiUid, apiKey, gid, token, tag, vote) }.onSuccess { result -> if (result != null) { - activity.showTip(result) + snackbarState.showSnackbar(result) } else { - activity.showTip(voteSuccess) + snackbarState.showSnackbar(voteSuccess) } }.onFailure { - activity.showTip(voteFailed) + snackbarState.showSnackbar(voteFailed) } } @@ -306,83 +305,88 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa val archiveFree = stringResource(R.string.archive_free) val archiveOriginal = stringResource(R.string.archive_original) val archiveResample = stringResource(R.string.archive_resample) + val failureNoHath = stringResource(R.string.download_archive_failure_no_hath) + val noArchive = stringResource(R.string.no_archives) + val downloadStarted = stringResource(R.string.download_archive_started) + val downloadFailed = stringResource(R.string.download_archive_failure) + val signInFirst = stringResource(R.string.sign_in_first) fun showArchiveDialog() { val galleryDetail = galleryInfo as? GalleryDetail ?: return - if (galleryDetail.apiUid < 0) { - activity.showTip(R.string.sign_in_first) - return - } coroutineScope.launchIO { - runSuspendCatching { - if (mArchiveList == null) { - val result = dialogState.bgWork { - withIOContext { - EhEngine.getArchiveList(galleryDetail.archiveUrl!!, gid, token) - } - } - mArchiveFormParamOr = result.paramOr - mArchiveList = result.archiveList - mCurrentFunds = result.funds - } - if (mArchiveList!!.isEmpty()) { - activity.showTip(R.string.no_archives) - } else { - val items = mArchiveList!!.map { - it.run { - if (isHAtH) { - val costStr = if (cost == "Free") archiveFree else cost - "[H@H] $name [$size] [$costStr]" - } else { - val nameStr = if (res == "org") archiveOriginal else archiveResample - val costStr = if (cost == "Free!") archiveFree else cost - "$nameStr [$size] [$costStr]" + if (galleryDetail.apiUid < 0) { + snackbarState.showSnackbar(signInFirst) + } else { + runSuspendCatching { + if (mArchiveList == null) { + val result = dialogState.bgWork { + withIOContext { + EhEngine.getArchiveList(galleryDetail.archiveUrl!!, gid, token) } } + mArchiveFormParamOr = result.paramOr + mArchiveList = result.archiveList + mCurrentFunds = result.funds } - var fundsGP = mCurrentFunds!!.fundsGP.toString() - // Ex GP numbers are rounded down to the nearest thousand - if (EhUtils.isExHentai) { - fundsGP += "+" - } - val title = context.getString(R.string.current_funds, fundsGP, mCurrentFunds!!.fundsC) - val selected = dialogState.showSelectItem(items, title.left()) - val res = mArchiveList!![selected].res - val isHAtH = mArchiveList!![selected].isHAtH - EhEngine.downloadArchive(gid, token, mArchiveFormParamOr!!, res, isHAtH)?.let { - val uri = Uri.parse(it) - val intent = Intent().apply { - action = Intent.ACTION_VIEW - setDataAndType(uri, "application/zip") - } - val name = "$gid-${EhUtils.getSuitableTitle(galleryDetail)}.zip" - try { - activity.startActivity(intent) - withUIContext { context.addTextToClipboard(name, true) } - } catch (_: ActivityNotFoundException) { - val r = DownloadManager.Request(uri) - r.setDestinationInExternalPublicDir( - Environment.DIRECTORY_DOWNLOADS, - AppConfig.APP_DIRNAME + "/" + FileUtils.sanitizeFilename(name), - ) - r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) - downloadManager.enqueue(r) - } - if (Settings.archiveMetadata) { - SpiderDen(galleryDetail).apply { - initDownloadDir() - writeComicInfo() + if (mArchiveList!!.isEmpty()) { + snackbarState.showSnackbar(noArchive) + } else { + val items = mArchiveList!!.map { + it.run { + if (isHAtH) { + val costStr = if (cost == "Free") archiveFree else cost + "[H@H] $name [$size] [$costStr]" + } else { + val nameStr = if (res == "org") archiveOriginal else archiveResample + val costStr = if (cost == "Free!") archiveFree else cost + "$nameStr [$size] [$costStr]" + } } } + var fundsGP = mCurrentFunds!!.fundsGP.toString() + // Ex GP numbers are rounded down to the nearest thousand + if (EhUtils.isExHentai) { + fundsGP += "+" + } + val title = context.getString(R.string.current_funds, fundsGP, mCurrentFunds!!.fundsC) + val selected = dialogState.showSelectItem(items, title.left()) + val res = mArchiveList!![selected].res + val isHAtH = mArchiveList!![selected].isHAtH + EhEngine.downloadArchive(gid, token, mArchiveFormParamOr!!, res, isHAtH)?.let { + val uri = Uri.parse(it) + val intent = Intent().apply { + action = Intent.ACTION_VIEW + setDataAndType(uri, "application/zip") + } + val name = "$gid-${EhUtils.getSuitableTitle(galleryDetail)}.zip" + try { + activity.startActivity(intent) + withUIContext { context.addTextToClipboard(name, true) } + } catch (_: ActivityNotFoundException) { + val r = DownloadManager.Request(uri) + r.setDestinationInExternalPublicDir( + Environment.DIRECTORY_DOWNLOADS, + AppConfig.APP_DIRNAME + "/" + FileUtils.sanitizeFilename(name), + ) + r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) + downloadManager.enqueue(r) + } + if (Settings.archiveMetadata) { + SpiderDen(galleryDetail).apply { + initDownloadDir() + writeComicInfo() + } + } + } + snackbarState.showSnackbar(downloadStarted) } - activity.showTip(R.string.download_archive_started) - } - }.onFailure { - when (it) { - is NoHAtHClientException -> activity.showTip(R.string.download_archive_failure_no_hath) - is EhException -> activity.showTip(ExceptionUtils.getReadableString(it)) - else -> { - logcat(it) - activity.showTip(R.string.download_archive_failure) + }.onFailure { + when (it) { + is NoHAtHClientException -> snackbarState.showSnackbar(failureNoHath) + is EhException -> snackbarState.showSnackbar(ExceptionUtils.getReadableString(it)) + else -> { + logcat(it) + snackbarState.showSnackbar(downloadFailed) + } } } } @@ -519,26 +523,29 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa } val favoritesLock = remember { Mutex() } Column(horizontalAlignment = Alignment.CenterHorizontally) { + val removeSucceed = stringResource(R.string.remove_from_favorite_success) + val addSucceed = stringResource(R.string.add_to_favorite_success) + val removeFailed = stringResource(R.string.remove_from_favorite_failure) + val addFailed = stringResource(R.string.add_to_favorite_failure) FilledTertiaryIconToggleButton( checked = favSlot != NOT_FAVORITED, onCheckedChange = { coroutineScope.launchIO { favoritesLock.withLock { var remove = false - runCatching { + runSuspendCatching { remove = !dialogState.modifyFavorites(galleryDetail.galleryInfo) + }.onSuccess { if (remove) { - activity.showTip(R.string.remove_from_favorite_success) + snackbarState.showSnackbar(removeSucceed) } else { - activity.showTip(R.string.add_to_favorite_success) + snackbarState.showSnackbar(addSucceed) } }.onFailure { - if (it !is CancellationException) { - if (remove) { - activity.showTip(R.string.remove_from_favorite_failure) - } else { - activity.showTip(R.string.add_to_favorite_failure) - } + if (remove) { + snackbarState.showSnackbar(removeFailed) + } else { + snackbarState.showSnackbar(addFailed) } } } @@ -597,6 +604,8 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa ) val torrentText = stringResource(R.string.torrent_count, galleryDetail.torrentCount) val permissionDenied = stringResource(R.string.permission_denied) + val downloadTorrentFailed = stringResource(R.string.download_torrent_failure) + val downloadTorrentStarted = stringResource(R.string.download_torrent_started) val noTorrents = stringResource(R.string.no_torrents) var mTorrentList by remember { mutableStateOf(null) } suspend fun showTorrentDialog() { @@ -619,28 +628,29 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) r.addRequestHeader("Cookie", EhCookieStore.getCookieHeader(url)) downloadManager.enqueue(r) - activity.showTip(R.string.download_torrent_started) } EhIconButton( icon = Icons.Default.SwapVerticalCircle, text = torrentText, onClick = { - if (galleryDetail.torrentCount > 0) { - coroutineScope.launchIO { + coroutineScope.launchIO { + if (galleryDetail.torrentCount > 0) { val granted = isAtLeastQ || context.requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) if (granted) { runSuspendCatching { showTorrentDialog() + }.onSuccess { + snackbarState.showSnackbar(downloadTorrentStarted) }.onFailure { logcat(it) - activity.showTip(R.string.download_torrent_failure) + snackbarState.showSnackbar(downloadTorrentFailed) } } else { - activity.showTip(permissionDenied) + snackbarState.showSnackbar(permissionDenied) } + } else { + snackbarState.showSnackbar(noTorrents) } - } else { - activity.showTip(noTorrents) } }, ) @@ -657,12 +667,14 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa var ratingText by rememberSaveable { mutableStateOf(getAllRatingText(galleryDetail.rating, galleryDetail.ratingCount)) } + val rateSucceed = stringResource(R.string.rate_successfully) + val rateFailed = stringResource(R.string.rate_failed) fun showRateDialog() { - if (galleryDetail.apiUid < 0) { - activity.showTip(R.string.sign_in_first) - return - } coroutineScope.launchIO { + if (galleryDetail.apiUid < 0) { + snackbarState.showSnackbar(signInFirst) + return@launchIO + } dialogState.awaitPermissionOrCancel(title = R.string.rate) { Column(modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) { var rating by remember { mutableFloatStateOf(galleryDetail.rating.coerceAtLeast(.5f)) } @@ -681,15 +693,15 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa galleryDetail.runSuspendCatching { EhEngine.rateGallery(apiUid, apiKey, gid, token, rating) }.onSuccess { result -> - activity.showTip(R.string.rate_successfully) galleryInfo = galleryDetail.apply { rating = result.rating ratingCount = result.ratingCount } ratingText = getAllRatingText(result.rating, result.ratingCount) + snackbarState.showSnackbar(rateSucceed) }.onFailure { logcat(it) - activity.showTip(R.string.rate_failed) + snackbarState.showSnackbar(rateFailed) } } } @@ -752,7 +764,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa onSelect(addFilter) { dialogState.awaitPermissionOrCancel { Text(text = stringResource(R.string.filter_the_tag, tag)) } Filter(FilterMode.TAG, tag).remember() - showTip(filterAdded) + snackbarState.showSnackbar(filterAdded) } if (galleryDetail.apiUid >= 0) { onSelect(upTag) { galleryDetail.voteTag(tag, 1) } @@ -835,6 +847,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa showBottomSheet = true } + val filterAdded = stringResource(R.string.filter_added) fun showFilterUploaderDialog(galleryInfo: GalleryInfo) { val uploader = galleryInfo.uploader val disowned = uploader == "(Disowned)" @@ -846,7 +859,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa Text(text = stringResource(R.string.filter_the_uploader, uploader)) } Filter(FilterMode.UPLOADER, uploader).remember() - activity.showTip(R.string.filter_added) + snackbarState.showSnackbar(filterAdded) } } val onDownloadButtonClick = rememberLambda(galleryInfo) { @@ -1034,10 +1047,10 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa onClick = { dropdown = false val detail = galleryInfo as? GalleryDetail ?: return@DropdownMenuItem - if (detail.apiUid < 0) { - activity.showTip(R.string.sign_in_first) - } else { - coroutineScope.launchIO { + coroutineScope.launchIO { + if (detail.apiUid < 0) { + snackbarState.showSnackbar(signInFirst) + } else { val text = dialogState.awaitInputText( title = addTag, hint = addTagTip, @@ -1076,7 +1089,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa val key = getImageKey(gd.gid, it) imageCache.remove(key) } - activity.showTip(imageCacheClear) + snackbarState.showSnackbar(imageCacheClear) } }, )