Skip to content

Commit

Permalink
refactor record actions delegates
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Aug 10, 2024
1 parent 498245d commit b7ebb4f
Show file tree
Hide file tree
Showing 12 changed files with 466 additions and 260 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package com.example.util.simpletimetracker.feature_change_record.mapper

import com.example.util.simpletimetracker.feature_change_record.viewModel.ChangeRecordActionsAdjustDelegate
import com.example.util.simpletimetracker.feature_change_record.viewModel.ChangeRecordActionsContinueDelegate
import com.example.util.simpletimetracker.feature_change_record.viewModel.ChangeRecordActionsDelegate
import com.example.util.simpletimetracker.feature_change_record.viewModel.ChangeRecordActionsDelegate.Parent.ViewDataParams
import com.example.util.simpletimetracker.feature_change_record.viewModel.ChangeRecordActionsDuplicateDelegate
import com.example.util.simpletimetracker.feature_change_record.viewModel.ChangeRecordActionsMergeDelegate
import com.example.util.simpletimetracker.feature_change_record.viewModel.ChangeRecordActionsRepeatDelegate
import com.example.util.simpletimetracker.feature_change_record.viewModel.ChangeRecordActionsSplitDelegate
import javax.inject.Inject

class ChangeRecordActionsDelegateMapper @Inject constructor() {

fun getSplitDelegateParent(
parent: ChangeRecordActionsDelegate.Parent?,
updateViewData: () -> Unit,
): ChangeRecordActionsSplitDelegate.Parent {
return object : ChangeRecordActionsSplitDelegate.Parent {
override fun getViewDataParams(): ChangeRecordActionsSplitDelegate.Parent.ViewDataParams? {
return parent?.getViewDataParams()?.mapSplitParams()
}

override fun update() {
updateViewData()
}

override suspend fun onSplitComplete() {
parent?.onSplitComplete()
}
}
}

fun getAdjustDelegateParent(
parent: ChangeRecordActionsDelegate.Parent?,
updateViewData: () -> Unit,
): ChangeRecordActionsAdjustDelegate.Parent {
return object : ChangeRecordActionsAdjustDelegate.Parent {
override fun getViewDataParams(): ChangeRecordActionsAdjustDelegate.Parent.ViewDataParams? {
return parent?.getViewDataParams()?.mapAdjustParams()
}

override fun update() {
updateViewData()
}

override suspend fun onAdjustComplete() {
parent?.onSaveClickDelegate()
}
}
}

fun getContinueActionsDelegateParent(
parent: ChangeRecordActionsDelegate.Parent?,
updateViewData: () -> Unit,
): ChangeRecordActionsContinueDelegate.Parent {
return object : ChangeRecordActionsContinueDelegate.Parent {
override fun getViewDataParams(): ChangeRecordActionsContinueDelegate.Parent.ViewDataParams? {
return parent?.getViewDataParams()?.mapContinueParams()
}

override fun update() {
updateViewData()
}

override suspend fun onSaveClickDelegate() {
parent?.onSaveClickDelegate()
}

override fun showMessage(stringResId: Int) {
parent?.showMessage(stringResId)
}
}
}

fun getRepeatActionsDelegateParent(
parent: ChangeRecordActionsDelegate.Parent?,
updateViewData: () -> Unit,
): ChangeRecordActionsRepeatDelegate.Parent {
return object : ChangeRecordActionsRepeatDelegate.Parent {
override fun getViewDataParams(): ChangeRecordActionsRepeatDelegate.Parent.ViewDataParams? {
return parent?.getViewDataParams()?.mapRepeatParams()
}

override fun update() {
updateViewData()
}

override suspend fun onSaveClickDelegate() {
parent?.onSaveClickDelegate()
}
}
}

fun getDuplicateActionsDelegateParent(
parent: ChangeRecordActionsDelegate.Parent?,
updateViewData: () -> Unit,
): ChangeRecordActionsDuplicateDelegate.Parent {
return object : ChangeRecordActionsDuplicateDelegate.Parent {
override fun getViewDataParams(): ChangeRecordActionsDuplicateDelegate.Parent.ViewDataParams? {
return parent?.getViewDataParams()?.mapDuplicateParams()
}

override fun update() {
updateViewData()
}

override suspend fun onSaveClickDelegate() {
parent?.onSaveClickDelegate()
}
}
}

fun getMergeDelegateParent(
parent: ChangeRecordActionsDelegate.Parent?,
updateViewData: () -> Unit,
): ChangeRecordActionsMergeDelegate.Parent {
return object : ChangeRecordActionsMergeDelegate.Parent {
override fun getViewDataParams(): ChangeRecordActionsMergeDelegate.Parent.ViewDataParams? {
return parent?.getViewDataParams()?.mapMergeParams()
}

override fun update() {
updateViewData()
}
}
}

private fun ViewDataParams.mapSplitParams(): ChangeRecordActionsSplitDelegate.Parent.ViewDataParams {
return ChangeRecordActionsSplitDelegate.Parent.ViewDataParams(
newTimeSplit = splitParams.newTimeSplit,
newTypeId = baseParams.newTypeId,
newTimeStarted = baseParams.newTimeStarted,
splitPreviewTimeEnded = splitParams.splitPreviewTimeEnded,
newComment = baseParams.newComment,
newCategoryIds = baseParams.newCategoryIds,
showTimeEndedOnSplitPreview = splitParams.showTimeEndedOnSplitPreview,
isButtonEnabled = baseParams.isButtonEnabled,
)
}

private fun ViewDataParams.mapAdjustParams(): ChangeRecordActionsAdjustDelegate.Parent.ViewDataParams {
return ChangeRecordActionsAdjustDelegate.Parent.ViewDataParams(
originalRecordId = adjustParams.originalRecordId,
adjustNextRecordAvailable = adjustParams.adjustNextRecordAvailable,
newTypeId = baseParams.newTypeId,
newTimeStarted = baseParams.newTimeStarted,
newTimeEnded = baseParams.newTimeEnded,
adjustPreviewTimeEnded = adjustParams.adjustPreviewTimeEnded,
originalTypeId = adjustParams.originalTypeId,
originalTimeStarted = adjustParams.originalTimeStarted,
adjustPreviewOriginalTimeEnded = adjustParams.adjustPreviewOriginalTimeEnded,
showTimeEndedOnAdjustPreview = adjustParams.showTimeEndedOnAdjustPreview,
isTimeEndedAvailable = adjustParams.isTimeEndedAvailable,
isButtonEnabled = baseParams.isButtonEnabled,
)
}

private fun ViewDataParams.mapContinueParams(): ChangeRecordActionsContinueDelegate.Parent.ViewDataParams {
return ChangeRecordActionsContinueDelegate.Parent.ViewDataParams(
originalRecordId = continueParams.originalRecordId,
newTypeId = baseParams.newTypeId,
newTimeStarted = baseParams.newTimeStarted,
newComment = baseParams.newComment,
newCategoryIds = baseParams.newCategoryIds,
isAdditionalActionsAvailable = continueParams.isAdditionalActionsAvailable,
isButtonEnabled = baseParams.isButtonEnabled,
)
}

private fun ViewDataParams.mapRepeatParams(): ChangeRecordActionsRepeatDelegate.Parent.ViewDataParams {
return ChangeRecordActionsRepeatDelegate.Parent.ViewDataParams(
newTypeId = baseParams.newTypeId,
newComment = baseParams.newComment,
newCategoryIds = baseParams.newCategoryIds,
isAdditionalActionsAvailable = repeatParams.isAdditionalActionsAvailable,
isButtonEnabled = baseParams.isButtonEnabled,
)
}

private fun ViewDataParams.mapDuplicateParams(): ChangeRecordActionsDuplicateDelegate.Parent.ViewDataParams {
return ChangeRecordActionsDuplicateDelegate.Parent.ViewDataParams(
newTypeId = baseParams.newTypeId,
newTimeStarted = baseParams.newTimeStarted,
newTimeEnded = baseParams.newTimeEnded,
newComment = baseParams.newComment,
newCategoryIds = baseParams.newCategoryIds,
isAdditionalActionsAvailable = duplicateParams.isAdditionalActionsAvailable,
isButtonEnabled = baseParams.isButtonEnabled,
)
}

private fun ViewDataParams.mapMergeParams(): ChangeRecordActionsMergeDelegate.Parent.ViewDataParams {
return ChangeRecordActionsMergeDelegate.Parent.ViewDataParams(
mergeAvailable = mergeParams.mergeAvailable,
prevRecord = mergeParams.prevRecord,
newTimeEnded = baseParams.newTimeEnded,
isButtonEnabled = baseParams.isButtonEnabled,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ class ChangeRecordActionsAdjustDelegate @Inject constructor(
}.let(resourceRepo::getString)
result += HintViewData(hintText)
val state = loadViewData(
recordId = params.recordId,
recordId = params.originalRecordId,
adjustNextRecordAvailable = params.adjustNextRecordAvailable,
newTypeId = params.newTypeId,
newTimeStarted = params.newTimeStarted,
adjustPreviewTimeEnded = params.adjustPreviewTimeEnded,
originalTypeId = params.originalTypeId,
originalTimeStarted = params.originalTimeStarted,
originalTimeEnded = params.originalTimeEnded,
showTimeEnded = params.showTimeEnded,
originalTimeEnded = params.adjustPreviewOriginalTimeEnded,
showTimeEnded = params.showTimeEndedOnAdjustPreview,
)
val previewData = state.currentData
result += ChangeRecordChangePreviewViewData(
Expand Down Expand Up @@ -153,7 +153,7 @@ class ChangeRecordActionsAdjustDelegate @Inject constructor(
val params = parent?.getViewDataParams() ?: return

val adjacentRecords = getAdjacentRecords(
recordId = params.recordId,
recordId = params.originalRecordId,
newTimeStarted = params.newTimeStarted,
newTimeEnded = params.newTimeEnded,
adjustNextRecordAvailable = params.adjustNextRecordAvailable,
Expand Down Expand Up @@ -440,16 +440,16 @@ class ChangeRecordActionsAdjustDelegate @Inject constructor(
suspend fun onAdjustComplete()

data class ViewDataParams(
val recordId: Long,
val originalRecordId: Long,
val adjustNextRecordAvailable: Boolean,
val newTypeId: Long,
val newTimeStarted: Long,
val newTimeEnded: Long,
val adjustPreviewTimeEnded: Long,
val originalTypeId: Long,
val originalTimeStarted: Long,
val originalTimeEnded: Long,
val showTimeEnded: Boolean,
val adjustPreviewOriginalTimeEnded: Long,
val showTimeEndedOnAdjustPreview: Boolean,
val isTimeEndedAvailable: Boolean,
val isButtonEnabled: Boolean,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ChangeRecordActionsContinueDelegate @Inject constructor(
suspend fun onContinueClickDelegate() {
val params = parent?.getViewDataParams() ?: return
recordActionContinueMediator.execute(
recordId = params.recordId,
recordId = params.originalRecordId,
typeId = params.newTypeId,
timeStarted = params.newTimeStarted,
comment = params.newComment,
Expand All @@ -60,6 +60,7 @@ class ChangeRecordActionsContinueDelegate @Inject constructor(
private fun loadContinueViewData(): List<ViewHolderType> {
val params = parent?.getViewDataParams()
?: return emptyList()
if (!params.isAdditionalActionsAvailable) return emptyList()

val result = mutableListOf<ViewHolderType>()
result += HintViewData(
Expand All @@ -83,11 +84,12 @@ class ChangeRecordActionsContinueDelegate @Inject constructor(
fun showMessage(stringResId: Int)

data class ViewDataParams(
val recordId: Long,
val originalRecordId: Long,
val newTypeId: Long,
val newTimeStarted: Long,
val newComment: String,
val newCategoryIds: List<Long>,
val isAdditionalActionsAvailable: Boolean,
val isButtonEnabled: Boolean,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.example.util.simpletimetracker.feature_change_record.viewModel

import androidx.lifecycle.LiveData
import com.example.util.simpletimetracker.domain.model.Record
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType

interface ChangeRecordActionsDelegate {
val actionsViewData: LiveData<List<ViewHolderType>>

interface Parent {
fun getViewDataParams(): ViewDataParams

fun updateViewData()

fun onRecordChangeButtonClick(
onProceed: suspend () -> Unit,
checkTypeSelected: Boolean = true,
)

suspend fun onSaveClickDelegate()

suspend fun onSplitComplete()

fun showMessage(stringResId: Int)

data class ViewDataParams(
val baseParams: BaseParams,
val splitParams: SplitParams,
val duplicateParams: DuplicateParams,
val continueParams: ContinueParams,
val repeatParams: RepeatParams,
val adjustParams: AdjustParams,
val mergeParams: MergeParams,
) {

data class BaseParams(
val newTypeId: Long,
val newTimeStarted: Long,
val newTimeEnded: Long,
val newComment: String,
val newCategoryIds: List<Long>,
val isButtonEnabled: Boolean,
)

data class SplitParams(
val newTimeSplit: Long,
val splitPreviewTimeEnded: Long,
val showTimeEndedOnSplitPreview: Boolean,
)

data class DuplicateParams(
val isAdditionalActionsAvailable: Boolean,
)

data class ContinueParams(
val originalRecordId: Long,
val isAdditionalActionsAvailable: Boolean,
)

data class RepeatParams(
val isAdditionalActionsAvailable: Boolean,
)

data class AdjustParams(
val originalRecordId: Long,
val originalTypeId: Long,
val originalTimeStarted: Long,
val adjustNextRecordAvailable: Boolean,
val adjustPreviewTimeEnded: Long,
val adjustPreviewOriginalTimeEnded: Long,
val showTimeEndedOnAdjustPreview: Boolean,
val isTimeEndedAvailable: Boolean,
)

data class MergeParams(
val mergeAvailable: Boolean,
val prevRecord: Record?,
)
}
}
}

This file was deleted.

Loading

0 comments on commit b7ebb4f

Please sign in to comment.