From 25f7e8b35cc930a94eed68e2586eafd7081dde9c Mon Sep 17 00:00:00 2001 From: tarsin Date: Fri, 6 Jan 2023 08:47:37 +0800 Subject: [PATCH] Always show toast in BottomSheet mode, Resolve #633 --- .../com/hippo/ehviewer/ui/GalleryInfoBottomSheet.kt | 4 ++-- .../main/java/com/hippo/ehviewer/ui/MainActivity.kt | 10 ++++++---- app/src/main/java/com/hippo/util/ClipboardUtil.kt | 9 +++++++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/ui/GalleryInfoBottomSheet.kt b/app/src/main/java/com/hippo/ehviewer/ui/GalleryInfoBottomSheet.kt index 6db38479b..72cca5dce 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/GalleryInfoBottomSheet.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/GalleryInfoBottomSheet.kt @@ -29,7 +29,7 @@ import com.hippo.ehviewer.UrlOpener import com.hippo.ehviewer.client.EhUrl import com.hippo.ehviewer.client.EhUtils import com.hippo.ehviewer.client.data.GalleryDetail -import com.hippo.util.tellClipboard +import com.hippo.util.tellClipboardWithToast class GalleryInfoBottomSheet(detail: GalleryDetail) : BottomSheetDialogFragment() { private var mKeys: ArrayList = arrayListOf() @@ -117,7 +117,7 @@ class GalleryInfoBottomSheet(detail: GalleryDetail) : BottomSheetDialogFragment( if (position == INDEX_PARENT) { UrlOpener.openUrl(context, mValues[position], true) } else { - requireActivity() tellClipboard mValues[position] + requireActivity() tellClipboardWithToast mValues[position] if (position == INDEX_URL) { // Save it to avoid detect the gallery Settings.putClipboardTextHashCode(mValues[position].hashCode()) diff --git a/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.kt b/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.kt index 8ef348212..d78a36805 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.kt @@ -516,15 +516,17 @@ class MainActivity : StageActivity(), NavigationView.OnNavigationItemSelectedLis } } - fun showTip(@StringRes id: Int, length: Int) { - showTip(getString(id), length) + @JvmOverloads + fun showTip(@StringRes id: Int, length: Int, useToast: Boolean = false) { + showTip(getString(id), length, useToast) } /** * If activity is running, show snack bar, otherwise show toast */ - fun showTip(message: CharSequence, length: Int) { - findViewById(R.id.snackbar)?.apply { + @JvmOverloads + fun showTip(message: CharSequence, length: Int, useToast: Boolean = false) { + findViewById(R.id.snackbar)?.takeUnless { useToast }?.apply { Snackbar.make( this, message, if (length == BaseScene.LENGTH_LONG) Snackbar.LENGTH_LONG else Snackbar.LENGTH_SHORT diff --git a/app/src/main/java/com/hippo/util/ClipboardUtil.kt b/app/src/main/java/com/hippo/util/ClipboardUtil.kt index f01fec827..f2b20f939 100644 --- a/app/src/main/java/com/hippo/util/ClipboardUtil.kt +++ b/app/src/main/java/com/hippo/util/ClipboardUtil.kt @@ -38,7 +38,12 @@ infix fun Context.tellClipboard(text: String?) { addTextToClipboard(text, false) } -fun Context.addTextToClipboard(text: String?, isSensitive: Boolean) { +infix fun Context.tellClipboardWithToast(text: String?) { + addTextToClipboard(text, false, true) +} + +@JvmOverloads +fun Context.addTextToClipboard(text: String?, isSensitive: Boolean, useToast: Boolean = false) { getClipboardManager().apply { setPrimaryClip(ClipData.newPlainText(null, text).apply { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && isSensitive) @@ -50,7 +55,7 @@ fun Context.addTextToClipboard(text: String?, isSensitive: Boolean) { // Avoid double notify user since system have done that on Tiramisu above if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) if (this is MainActivity) { - showTip(R.string.copied_to_clipboard, BaseScene.LENGTH_SHORT) + showTip(R.string.copied_to_clipboard, BaseScene.LENGTH_SHORT, useToast) } else if (this is SettingsActivity) { showTip(R.string.copied_to_clipboard, BaseScene.LENGTH_SHORT) }