-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
212 additions
and
293 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
...java/com/example/util/simpletimetracker/core/delegates/ColorSelectionViewModelDelegate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.example.util.simpletimetracker.core.delegates | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import com.example.util.simpletimetracker.core.base.ViewModelDelegate | ||
import com.example.util.simpletimetracker.core.extension.set | ||
import com.example.util.simpletimetracker.core.interactor.ColorViewDataInteractor | ||
import com.example.util.simpletimetracker.core.mapper.ColorMapper | ||
import com.example.util.simpletimetracker.domain.extension.orFalse | ||
import com.example.util.simpletimetracker.domain.model.AppColor | ||
import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType | ||
import com.example.util.simpletimetracker.feature_base_adapter.color.ColorViewData | ||
import com.example.util.simpletimetracker.navigation.Router | ||
import com.example.util.simpletimetracker.navigation.params.screen.ColorSelectionDialogParams | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
interface ColorSelectionViewModelDelegate { | ||
val colors: LiveData<List<ViewHolderType>> | ||
|
||
fun attach(parent: Parent) | ||
fun onColorClick(item: ColorViewData) | ||
fun onColorPaletteClick() | ||
fun onCustomColorSelected(colorInt: Int) | ||
suspend fun update() | ||
|
||
interface Parent { | ||
suspend fun update() | ||
suspend fun onColorSelected() = Unit | ||
suspend fun isColorSelectedCheck(): Boolean = true | ||
} | ||
} | ||
|
||
class ColorSelectionViewModelDelegateImpl @Inject constructor( | ||
private val router: Router, | ||
private val colorMapper: ColorMapper, | ||
private val colorViewDataInteractor: ColorViewDataInteractor, | ||
) : ColorSelectionViewModelDelegate, | ||
ViewModelDelegate() { | ||
|
||
override val colors: LiveData<List<ViewHolderType>> by lazy { | ||
return@lazy MutableLiveData<List<ViewHolderType>>().let { initial -> | ||
delegateScope.launch { initial.value = loadColorsViewData() } | ||
initial | ||
} | ||
} | ||
var newColor: AppColor = AppColor( | ||
colorId = (0..ColorMapper.colorsNumber).random(), | ||
colorInt = "", | ||
) | ||
|
||
private var parent: ColorSelectionViewModelDelegate.Parent? = null | ||
|
||
override fun attach(parent: ColorSelectionViewModelDelegate.Parent) { | ||
this.parent = parent | ||
} | ||
|
||
override suspend fun update() { | ||
updateColors() | ||
} | ||
|
||
override fun onColorClick(item: ColorViewData) { | ||
delegateScope.launch { | ||
if (item.colorId != newColor.colorId || newColor.colorInt.isNotEmpty()) { | ||
newColor = AppColor(colorId = item.colorId, colorInt = "") | ||
parent?.onColorSelected() | ||
parent?.update() | ||
updateColors() | ||
} | ||
} | ||
} | ||
|
||
override fun onColorPaletteClick() { | ||
ColorSelectionDialogParams( | ||
preselectedColor = colorMapper.mapToColorInt( | ||
color = newColor, | ||
isDarkTheme = false, // Pass original, not darkened color. | ||
), | ||
).let(router::navigate) | ||
} | ||
|
||
override fun onCustomColorSelected(colorInt: Int) { | ||
delegateScope.launch { | ||
if (colorInt.toString() != newColor.colorInt) { | ||
newColor = AppColor(colorId = 0, colorInt = colorInt.toString()) | ||
parent?.onColorSelected() | ||
parent?.update() | ||
updateColors() | ||
} | ||
} | ||
} | ||
|
||
private suspend fun updateColors() { | ||
val data = loadColorsViewData() | ||
colors.set(data) | ||
} | ||
|
||
private suspend fun loadColorsViewData(): List<ViewHolderType> { | ||
val currentColor = newColor.takeIf { parent?.isColorSelectedCheck().orFalse() } | ||
return colorViewDataInteractor.getColorsViewData(currentColor) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.