From 7b38171a9d07136864207a40e1b7d81e14832b1d Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Sat, 1 Jul 2023 17:43:00 +0800 Subject: [PATCH] Replace cascade if with when --- .../ehviewer/download/DownloadService.kt | 96 +++++++++++-------- .../com/hippo/ehviewer/ui/MainActivity.kt | 68 ++++++------- .../hippo/ehviewer/ui/legacy/ContentLayout.kt | 59 +++++++----- .../hippo/ehviewer/ui/scene/DownloadsScene.kt | 58 ++++++----- .../ehviewer/ui/scene/GalleryCommentsScene.kt | 26 +++-- 5 files changed, 173 insertions(+), 134 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/download/DownloadService.kt b/app/src/main/java/com/hippo/ehviewer/download/DownloadService.kt index f4c2b53c9..5be829678 100644 --- a/app/src/main/java/com/hippo/ehviewer/download/DownloadService.kt +++ b/app/src/main/java/com/hippo/ehviewer/download/DownloadService.kt @@ -93,55 +93,71 @@ class DownloadService : Service(), DownloadManager.DownloadListener { } private fun handleIntent(intent: Intent?) { - var action: String? = null - if (intent != null) { - action = intent.action - } - if (ACTION_START == action) { - val gi = intent!!.getParcelableExtraCompat(KEY_GALLERY_INFO) - val label = intent.getStringExtra(KEY_LABEL) - if (gi != null && mDownloadManager != null) { - mDownloadManager!!.startDownload(gi, label) + when (intent?.action) { + ACTION_START -> { + val gi = intent.getParcelableExtraCompat(KEY_GALLERY_INFO) + val label = intent.getStringExtra(KEY_LABEL) + if (gi != null && mDownloadManager != null) { + mDownloadManager!!.startDownload(gi, label) + } } - } else if (ACTION_START_RANGE == action) { - val gidList = intent!!.getParcelableExtraCompat(KEY_GID_LIST) - if (gidList != null && mDownloadManager != null) { - mDownloadManager!!.startRangeDownload(gidList) + + ACTION_START_RANGE -> { + val gidList = intent.getParcelableExtraCompat(KEY_GID_LIST) + if (gidList != null && mDownloadManager != null) { + mDownloadManager!!.startRangeDownload(gidList) + } } - } else if (ACTION_START_ALL == action) { - if (mDownloadManager != null) { - mDownloadManager!!.startAllDownload() + + ACTION_START_ALL -> { + if (mDownloadManager != null) { + mDownloadManager!!.startAllDownload() + } } - } else if (ACTION_STOP == action) { - val gid = intent!!.getLongExtra(KEY_GID, -1) - if (gid != -1L && mDownloadManager != null) { - mDownloadManager!!.stopDownload(gid) + + ACTION_STOP -> { + val gid = intent.getLongExtra(KEY_GID, -1) + if (gid != -1L && mDownloadManager != null) { + mDownloadManager!!.stopDownload(gid) + } } - } else if (ACTION_STOP_CURRENT == action) { - if (mDownloadManager != null) { - mDownloadManager!!.stopCurrentDownload() + + ACTION_STOP_CURRENT -> { + if (mDownloadManager != null) { + mDownloadManager!!.stopCurrentDownload() + } } - } else if (ACTION_STOP_RANGE == action) { - val gidList = intent!!.getParcelableExtraCompat(KEY_GID_LIST) - if (gidList != null && mDownloadManager != null) { - mDownloadManager!!.stopRangeDownload(gidList) + + ACTION_STOP_RANGE -> { + val gidList = intent.getParcelableExtraCompat(KEY_GID_LIST) + if (gidList != null && mDownloadManager != null) { + mDownloadManager!!.stopRangeDownload(gidList) + } } - } else if (ACTION_STOP_ALL == action) { - if (mDownloadManager != null) { - mDownloadManager!!.stopAllDownload() + + ACTION_STOP_ALL -> { + if (mDownloadManager != null) { + mDownloadManager!!.stopAllDownload() + } } - } else if (ACTION_DELETE == action) { - val gid = intent!!.getLongExtra(KEY_GID, -1) - if (gid != -1L && mDownloadManager != null) { - mDownloadManager!!.deleteDownload(gid) + + ACTION_DELETE -> { + val gid = intent.getLongExtra(KEY_GID, -1) + if (gid != -1L && mDownloadManager != null) { + mDownloadManager!!.deleteDownload(gid) + } } - } else if (ACTION_DELETE_RANGE == action) { - val gidList = intent!!.getParcelableExtraCompat(KEY_GID_LIST) - if (gidList != null && mDownloadManager != null) { - mDownloadManager!!.deleteRangeDownload(gidList) + + ACTION_DELETE_RANGE -> { + val gidList = intent.getParcelableExtraCompat(KEY_GID_LIST) + if (gidList != null && mDownloadManager != null) { + mDownloadManager!!.deleteRangeDownload(gidList) + } + } + + ACTION_CLEAR -> { + clear() } - } else if (ACTION_CLEAR == action) { - clear() } checkStopSelf() } 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 d0e7b6e82..ed888e95f 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.kt @@ -120,41 +120,45 @@ class MainActivity : EhActivity() { } private fun handleIntent(intent: Intent?): Boolean { - intent ?: return false - val action = intent.action - if (Intent.ACTION_VIEW == action) { - val uri = intent.data ?: return false - return navController.navWithUrl(uri.toString()) - } else if (Intent.ACTION_SEND == action) { - val type = intent.type - if ("text/plain" == type) { - val builder = ListUrlBuilder() - builder.keyword = intent.getStringExtra(Intent.EXTRA_TEXT) - navController.navAnimated( - R.id.galleryListScene, - builder.toStartArgs(), - ) - return true - } else if (type != null && type.startsWith("image/")) { - val uri = intent.getParcelableExtraCompat(Intent.EXTRA_STREAM) - if (null != uri) { - val temp = saveImageToTempFile(uri) - if (null != temp) { - val builder = ListUrlBuilder() - builder.mode = ListUrlBuilder.MODE_IMAGE_SEARCH - builder.imagePath = temp.path - builder.isUseSimilarityScan = true - navController.navAnimated( - R.id.galleryListScene, - builder.toStartArgs(), - ) - return true + when (intent?.action) { + Intent.ACTION_VIEW -> { + val uri = intent.data ?: return false + return navController.navWithUrl(uri.toString()) + } + + Intent.ACTION_SEND -> { + val type = intent.type + if ("text/plain" == type) { + val builder = ListUrlBuilder() + builder.keyword = intent.getStringExtra(Intent.EXTRA_TEXT) + navController.navAnimated( + R.id.galleryListScene, + builder.toStartArgs(), + ) + return true + } else if (type != null && type.startsWith("image/")) { + val uri = intent.getParcelableExtraCompat(Intent.EXTRA_STREAM) + if (null != uri) { + val temp = saveImageToTempFile(uri) + if (null != temp) { + val builder = ListUrlBuilder() + builder.mode = ListUrlBuilder.MODE_IMAGE_SEARCH + builder.imagePath = temp.path + builder.isUseSimilarityScan = true + navController.navAnimated( + R.id.galleryListScene, + builder.toStartArgs(), + ) + return true + } } } } - } else if (action == DownloadService.ACTION_START_DOWNLOADSCENE) { - val args = intent.getBundleExtra(DownloadService.ACTION_START_DOWNLOADSCENE_ARGS) - navController.navAnimated(R.id.nav_downloads, args) + + DownloadService.ACTION_START_DOWNLOADSCENE -> { + val args = intent.getBundleExtra(DownloadService.ACTION_START_DOWNLOADSCENE_ARGS) + navController.navAnimated(R.id.nav_downloads, args) + } } return false diff --git a/app/src/main/java/com/hippo/ehviewer/ui/legacy/ContentLayout.kt b/app/src/main/java/com/hippo/ehviewer/ui/legacy/ContentLayout.kt index e9ef791a9..2f2ffbfba 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/legacy/ContentLayout.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/legacy/ContentLayout.kt @@ -233,6 +233,7 @@ class ContentLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout } } } + fun init(contentLayout: ContentLayout) { binding = contentLayout.binding mNextPageScrollSize = dp2px(contentLayout.context, 48f) @@ -689,30 +690,40 @@ class ContentLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout * @throws IndexOutOfBoundsException if page < 0 or page >= mPages */ fun goTo(page: Int) { - if (page < 0 || page >= pages) { - throw IndexOutOfBoundsException("Page count is $pages, page is $page") - } else if (page in mStartPage until mEndPage) { - cancelCurrentTask() - val position = getPageStart(page) - scrollToPosition(position) - } else if (page == mStartPage - 1) { - showProgress(PROGRESS_TOP) - mCurrentTaskId = mIdGenerator.nextId() - mCurrentTaskType = TYPE_PRE_PAGE - mCurrentTaskPage = page - getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, null, true) - } else if (page == mEndPage) { - showProgress(PROGRESS_BOTTOM) - mCurrentTaskId = mIdGenerator.nextId() - mCurrentTaskType = TYPE_NEXT_PAGE - mCurrentTaskPage = page - getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, null, true) - } else { - showProgress(PROGRESS_TOP) - mCurrentTaskId = mIdGenerator.nextId() - mCurrentTaskType = TYPE_SOMEWHERE - mCurrentTaskPage = page - getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, null, true) + when (page) { + !in 0 until pages -> { + throw IndexOutOfBoundsException("Page count is $pages, page is $page") + } + + in mStartPage until mEndPage -> { + cancelCurrentTask() + val position = getPageStart(page) + scrollToPosition(position) + } + + mStartPage - 1 -> { + showProgress(PROGRESS_TOP) + mCurrentTaskId = mIdGenerator.nextId() + mCurrentTaskType = TYPE_PRE_PAGE + mCurrentTaskPage = page + getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, null, true) + } + + mEndPage -> { + showProgress(PROGRESS_BOTTOM) + mCurrentTaskId = mIdGenerator.nextId() + mCurrentTaskType = TYPE_NEXT_PAGE + mCurrentTaskPage = page + getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, null, true) + } + + else -> { + showProgress(PROGRESS_TOP) + mCurrentTaskId = mIdGenerator.nextId() + mCurrentTaskType = TYPE_SOMEWHERE + mCurrentTaskPage = page + getPageData(mCurrentTaskId, mCurrentTaskType, mCurrentTaskPage, null, true) + } } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.kt index dd733b6f1..170ae005e 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.kt @@ -193,16 +193,22 @@ class DownloadsScene : @SuppressLint("NotifyDataSetChanged") private fun updateForLabel() { var list: List? - if (mLabel == null) { - list = DownloadManager.allDownloadInfoList - } else if (mLabel == getString(R.string.default_download_label_name)) { - list = DownloadManager.defaultDownloadInfoList - } else { - list = DownloadManager.getLabelDownloadInfoList(mLabel) - if (list == null) { - mLabel = null + when (mLabel) { + null -> { list = DownloadManager.allDownloadInfoList } + + getString(R.string.default_download_label_name) -> { + list = DownloadManager.defaultDownloadInfoList + } + + else -> { + list = DownloadManager.getLabelDownloadInfoList(mLabel) + if (list == null) { + mLabel = null + list = DownloadManager.allDownloadInfoList + } + } } mList = if (mType != -1) { list.filter { it.state == mType } @@ -827,21 +833,27 @@ class DownloadsScene : if (index < 0 || index >= size) { return } - if (binding.thumb === v) { - val args = Bundle() - args.putString( - GalleryDetailScene.KEY_ACTION, - GalleryDetailScene.ACTION_GALLERY_INFO, - ) - args.putParcelable(GalleryDetailScene.KEY_GALLERY_INFO, list[index]) - navAnimated(R.id.galleryDetailScene, args) - } else if (binding.start === v) { - val intent = Intent(activity, DownloadService::class.java) - intent.action = DownloadService.ACTION_START - intent.putExtra(DownloadService.KEY_GALLERY_INFO, list[index]) - ContextCompat.startForegroundService(activity, intent) - } else if (binding.stop === v) { - DownloadManager.stopDownload(list[index].gid) + when (v) { + binding.thumb -> { + val args = Bundle() + args.putString( + GalleryDetailScene.KEY_ACTION, + GalleryDetailScene.ACTION_GALLERY_INFO, + ) + args.putParcelable(GalleryDetailScene.KEY_GALLERY_INFO, list[index]) + navAnimated(R.id.galleryDetailScene, args) + } + + binding.start -> { + val intent = Intent(activity, DownloadService::class.java) + intent.action = DownloadService.ACTION_START + intent.putExtra(DownloadService.KEY_GALLERY_INFO, list[index]) + ContextCompat.startForegroundService(activity, intent) + } + + binding.stop -> { + DownloadManager.stopDownload(list[index].gid) + } } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.kt index 0e9d7871f..9226be207 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.kt @@ -428,21 +428,17 @@ class GalleryCommentsScene : BaseToolbarScene(), View.OnClickListener, OnRefresh if (which < 0 || which >= menuId.size) { return@setItems } - val id = menuId[which] - if (id == R.id.copy) { - requireActivity().addTextToClipboard(text, false) - } else if (id == R.id.block_commenter) { - showFilterCommenterDialog(comment.user, position) - } else if (id == R.id.vote_up) { - voteComment(comment.id, 1) - } else if (id == R.id.vote_down) { - voteComment(comment.id, -1) - } else if (id == R.id.check_vote_status) { - showVoteStatusDialog(context, comment.voteState!!) - } else if (id == R.id.edit_comment) { - prepareEditComment(comment.id, text) - if (!mInAnimation && binding.editPanel.visibility != View.VISIBLE) { - showEditPanel(true) + when (menuId[which]) { + R.id.copy -> requireActivity().addTextToClipboard(text, false) + R.id.block_commenter -> showFilterCommenterDialog(comment.user, position) + R.id.vote_up -> voteComment(comment.id, 1) + R.id.vote_down -> voteComment(comment.id, -1) + R.id.check_vote_status -> showVoteStatusDialog(context, comment.voteState!!) + R.id.edit_comment -> { + prepareEditComment(comment.id, text) + if (!mInAnimation && binding.editPanel.visibility != View.VISIBLE) { + showEditPanel(true) + } } } }.show()