decouple business logic from the ui

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-12-04 11:41:47 +01:00 committed by Alper Öztürk
parent 8f698167eb
commit 92c99b2233
3 changed files with 83 additions and 42 deletions

View file

@ -48,11 +48,9 @@ import com.nextcloud.client.assistant.taskTypes.TaskTypesRow
import com.nextcloud.ui.composeActivity.ComposeActivity import com.nextcloud.ui.composeActivity.ComposeActivity
import com.nextcloud.ui.composeComponents.alertDialog.SimpleAlertDialog import com.nextcloud.ui.composeComponents.alertDialog.SimpleAlertDialog
import com.nextcloud.ui.composeComponents.bottomSheet.MoreActionsBottomSheet import com.nextcloud.ui.composeComponents.bottomSheet.MoreActionsBottomSheet
import com.nextcloud.utils.extensions.showShareIntent
import com.owncloud.android.R import com.owncloud.android.R
import com.owncloud.android.lib.resources.assistant.model.Task import com.owncloud.android.lib.resources.assistant.model.Task
import com.owncloud.android.lib.resources.assistant.model.TaskTypeData import com.owncloud.android.lib.resources.assistant.model.TaskTypeData
import com.owncloud.android.utils.ClipboardUtil
import com.owncloud.android.utils.DisplayUtils import com.owncloud.android.utils.DisplayUtils
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -204,47 +202,15 @@ private fun ShowOverlayState(state: ScreenOverlayState?, activity: Activity, vie
} }
is ScreenOverlayState.TaskActions -> { is ScreenOverlayState.TaskActions -> {
val bottomSheetAction = listOf( val actions = state.getAction(activity, onEditCompleted = { addTask ->
Triple( viewModel.updateScreenState(addTask)
R.drawable.ic_share, }, onDeleteCompleted = { deleteTask ->
R.string.common_share viewModel.updateScreenState(deleteTask)
) { })
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)
}
)
MoreActionsBottomSheet( MoreActionsBottomSheet(
title = state.task.getInputTitle(), title = state.task.getInputTitle(),
actions = bottomSheetAction, actions = actions,
dismiss = { viewModel.updateScreenState(null) } dismiss = { viewModel.updateScreenState(null) }
) )
} }

View file

@ -17,10 +17,14 @@ import java.util.Date
import java.util.Locale import java.util.Locale
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
fun Task.getInput(): String? = input?.input
fun Task.getOutput(): String? = output?.output
@Suppress("MagicNumber") @Suppress("MagicNumber")
fun Task.getInputTitle(): String { fun Task.getInputTitle(): String {
val maxTitleLength = 20 val maxTitleLength = 20
val title = input?.input ?: "" val title = getInput() ?: ""
return if (title.length > maxTitleLength) { return if (title.length > maxTitleLength) {
title.take(maxTitleLength) + "..." title.take(maxTitleLength) + "..."

View file

@ -7,11 +7,82 @@
package com.nextcloud.client.assistant.model 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.Task
import com.owncloud.android.lib.resources.assistant.model.TaskTypeData import com.owncloud.android.lib.resources.assistant.model.TaskTypeData
import com.owncloud.android.utils.ClipboardUtil
sealed class ScreenOverlayState { sealed class ScreenOverlayState {
data class DeleteTask(val id: Long) : ScreenOverlayState() data class DeleteTask(val id: Long) : ScreenOverlayState()
data class AddTask(val taskType: TaskTypeData, val input: String) : 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<Int, Int, () -> Unit> {
return Triple(
R.drawable.ic_content_copy,
R.string.common_copy
) {
ClipboardUtil.copyToClipboard(activity, getOutput())
}
}
private fun getShareAction(activity: Activity): Triple<Int, Int, () -> Unit> {
return Triple(
R.drawable.ic_share,
R.string.common_share
) {
activity.showShareIntent(getOutput())
}
}
private fun getEditAction(activity: Activity, onComplete: (AddTask) -> Unit): Triple<Int, Int, () -> 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<Int, Int, () -> 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<Triple<Int, Int, () -> Unit>> {
return listOf(
getShareAction(activity),
getCopyToClipboardAction(activity),
getEditAction(activity, onComplete = {
onEditCompleted(it)
}),
getDeleteAction(onComplete = {
onDeleteCompleted(it)
})
)
}
}
} }