From 2829d081cc97cfa6c0af66aa82405c3eaa798a62 Mon Sep 17 00:00:00 2001 From: tarsin Date: Tue, 27 Jun 2023 12:20:05 +0800 Subject: [PATCH] Cleanup with bundleOf ext --- .../ehviewer/ui/scene/GalleryListScene.kt | 25 +++++------ .../hippo/ehviewer/ui/scene/HistoryScene.kt | 12 +++-- .../com/hippo/ehviewer/ui/scene/MainNav.kt | 45 +++++++++++-------- .../hippo/ehviewer/ui/scene/ProgressScene.kt | 16 ++++--- .../com/hippo/ehviewer/util/ClipboardUtil.kt | 6 +-- 5 files changed, 57 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.kt index fe98d4ab1..fb4e7341e 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.kt @@ -35,6 +35,7 @@ import androidx.activity.OnBackPressedCallback import androidx.annotation.IntDef import androidx.compose.ui.platform.ComposeView import androidx.core.content.ContextCompat +import androidx.core.os.bundleOf import androidx.core.view.GravityCompat import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsAnimationCompat @@ -1089,14 +1090,12 @@ class GalleryListScene : SearchBarScene(), OnDragHandlerListener, SearchLayout.H return R.id.progressScene } - override fun getArgs(): Bundle { - val args = Bundle() - args.putString(ProgressScene.KEY_ACTION, ProgressScene.ACTION_GALLERY_TOKEN) - args.putLong(ProgressScene.KEY_GID, mGid) - args.putString(ProgressScene.KEY_PTOKEN, mPToken) - args.putInt(ProgressScene.KEY_PAGE, mPage) - return args - } + override fun getArgs() = bundleOf( + ProgressScene.KEY_ACTION to ProgressScene.ACTION_GALLERY_TOKEN, + ProgressScene.KEY_GID to mGid, + ProgressScene.KEY_PTOKEN to mPToken, + ProgressScene.KEY_PAGE to mPage, + ) } private inner class GalleryListAdapter( @@ -1266,12 +1265,10 @@ class GalleryListScene : SearchBarScene(), OnDragHandlerListener, SearchLayout.H private const val STATE_SEARCH = 2 private const val STATE_SEARCH_SHOW_LIST = 3 private const val ANIMATE_TIME = 300L - fun ListUrlBuilder.toStartArgs(): Bundle { - val args = Bundle() - args.putString(KEY_ACTION, ACTION_LIST_URL_BUILDER) - args.putParcelable(KEY_LIST_URL_BUILDER, this) - return args - } + fun ListUrlBuilder.toStartArgs() = bundleOf( + KEY_ACTION to ACTION_LIST_URL_BUILDER, + KEY_LIST_URL_BUILDER to this, + ) } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.kt index e32c4e954..6a4be80a4 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.kt @@ -52,6 +52,7 @@ import androidx.compose.ui.res.dimensionResource import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp +import androidx.core.os.bundleOf import androidx.core.view.GravityCompat import androidx.lifecycle.lifecycleScope import androidx.paging.Pager @@ -199,10 +200,13 @@ class HistoryScene : BaseScene() { } private fun onItemClick(gi: GalleryInfo) { - val args = Bundle() - args.putString(GalleryDetailScene.KEY_ACTION, GalleryDetailScene.ACTION_GALLERY_INFO) - args.putParcelable(GalleryDetailScene.KEY_GALLERY_INFO, gi) - navAnimated(R.id.galleryDetailScene, args) + navAnimated( + R.id.galleryDetailScene, + bundleOf( + GalleryDetailScene.KEY_ACTION to GalleryDetailScene.ACTION_GALLERY_INFO, + GalleryDetailScene.KEY_GALLERY_INFO to gi, + ), + ) } private val cardHeight = (3 * listThumbSize * 3).pxToDp.dp diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/MainNav.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/MainNav.kt index d248c7104..06ec0a8ae 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/MainNav.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/MainNav.kt @@ -3,6 +3,7 @@ package com.hippo.ehviewer.ui.scene import android.annotation.SuppressLint import android.os.Bundle import androidx.annotation.MainThread +import androidx.core.os.bundleOf import androidx.fragment.app.Fragment import androidx.navigation.NavController import androidx.navigation.NavOptions @@ -27,33 +28,39 @@ fun NavController.navAnimated(id: Int, args: Bundle?, singleTop: Boolean = false @MainThread fun NavController.navWithUrl(url: String): Boolean { if (url.isEmpty()) return false - GalleryListUrlParser.parse(url)?.let { lub -> - Bundle().apply { - putString(GalleryListScene.KEY_ACTION, GalleryListScene.ACTION_LIST_URL_BUILDER) - putParcelable(GalleryListScene.KEY_LIST_URL_BUILDER, lub) - navAnimated(R.id.galleryListScene, this) - } + GalleryListUrlParser.parse(url)?.let { + navAnimated( + R.id.galleryListScene, + bundleOf( + GalleryListScene.KEY_ACTION to GalleryListScene.ACTION_LIST_URL_BUILDER, + GalleryListScene.KEY_LIST_URL_BUILDER to it, + ), + ) return true } GalleryDetailUrlParser.parse(url)?.apply { - Bundle().apply { - putString(GalleryDetailScene.KEY_ACTION, GalleryDetailScene.ACTION_GID_TOKEN) - putLong(GalleryDetailScene.KEY_GID, gid) - putString(GalleryDetailScene.KEY_TOKEN, token) - navAnimated(R.id.galleryDetailScene, this) - } + navAnimated( + R.id.galleryDetailScene, + bundleOf( + GalleryDetailScene.KEY_ACTION to GalleryDetailScene.ACTION_GID_TOKEN, + GalleryDetailScene.KEY_GID to gid, + GalleryDetailScene.KEY_TOKEN to token, + ), + ) return true } GalleryPageUrlParser.parse(url)?.apply { - Bundle().apply { - putString(ProgressScene.KEY_ACTION, ProgressScene.ACTION_GALLERY_TOKEN) - putLong(ProgressScene.KEY_GID, gid) - putString(ProgressScene.KEY_PTOKEN, pToken) - putInt(ProgressScene.KEY_PAGE, page) - navAnimated(R.id.progressScene, this) - } + navAnimated( + R.id.progressScene, + bundleOf( + ProgressScene.KEY_ACTION to ProgressScene.ACTION_GALLERY_TOKEN, + ProgressScene.KEY_GID to gid, + ProgressScene.KEY_PTOKEN to pToken, + ProgressScene.KEY_PAGE to page, + ), + ) return true } return false diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.kt index cbc5cc203..7af054dc3 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.kt @@ -24,6 +24,7 @@ import androidx.compose.ui.platform.ComposeView import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp +import androidx.core.os.bundleOf import androidx.navigation.fragment.findNavController import com.hippo.ehviewer.R import com.hippo.ehviewer.client.EhEngine @@ -52,14 +53,17 @@ class ProgressScene : BaseScene() { runSuspendCatching { EhEngine.getGalleryToken(gid, token, page) }.onSuccess { - val arg = Bundle() - arg.putString(GalleryDetailScene.KEY_ACTION, GalleryDetailScene.ACTION_GID_TOKEN) - arg.putLong(GalleryDetailScene.KEY_GID, gid) - arg.putString(GalleryDetailScene.KEY_TOKEN, it) - arg.putInt(GalleryDetailScene.KEY_PAGE, page) withUIContext { findNavController().popBackStack() - navAnimated(R.id.galleryDetailScene, arg) + navAnimated( + R.id.galleryDetailScene, + bundleOf( + GalleryDetailScene.KEY_ACTION to GalleryDetailScene.ACTION_GID_TOKEN, + GalleryDetailScene.KEY_GID to gid, + GalleryDetailScene.KEY_TOKEN to it, + GalleryDetailScene.KEY_PAGE to page, + ), + ) } }.onFailure { error = ExceptionUtils.getReadableString(it) diff --git a/app/src/main/java/com/hippo/ehviewer/util/ClipboardUtil.kt b/app/src/main/java/com/hippo/ehviewer/util/ClipboardUtil.kt index 0f665d766..1c4300ce0 100644 --- a/app/src/main/java/com/hippo/ehviewer/util/ClipboardUtil.kt +++ b/app/src/main/java/com/hippo/ehviewer/util/ClipboardUtil.kt @@ -22,8 +22,8 @@ import android.content.ClipDescription import android.content.ClipboardManager import android.content.Context import android.os.Build -import android.os.PersistableBundle 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 @@ -42,9 +42,7 @@ fun Context.addTextToClipboard(text: CharSequence?, isSensitive: Boolean, useToa setPrimaryClip( ClipData.newPlainText(null, text).apply { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && isSensitive) { - description.extras = PersistableBundle().apply { - putBoolean(ClipDescription.EXTRA_IS_SENSITIVE, true) - } + description.extras = persistableBundleOf(ClipDescription.EXTRA_IS_SENSITIVE to true) } }, )