Cleanup snackbar usage

`SnackbarDuration.Short` is already longer than `Snackbar.LENGTH_LONG`
This commit is contained in:
FooIbar committed 2023-11-27 15:16:11 +08:00
1 parent 0114081a2b
commit c23cadec8a
9 files changed
+57 -76

No files matched your search

@@ -60,7 +60,6 @@ import com.hippo.ehviewer.download.DownloadManager
import com.hippo.ehviewer.download.DownloadService
import com.hippo.ehviewer.download.downloadDir
import com.hippo.ehviewer.download.downloadLocation
import com.hippo.ehviewer.ui.scene.BaseScene
import com.hippo.ehviewer.ui.tools.DialogState
import com.hippo.ehviewer.util.FavouriteStatusRouter
import com.hippo.ehviewer.util.findActivity
@@ -118,7 +117,7 @@ suspend fun DialogState.startDownload(
}
if (toAdd.isEmpty()) {
return with(findActivity<MainActivity>()) {
showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT)
showTip(R.string.added_to_download_list)
}
}
var justStart = forceDefault
@@ -144,7 +143,7 @@ suspend fun DialogState.startDownload(
}
// Notify
with(findActivity<MainActivity>()) {
showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT)
showTip(R.string.added_to_download_list)
}
} else {
// Let use chose label
@@ -175,7 +174,7 @@ suspend fun DialogState.startDownload(
Settings.hasDefaultDownloadLabel = false
}
with(context.findActivity<MainActivity>()) {
showTip(R.string.added_to_download_list, BaseScene.LENGTH_SHORT)
showTip(R.string.added_to_download_list)
}
}
}
@@ -301,16 +300,16 @@ suspend fun DialogState.doGalleryInfoAction(info: BaseGalleryInfo, context: Cont
2 -> if (favourite) {
runSuspendCatching {
removeFromFavorites(info)
showTip(R.string.remove_from_favorite_success, BaseScene.LENGTH_SHORT)
showTip(R.string.remove_from_favorite_success)
}.onFailure {
showTip(R.string.remove_from_favorite_failure, BaseScene.LENGTH_LONG)
showTip(R.string.remove_from_favorite_failure)
}
} else {
runSuspendCatching {
modifyFavorites(info)
showTip(R.string.add_to_favorite_success, BaseScene.LENGTH_SHORT)
showTip(R.string.add_to_favorite_success)
}.onFailure {
showTip(R.string.add_to_favorite_failure, BaseScene.LENGTH_LONG)
showTip(R.string.add_to_favorite_failure)
}
}
@@ -109,7 +109,6 @@ import com.hippo.ehviewer.ui.destinations.ToplistScreenDestination
import com.hippo.ehviewer.ui.destinations.WhatshotScreenDestination
import com.hippo.ehviewer.ui.legacy.BaseDialogBuilder
import com.hippo.ehviewer.ui.legacy.EditTextDialogBuilder
import com.hippo.ehviewer.ui.scene.BaseScene
import com.hippo.ehviewer.ui.scene.TokenArgs
import com.hippo.ehviewer.ui.scene.navWithUrl
import com.hippo.ehviewer.ui.scene.navigate
@@ -260,7 +259,7 @@ class MainActivity : EhActivity() {
}
}
}.onFailure {
showTip(getString(R.string.update_failed, ExceptionUtils.getReadableString(it)), BaseScene.LENGTH_LONG)
showTip(getString(R.string.update_failed, ExceptionUtils.getReadableString(it)))
}
}
val snackbarState = remember { SnackbarHostState() }
@@ -468,13 +467,13 @@ class MainActivity : EhActivity() {
super.onResume()
}
fun showTip(@StringRes id: Int, length: Int, useToast: Boolean = false) {
showTip(getString(id), length, useToast)
fun showTip(@StringRes id: Int, useToast: Boolean = false) {
showTip(getString(id), useToast)
}
fun showTip(message: CharSequence, length: Int, useToast: Boolean = false) {
fun showTip(message: CharSequence, useToast: Boolean = false) {
if (useToast || tipFlow.tryEmit(message.toString())) {
Toast.makeText(this, message, if (length == BaseScene.LENGTH_LONG) Toast.LENGTH_LONG else Toast.LENGTH_SHORT).show()
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
}
@@ -1,6 +0,0 @@
package com.hippo.ehviewer.ui.scene
object BaseScene {
const val LENGTH_SHORT = 0
const val LENGTH_LONG = 1
}
@@ -309,8 +309,8 @@ fun DownloadsScreen(navigator: DestinationsNavigator) {
searchFieldHint = hint,
showSearchFab = selectMode,
onApplySearch = { keyword = it.takeUnless { it.isBlank() } },
onSearchHidden = { },
onSearchExpanded = { selectMode = false },
onSearchHidden = {},
searchBarOffsetY = searchBarOffsetY,
trailingIcon = {
var expanded by remember { mutableStateOf(false) }
@@ -169,6 +169,7 @@ fun GalleryCommentsScreen(galleryDetail: GalleryDetail, navigator: DestinationsN
var refreshing by remember { mutableStateOf(false) }
val context = LocalContext.current
val density = LocalDensity.current
val activity = remember { context.findActivity<MainActivity>() }
suspend fun refreshComment(showAll: Boolean) {
val url = EhUrl.getGalleryDetailUrl(galleryDetail.gid, galleryDetail.token, 0, showAll)
@@ -200,28 +201,22 @@ fun GalleryCommentsScreen(galleryDetail: GalleryDetail, navigator: DestinationsN
logcat { bbcode }
EhEngine.commentGallery(url, bbcode, commentId)
}.onSuccess {
findActivity<MainActivity>().showTip(
if (commentId != -1L) editCommentSuccess else commentSuccess,
BaseScene.LENGTH_SHORT,
)
activity.showTip(if (commentId != -1L) editCommentSuccess else commentSuccess)
userComment = TextFieldValue()
commentId = -1L
comments = it
}.onFailure {
val text = if (commentId != -1L) editCommentFail else commentFail
findActivity<MainActivity>().showTip(
text + "\n" + ExceptionUtils.getReadableString(it),
BaseScene.LENGTH_LONG,
)
activity.showTip(text + "\n" + ExceptionUtils.getReadableString(it))
}
}
suspend fun Context.showFilterCommenter(comment: GalleryComment) {
suspend fun showFilterCommenter(comment: GalleryComment) {
val commenter = comment.user ?: return
dialogState.awaitPermissionOrCancel { Text(text = stringResource(R.string.filter_the_commenter, commenter)) }
Filter(FilterMode.COMMENTER, commenter).remember()
comments = comments.copy(comments = comments.comments.filterNot { it.user == commenter })
findActivity<MainActivity>().showTip(R.string.filter_added, BaseScene.LENGTH_SHORT)
activity.showTip(R.string.filter_added)
}
suspend fun showCommentVoteStatus(comment: GalleryComment) {
@@ -310,13 +305,16 @@ fun GalleryCommentsScreen(galleryDetail: GalleryDetail, navigator: DestinationsN
),
) {
items(comments.comments) { item ->
suspend fun Context.voteComment(comment: GalleryComment, isUp: Boolean) {
suspend fun voteComment(comment: GalleryComment, isUp: Boolean) {
galleryDetail.runSuspendCatching {
EhEngine.voteComment(apiUid, apiKey, gid, token, comment.id, if (isUp) 1 else -1)
}.onSuccess { result ->
findActivity<MainActivity>().showTip(
if (isUp) (if (0 != result.vote) R.string.vote_up_successfully else R.string.cancel_vote_up_successfully) else if (0 != result.vote) R.string.vote_down_successfully else R.string.cancel_vote_down_successfully,
BaseScene.LENGTH_SHORT,
activity.showTip(
if (isUp) {
if (0 != result.vote) R.string.vote_up_successfully else R.string.cancel_vote_up_successfully
} else {
if (0 != result.vote) R.string.vote_down_successfully else R.string.cancel_vote_down_successfully
},
)
val newComment = comment.copy(
score = result.score,
@@ -326,13 +324,13 @@ fun GalleryCommentsScreen(galleryDetail: GalleryDetail, navigator: DestinationsN
val list = comments.comments.replace(comment, newComment)
comments = comments.copy(comments = list)
}.onFailure {
findActivity<MainActivity>().showTip(R.string.vote_failed, BaseScene.LENGTH_LONG)
activity.showTip(R.string.vote_failed)
}
}
suspend fun Context.doCommentAction(comment: GalleryComment) = dialogState.showSelectActions {
suspend fun doCommentAction(comment: GalleryComment) = dialogState.showSelectActions {
onSelect(copyComment) {
findActivity<MainActivity>().addTextToClipboard(comment.comment.parseAsHtml())
activity.addTextToClipboard(comment.comment.parseAsHtml())
}
if (!comment.uploader && !comment.editable) {
onSelect(blockCommenter) { showFilterCommenter(comment) }
@@ -372,11 +370,10 @@ fun GalleryCommentsScreen(galleryDetail: GalleryDetail, navigator: DestinationsN
},
onCardClick = {
coroutineScope.launch {
context.doCommentAction(item)
doCommentAction(item)
}
},
onUrlClick = {
val activity = context.findActivity<MainActivity>()
if (!activity.jumpToReaderByPage(it, galleryDetail)) {
if (!navigator.navWithUrl(it)) {
activity.openBrowser(it)
@@ -279,12 +279,12 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
EhEngine.voteTag(apiUid, apiKey, gid, token, tag, vote)
}.onSuccess { result ->
if (result != null) {
activity.showTip(result, BaseScene.LENGTH_SHORT)
activity.showTip(result)
} else {
activity.showTip(voteSuccess, BaseScene.LENGTH_SHORT)
activity.showTip(voteSuccess)
}
}.onFailure {
activity.showTip(voteFailed, BaseScene.LENGTH_LONG)
activity.showTip(voteFailed)
}
}
@@ -298,7 +298,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
fun showArchiveDialog() {
val galleryDetail = galleryInfo as? GalleryDetail ?: return
if (galleryDetail.apiUid < 0) {
activity.showTip(R.string.sign_in_first, BaseScene.LENGTH_LONG)
activity.showTip(R.string.sign_in_first)
return
}
class ArchiveListDialogHelper : DialogInterface.OnDismissListener {
@@ -332,12 +332,12 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
it.printStackTrace()
}
}
activity.showTip(R.string.download_archive_started, BaseScene.LENGTH_SHORT)
activity.showTip(R.string.download_archive_started)
}.onFailure {
when (it) {
is NoHAtHClientException -> activity.showTip(R.string.download_archive_failure_no_hath, BaseScene.LENGTH_LONG)
is EhException -> activity.showTip(ExceptionUtils.getReadableString(it), BaseScene.LENGTH_LONG)
else -> activity.showTip(R.string.download_archive_failure, BaseScene.LENGTH_LONG)
is NoHAtHClientException -> activity.showTip(R.string.download_archive_failure_no_hath)
is EhException -> activity.showTip(ExceptionUtils.getReadableString(it))
else -> activity.showTip(R.string.download_archive_failure)
}
}
}
@@ -566,16 +566,16 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
runCatching {
remove = !dialogState.modifyFavorites(galleryDetail.galleryInfo)
if (remove) {
activity.showTip(R.string.remove_from_favorite_success, BaseScene.LENGTH_SHORT)
activity.showTip(R.string.remove_from_favorite_success)
} else {
activity.showTip(R.string.add_to_favorite_success, BaseScene.LENGTH_SHORT)
activity.showTip(R.string.add_to_favorite_success)
}
}.onFailure {
if (it !is CancellationException) {
if (remove) {
activity.showTip(R.string.remove_from_favorite_failure, BaseScene.LENGTH_LONG)
activity.showTip(R.string.remove_from_favorite_failure)
} else {
activity.showTip(R.string.add_to_favorite_failure, BaseScene.LENGTH_LONG)
activity.showTip(R.string.add_to_favorite_failure)
}
}
}
@@ -672,11 +672,11 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
r.addRequestHeader("Cookie", EhCookieStore.getCookieHeader(itemUrl.toHttpUrl()))
try {
downloadManager.enqueue(r)
activity.showTip(R.string.download_torrent_started, BaseScene.LENGTH_SHORT)
activity.showTip(R.string.download_torrent_started)
} catch (e: Throwable) {
e.printStackTrace()
ExceptionUtils.throwIfFatal(e)
activity.showTip(R.string.download_torrent_failure, BaseScene.LENGTH_SHORT)
activity.showTip(R.string.download_torrent_failure)
}
}
mDialog?.dismiss()
@@ -743,7 +743,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
helper.setDialog(dialog, binding, galleryDetail.torrentUrl)
}
} else {
activity.showTip(permissionDenied, BaseScene.LENGTH_SHORT)
activity.showTip(permissionDenied)
}
}
},
@@ -763,7 +763,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
}
fun showRateDialog() {
if (galleryDetail.apiUid < 0) {
activity.showTip(R.string.sign_in_first, BaseScene.LENGTH_LONG)
activity.showTip(R.string.sign_in_first)
return
}
val binding = DialogRateBinding.inflate(context.layoutInflater)
@@ -785,7 +785,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
galleryDetail.runSuspendCatching {
EhEngine.rateGallery(apiUid, apiKey, gid, token, r)
}.onSuccess { result ->
activity.showTip(R.string.rate_successfully, BaseScene.LENGTH_SHORT)
activity.showTip(R.string.rate_successfully)
galleryInfo = galleryDetail.apply {
rating = result.rating
ratingCount = result.ratingCount
@@ -793,7 +793,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
ratingText = getAllRatingText(result.rating, result.ratingCount)
}.onFailure {
it.printStackTrace()
activity.showTip(R.string.rate_failed, BaseScene.LENGTH_LONG)
activity.showTip(R.string.rate_failed)
}
}
}
@@ -865,7 +865,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, BaseScene.LENGTH_SHORT)
showTip(filterAdded)
}
if (galleryDetail.apiUid >= 0) {
onSelect(upTag) { galleryDetail.voteTag(tag, 1) }
@@ -971,7 +971,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, BaseScene.LENGTH_SHORT)
activity.showTip(R.string.filter_added)
}
}
val onDownloadButtonClick = rememberLambda(galleryInfo) {
@@ -1154,7 +1154,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
dropdown = false
val detail = galleryInfo as? GalleryDetail ?: return@DropdownMenuItem
if (detail.apiUid < 0) {
activity.showTip(R.string.sign_in_first, BaseScene.LENGTH_LONG)
activity.showTip(R.string.sign_in_first)
} else {
coroutineScope.launchIO {
val text = dialogState.awaitInputText(
@@ -1195,7 +1195,7 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: DestinationsNa
val key = getImageKey(gd.gid, it)
imageCache.remove(key)
}
activity.showTip(imageCacheClear, BaseScene.LENGTH_SHORT)
activity.showTip(imageCacheClear)
}
},
)
@@ -50,8 +50,6 @@ import androidx.compose.material3.LeadingIconTab
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.PrimaryTabRow
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
@@ -234,7 +232,6 @@ fun GalleryListScreen(lub: ListUrlBuilder, navigator: DestinationsNavigator) {
val density = LocalDensity.current
val dialogState = LocalDialogState.current
val coroutineScope = rememberCoroutineScope()
val snackbarHostState = remember { SnackbarHostState() }
val listState = rememberLazyGridState()
val gridState = rememberLazyStaggeredGridState()
val isTopList = remember(urlBuilder) { urlBuilder.mode == MODE_TOPLIST }
@@ -303,7 +300,6 @@ fun GalleryListScreen(lub: ListUrlBuilder, navigator: DestinationsNavigator) {
val quickSearchName = getSuitableTitleForUrlBuilder(urlBuilder, false)
var saveProgress by Settings::qSSaveProgress.observed
fun launchSnackbar(content: String) = coroutineScope.launch { snackbarHostState.showSnackbar(content) }
fun getFirstVisibleItemIndex() = if (listMode == 0) {
listState.firstVisibleItemIndex
} else {
@@ -361,7 +357,7 @@ fun GalleryListScreen(lub: ListUrlBuilder, navigator: DestinationsNavigator) {
if (data.itemCount == 0) return@IconButton
if (urlBuilder.mode == MODE_IMAGE_SEARCH) {
launchSnackbar(context.getString(R.string.image_search_not_quick_search))
activity.showTip(R.string.image_search_not_quick_search, true)
return@IconButton
}
@@ -371,7 +367,7 @@ fun GalleryListScreen(lub: ListUrlBuilder, navigator: DestinationsNavigator) {
if (urlBuilder.equalsQuickSearch(q)) {
val nextStr = q.name.substringAfterLast('@', "")
if (nextStr.toLongOrNull() == next) {
launchSnackbar(context.getString(R.string.duplicate_quick_search, q.name))
activity.showTip(context.getString(R.string.duplicate_quick_search, q.name), true)
return@IconButton
}
}
@@ -572,10 +568,10 @@ fun GalleryListScreen(lub: ListUrlBuilder, navigator: DestinationsNavigator) {
val pageFrom = advancedSearchOption.fromPage
val pageTo = advancedSearchOption.toPage
if (pageTo != -1 && pageTo < 10) {
activity.showTip(searchErr1, BaseScene.LENGTH_LONG)
activity.showTip(searchErr1)
return@SearchBarScreen
} else if (pageFrom != -1 && pageTo != -1 && pageTo - pageFrom < 20) {
activity.showTip(searchErr2, BaseScene.LENGTH_LONG)
activity.showTip(searchErr2)
return@SearchBarScreen
}
builder.pageFrom = pageFrom
@@ -584,7 +580,7 @@ fun GalleryListScreen(lub: ListUrlBuilder, navigator: DestinationsNavigator) {
} else {
builder.mode = MODE_IMAGE_SEARCH
if (imagePath.isBlank()) {
activity.showTip(selectImageFirst, BaseScene.LENGTH_LONG)
activity.showTip(selectImageFirst)
return@SearchBarScreen
}
val uri = Uri.parse(imagePath)
@@ -620,7 +616,6 @@ fun GalleryListScreen(lub: ListUrlBuilder, navigator: DestinationsNavigator) {
}
},
searchBarOffsetY = searchBarOffsetY,
snackbarHost = { SnackbarHost(snackbarHostState) },
trailingIcon = {
val sheetState = LocalSideSheetState.current
IconButton(onClick = { coroutineScope.launch { sheetState.open() } }) {
@@ -115,7 +115,6 @@ fun SearchBarScreen(
suggestionProvider: SuggestionProvider? = null,
searchBarOffsetY: Int,
trailingIcon: @Composable () -> Unit,
snackbarHost: @Composable () -> Unit = {},
content: @Composable (PaddingValues) -> Unit,
) {
var mSuggestionList by remember { mutableStateOf(emptyList<Suggestion>()) }
@@ -262,7 +261,6 @@ fun SearchBarScreen(
.height(SearchBarDefaults.InputFieldHeight + 16.dp),
)
},
snackbarHost = snackbarHost,
floatingActionButton = {
val hiddenState by animateFloatAsState(
targetValue = if (showSearchFab) 1f else 0f,
@@ -26,7 +26,6 @@ import android.view.textclassifier.TextClassifier
import androidx.core.os.persistableBundleOf
import com.hippo.ehviewer.R
import com.hippo.ehviewer.ui.MainActivity
import com.hippo.ehviewer.ui.scene.BaseScene
import splitties.systemservices.clipboardManager
fun copyTextToClipboard(text: CharSequence?, isSensitive: Boolean) {
@@ -46,7 +45,7 @@ fun Context.addTextToClipboard(text: CharSequence?, useToast: Boolean = false) {
// Avoid double notify user since system have done that on Tiramisu above
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
val activity = findActivity<MainActivity>()
activity.showTip(R.string.copied_to_clipboard, BaseScene.LENGTH_SHORT, useToast)
activity.showTip(R.string.copied_to_clipboard, useToast)
}
}