Skip to content

Commit

Permalink
🎨 Add option to use monochrome icons
Browse files Browse the repository at this point in the history
This commit adds an option to use monochrome icons in the app. This is achieved by adding a new setting in the preferences screen that allows the user to toggle the use of monochrome icons. The setting is enabled by default.

When the setting is enabled, the icons in the food cards are displayed in a monochrome color scheme.
  • Loading branch information
lorenzovngl committed Dec 7, 2024
1 parent 957218c commit ca0f10a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class PreferencesRepository {
const val keyThemeMode = "theme_mode"
const val keyTopBarFont = "top_bar_font"
const val keyDynamicColors = "dynamic_colors"
const val keyMonochromeIcons = "monochrome_icons"
private val availLocaleDateFormats = arrayOf(DateFormat.SHORT, DateFormat.MEDIUM)
private val availOtherDateFormats =
arrayOf(
Expand Down Expand Up @@ -174,8 +175,8 @@ class PreferencesRepository {
sharedPrefs: String = sharedPrefsName,
): Boolean {
try {
return context.getSharedPreferences(sharedPrefs, Context.MODE_PRIVATE)
.getBoolean(keyDynamicColors, false)
return context.getSharedPreferences(sharedPrefs, Context.MODE_PRIVATE)
.getBoolean(keyDynamicColors, false)
} catch (e: Exception){
e.printStackTrace()
}
Expand All @@ -190,5 +191,27 @@ class PreferencesRepository {
return context.getSharedPreferences(sharedPrefs, Context.MODE_PRIVATE)
.edit().putBoolean(keyDynamicColors, dynamicColorsEnabled).apply()
}

fun getMonochromeIcons(
context: Context,
sharedPrefs: String = sharedPrefsName,
): Boolean {
try {
return context.getSharedPreferences(sharedPrefs, Context.MODE_PRIVATE)
.getBoolean(keyMonochromeIcons, true)
} catch (e: Exception){
e.printStackTrace()
}
return true
}

fun setMonochromeIcons(
context: Context,
sharedPrefs: String = sharedPrefsName,
monochromeIconsEnabled: Boolean
) {
return context.getSharedPreferences(sharedPrefs, Context.MODE_PRIVATE)
.edit().putBoolean(keyMonochromeIcons, monochromeIconsEnabled).apply()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fun FoodCard(
) {
val context = LocalContext.current
val dateFormat = PreferencesRepository.getUserDateFormat(context)
val monochromeIcons = PreferencesRepository.getMonochromeIcons(context)
val sdf = SimpleDateFormat(dateFormat, context.resources.configuration.locales[0])
val today = Calendar.getInstance()
val twoDaysAgo = Calendar.getInstance().apply {
Expand Down Expand Up @@ -144,10 +145,12 @@ fun FoodCard(
.size(36.dp)
.alpha(0.8f)
) {
drawImage(
image = imageBitmap,
colorFilter = ColorFilter.tint(color, BlendMode.Color)
)
if (monochromeIcons) {
drawImage(
image = imageBitmap,
colorFilter = ColorFilter.tint(color, BlendMode.Color)
)
}
drawImage(
image = imageBitmap,
blendMode = BlendMode.DstAtop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ fun SettingsScreen(
?: PreferencesRepository.Companion.ThemeMode.SYSTEM.ordinal
val dynamicColorsState = prefsViewModel?.getDynamicColors(context)?.collectAsState()?.value
?: false
val monochromeIconsState = prefsViewModel?.getMonochromeIcons(context)?.collectAsState()?.value
?: true
val topBarFontState = prefsViewModel?.getTopBarFont(context)?.collectAsState()?.value
?: PreferencesRepository.Companion.TopBarFont.NORMAL.ordinal

Expand Down Expand Up @@ -284,6 +286,21 @@ fun SettingsScreen(
)
}
}
SettingsItem(
label = stringResource(R.string.monochrome_icons)
) {
Spacer(
Modifier
.weight(1f)
.fillMaxHeight()
)
Switch(
checked = monochromeIconsState,
onCheckedChange = {
prefsViewModel?.setMonochromeIcons(context, it)
}
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class PreferencesViewModel @Inject constructor(): ViewModel() {
private var themeMode = _themeMode.asStateFlow()
private var _dynamicColors = MutableStateFlow(false)
private var dynamicColors = _dynamicColors.asStateFlow()
private var _monochromeIcons = MutableStateFlow(true)
private var monochromeIcons = _monochromeIcons.asStateFlow()

private var _topBarFont = MutableStateFlow(0)
private var topbarFont = _topBarFont.asStateFlow()
Expand Down Expand Up @@ -124,4 +126,20 @@ class PreferencesViewModel @Inject constructor(): ViewModel() {
_dynamicColors.value = colors
}

fun getMonochromeIcons(context: Context): StateFlow<Boolean> {
viewModelScope.launch {
_monochromeIcons.value = PreferencesRepository.getMonochromeIcons(context)
}
return monochromeIcons
}

fun setMonochromeIcons(context: Context, icons: Boolean) {
viewModelScope.launch {
PreferencesRepository.setMonochromeIcons(
context = context,
monochromeIconsEnabled = icons
)
}
_monochromeIcons.value = icons
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<string name="retry">Retry</string>
<string name="scanning_error">Scanning error</string>
<string name="opened">Opened</string>
<string name="monochrome_icons">Monochrome icons</string>

<string-array name="example_foods">
<item>Eggs</item>
Expand Down

0 comments on commit ca0f10a

Please sign in to comment.