From 3551ed7c7a75d8eb80f008a2aa4facbef9323d57 Mon Sep 17 00:00:00 2001 From: Lokesh Gidra Date: Mon, 20 Nov 2023 21:15:02 +0800 Subject: [PATCH] Refine ActionBuilder dsl --- .../com/hippo/ehviewer/ui/scene/ActionBuilder.kt | 4 ++-- .../ehviewer/ui/scene/GalleryCommentsFragment.kt | 12 ++++++------ .../hippo/ehviewer/ui/scene/GalleryDetailScene.kt | 12 ++++++------ 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/ActionBuilder.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/ActionBuilder.kt index 4ed584e6a..82ca6497b 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/ActionBuilder.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/ActionBuilder.kt @@ -1,9 +1,9 @@ package com.hippo.ehviewer.ui.scene fun interface ActionScope { - infix fun String.thenDo(that: suspend () -> Unit) + fun onSelect(action: String, that: suspend () -> Unit) } inline fun buildAction(builder: ActionScope.() -> Unit) = buildList { - builder { that -> add(this to that) } + builder { action, that -> add(action to that) } } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryCommentsFragment.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryCommentsFragment.kt index e39a02a00..e3b933939 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryCommentsFragment.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryCommentsFragment.kt @@ -355,25 +355,25 @@ fun GalleryCommentsScreen(galleryDetail: GalleryDetail, navigator: NavController suspend fun Context.doCommentAction(comment: GalleryComment) { val actions = buildAction { - copyComment thenDo { findActivity().addTextToClipboard(comment.comment.parseAsHtml()) } + onSelect(copyComment) { findActivity().addTextToClipboard(comment.comment.parseAsHtml()) } if (!comment.uploader && !comment.editable) { - blockCommenter thenDo { showFilterCommenter(comment) } + onSelect(blockCommenter) { showFilterCommenter(comment) } } if (comment.editable) { - editComment thenDo { + onSelect(editComment) { userComment = TextFieldValue(comment.comment.parseAsHtml().toAnnotatedString()) commentId = comment.id commenting = true } } if (comment.voteUpAble) { - (if (comment.voteUpEd) cancelVoteUp else voteUp) thenDo { voteComment(comment, true) } + onSelect(if (comment.voteUpEd) cancelVoteUp else voteUp) { voteComment(comment, true) } } if (comment.voteDownAble) { - (if (comment.voteDownEd) cancelVoteDown else voteDown) thenDo { voteComment(comment, false) } + onSelect(if (comment.voteDownEd) cancelVoteDown else voteDown) { voteComment(comment, false) } } if (!comment.voteState.isNullOrEmpty()) { - checkVoteStatus thenDo { showCommentVoteStatus(comment) } + onSelect(checkVoteStatus) { showCommentVoteStatus(comment) } } } dialogState.showSelectItem(*actions.toTypedArray()).invoke() diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt index dcf6f2f36..128b36cb9 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/scene/GalleryDetailScene.kt @@ -875,19 +875,19 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: NavController) tag } val actions = buildAction { - copy thenDo { activity.addTextToClipboard(tag) } + onSelect(copy) { activity.addTextToClipboard(tag) } if (temp != translated) { - copyTrans thenDo { activity.addTextToClipboard(translated) } + onSelect(copyTrans) { activity.addTextToClipboard(translated) } } - showDefine thenDo { context.openBrowser(EhUrl.getTagDefinitionUrl(temp)) } - addFilter thenDo { + onSelect(showDefine) { context.openBrowser(EhUrl.getTagDefinitionUrl(temp)) } + onSelect(addFilter) { dialogState.awaitPermissionOrCancel { Text(text = stringResource(R.string.filter_the_tag, tag)) } Filter(FilterMode.TAG, tag).remember() activity.showTip(filterAdded, BaseScene.LENGTH_SHORT) } if (galleryDetail.apiUid >= 0) { - upTag thenDo { galleryDetail.voteTag(tag, 1) } - downTag thenDo { galleryDetail.voteTag(tag, -1) } + onSelect(upTag) { galleryDetail.voteTag(tag, 1) } + onSelect(downTag) { galleryDetail.voteTag(tag, -1) } } }.toTypedArray() coroutineScope.launchIO {