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 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),
)
}
}
}

View file

@ -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(

View file

@ -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<Appearance, List<Any>> {
@Suppress("UNCHECKED_CAST")
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(
colorPalette = colorPalette,
typography = typographyOf(colorPalette.text),
colorPalette = ColorPalette.restore(value[0] as List<Any>),
typography = Typography.restore(value[1] as List<Any>),
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

View file

@ -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<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(
background0 = Color(0xff16171d),

View file

@ -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,11 +20,38 @@ 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<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(
fontFamily = FontFamily(
fontFamily = if (useSystemFont) {
FontFamily.Default
} else {
FontFamily(
Font(
resId = R.font.poppins_w300,
weight = FontWeight.Light
@ -43,10 +72,11 @@ fun typographyOf(color: Color): Typography {
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)
)
}

View file

@ -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"