Add option to use system font (#182)

This commit is contained in:
vfsfitvnm 2022-10-19 15:23:33 +02:00
parent fc569ea5f9
commit d1f4aa4577
6 changed files with 136 additions and 58 deletions

View file

@ -57,6 +57,10 @@ import androidx.media3.common.MediaItem
import androidx.media3.common.Player import androidx.media3.common.Player
import com.valentinilk.shimmer.LocalShimmerTheme import com.valentinilk.shimmer.LocalShimmerTheme
import com.valentinilk.shimmer.defaultShimmerTheme 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.ColorPaletteMode
import it.vfsfitvnm.vimusic.enums.ColorPaletteName import it.vfsfitvnm.vimusic.enums.ColorPaletteName
import it.vfsfitvnm.vimusic.enums.ThumbnailRoundness 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.colorPaletteOf
import it.vfsfitvnm.vimusic.ui.styling.dynamicColorPaletteOf import it.vfsfitvnm.vimusic.ui.styling.dynamicColorPaletteOf
import it.vfsfitvnm.vimusic.ui.styling.typographyOf import it.vfsfitvnm.vimusic.ui.styling.typographyOf
import it.vfsfitvnm.vimusic.utils.applyFontPaddingKey
import it.vfsfitvnm.vimusic.utils.asMediaItem import it.vfsfitvnm.vimusic.utils.asMediaItem
import it.vfsfitvnm.vimusic.utils.colorPaletteModeKey import it.vfsfitvnm.vimusic.utils.colorPaletteModeKey
import it.vfsfitvnm.vimusic.utils.colorPaletteNameKey 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.intent
import it.vfsfitvnm.vimusic.utils.preferences import it.vfsfitvnm.vimusic.utils.preferences
import it.vfsfitvnm.vimusic.utils.thumbnailRoundnessKey import it.vfsfitvnm.vimusic.utils.thumbnailRoundnessKey
import it.vfsfitvnm.innertube.Innertube import it.vfsfitvnm.vimusic.utils.useSystemFontKey
import it.vfsfitvnm.innertube.models.bodies.BrowseBody
import it.vfsfitvnm.innertube.requests.playlistPage
import it.vfsfitvnm.innertube.requests.song
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.filterNotNull
@ -141,6 +143,9 @@ class MainActivity : ComponentActivity() {
val thumbnailRoundness = val thumbnailRoundness =
getEnum(thumbnailRoundnessKey, ThumbnailRoundness.Light) getEnum(thumbnailRoundnessKey, ThumbnailRoundness.Light)
val useSystemFont = getBoolean(useSystemFontKey, false)
val applyFontPadding = getBoolean(applyFontPaddingKey, false)
val colorPalette = val colorPalette =
colorPaletteOf(colorPaletteName, colorPaletteMode, isSystemInDarkTheme) colorPaletteOf(colorPaletteName, colorPaletteMode, isSystemInDarkTheme)
@ -149,7 +154,7 @@ class MainActivity : ComponentActivity() {
mutableStateOf( mutableStateOf(
Appearance( Appearance(
colorPalette = colorPalette, colorPalette = colorPalette,
typography = typographyOf(colorPalette.text), typography = typographyOf(colorPalette.text, useSystemFont, applyFontPadding),
thumbnailShape = thumbnailRoundness.shape() thumbnailShape = thumbnailRoundness.shape()
) )
) )
@ -176,7 +181,7 @@ class MainActivity : ComponentActivity() {
appearance = appearance.copy( appearance = appearance.copy(
colorPalette = colorPalette, colorPalette = colorPalette,
typography = typographyOf(colorPalette.text) typography = appearance.typography.copy(colorPalette.text)
) )
return@setBitmapListener return@setBitmapListener
@ -189,7 +194,7 @@ class MainActivity : ComponentActivity() {
} }
appearance = appearance.copy( appearance = appearance.copy(
colorPalette = it, colorPalette = it,
typography = typographyOf(it.text) typography = appearance.typography.copy(it.text)
) )
} }
} }
@ -228,7 +233,7 @@ class MainActivity : ComponentActivity() {
appearance = appearance.copy( appearance = appearance.copy(
colorPalette = colorPalette, colorPalette = colorPalette,
typography = typographyOf(colorPalette.text), typography = appearance.typography.copy(colorPalette.text),
) )
} }
} }
@ -241,6 +246,15 @@ class MainActivity : ComponentActivity() {
thumbnailShape = thumbnailRoundness.shape() 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),
)
}
} }
} }

