Refine ActionBuilder dsl

This commit is contained in:
Lokesh Gidra authored and Vladimir Dostoyevskiy committed 2023-11-20 21:45:40 +08:00
1 parent 5f8ab98e9e
commit 3551ed7c7a
3 files changed
+14 -14

No files matched your search

@@ -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) }
}
@@ -355,25 +355,25 @@ fun GalleryCommentsScreen(galleryDetail: GalleryDetail, navigator: NavController
suspend fun Context.doCommentAction(comment: GalleryComment) {
val actions = buildAction {
copyComment thenDo { findActivity<MainActivity>().addTextToClipboard(comment.comment.parseAsHtml()) }
onSelect(copyComment) { findActivity<MainActivity>().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()
@@ -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 {