From d1f4aa4577838231534d26a78adbd6b990d207ba Mon Sep 17 00:00:00 2001 From: vfsfitvnm Date: Wed, 19 Oct 2022 15:23:33 +0200 Subject: [PATCH] Add option to use system font (#182) --- .../it/vfsfitvnm/vimusic/MainActivity.kt | 30 +++++-- .../ui/screens/settings/AppearanceSettings.kt | 22 +++++ .../vimusic/ui/styling/Appearance.kt | 28 ++----- .../vimusic/ui/styling/ColorPalette.kt | 29 ++++++- .../vimusic/ui/styling/Typography.kt | 82 +++++++++++++------ .../it/vfsfitvnm/vimusic/utils/Preferences.kt | 3 +- 6 files changed, 136 insertions(+), 58 deletions(-) diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/MainActivity.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/MainActivity.kt index 20808da..cafb18e 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/MainActivity.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/MainActivity.kt @@ -57,6 +57,10 @@ import androidx.media3.common.MediaItem import androidx.media3.common.Player import com.valentinilk.shimmer.LocalShimmerTheme import com.valentinilk.shimmer.defaultShimmerTheme +import it.vfsfitvnm.innertube.Innertube +import it.vfsfitvnm.innertube.models.bodies.BrowseBody +import it.vfsfitvnm.innertube.requests.playlistPage +import it.vfsfitvnm.innertube.requests.song import it.vfsfitvnm.vimusic.enums.ColorPaletteMode import it.vfsfitvnm.vimusic.enums.ColorPaletteName import it.vfsfitvnm.vimusic.enums.ThumbnailRoundness @@ -75,6 +79,7 @@ import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance import it.vfsfitvnm.vimusic.ui.styling.colorPaletteOf import it.vfsfitvnm.vimusic.ui.styling.dynamicColorPaletteOf import it.vfsfitvnm.vimusic.ui.styling.typographyOf +import it.vfsfitvnm.vimusic.utils.applyFontPaddingKey import it.vfsfitvnm.vimusic.utils.asMediaItem import it.vfsfitvnm.vimusic.utils.colorPaletteModeKey import it.vfsfitvnm.vimusic.utils.colorPaletteNameKey @@ -83,10 +88,7 @@ import it.vfsfitvnm.vimusic.utils.getEnum import it.vfsfitvnm.vimusic.utils.intent import it.vfsfitvnm.vimusic.utils.preferences import it.vfsfitvnm.vimusic.utils.thumbnailRoundnessKey -import it.vfsfitvnm.innertube.Innertube -import it.vfsfitvnm.innertube.models.bodies.BrowseBody -import it.vfsfitvnm.innertube.requests.playlistPage -import it.vfsfitvnm.innertube.requests.song +import it.vfsfitvnm.vimusic.utils.useSystemFontKey import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.flow.filterNotNull @@ -141,6 +143,9 @@ class MainActivity : ComponentActivity() { val thumbnailRoundness = getEnum(thumbnailRoundnessKey, ThumbnailRoundness.Light) + val useSystemFont = getBoolean(useSystemFontKey, false) + val applyFontPadding = getBoolean(applyFontPaddingKey, false) + val colorPalette = colorPaletteOf(colorPaletteName, colorPaletteMode, isSystemInDarkTheme) @@ -149,7 +154,7 @@ class MainActivity : ComponentActivity() { mutableStateOf( Appearance( colorPalette = colorPalette, - typography = typographyOf(colorPalette.text), + typography = typographyOf(colorPalette.text, useSystemFont, applyFontPadding), thumbnailShape = thumbnailRoundness.shape() ) ) @@ -176,7 +181,7 @@ class MainActivity : ComponentActivity() { appearance = appearance.copy( colorPalette = colorPalette, - typography = typographyOf(colorPalette.text) + typography = appearance.typography.copy(colorPalette.text) ) return@setBitmapListener @@ -189,7 +194,7 @@ class MainActivity : ComponentActivity() { } appearance = appearance.copy( colorPalette = it, - typography = typographyOf(it.text) + typography = appearance.typography.copy(it.text) ) } } @@ -228,7 +233,7 @@ class MainActivity : ComponentActivity() { appearance = appearance.copy( colorPalette = colorPalette, - typography = typographyOf(colorPalette.text), + typography = appearance.typography.copy(colorPalette.text), ) } } @@ -241,6 +246,15 @@ class MainActivity : ComponentActivity() { thumbnailShape = thumbnailRoundness.shape() ) } + + useSystemFontKey, applyFontPaddingKey -> { + val useSystemFont = sharedPreferences.getBoolean(useSystemFontKey, false) + val applyFontPadding = sharedPreferences.getBoolean(applyFontPaddingKey, false) + + appearance = appearance.copy( + typography = typographyOf(appearance.colorPalette.text, useSystemFont, applyFontPadding), + ) + } } } diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/AppearanceSettings.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/AppearanceSettings.kt index f6223d8..8c3ffad 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/AppearanceSettings.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/AppearanceSettings.kt @@ -24,11 +24,13 @@ import it.vfsfitvnm.vimusic.enums.ColorPaletteName import it.vfsfitvnm.vimusic.enums.ThumbnailRoundness import it.vfsfitvnm.vimusic.ui.components.themed.Header import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance +import it.vfsfitvnm.vimusic.utils.applyFontPaddingKey import it.vfsfitvnm.vimusic.utils.colorPaletteModeKey import it.vfsfitvnm.vimusic.utils.colorPaletteNameKey import it.vfsfitvnm.vimusic.utils.isShowingThumbnailInLockscreenKey import it.vfsfitvnm.vimusic.utils.rememberPreference import it.vfsfitvnm.vimusic.utils.thumbnailRoundnessKey +import it.vfsfitvnm.vimusic.utils.useSystemFontKey @ExperimentalAnimationApi @Composable @@ -41,6 +43,8 @@ fun AppearanceSettings() { thumbnailRoundnessKey, ThumbnailRoundness.Light ) + var useSystemFont by rememberPreference(useSystemFontKey, false) + var applyFontPadding by rememberPreference(applyFontPaddingKey, false) var isShowingThumbnailInLockscreen by rememberPreference( isShowingThumbnailInLockscreenKey, false @@ -94,6 +98,24 @@ fun AppearanceSettings() { SettingsGroupSpacer() + SettingsEntryGroupText(title = "TEXT") + + SwitchSettingEntry( + title = "Use system font", + text = "Use the font applied by the system", + isChecked = useSystemFont, + onCheckedChange = { useSystemFont = it } + ) + + SwitchSettingEntry( + title = "Apply font padding", + text = "Add spacing around texts", + isChecked = applyFontPadding, + onCheckedChange = { applyFontPadding = it } + ) + + SettingsGroupSpacer() + SettingsEntryGroupText(title = "LOCKSCREEN") SwitchSettingEntry( diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/styling/Appearance.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/styling/Appearance.kt index 6c4c468..42fa241 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/styling/Appearance.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/styling/Appearance.kt @@ -5,43 +5,27 @@ import androidx.compose.runtime.saveable.Saver import androidx.compose.runtime.saveable.SaverScope import androidx.compose.runtime.staticCompositionLocalOf import androidx.compose.ui.graphics.Shape -import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.unit.dp -import androidx.core.graphics.ColorUtils data class Appearance( val colorPalette: ColorPalette, val typography: Typography, - val thumbnailShape: Shape + val thumbnailShape: Shape, ) { companion object : Saver> { + @Suppress("UNCHECKED_CAST") override fun restore(value: List): Appearance { - val colorPalette = when (val accent = value[0] as Int) { - 0 -> DefaultDarkColorPalette - 1 -> DefaultLightColorPalette - 2 -> PureBlackColorPalette - else -> dynamicColorPaletteOf( - FloatArray(3).apply { ColorUtils.colorToHSL(accent, this) }, - value[1] as Boolean - ) - } - return Appearance( - colorPalette = colorPalette, - typography = typographyOf(colorPalette.text), + colorPalette = ColorPalette.restore(value[0] as List), + typography = Typography.restore(value[1] as List), thumbnailShape = RoundedCornerShape((value[2] as Int).dp) ) } override fun SaverScope.save(value: Appearance) = listOf( - when { - value.colorPalette === DefaultDarkColorPalette -> 0 - value.colorPalette === DefaultLightColorPalette -> 1 - value.colorPalette === PureBlackColorPalette -> 2 - else -> value.colorPalette.accent.toArgb() - }, - value.colorPalette.isDark, + with (ColorPalette.Companion) { save(value.colorPalette) }, + with (Typography.Companion) { save(value.typography) }, when (value.thumbnailShape) { RoundedCornerShape(2.dp) -> 2 RoundedCornerShape(4.dp) -> 4 diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/styling/ColorPalette.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/styling/ColorPalette.kt index f2b31dc..84cf6b2 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/styling/ColorPalette.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/styling/ColorPalette.kt @@ -2,7 +2,11 @@ package it.vfsfitvnm.vimusic.ui.styling import android.graphics.Bitmap import androidx.compose.runtime.Immutable +import androidx.compose.runtime.saveable.Saver +import androidx.compose.runtime.saveable.SaverScope import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.toArgb +import androidx.core.graphics.ColorUtils import androidx.palette.graphics.Palette import it.vfsfitvnm.vimusic.enums.ColorPaletteMode import it.vfsfitvnm.vimusic.enums.ColorPaletteName @@ -20,7 +24,30 @@ data class ColorPalette( val textSecondary: Color, val textDisabled: Color, val isDark: Boolean -) +) { + companion object : Saver> { + override fun restore(value: List) = when (val accent = value[0] as Int) { + 0 -> DefaultDarkColorPalette + 1 -> DefaultLightColorPalette + 2 -> PureBlackColorPalette + else -> dynamicColorPaletteOf( + FloatArray(3).apply { ColorUtils.colorToHSL(accent, this) }, + value[1] as Boolean + ) + } + + override fun SaverScope.save(value: ColorPalette) = + listOf( + when { + value === DefaultDarkColorPalette -> 0 + value === DefaultLightColorPalette -> 1 + value === PureBlackColorPalette -> 2 + else -> value.accent.toArgb() + }, + value.isDark + ) + } +} val DefaultDarkColorPalette = ColorPalette( background0 = Color(0xff16171d), diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/styling/Typography.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/styling/Typography.kt index d17b065..1c1faa1 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/styling/Typography.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/styling/Typography.kt @@ -1,6 +1,8 @@ package it.vfsfitvnm.vimusic.ui.styling import androidx.compose.runtime.Immutable +import androidx.compose.runtime.saveable.Saver +import androidx.compose.runtime.saveable.SaverScope import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.PlatformTextStyle import androidx.compose.ui.text.TextStyle @@ -18,35 +20,63 @@ data class Typography( val m: TextStyle, val l: TextStyle, val xxl: TextStyle, -) +) { + fun copy(color: Color) = Typography( + xxs = xxs.copy(color = color), + xs = xs.copy(color = color), + s = s.copy(color = color), + m = m.copy(color = color), + l = l.copy(color = color), + xxl = xxl.copy(color = color) + ) -fun typographyOf(color: Color): Typography { + companion object : Saver> { + override fun restore(value: List) = typographyOf( + Color((value[0] as Long).toULong()), + value[1] as Boolean, + value[2] as Boolean + ) + + override fun SaverScope.save(value: Typography) = + listOf( + value.xxs.color.value.toLong(), + value.xxs.fontFamily == FontFamily.Default, + value.xxs.platformStyle?.paragraphStyle?.includeFontPadding ?: false + ) + } +} + +fun typographyOf(color: Color, useSystemFont: Boolean, applyFontPadding: Boolean): Typography { val textStyle = TextStyle( - fontFamily = FontFamily( - Font( - resId = R.font.poppins_w300, - weight = FontWeight.Light - ), - Font( - resId = R.font.poppins_w400, - weight = FontWeight.Normal - ), - Font( - resId = R.font.poppins_w500, - weight = FontWeight.Medium - ), - Font( - resId = R.font.poppins_w600, - weight = FontWeight.SemiBold - ), - Font( - resId = R.font.poppins_w700, - weight = FontWeight.Bold - ), - ), + fontFamily = if (useSystemFont) { + FontFamily.Default + } else { + FontFamily( + Font( + resId = R.font.poppins_w300, + weight = FontWeight.Light + ), + Font( + resId = R.font.poppins_w400, + weight = FontWeight.Normal + ), + Font( + resId = R.font.poppins_w500, + weight = FontWeight.Medium + ), + Font( + resId = R.font.poppins_w600, + weight = FontWeight.SemiBold + ), + Font( + resId = R.font.poppins_w700, + weight = FontWeight.Bold + ), + ) + }, fontWeight = FontWeight.Normal, color = color, - platformStyle = @Suppress("DEPRECATION") (PlatformTextStyle(includeFontPadding = false)) + platformStyle = @Suppress("DEPRECATION") (PlatformTextStyle(includeFontPadding = applyFontPadding)) ) return Typography( @@ -55,6 +85,6 @@ fun typographyOf(color: Color): Typography { s = textStyle.copy(fontSize = 16.sp), m = textStyle.copy(fontSize = 18.sp), l = textStyle.copy(fontSize = 20.sp), - xxl = textStyle.copy(fontSize = 32.sp), + xxl = textStyle.copy(fontSize = 32.sp) ) } diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/utils/Preferences.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/utils/Preferences.kt index 774138a..5600eea 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/utils/Preferences.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/utils/Preferences.kt @@ -16,7 +16,8 @@ const val thumbnailRoundnessKey = "thumbnailRoundness" const val coilDiskCacheMaxSizeKey = "coilDiskCacheMaxSize" const val exoPlayerDiskCacheMaxSizeKey = "exoPlayerDiskCacheMaxSize" const val isInvincibilityEnabledKey = "isInvincibilityEnabled" -const val isFirstLaunchKey = "isFirstLaunch" +const val useSystemFontKey = "useSystemFont" +const val applyFontPaddingKey = "applyFontPadding" const val songSortOrderKey = "songSortOrder" const val songSortByKey = "songSortBy" const val playlistSortOrderKey = "playlistSortOrder"