View file

@ -24,11 +24,13 @@ import it.vfsfitvnm.vimusic.enums.ColorPaletteName
import it.vfsfitvnm.vimusic.enums.ThumbnailRoundness import it.vfsfitvnm.vimusic.enums.ThumbnailRoundness
import it.vfsfitvnm.vimusic.ui.components.themed.Header import it.vfsfitvnm.vimusic.ui.components.themed.Header
import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance
import it.vfsfitvnm.vimusic.utils.applyFontPaddingKey
import it.vfsfitvnm.vimusic.utils.colorPaletteModeKey import it.vfsfitvnm.vimusic.utils.colorPaletteModeKey
import it.vfsfitvnm.vimusic.utils.colorPaletteNameKey import it.vfsfitvnm.vimusic.utils.colorPaletteNameKey
import it.vfsfitvnm.vimusic.utils.isShowingThumbnailInLockscreenKey import it.vfsfitvnm.vimusic.utils.isShowingThumbnailInLockscreenKey
import it.vfsfitvnm.vimusic.utils.rememberPreference import it.vfsfitvnm.vimusic.utils.rememberPreference
import it.vfsfitvnm.vimusic.utils.thumbnailRoundnessKey import it.vfsfitvnm.vimusic.utils.thumbnailRoundnessKey
import it.vfsfitvnm.vimusic.utils.useSystemFontKey
@ExperimentalAnimationApi @ExperimentalAnimationApi
@Composable @Composable
@ -41,6 +43,8 @@ fun AppearanceSettings() {
thumbnailRoundnessKey, thumbnailRoundnessKey,
ThumbnailRoundness.Light ThumbnailRoundness.Light
) )
var useSystemFont by rememberPreference(useSystemFontKey, false)
var applyFontPadding by rememberPreference(applyFontPaddingKey, false)
var isShowingThumbnailInLockscreen by rememberPreference( var isShowingThumbnailInLockscreen by rememberPreference(
isShowingThumbnailInLockscreenKey, isShowingThumbnailInLockscreenKey,
false false
@ -94,6 +98,24 @@ fun AppearanceSettings() {
SettingsGroupSpacer() 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") SettingsEntryGroupText(title = "LOCKSCREEN")
SwitchSettingEntry( SwitchSettingEntry(

View file

@ -5,43 +5,27 @@ import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.SaverScope import androidx.compose.runtime.saveable.SaverScope
import androidx.compose.runtime.staticCompositionLocalOf import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Shape import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.core.graphics.ColorUtils
data class Appearance( data class Appearance(
val colorPalette: ColorPalette, val colorPalette: ColorPalette,
val typography: Typography, val typography: Typography,
val thumbnailShape: Shape val thumbnailShape: Shape,
) { ) {
companion object : Saver<Appearance, List<Any>> { companion object : Saver<Appearance, List<Any>> {
@Suppress("UNCHECKED_CAST")
override fun restore(value: List<Any>): Appearance { override fun restore(value: List<Any>): 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( return Appearance(
colorPalette = colorPalette, colorPalette = ColorPalette.restore(value[0] as List<Any>),
typography = typographyOf(colorPalette.text), typography = Typography.restore(value[1] as List<Any>),
thumbnailShape = RoundedCornerShape((value[2] as Int).dp) thumbnailShape = RoundedCornerShape((value[2] as Int).dp)
) )
} }
override fun SaverScope.save(value: Appearance) = override fun SaverScope.save(value: Appearance) =
listOf( listOf(
when { with (ColorPalette.Companion) { save(value.colorPalette) },
value.colorPalette === DefaultDarkColorPalette -> 0 with (Typography.Companion) { save(value.typography) },
value.colorPalette === DefaultLightColorPalette -> 1
value.colorPalette === PureBlackColorPalette -> 2
else -> value.colorPalette.accent.toArgb()
},
value.colorPalette.isDark,
when (value.thumbnailShape) { when (value.thumbnailShape) {
RoundedCornerShape(2.dp) -> 2 RoundedCornerShape(2.dp) -> 2
RoundedCornerShape(4.dp) -> 4 RoundedCornerShape(4.dp) -> 4

View file

@ -2,7 +2,11 @@ package it.vfsfitvnm.vimusic.ui.styling
import android.graphics.Bitmap import android.graphics.Bitmap
import androidx.compose.runtime.Immutable 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.Color
import androidx.compose.ui.graphics.toArgb
import androidx.core.graphics.ColorUtils
import androidx.palette.graphics.Palette import androidx.palette.graphics.Palette
import it.vfsfitvnm.vimusic.enums.ColorPaletteMode import it.vfsfitvnm.vimusic.enums.ColorPaletteMode
import it.vfsfitvnm.vimusic.enums.ColorPaletteName import it.vfsfitvnm.vimusic.enums.ColorPaletteName
@ -20,7 +24,30 @@ data class ColorPalette(
val textSecondary: Color, val textSecondary: Color,
val textDisabled: Color, val textDisabled: Color,
val isDark: Boolean val isDark: Boolean
) ) {
companion object : Saver<ColorPalette, List<Any>> {
override fun restore(value: List<Any>) = 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( val DefaultDarkColorPalette = ColorPalette(
background0 = Color(0xff16171d), background0 = Color(0xff16171d),

View file

@ -1,6 +1,8 @@
package it.vfsfitvnm.vimusic.ui.styling package it.vfsfitvnm.vimusic.ui.styling
import androidx.compose.runtime.Immutable 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.Color
import androidx.compose.ui.text.PlatformTextStyle import androidx.compose.ui.text.PlatformTextStyle
import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.TextStyle
@ -18,35 +20,63 @@ data class Typography(
val m: TextStyle, val m: TextStyle,
val l: TextStyle, val l: TextStyle,
val xxl: 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<Typography, List<Any>> {
override fun restore(value: List<Any>) = 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( val textStyle = TextStyle(
fontFamily = FontFamily( fontFamily = if (useSystemFont) {
Font( FontFamily.Default
resId = R.font.poppins_w300, } else {
weight = FontWeight.Light FontFamily(
), Font(
Font( resId = R.font.poppins_w300,
resId = R.font.poppins_w400, weight = FontWeight.Light
weight = FontWeight.Normal ),
), Font(
Font( resId = R.font.poppins_w400,
resId = R.font.poppins_w500, weight = FontWeight.Normal
weight = FontWeight.Medium ),
), Font(
Font( resId = R.font.poppins_w500,
resId = R.font.poppins_w600, weight = FontWeight.Medium
weight = FontWeight.SemiBold ),
), Font(
Font( resId = R.font.poppins_w600,
resId = R.font.poppins_w700, weight = FontWeight.SemiBold
weight = FontWeight.Bold ),
), Font(
), resId = R.font.poppins_w700,
weight = FontWeight.Bold
),
)
},
fontWeight = FontWeight.Normal, fontWeight = FontWeight.Normal,
color = color, color = color,
platformStyle = @Suppress("DEPRECATION") (PlatformTextStyle(includeFontPadding = false)) platformStyle = @Suppress("DEPRECATION") (PlatformTextStyle(includeFontPadding = applyFontPadding))
) )
return Typography( return Typography(
@ -55,6 +85,6 @@ fun typographyOf(color: Color): Typography {
s = textStyle.copy(fontSize = 16.sp), s = textStyle.copy(fontSize = 16.sp),
m = textStyle.copy(fontSize = 18.sp), m = textStyle.copy(fontSize = 18.sp),
l = textStyle.copy(fontSize = 20.sp), l = textStyle.copy(fontSize = 20.sp),
xxl = textStyle.copy(fontSize = 32.sp), xxl = textStyle.copy(fontSize = 32.sp)
) )
} }

View file

@ -16,7 +16,8 @@ const val thumbnailRoundnessKey = "thumbnailRoundness"
const val coilDiskCacheMaxSizeKey = "coilDiskCacheMaxSize" const val coilDiskCacheMaxSizeKey = "coilDiskCacheMaxSize"
const val exoPlayerDiskCacheMaxSizeKey = "exoPlayerDiskCacheMaxSize" const val exoPlayerDiskCacheMaxSizeKey = "exoPlayerDiskCacheMaxSize"
const val isInvincibilityEnabledKey = "isInvincibilityEnabled" const val isInvincibilityEnabledKey = "isInvincibilityEnabled"
const val isFirstLaunchKey = "isFirstLaunch" const val useSystemFontKey = "useSystemFont"
const val applyFontPaddingKey = "applyFontPadding"
const val songSortOrderKey = "songSortOrder" const val songSortOrderKey = "songSortOrder"
const val songSortByKey = "songSortBy" const val songSortByKey = "songSortBy"
const val playlistSortOrderKey = "playlistSortOrder" const val playlistSortOrderKey = "playlistSortOrder"