Remove showTip usage in GalleryDetailScreen

This commit is contained in:
GG Bond authored and Vladimir Dostoyevskiy committed 2024-02-19 12:57:10 +08:00
1 parent a2af7437c0
commit d4609593e2
1 file changed
+114 -101
@@ -164,7 +164,6 @@ import eu.kanade.tachiyomi.util.lang.withIOContext
import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.lang.withUIContext
import eu.kanade.tachiyomi.util.system.logcat import eu.kanade.tachiyomi.util.system.logcat
import kotlin.math.roundToInt import kotlin.math.roundToInt
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
@@ -290,12 +289,12 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
EhEngine.voteTag(apiUid, apiKey, gid, token, tag, vote) EhEngine.voteTag(apiUid, apiKey, gid, token, tag, vote)
}.onSuccess { result -> }.onSuccess { result ->
if (result != null) { if (result != null) {
activity.showTip(result) snackbarState.showSnackbar(result)
} else { } else {
activity.showTip(voteSuccess) snackbarState.showSnackbar(voteSuccess)
} }
}.onFailure { }.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 archiveFree = stringResource(R.string.archive_free)
val archiveOriginal = stringResource(R.string.archive_original) val archiveOriginal = stringResource(R.string.archive_original)
val archiveResample = stringResource(R.string.archive_resample) 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() { fun showArchiveDialog() {
val galleryDetail = galleryInfo as? GalleryDetail ?: return val galleryDetail = galleryInfo as? GalleryDetail ?: return
if (galleryDetail.apiUid < 0) {
activity.showTip(R.string.sign_in_first)
return
}
coroutineScope.launchIO { coroutineScope.launchIO {
runSuspendCatching { if (galleryDetail.apiUid < 0) {
if (mArchiveList == null) { snackbarState.showSnackbar(signInFirst)
val result = dialogState.bgWork { } else {
withIOContext { runSuspendCatching {
EhEngine.getArchiveList(galleryDetail.archiveUrl!!, gid, token) if (mArchiveList == null) {
} val result = dialogState.bgWork {
} withIOContext {
mArchiveFormParamOr = result.paramOr EhEngine.getArchiveList(galleryDetail.archiveUrl!!, gid, token)
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]"
} }
} }
mArchiveFormParamOr = result.paramOr
mArchiveList = result.archiveList
mCurrentFunds = result.funds
} }
var fundsGP = mCurrentFunds!!.fundsGP.toString() if (mArchiveList!!.isEmpty()) {
// Ex GP numbers are rounded down to the nearest thousand snackbarState.showSnackbar(noArchive)
if (EhUtils.isExHentai) { } else {
fundsGP += "+" val items = mArchiveList!!.map {
} it.run {
val title = context.getString(R.string.current_funds, fundsGP, mCurrentFunds!!.fundsC) if (isHAtH) {
val selected = dialogState.showSelectItem(items, title.left()) val costStr = if (cost == "Free") archiveFree else cost
val res = mArchiveList!![selected].res "[H@H] $name [$size] [$costStr]"
val isHAtH = mArchiveList!![selected].isHAtH } else {
EhEngine.downloadArchive(gid, token, mArchiveFormParamOr!!, res, isHAtH)?.let { val nameStr = if (res == "org") archiveOriginal else archiveResample
val uri = Uri.parse(it) val costStr = if (cost == "Free!") archiveFree else cost
val intent = Intent().apply { "$nameStr [$size] [$costStr]"
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()
} }
} }
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) {
}.onFailure { is NoHAtHClientException -> snackbarState.showSnackbar(failureNoHath)
when (it) { is EhException -> snackbarState.showSnackbar(ExceptionUtils.getReadableString(it))
is NoHAtHClientException -> activity.showTip(R.string.download_archive_failure_no_hath) else -> {
is EhException -> activity.showTip(ExceptionUtils.getReadableString(it)) logcat(it)
else -> { snackbarState.showSnackbar(downloadFailed)
logcat(it) }
activity.showTip(R.string.download_archive_failure)
} }
} }
} }
@@ -519,26 +523,29 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
} }
val favoritesLock = remember { Mutex() } val favoritesLock = remember { Mutex() }
Column(horizontalAlignment = Alignment.CenterHorizontally) { 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( FilledTertiaryIconToggleButton(
checked = favSlot != NOT_FAVORITED, checked = favSlot != NOT_FAVORITED,
onCheckedChange = { onCheckedChange = {
coroutineScope.launchIO { coroutineScope.launchIO {
favoritesLock.withLock { favoritesLock.withLock {
var remove = false var remove = false
runCatching { runSuspendCatching {
remove = !dialogState.modifyFavorites(galleryDetail.galleryInfo) remove = !dialogState.modifyFavorites(galleryDetail.galleryInfo)
}.onSuccess {
if (remove) { if (remove) {
activity.showTip(R.string.remove_from_favorite_success) snackbarState.showSnackbar(removeSucceed)
} else { } else {
activity.showTip(R.string.add_to_favorite_success) snackbarState.showSnackbar(addSucceed)
} }
}.onFailure { }.onFailure {
if (it !is CancellationException) { if (remove) {
if (remove) { snackbarState.showSnackbar(removeFailed)
activity.showTip(R.string.remove_from_favorite_failure) } else {
} else { snackbarState.showSnackbar(addFailed)
activity.showTip(R.string.add_to_favorite_failure)
}
} }
} }
} }
@@ -597,6 +604,8 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
) )
val torrentText = stringResource(R.string.torrent_count, galleryDetail.torrentCount) val torrentText = stringResource(R.string.torrent_count, galleryDetail.torrentCount)
val permissionDenied = stringResource(R.string.permission_denied) 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) val noTorrents = stringResource(R.string.no_torrents)
var mTorrentList by remember { mutableStateOf<TorrentResult?>(null) } var mTorrentList by remember { mutableStateOf<TorrentResult?>(null) }
suspend fun showTorrentDialog() { suspend fun showTorrentDialog() {
@@ -619,28 +628,29 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
r.addRequestHeader("Cookie", EhCookieStore.getCookieHeader(url)) r.addRequestHeader("Cookie", EhCookieStore.getCookieHeader(url))
downloadManager.enqueue(r) downloadManager.enqueue(r)
activity.showTip(R.string.download_torrent_started)
} }
EhIconButton( EhIconButton(
icon = Icons.Default.SwapVerticalCircle, icon = Icons.Default.SwapVerticalCircle,
text = torrentText, text = torrentText,
onClick = { onClick = {
if (galleryDetail.torrentCount > 0) { coroutineScope.launchIO {
coroutineScope.launchIO { if (galleryDetail.torrentCount > 0) {
val granted = isAtLeastQ || context.requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) val granted = isAtLeastQ || context.requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
if (granted) { if (granted) {
runSuspendCatching { runSuspendCatching {
showTorrentDialog() showTorrentDialog()
}.onSuccess {
snackbarState.showSnackbar(downloadTorrentStarted)
}.onFailure { }.onFailure {
logcat(it) logcat(it)
activity.showTip(R.string.download_torrent_failure) snackbarState.showSnackbar(downloadTorrentFailed)
} }
} else { } 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 { var ratingText by rememberSaveable {
mutableStateOf(getAllRatingText(galleryDetail.rating, galleryDetail.ratingCount)) mutableStateOf(getAllRatingText(galleryDetail.rating, galleryDetail.ratingCount))
} }
val rateSucceed = stringResource(R.string.rate_successfully)
val rateFailed = stringResource(R.string.rate_failed)
fun showRateDialog() { fun showRateDialog() {
if (galleryDetail.apiUid < 0) {
activity.showTip(R.string.sign_in_first)
return
}
coroutineScope.launchIO { coroutineScope.launchIO {
if (galleryDetail.apiUid < 0) {
snackbarState.showSnackbar(signInFirst)
return@launchIO
}
dialogState.awaitPermissionOrCancel(title = R.string.rate) { dialogState.awaitPermissionOrCancel(title = R.string.rate) {
Column(modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) { Column(modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
var rating by remember { mutableFloatStateOf(galleryDetail.rating.coerceAtLeast(.5f)) } var rating by remember { mutableFloatStateOf(galleryDetail.rating.coerceAtLeast(.5f)) }
@@ -681,15 +693,15 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
galleryDetail.runSuspendCatching { galleryDetail.runSuspendCatching {
EhEngine.rateGallery(apiUid, apiKey, gid, token, rating) EhEngine.rateGallery(apiUid, apiKey, gid, token, rating)
}.onSuccess { result -> }.onSuccess { result ->
activity.showTip(R.string.rate_successfully)
galleryInfo = galleryDetail.apply { galleryInfo = galleryDetail.apply {
rating = result.rating rating = result.rating
ratingCount = result.ratingCount ratingCount = result.ratingCount
} }
ratingText = getAllRatingText(result.rating, result.ratingCount) ratingText = getAllRatingText(result.rating, result.ratingCount)
snackbarState.showSnackbar(rateSucceed)
}.onFailure { }.onFailure {
logcat(it) logcat(it)
activity.showTip(R.string.rate_failed) snackbarState.showSnackbar(rateFailed)
} }
} }
} }
@@ -752,7 +764,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
onSelect(addFilter) { onSelect(addFilter) {
dialogState.awaitPermissionOrCancel { Text(text = stringResource(R.string.filter_the_tag, tag)) } dialogState.awaitPermissionOrCancel { Text(text = stringResource(R.string.filter_the_tag, tag)) }
Filter(FilterMode.TAG, tag).remember() Filter(FilterMode.TAG, tag).remember()
showTip(filterAdded) snackbarState.showSnackbar(filterAdded)
} }
if (galleryDetail.apiUid >= 0) { if (galleryDetail.apiUid >= 0) {
onSelect(upTag) { galleryDetail.voteTag(tag, 1) } onSelect(upTag) { galleryDetail.voteTag(tag, 1) }
@@ -835,6 +847,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
showBottomSheet = true showBottomSheet = true
} }
val filterAdded = stringResource(R.string.filter_added)
fun showFilterUploaderDialog(galleryInfo: GalleryInfo) { fun showFilterUploaderDialog(galleryInfo: GalleryInfo) {
val uploader = galleryInfo.uploader val uploader = galleryInfo.uploader
val disowned = uploader == "(Disowned)" val disowned = uploader == "(Disowned)"
@@ -846,7 +859,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
Text(text = stringResource(R.string.filter_the_uploader, uploader)) Text(text = stringResource(R.string.filter_the_uploader, uploader))
} }
Filter(FilterMode.UPLOADER, uploader).remember() Filter(FilterMode.UPLOADER, uploader).remember()
activity.showTip(R.string.filter_added) snackbarState.showSnackbar(filterAdded)
} }
} }
val onDownloadButtonClick = rememberLambda(galleryInfo) { val onDownloadButtonClick = rememberLambda(galleryInfo) {
@@ -1034,10 +1047,10 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
onClick = { onClick = {
dropdown = false dropdown = false
val detail = galleryInfo as? GalleryDetail ?: return@DropdownMenuItem val detail = galleryInfo as? GalleryDetail ?: return@DropdownMenuItem
if (detail.apiUid < 0) { coroutineScope.launchIO {
activity.showTip(R.string.sign_in_first) if (detail.apiUid < 0) {
} else { snackbarState.showSnackbar(signInFirst)
coroutineScope.launchIO { } else {
val text = dialogState.awaitInputText( val text = dialogState.awaitInputText(
title = addTag, title = addTag,
hint = addTagTip, hint = addTagTip,
@@ -1076,7 +1089,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
val key = getImageKey(gd.gid, it) val key = getImageKey(gd.gid, it)
imageCache.remove(key) imageCache.remove(key)
} }
activity.showTip(imageCacheClear) snackbarState.showSnackbar(imageCacheClear)
} }
}, },
) )