Cleanup ActionBuilder

This commit is contained in:
GG Bond authored and Vladimir Dostoyevskiy committed 2023-11-24 22:21:03 +08:00
1 parent 0d00ae207a
commit b991e5d70a
4 files changed
+60 -46

No files matched your search

@@ -1,9 +0,0 @@
package com.hippo.ehviewer.ui.scene
fun interface ActionScope {
fun onSelect(action: String, that: suspend () -> Unit)
}
inline fun buildAction(builder: ActionScope.() -> Unit) = buildList {
builder { action, that -> add(action to that) }
}
@@ -351,30 +351,35 @@ fun GalleryCommentsScreen(galleryDetail: GalleryDetail, navigator: NavController
}
}
suspend fun Context.doCommentAction(comment: GalleryComment) {
val actions = buildAction {
onSelect(copyComment) { findActivity<MainActivity>().addTextToClipboard(comment.comment.parseAsHtml()) }
if (!comment.uploader && !comment.editable) {
onSelect(blockCommenter) { showFilterCommenter(comment) }
suspend fun Context.doCommentAction(comment: GalleryComment) = dialogState.showSelectActions {
onSelect(copyComment) {
findActivity<MainActivity>().addTextToClipboard(comment.comment.parseAsHtml())
}
if (!comment.uploader && !comment.editable) {
onSelect(blockCommenter) { showFilterCommenter(comment) }
}
if (comment.editable) {
onSelect(editComment) {
userComment = TextFieldValue(comment.comment.parseAsHtml().toAnnotatedString())
commentId = comment.id
commenting = true
}
if (comment.editable) {
onSelect(editComment) {
userComment = TextFieldValue(comment.comment.parseAsHtml().toAnnotatedString())
commentId = comment.id
commenting = true
}
}
if (comment.voteUpAble) {
onSelect(if (comment.voteUpEd) cancelVoteUp else voteUp) {
voteComment(comment, true)
}
if (comment.voteUpAble) {
onSelect(if (comment.voteUpEd) cancelVoteUp else voteUp) { voteComment(comment, true) }
}
if (comment.voteDownAble) {
onSelect(if (comment.voteDownEd) cancelVoteDown else voteDown) {
voteComment(comment, false)
}
if (comment.voteDownAble) {
onSelect(if (comment.voteDownEd) cancelVoteDown else voteDown) { voteComment(comment, false) }
}
if (!comment.voteState.isNullOrEmpty()) {
onSelect(checkVoteStatus) { showCommentVoteStatus(comment) }
}
if (!comment.voteState.isNullOrEmpty()) {
onSelect(checkVoteStatus) {
showCommentVoteStatus(comment)
}
}
dialogState.showSelectItem(*actions.toTypedArray()).invoke()
}
GalleryCommentCard(
@@ -874,25 +874,31 @@ fun GalleryDetailScreen(args: GalleryDetailScreenArgs, navigator: NavController)
} else {
tag
}
val actions = buildAction {
onSelect(copy) { activity.addTextToClipboard(tag) }
if (temp != translated) {
onSelect(copyTrans) { activity.addTextToClipboard(translated) }
}
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) {
onSelect(upTag) { galleryDetail.voteTag(tag, 1) }
onSelect(downTag) { galleryDetail.voteTag(tag, -1) }
}
}.toTypedArray()
coroutineScope.launchIO {
val action = dialogState.showSelectItem(*actions)
action.invoke()
dialogState.showSelectActions {
with(activity) {
onSelect(copy) {
addTextToClipboard(tag)
}
if (temp != translated) {
onSelect(copyTrans) {
addTextToClipboard(translated)
}
}
onSelect(showDefine) {
openBrowser(EhUrl.getTagDefinitionUrl(temp))
}
onSelect(addFilter) {
dialogState.awaitPermissionOrCancel { Text(text = stringResource(R.string.filter_the_tag, tag)) }
Filter(FilterMode.TAG, tag).remember()
showTip(filterAdded, BaseScene.LENGTH_SHORT)
}
if (galleryDetail.apiUid >= 0) {
onSelect(upTag) { galleryDetail.voteTag(tag, 1) }
onSelect(downTag) { galleryDetail.voteTag(tag, -1) }
}
}
}
}
},
)
@@ -61,6 +61,10 @@ import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
fun interface ActionScope {
fun onSelect(action: String, that: suspend () -> Unit)
}
interface DialogScope<R> {
var expectedValue: R
}
@@ -341,6 +345,14 @@ class DialogState {
}
}
suspend inline fun showSelectActions(
@StringRes title: Int? = null,
builder: ActionScope.() -> Unit,
) = showSelectItem(
*buildList { builder(ActionScope { action, that -> add(action to that) }) }.toTypedArray(),
title = title,
).invoke()
suspend fun showSelectItemWithCheckBox(
vararg items: String?,
@StringRes title: Int,