Replace cascade if with when

This commit is contained in:
FooIbar authored and tarsin committed 2023-07-01 20:00:24 +08:00
1 parent c9ef60db67
commit 7b38171a9d
5 files changed
+173 -134

No files matched your search

@@ -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<GalleryInfo>(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<GalleryInfo>(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<LongList>(KEY_GID_LIST)
if (gidList != null && mDownloadManager != null) {
mDownloadManager!!.startRangeDownload(gidList)
ACTION_START_RANGE -> {
val gidList = intent.getParcelableExtraCompat<LongList>(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<LongList>(KEY_GID_LIST)
if (gidList != null && mDownloadManager != null) {
mDownloadManager!!.stopRangeDownload(gidList)
ACTION_STOP_RANGE -> {
val gidList = intent.getParcelableExtraCompat<LongList>(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<LongList>(KEY_GID_LIST)
if (gidList != null && mDownloadManager != null) {
mDownloadManager!!.deleteRangeDownload(gidList)
ACTION_DELETE_RANGE -> {
val gidList = intent.getParcelableExtraCompat<LongList>(KEY_GID_LIST)
if (gidList != null && mDownloadManager != null) {
mDownloadManager!!.deleteRangeDownload(gidList)
}
}
ACTION_CLEAR -> {
clear()
}
} else if (ACTION_CLEAR == action) {
clear()
}
checkStopSelf()
}
@@ -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<Uri>(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<Uri>(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
@@ -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)
}
}
}
@@ -193,16 +193,22 @@ class DownloadsScene :
@SuppressLint("NotifyDataSetChanged")
private fun updateForLabel() {
var list: List<DownloadInfo>?
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)
}
}
}
@@ -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()