Skip to content

Commit

Permalink
Added clearing recent colors and reordering favorite colors by #1629
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Feb 6, 2025
1 parent cf96134 commit 5d727b1
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ interface SimpleSettingsInteractor {
value: Boolean
)

suspend fun clearRecentColors()

suspend fun updateFavoriteColors(
colors: List<ColorModel>
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package ru.tech.imageresizershrinker.core.ui.widget.color_picker

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
Expand All @@ -25,6 +26,7 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyRow
Expand All @@ -36,30 +38,42 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.History
import androidx.compose.material.icons.rounded.BookmarkBorder
import androidx.compose.material.icons.rounded.ContentPasteGo
import androidx.compose.material.icons.rounded.DeleteSweep
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
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.draw.scale
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.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.blend
import ru.tech.imageresizershrinker.core.ui.theme.inverse
import ru.tech.imageresizershrinker.core.ui.theme.takeColorFromScheme
import ru.tech.imageresizershrinker.core.ui.utils.helper.toModel
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedIconButton
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.hapticsClickable
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.text.TitleItem
import sh.calvin.reorderable.ReorderableItem
import sh.calvin.reorderable.rememberReorderableLazyListState
import kotlin.math.roundToInt

@Composable
Expand All @@ -70,6 +84,8 @@ fun RecentAndFavoriteColorsCard(
val settingsState = LocalSettingsState.current
val recentColors = settingsState.recentColors
val favoriteColors = settingsState.favoriteColors
val interactor = LocalSimpleSettingsInteractor.current
val scope = rememberCoroutineScope()

BoxAnimatedVisibility(
visible = recentColors.isNotEmpty() || favoriteColors.isNotEmpty()
Expand All @@ -91,7 +107,25 @@ fun RecentAndFavoriteColorsCard(
TitleItem(
text = stringResource(R.string.recently_used),
icon = Icons.Outlined.History,
modifier = Modifier
modifier = Modifier,
endContent = {
EnhancedIconButton(
onClick = {
scope.launch {
interactor.clearRecentColors()
}
},
modifier = Modifier.offset(x = 8.dp)
) {
Icon(
imageVector = Icons.Rounded.DeleteSweep,
contentDescription = null,
tint = takeColorFromScheme {
primary.blend(error, 0.8f)
}
)
}
}
)
Spacer(Modifier.height(12.dp))
val recentState = rememberLazyListState()
Expand Down Expand Up @@ -178,6 +212,15 @@ fun RecentAndFavoriteColorsCard(
(favoriteState.layoutInfo.viewportSize.width / itemWidth).roundToInt()
}
}
val data = remember(favoriteColors) { mutableStateOf(favoriteColors) }
val reorderableState = rememberReorderableLazyListState(
lazyListState = favoriteState,
onMove = { from, to ->
data.value = data.value.toMutableList().apply {
add(to.index, removeAt(from.index))
}
}
)
LazyRow(
state = favoriteState,
modifier = Modifier
Expand All @@ -187,44 +230,60 @@ fun RecentAndFavoriteColorsCard(
verticalAlignment = Alignment.CenterVertically
) {
items(
items = favoriteColors,
items = data.value,
key = { it.toArgb() }
) { color ->
Box(
modifier = Modifier
.size(40.dp)
.aspectRatio(1f)
.container(
shape = CircleShape,
color = color,
resultPadding = 0.dp
)
.transparencyChecker()
.background(color, CircleShape)
.hapticsClickable {
onFavoriteColorClick(color)
}
.animateItem(),
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
),
ReorderableItem(
state = reorderableState,
key = color.toArgb()
) { isDragging ->
Box(
modifier = Modifier
.size(24.dp)
.background(
color = color.copy(alpha = 1f),
shape = CircleShape
.size(40.dp)
.aspectRatio(1f)
.scale(
animateFloatAsState(
if (!reorderableState.isAnyItemDragging || isDragging) 1f
else 0.8f
).value
)
.padding(3.dp)
)
.container(
shape = CircleShape,
color = color,
resultPadding = 0.dp
)
.transparencyChecker()
.background(color, CircleShape)
.hapticsClickable {
onFavoriteColorClick(color)
}
.longPressDraggableHandle {
scope.launch {
interactor.updateFavoriteColors(data.value.map { it.toModel() })
}
}
.animateItem(),
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ internal class AndroidSettingsManager @Inject constructor(
listOf(color) + current
}

it[FAVOURITE_COLORS] = newColors.map { it.colorInt.toString() }.toSet()
it[FAVORITE_COLORS] = newColors.map { it.colorInt.toString() }.joinToString("/")
}

override suspend fun toggleOpenEditInsteadOfPreview() = toggle(
Expand Down Expand Up @@ -636,6 +636,16 @@ internal class AndroidSettingsManager @Inject constructor(
}
}

override suspend fun clearRecentColors() = edit {
it[RECENT_COLORS] = emptySet()
}

override suspend fun updateFavoriteColors(
colors: List<ColorModel>
) = edit {
it[FAVORITE_COLORS] = colors.map { it.colorInt.toString() }.joinToString("/")
}

override suspend fun setFastSettingsSide(side: FastSettingsSide) = edit {
it[FAST_SETTINGS_SIDE] = side.ordinal
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ internal fun Preferences.toSettingsState(
?: default.addTimestampToFilename,
useFormattedFilenameTimestamp = this[USE_FORMATTED_TIMESTAMP]
?: default.useFormattedFilenameTimestamp,
favoriteColors = this[FAVOURITE_COLORS]?.mapNotNull { color ->
favoriteColors = this[FAVORITE_COLORS]?.split("/")?.mapNotNull { color ->
color.toIntOrNull()?.let { ColorModel(it) }
} ?: default.favoriteColors,
defaultResizeType = this[DEFAULT_RESIZE_TYPE]?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,4 @@ internal val CHECKSUM_TYPE_FOR_FILENAME = stringPreferencesKey("CHECKSUM_TYPE_FO
internal val CUSTOM_FONTS = stringSetPreferencesKey("CUSTOM_FONTS")
internal val ENABLE_TOOL_EXIT_CONFIRMATION = booleanPreferencesKey("ENABLE_TOOL_EXIT_CONFIRMATION")
internal val RECENT_COLORS = stringSetPreferencesKey("RECENT_COLORS")
internal val FAVOURITE_COLORS = stringSetPreferencesKey("FAVOURITE_COLORS")
internal val FAVORITE_COLORS = stringPreferencesKey("FAVORITE_COLORS_KEY")

0 comments on commit 5d727b1

Please sign in to comment.