From cb80b02c2c583ec69303e42bb1c4097636c7668d Mon Sep 17 00:00:00 2001 From: razeeman Date: Sat, 18 Dec 2021 20:32:50 +0300 Subject: [PATCH] add color save for color selection --- .../di/NavigationDialogMapModule.kt | 3 +- .../core/mapper/ColorMapper.kt | 24 +++++++++++- .../viewModel/ChangeRecordTypeViewModel.kt | 8 +++- .../view/ColorSelectionDialogFragment.kt | 19 +++++++++- .../viewModel/ColorSelectionViewModel.kt | 38 +++++++++++-------- .../screen/ColorSelectionDialogParams.kt | 10 ++++- 6 files changed, 81 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/example/util/simpletimetracker/di/NavigationDialogMapModule.kt b/app/src/main/java/com/example/util/simpletimetracker/di/NavigationDialogMapModule.kt index 23f3e6f5..0f8d839d 100644 --- a/app/src/main/java/com/example/util/simpletimetracker/di/NavigationDialogMapModule.kt +++ b/app/src/main/java/com/example/util/simpletimetracker/di/NavigationDialogMapModule.kt @@ -3,6 +3,7 @@ package com.example.util.simpletimetracker.di import com.example.util.simpletimetracker.R import com.example.util.simpletimetracker.feature_dialogs.archive.view.ArchiveDialogFragment import com.example.util.simpletimetracker.feature_dialogs.cardOrder.view.CardOrderDialogFragment +import com.example.util.simpletimetracker.feature_dialogs.colorSelection.view.ColorSelectionDialogFragment import com.example.util.simpletimetracker.feature_dialogs.customRangeSelection.view.CustomRangeSelectionDialogFragment import com.example.util.simpletimetracker.feature_dialogs.dateTime.DateTimeDialogFragment import com.example.util.simpletimetracker.feature_dialogs.duration.view.DurationDialogFragment @@ -122,7 +123,7 @@ class NavigationDialogMapModule { fun colorSelectionDialog(): NavigationData { return NavigationData( R.id.colorSelectionDialogFragment, - BundleCreator.empty() + bundleCreatorDelegate(ColorSelectionDialogFragment::createBundle) ) } diff --git a/core/src/main/java/com/example/util/simpletimetracker/core/mapper/ColorMapper.kt b/core/src/main/java/com/example/util/simpletimetracker/core/mapper/ColorMapper.kt index 540216e3..f2f3c71a 100644 --- a/core/src/main/java/com/example/util/simpletimetracker/core/mapper/ColorMapper.kt +++ b/core/src/main/java/com/example/util/simpletimetracker/core/mapper/ColorMapper.kt @@ -1,11 +1,13 @@ package com.example.util.simpletimetracker.core.mapper +import android.graphics.Color +import androidx.annotation.ColorInt import com.example.util.simpletimetracker.core.R import com.example.util.simpletimetracker.core.repo.ResourceRepo import javax.inject.Inject class ColorMapper @Inject constructor( - private val resourceRepo: ResourceRepo + private val resourceRepo: ResourceRepo, ) { fun mapToColorResId(colorId: Int, isDarkTheme: Boolean): Int { @@ -60,6 +62,26 @@ class ColorMapper @Inject constructor( }.let(resourceRepo::getColor) } + fun mapColorToHex(@ColorInt colorInt: Int): String { + val currentRed = Color.red(colorInt) + .let(Integer::toHexString).padStart(2, '0') + val currentGreen = Color.green(colorInt) + .let(Integer::toHexString).padStart(2, '0') + val currentBlue = Color.blue(colorInt) + .let(Integer::toHexString).padStart(2, '0') + + return "#$currentRed$currentGreen$currentBlue" + } + + @ColorInt fun mapHexToColor(colorHex: String): Int { + return runCatching { + Color.parseColor(colorHex) + }.fold( + onSuccess = { it }, + onFailure = { Color.BLACK }, + ) + } + companion object { fun getAvailableColors(isDarkTheme: Boolean = false): List { return if (isDarkTheme) availableColorsDark else availableColors diff --git a/features/feature_change_record_type/src/main/java/com/example/util/simpletimetracker/feature_change_record_type/viewModel/ChangeRecordTypeViewModel.kt b/features/feature_change_record_type/src/main/java/com/example/util/simpletimetracker/feature_change_record_type/viewModel/ChangeRecordTypeViewModel.kt index ddf5f2d5..1660b4f2 100644 --- a/features/feature_change_record_type/src/main/java/com/example/util/simpletimetracker/feature_change_record_type/viewModel/ChangeRecordTypeViewModel.kt +++ b/features/feature_change_record_type/src/main/java/com/example/util/simpletimetracker/feature_change_record_type/viewModel/ChangeRecordTypeViewModel.kt @@ -60,6 +60,7 @@ class ChangeRecordTypeViewModel @Inject constructor( private val changeRecordTypeMapper: ChangeRecordTypeMapper, private val resourceRepo: ResourceRepo, private val iconEmojiMapper: IconEmojiMapper, + private val colorMapper: ColorMapper, ) : ViewModel() { lateinit var extra: ChangeRecordTypeParams @@ -127,6 +128,7 @@ class ChangeRecordTypeViewModel @Inject constructor( private var newIconName: String = "" private var newCategories: MutableList = mutableListOf() private var newColorId: Int = (0..ColorMapper.colorsNumber).random() + private var lastSelectedCustomColor: String = "#FF0000" private var newGoalTime: Long = 0L fun onNameChange(name: String) { @@ -188,7 +190,9 @@ class ChangeRecordTypeViewModel @Inject constructor( } fun onColorPaletteClick() { - router.navigate(ColorSelectionDialogParams) + ColorSelectionDialogParams( + preselectedColor = colorMapper.mapHexToColor(lastSelectedCustomColor) + ).let(router::navigate) } fun onIconTypeClick(viewData: ButtonsRowViewData) { @@ -240,7 +244,7 @@ class ChangeRecordTypeViewModel @Inject constructor( } fun onCustomColorSelected(colorHex: String) { - // TODO set custom color + lastSelectedCustomColor = colorHex } fun onGoalTimeClick() { diff --git a/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/colorSelection/view/ColorSelectionDialogFragment.kt b/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/colorSelection/view/ColorSelectionDialogFragment.kt index 94555cec..151b5dd3 100644 --- a/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/colorSelection/view/ColorSelectionDialogFragment.kt +++ b/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/colorSelection/view/ColorSelectionDialogFragment.kt @@ -1,6 +1,7 @@ package com.example.util.simpletimetracker.feature_dialogs.colorSelection.view import android.content.Context +import android.os.Bundle import android.text.TextWatcher import android.view.LayoutInflater import android.view.ViewGroup @@ -13,11 +14,13 @@ import com.example.util.simpletimetracker.core.dialog.ColorSelectionDialogListen import com.example.util.simpletimetracker.core.extension.getAllFragments import com.example.util.simpletimetracker.core.extension.setFullScreen import com.example.util.simpletimetracker.core.extension.setSkipCollapsed +import com.example.util.simpletimetracker.core.utils.fragmentArgumentDelegate import com.example.util.simpletimetracker.feature_dialogs.colorSelection.customView.ColorSelectionView -import com.example.util.simpletimetracker.feature_dialogs.colorSelection.viewModel.ColorSelectionViewModel import com.example.util.simpletimetracker.feature_dialogs.colorSelection.model.HSVUpdate import com.example.util.simpletimetracker.feature_dialogs.colorSelection.model.RGBUpdate +import com.example.util.simpletimetracker.feature_dialogs.colorSelection.viewModel.ColorSelectionViewModel import com.example.util.simpletimetracker.feature_views.extension.setOnClick +import com.example.util.simpletimetracker.navigation.params.screen.ColorSelectionDialogParams import com.google.android.material.textfield.TextInputEditText import dagger.hilt.android.AndroidEntryPoint import javax.inject.Inject @@ -36,6 +39,9 @@ class ColorSelectionDialogFragment : BaseBottomSheetFragment() { factoryProducer = { viewModelFactory } ) + private val params: ColorSelectionDialogParams by fragmentArgumentDelegate( + key = ARGS_PARAMS, default = ColorSelectionDialogParams(), + ) private var colorSelectionDialogListener: ColorSelectionDialogListener? = null // TODO do better? @@ -102,7 +108,10 @@ class ColorSelectionDialogFragment : BaseBottomSheetFragment() { } override fun initViewModel(): Unit = with(viewModel) { + extra = params colorData.observe { + binding.sliderColorSelectionHue.value = it.colorHue + binding.viewColorSelectionView.setHue( hue = it.colorHue, saturation = it.colorSaturation, @@ -136,4 +145,12 @@ class ColorSelectionDialogFragment : BaseBottomSheetFragment() { colorSelectionDialogListener?.onColorSelected(colorHex) dismiss() } + + companion object { + private const val ARGS_PARAMS = "args_params" + + fun createBundle(data: ColorSelectionDialogParams): Bundle = Bundle().apply { + putParcelable(ARGS_PARAMS, data) + } + } } \ No newline at end of file diff --git a/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/colorSelection/viewModel/ColorSelectionViewModel.kt b/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/colorSelection/viewModel/ColorSelectionViewModel.kt index 35aee89b..bbe2c498 100644 --- a/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/colorSelection/viewModel/ColorSelectionViewModel.kt +++ b/features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/colorSelection/viewModel/ColorSelectionViewModel.kt @@ -6,14 +6,23 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.example.util.simpletimetracker.core.extension.set +import com.example.util.simpletimetracker.core.mapper.ColorMapper import com.example.util.simpletimetracker.feature_dialogs.colorSelection.model.HSVUpdate import com.example.util.simpletimetracker.feature_dialogs.colorSelection.model.RGBUpdate import com.example.util.simpletimetracker.feature_dialogs.colorSelection.viewData.ColorSelectionViewData +import com.example.util.simpletimetracker.navigation.params.screen.ColorSelectionDialogParams import javax.inject.Inject -class ColorSelectionViewModel @Inject constructor() : ViewModel() { +class ColorSelectionViewModel @Inject constructor( + private val colorMapper: ColorMapper, +) : ViewModel() { - val colorData: LiveData by lazy { MutableLiveData(loadColorData()) } + lateinit var extra: ColorSelectionDialogParams + + val colorData: LiveData by lazy { + initialize() + MutableLiveData(loadColorData()) + } val colorSelected: LiveData = MutableLiveData() private var colorHue: Float = 0f // 0..360 @@ -91,7 +100,17 @@ class ColorSelectionViewModel @Inject constructor() : ViewModel() { } fun onSaveClick() { - getCurrentColorInt().let(::mapColorToHex).let(colorSelected::set) + // TODO save colorInt instead of hex? It has more definition. + getCurrentColorInt().let(colorMapper::mapColorToHex).let(colorSelected::set) + } + + private fun initialize() { + val hsv = FloatArray(3) + Color.colorToHSV(extra.preselectedColor, hsv) + + colorHue = hsv[0] + colorSaturation = hsv[1] + colorValue = hsv[2] } private fun updateColorData() { @@ -107,7 +126,7 @@ class ColorSelectionViewModel @Inject constructor() : ViewModel() { colorHue = colorHue, colorSaturation = colorSaturation, colorValue = colorValue, - colorHex = mapColorToHex(colorInt), + colorHex = colorMapper.mapColorToHex(colorInt), colorRedString = Color.red(colorInt).toString(), colorGreenString = Color.green(colorInt).toString(), colorBlueString = Color.blue(colorInt).toString(), @@ -117,17 +136,6 @@ class ColorSelectionViewModel @Inject constructor() : ViewModel() { ) } - private fun mapColorToHex(@ColorInt color: Int): String { - val currentRed = Color.red(color) - .let(Integer::toHexString).padStart(2, '0') - val currentGreen = Color.green(color) - .let(Integer::toHexString).padStart(2, '0') - val currentBlue = Color.blue(color) - .let(Integer::toHexString).padStart(2, '0') - - return "#$currentRed$currentGreen$currentBlue" - } - @ColorInt private fun getCurrentColorInt(): Int { return floatArrayOf(colorHue, colorSaturation, colorValue) .let(Color::HSVToColor) diff --git a/navigation/src/main/java/com/example/util/simpletimetracker/navigation/params/screen/ColorSelectionDialogParams.kt b/navigation/src/main/java/com/example/util/simpletimetracker/navigation/params/screen/ColorSelectionDialogParams.kt index fe073a02..e71e4395 100644 --- a/navigation/src/main/java/com/example/util/simpletimetracker/navigation/params/screen/ColorSelectionDialogParams.kt +++ b/navigation/src/main/java/com/example/util/simpletimetracker/navigation/params/screen/ColorSelectionDialogParams.kt @@ -1,3 +1,11 @@ package com.example.util.simpletimetracker.navigation.params.screen -object ColorSelectionDialogParams : ScreenParams \ No newline at end of file +import android.graphics.Color +import android.os.Parcelable +import androidx.annotation.ColorInt +import kotlinx.parcelize.Parcelize + +@Parcelize +data class ColorSelectionDialogParams( + @ColorInt val preselectedColor: Int = Color.RED, +) : ScreenParams, Parcelable \ No newline at end of file -- 2.51.2