Skip to content

Commit

Permalink
Added Recently used colors in selection of them
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Sep 5, 2024
1 parent 2709962 commit f3389cc
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 13 deletions.
1 change: 1 addition & 0 deletions core/resources/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1368,4 +1368,5 @@
<string name="enable_timestamps_to_format_them">Enable Timestamps to select their format</string>
<string name="one_time_save_location">One Time Save Location</string>
<string name="one_time_save_location_sub">View and Edit one time save locations which you can use by long pressing the save button in mostly all options</string>
<string name="recently_used">Recently Used</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package ru.tech.imageresizershrinker.core.settings.domain

import ru.tech.imageresizershrinker.core.domain.model.ColorModel
import ru.tech.imageresizershrinker.core.settings.domain.model.OneTimeSaveLocation

interface SimpleSettingsInteractor {
Expand All @@ -25,4 +26,9 @@ interface SimpleSettingsInteractor {

suspend fun setOneTimeSaveLocations(value: List<OneTimeSaveLocation>)

suspend fun toggleFavoriteColor(
color: ColorModel,
forceExclude: Boolean
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ data class SettingsState(
val defaultDrawColor: ColorModel,
val defaultDrawPathMode: Int,
val addTimestampToFilename: Boolean,
val useFormattedFilenameTimestamp: Boolean
val useFormattedFilenameTimestamp: Boolean,
val favoriteColors: List<ColorModel>
) {

companion object {
Expand Down Expand Up @@ -182,7 +183,8 @@ data class SettingsState(
defaultDrawColor = ColorModel(Color.Black.toArgb()),
defaultDrawPathMode = 0,
addTimestampToFilename = true,
useFormattedFilenameTimestamp = true
useFormattedFilenameTimestamp = true,
favoriteColors = emptyList()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ data class UiSettingsState(
val defaultDrawColor: Color,
val defaultDrawPathMode: Int,
val addTimestampToFilename: Boolean,
val useFormattedFilenameTimestamp: Boolean
val useFormattedFilenameTimestamp: Boolean,
val favoriteColors: List<Color>
)

fun UiSettingsState.isFirstLaunch(
Expand Down Expand Up @@ -291,7 +292,12 @@ fun SettingsState.toUiState(
defaultDrawColor = Color(defaultDrawColor.colorInt),
defaultDrawPathMode = defaultDrawPathMode,
addTimestampToFilename = addTimestampToFilename,
useFormattedFilenameTimestamp = useFormattedFilenameTimestamp
useFormattedFilenameTimestamp = useFormattedFilenameTimestamp,
favoriteColors = remember(favoriteColors) {
derivedStateOf {
favoriteColors.map { Color(it.colorInt) }
}
}.value
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ fun AlphaColorSelection(
.container(
shape = CircleShape,
resultPadding = 0.dp,
color = Color.Transparent,
composeColorOnTopOfBackground = false,
clip = false,
isShadowClip = true
)
Expand All @@ -106,8 +104,6 @@ fun AlphaColorSelection(
.container(
shape = CircleShape,
resultPadding = 0.dp,
color = Color.Transparent,
composeColorOnTopOfBackground = false,
clip = false,
isShadowClip = true
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ fun ColorSelection(
.container(
shape = CircleShape,
resultPadding = 0.dp,
color = Color.Transparent,
composeColorOnTopOfBackground = false,
clip = false,
isShadowClip = true
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
Expand All @@ -34,8 +35,11 @@ import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.History
import androidx.compose.material.icons.rounded.ContentPasteGo
import androidx.compose.material.icons.rounded.Draw
import androidx.compose.material.icons.rounded.Palette
import androidx.compose.material3.Icon
Expand All @@ -46,23 +50,33 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.luminance
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch
import ru.tech.imageresizershrinker.core.domain.model.ColorModel
import ru.tech.imageresizershrinker.core.resources.R
import ru.tech.imageresizershrinker.core.settings.presentation.provider.LocalSettingsState
import ru.tech.imageresizershrinker.core.settings.presentation.provider.LocalSimpleSettingsInteractor
import ru.tech.imageresizershrinker.core.ui.theme.inverse
import ru.tech.imageresizershrinker.core.ui.widget.buttons.EnhancedButton
import ru.tech.imageresizershrinker.core.ui.widget.modifier.container
import ru.tech.imageresizershrinker.core.ui.widget.modifier.fadingEdges
import ru.tech.imageresizershrinker.core.ui.widget.modifier.transparencyChecker
import ru.tech.imageresizershrinker.core.ui.widget.other.BoxAnimatedVisibility
import ru.tech.imageresizershrinker.core.ui.widget.sheets.SimpleSheet
import ru.tech.imageresizershrinker.core.ui.widget.text.AutoSizeText
import ru.tech.imageresizershrinker.core.ui.widget.text.TitleItem
import kotlin.math.roundToInt

@Composable
fun ColorSelectionRow(
Expand Down Expand Up @@ -169,14 +183,98 @@ fun ColorSelectionRow(
var tempColor by remember(showColorPicker) {
mutableIntStateOf(customColor?.toArgb() ?: 0)
}
val settingsState = LocalSettingsState.current
val scope = rememberCoroutineScope()
val simpleSettingsInteractor = LocalSimpleSettingsInteractor.current
SimpleSheet(
sheetContent = {
Box {
Column(
Modifier
modifier = Modifier
.verticalScroll(rememberScrollState())
.padding(start = 36.dp, top = 36.dp, end = 36.dp, bottom = 24.dp)
.padding(24.dp)
) {
val favoriteColors = settingsState.favoriteColors
BoxAnimatedVisibility(favoriteColors.isNotEmpty()) {
Column(
modifier = Modifier
.padding(bottom = 16.dp)
.container(
shape = RoundedCornerShape(24.dp),
resultPadding = 0.dp
)
.padding(16.dp)
) {
TitleItem(
text = stringResource(R.string.recently_used),
icon = Icons.Outlined.History,
modifier = Modifier
)
Spacer(Modifier.height(16.dp))
val rowState = rememberLazyListState()
val itemWidth = with(LocalDensity.current) { 48.dp.toPx() }
val possibleCount =
(rowState.layoutInfo.viewportSize.width / itemWidth).roundToInt()
LazyRow(
state = rowState,
modifier = modifier
.fillMaxWidth()
.fadingEdges(rowState),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
items(favoriteColors) { color ->
Box(
modifier = Modifier
.size(40.dp)
.aspectRatio(1f)
.container(
shape = CircleShape,
color = color,
resultPadding = 0.dp
)
.transparencyChecker()
.background(color, CircleShape)
.clickable {
tempColor = color.toArgb()
},
contentAlignment = Alignment.Center
) {
Icon(
imageVector = Icons.Rounded.ContentPasteGo,
contentDescription = null,
tint = color.inverse(
fraction = {
if (it) 0.8f
else 0.5f
},
darkMode = color.luminance() < 0.3f
),
modifier = Modifier
.size(24.dp)
.background(
color = color.copy(alpha = 1f),
shape = CircleShape
)
.padding(3.dp)
)
}
}
if (favoriteColors.size < possibleCount) {
items(possibleCount - favoriteColors.size) {
Box(
modifier = Modifier
.size(40.dp)
.clip(CircleShape)
.alpha(0.4f)
.transparencyChecker()
)
}
}
}
}
}

if (allowAlpha) {
AlphaColorSelection(
color = tempColor,
Expand Down Expand Up @@ -209,6 +307,12 @@ fun ColorSelectionRow(
EnhancedButton(
containerColor = MaterialTheme.colorScheme.secondaryContainer,
onClick = {
scope.launch {
simpleSettingsInteractor.toggleFavoriteColor(
color = ColorModel(colorInt = tempColor),
forceExclude = false
)
}
onValueChange(Color(tempColor))
customColor = Color(tempColor)
showColorPicker = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.DYNAMIC_CO
import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.EMOJI_COUNT
import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.EXIF_WIDGET_INITIAL_STATE
import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.FAB_ALIGNMENT
import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.FAVORITE_COLORS
import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.FAVORITE_SCREENS
import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.FILENAME_PREFIX
import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.FILENAME_SUFFIX
Expand Down Expand Up @@ -268,7 +269,10 @@ internal class AndroidSettingsManager @Inject constructor(
addTimestampToFilename = prefs[ADD_TIMESTAMP_TO_FILENAME]
?: default.addTimestampToFilename,
useFormattedFilenameTimestamp = prefs[USE_FORMATTED_TIMESTAMP]
?: default.useFormattedFilenameTimestamp
?: default.useFormattedFilenameTimestamp,
favoriteColors = prefs[FAVORITE_COLORS]?.split("/")?.mapNotNull { color ->
color.toIntOrNull()?.let { ColorModel(it) }
} ?: default.favoriteColors
)
}.onEach { currentSettings = it }

Expand Down Expand Up @@ -838,6 +842,23 @@ internal class AndroidSettingsManager @Inject constructor(
}
}

override suspend fun toggleFavoriteColor(
color: ColorModel,
forceExclude: Boolean
) {
val current = currentSettings.favoriteColors
val newColors = if (color in current) {
if (forceExclude) {
current - color
} else {
listOf(color) + (current - color)
}
} else {
listOf(color) + current
}
setFavoriteColors(newColors)
}

override suspend fun toggleOpenEditInsteadOfPreview() {
dataStore.edit {
it.toggle(
Expand Down Expand Up @@ -970,6 +991,12 @@ internal class AndroidSettingsManager @Inject constructor(
}
}

private suspend fun setFavoriteColors(data: List<ColorModel>) {
dataStore.edit { prefs ->
prefs[FAVORITE_COLORS] = data.take(30).joinToString("/") { it.colorInt.toString() }
}
}

private fun MutablePreferences.toggle(
key: Preferences.Key<Boolean>,
defaultValue: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@ internal object SettingKeys {
val DEFAULT_DRAW_PATH_MODE = intPreferencesKey("DEFAULT_DRAW_PATH_MODE")
val ADD_TIMESTAMP_TO_FILENAME = booleanPreferencesKey("ADD_TIMESTAMP_TO_FILENAME")
val USE_FORMATTED_TIMESTAMP = booleanPreferencesKey("USE_FORMATTED_TIMESTAMP")
val FAVORITE_COLORS = stringPreferencesKey("FAVORITE_COLORS")
}

0 comments on commit f3389cc

Please sign in to comment.