From 92c99b22332792aa8604b30fbcc3b2a3904c5e6a Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 4 Dec 2024 11:41:47 +0100 Subject: [PATCH] decouple business logic from the ui Signed-off-by: alperozturk --- .../client/assistant/AsssistantScreen.kt | 46 ++---------- .../assistant/extensions/TaskExtensions.kt | 6 +- .../assistant/model/ScreenOverlayState.kt | 73 ++++++++++++++++++- 3 files changed, 83 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/nextcloud/client/assistant/AsssistantScreen.kt b/app/src/main/java/com/nextcloud/client/assistant/AsssistantScreen.kt index 8b31d54d21..2ff5ab3db7 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/AsssistantScreen.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/AsssistantScreen.kt @@ -48,11 +48,9 @@ import com.nextcloud.client.assistant.taskTypes.TaskTypesRow import com.nextcloud.ui.composeActivity.ComposeActivity import com.nextcloud.ui.composeComponents.alertDialog.SimpleAlertDialog import com.nextcloud.ui.composeComponents.bottomSheet.MoreActionsBottomSheet -import com.nextcloud.utils.extensions.showShareIntent import com.owncloud.android.R import com.owncloud.android.lib.resources.assistant.model.Task import com.owncloud.android.lib.resources.assistant.model.TaskTypeData -import com.owncloud.android.utils.ClipboardUtil import com.owncloud.android.utils.DisplayUtils import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -204,47 +202,15 @@ private fun ShowOverlayState(state: ScreenOverlayState?, activity: Activity, vie } is ScreenOverlayState.TaskActions -> { - val bottomSheetAction = listOf( - Triple( - R.drawable.ic_share, - R.string.common_share - ) { - activity.showShareIntent(state.task.output?.output) - }, - Triple( - R.drawable.ic_content_copy, - R.string.common_copy - ) { - ClipboardUtil.copyToClipboard(activity, state.task.output?.output) - }, - Triple( - R.drawable.ic_edit, - R.string.action_edit - ) { - val taskType = TaskTypeData( - state.task.type, - activity.getString(R.string.assistant_screen_add_task_alert_dialog_title), - null, - null, - null - ) - val newState = - ScreenOverlayState.AddTask(taskType, state.task.input?.input ?: "") - viewModel.updateScreenState(newState) - }, - Triple( - R.drawable.ic_delete, - R.string.assistant_screen_task_more_actions_bottom_sheet_delete_action - ) { - val newState = - ScreenOverlayState.DeleteTask(state.task.id) - viewModel.updateScreenState(newState) - } - ) + val actions = state.getAction(activity, onEditCompleted = { addTask -> + viewModel.updateScreenState(addTask) + }, onDeleteCompleted = { deleteTask -> + viewModel.updateScreenState(deleteTask) + }) MoreActionsBottomSheet( title = state.task.getInputTitle(), - actions = bottomSheetAction, + actions = actions, dismiss = { viewModel.updateScreenState(null) } ) } diff --git a/app/src/main/java/com/nextcloud/client/assistant/extensions/TaskExtensions.kt b/app/src/main/java/com/nextcloud/client/assistant/extensions/TaskExtensions.kt index 8186283ed7..4654dbecaa 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/extensions/TaskExtensions.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/extensions/TaskExtensions.kt @@ -17,10 +17,14 @@ import java.util.Date import java.util.Locale import java.util.concurrent.TimeUnit +fun Task.getInput(): String? = input?.input + +fun Task.getOutput(): String? = output?.output + @Suppress("MagicNumber") fun Task.getInputTitle(): String { val maxTitleLength = 20 - val title = input?.input ?: "" + val title = getInput() ?: "" return if (title.length > maxTitleLength) { title.take(maxTitleLength) + "..." diff --git a/app/src/main/java/com/nextcloud/client/assistant/model/ScreenOverlayState.kt b/app/src/main/java/com/nextcloud/client/assistant/model/ScreenOverlayState.kt index f6459c4d9f..9dd131cc78 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/model/ScreenOverlayState.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/model/ScreenOverlayState.kt @@ -7,11 +7,82 @@ package com.nextcloud.client.assistant.model +import android.app.Activity +import com.nextcloud.client.assistant.extensions.getInput +import com.nextcloud.client.assistant.extensions.getOutput +import com.nextcloud.utils.extensions.showShareIntent +import com.owncloud.android.R import com.owncloud.android.lib.resources.assistant.model.Task import com.owncloud.android.lib.resources.assistant.model.TaskTypeData +import com.owncloud.android.utils.ClipboardUtil sealed class ScreenOverlayState { data class DeleteTask(val id: Long) : ScreenOverlayState() data class AddTask(val taskType: TaskTypeData, val input: String) : ScreenOverlayState() - data class TaskActions(val task: Task) : ScreenOverlayState() + data class TaskActions(val task: Task) : ScreenOverlayState() { + private fun getInput(): String? = task.getInput() + private fun getOutput(): String? = task.getOutput() + + private fun getCopyToClipboardAction(activity: Activity): Triple Unit> { + return Triple( + R.drawable.ic_content_copy, + R.string.common_copy + ) { + ClipboardUtil.copyToClipboard(activity, getOutput()) + } + } + + private fun getShareAction(activity: Activity): Triple Unit> { + return Triple( + R.drawable.ic_share, + R.string.common_share + ) { + activity.showShareIntent(getOutput()) + } + } + + private fun getEditAction(activity: Activity, onComplete: (AddTask) -> Unit): Triple Unit> { + return Triple( + R.drawable.ic_edit, + R.string.action_edit + ) { + val taskType = TaskTypeData( + task.type, + activity.getString(R.string.assistant_screen_add_task_alert_dialog_title), + null, + null, + null + ) + val newState = AddTask(taskType, getInput() ?: "") + onComplete(newState) + } + } + + private fun getDeleteAction(onComplete: (DeleteTask) -> Unit): Triple Unit> { + return Triple( + R.drawable.ic_delete, + R.string.assistant_screen_task_more_actions_bottom_sheet_delete_action + ) { + val newState = DeleteTask(task.id) + onComplete(newState) + } + } + + fun getAction( + activity: Activity, + onEditCompleted: (AddTask) -> Unit, + onDeleteCompleted: (DeleteTask) -> Unit + ): List Unit>> { + return listOf( + getShareAction(activity), + getCopyToClipboardAction(activity), + getEditAction(activity, onComplete = { + onEditCompleted(it) + }), + getDeleteAction(onComplete = { + onDeleteCompleted(it) + }) + ) + } + } }