Add an option to use system font

closes #81
This commit is contained in:
Sad Ellie 2023-09-05 15:39:25 +03:00
parent 6fef198541
commit 50f0ddbe91
7 changed files with 166 additions and 10 deletions

View File

@ -46,7 +46,8 @@ import com.sadellie.unitto.core.ui.common.isOpen
import com.sadellie.unitto.core.ui.common.open
import com.sadellie.unitto.core.ui.common.rememberUnittoDrawerState
import com.sadellie.unitto.core.ui.model.DrawerItems
import com.sadellie.unitto.core.ui.theme.AppTypography
import com.sadellie.unitto.core.ui.theme.AppTypographyUnitto
import com.sadellie.unitto.core.ui.theme.AppTypographySystem
import com.sadellie.unitto.core.ui.theme.DarkThemeColors
import com.sadellie.unitto.core.ui.theme.LightThemeColors
import com.sadellie.unitto.data.userprefs.UIPreferences
@ -105,7 +106,7 @@ internal fun UnittoApp(uiPrefs: UIPreferences) {
Themmo(
themmoController = themmoController,
typography = AppTypography,
typography = if (uiPrefs.systemFont) AppTypographySystem else AppTypographyUnitto,
animationSpec = tween(250)
) {
val backgroundColor = MaterialTheme.colorScheme.background

View File

@ -1391,4 +1391,6 @@ Used in this dialog window. Should be short -->
<string name="locale_ru" translatable="false">Русский</string>
<string name="language_setting">Language</string>
<string name="language_setting_support">Change the app language</string>
<string name="system_font_setting">System font</string>
<string name="system_font_setting_support">Use system font for texts in app</string>
</resources>

View File

@ -56,7 +56,7 @@ val NumbersTextStyleDisplayMedium = TextStyle(
letterSpacing = (-0.25).sp,
)
val AppTypography = Typography(
val AppTypographyUnitto = Typography(
displayLarge = TextStyle(
fontFamily = Montserrat,
fontWeight = FontWeight.Normal,
@ -163,3 +163,96 @@ val AppTypography = Typography(
letterSpacing = 0.sp,
),
)
val AppTypographySystem = Typography(
displayLarge = TextStyle(
fontWeight = FontWeight.Normal,
fontSize = 57.sp,
lineHeight = 64.sp,
letterSpacing = (-0.25).sp,
),
displayMedium = TextStyle(
fontWeight = FontWeight.Normal,
fontSize = 45.sp,
lineHeight = 52.sp,
letterSpacing = 0.sp,
),
displaySmall = TextStyle(
fontWeight = FontWeight.Normal,
fontSize = 36.sp,
lineHeight = 44.sp,
letterSpacing = 0.sp,
),
headlineLarge = TextStyle(
fontWeight = FontWeight.Normal,
fontSize = 32.sp,
lineHeight = 40.sp,
letterSpacing = 0.sp,
),
headlineMedium = TextStyle(
fontWeight = FontWeight.Normal,
fontSize = 28.sp,
lineHeight = 36.sp,
letterSpacing = 0.sp,
),
headlineSmall = TextStyle(
fontWeight = FontWeight.Normal,
fontSize = 24.sp,
lineHeight = 32.sp,
letterSpacing = 0.sp,
),
titleLarge = TextStyle(
fontWeight = FontWeight.Medium,
fontSize = 22.sp,
lineHeight = 28.sp,
letterSpacing = 0.sp,
),
titleMedium = TextStyle(
fontWeight = FontWeight.Bold,
fontSize = 18.sp,
lineHeight = 24.sp,
letterSpacing = 0.1.sp,
),
titleSmall = TextStyle(
fontWeight = FontWeight.Medium,
fontSize = 14.sp,
lineHeight = 20.sp,
letterSpacing = 0.1.sp,
),
bodyLarge = TextStyle(
fontWeight = FontWeight.Normal,
fontSize = 16.sp,
lineHeight = 24.sp,
letterSpacing = 0.5.sp,
),
bodyMedium = TextStyle(
fontWeight = FontWeight.Normal,
fontSize = 14.sp,
lineHeight = 20.sp,
letterSpacing = 0.25.sp,
),
bodySmall = TextStyle(
fontWeight = FontWeight.Normal,
fontSize = 12.sp,
lineHeight = 16.sp,
letterSpacing = 0.4.sp,
),
labelLarge = TextStyle(
fontWeight = FontWeight.Medium,
fontSize = 14.sp,
lineHeight = 20.sp,
letterSpacing = 0.1.sp,
),
labelMedium = TextStyle(
fontWeight = FontWeight.Medium,
fontSize = 12.sp,
lineHeight = 16.sp,
letterSpacing = 0.5.sp,
),
labelSmall = TextStyle(
fontWeight = FontWeight.Medium,
fontSize = 10.sp,
lineHeight = 16.sp,
letterSpacing = 0.sp,
),
)

View File

@ -84,6 +84,7 @@ data class UserPreferences(
val unitConverterFormatTime: Boolean = false,
val unitConverterSorting: UnitsListSorting = UnitsListSorting.USAGE,
val middleZero: Boolean = false,
val systemFont: Boolean = false,
)
data class UIPreferences(
@ -94,6 +95,7 @@ data class UIPreferences(
val monetMode: MonetMode = MonetMode.TONAL_SPOT,
val startingScreen: String = TopLevelDestinations.Calculator.graph,
val enableToolsExperiment: Boolean = false,
val systemFont: Boolean = false,
)
data class MainPreferences(
@ -138,6 +140,7 @@ class UserPreferencesRepository @Inject constructor(private val dataStore: DataS
val UNIT_CONVERTER_FORMAT_TIME = booleanPreferencesKey("UNIT_CONVERTER_FORMAT_TIME_PREF_KEY")
val UNIT_CONVERTER_SORTING = stringPreferencesKey("UNIT_CONVERTER_SORTING_PREF_KEY")
val MIDDLE_ZERO = booleanPreferencesKey("MIDDLE_ZERO_PREF_KEY")
val SYSTEM_FONT = booleanPreferencesKey("SYSTEM_FONT_PREF_KEY")
}
val uiPreferencesFlow: Flow<UIPreferences> = dataStore.data
@ -158,6 +161,7 @@ class UserPreferencesRepository @Inject constructor(private val dataStore: DataS
?: MonetMode.TONAL_SPOT
val startingScreen: String = preferences[PrefsKeys.STARTING_SCREEN] ?: TopLevelDestinations.Calculator.graph
val enableToolsExperiment: Boolean = preferences[PrefsKeys.ENABLE_TOOLS_EXPERIMENT] ?: false
val systemFont: Boolean = preferences[PrefsKeys.SYSTEM_FONT] ?: false
UIPreferences(
themingMode = themingMode,
@ -166,7 +170,8 @@ class UserPreferencesRepository @Inject constructor(private val dataStore: DataS
customColor = customColor,
monetMode = monetMode,
startingScreen = startingScreen,
enableToolsExperiment = enableToolsExperiment
enableToolsExperiment = enableToolsExperiment,
systemFont = systemFont
)
}
@ -243,6 +248,7 @@ class UserPreferencesRepository @Inject constructor(private val dataStore: DataS
unitConverterFormatTime = main.unitConverterFormatTime,
unitConverterSorting = main.unitConverterSorting,
middleZero = main.middleZero,
systemFont = ui.systemFont,
)
}
@ -441,4 +447,15 @@ class UserPreferencesRepository @Inject constructor(private val dataStore: DataS
preferences[PrefsKeys.UNIT_CONVERTER_SORTING] = sorting.name
}
}
/**
* Update system font preference.
*
* @param enabled When true will use system font.
*/
suspend fun updateSystemFont(enabled: Boolean) {
dataStore.edit { preferences ->
preferences[PrefsKeys.SYSTEM_FONT] = enabled
}
}
}

View File

@ -33,6 +33,7 @@ import androidx.compose.foundation.rememberScrollState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Colorize
import androidx.compose.material.icons.filled.DarkMode
import androidx.compose.material.icons.filled.FontDownload
import androidx.compose.material.icons.filled.Palette
import androidx.compose.material.icons.outlined.DarkMode
import androidx.compose.material.icons.outlined.HdrAuto
@ -48,6 +49,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.sadellie.unitto.core.base.R
import com.sadellie.unitto.core.ui.common.Header
import com.sadellie.unitto.core.ui.common.NavigateUpButton
@ -83,6 +85,8 @@ internal fun ThemesRoute(
themmoController: ThemmoController,
viewModel: ThemesViewModel = hiltViewModel()
) {
val systemFont = viewModel.systemFont.collectAsStateWithLifecycle()
ThemesScreen(
navigateUpAction = navigateUpAction,
currentThemingMode = themmoController.currentThemingMode,
@ -114,7 +118,9 @@ internal fun ThemesRoute(
onMonetModeChange = {
themmoController.setMonetMode(it)
viewModel.updateMonetMode(it)
}
},
systemFont = systemFont.value,
onSystemFontChange = viewModel::updateSystemFont
)
}
@ -130,7 +136,9 @@ private fun ThemesScreen(
selectedColor: Color,
onColorChange: (Color) -> Unit,
monetMode: MonetMode,
onMonetModeChange: (MonetMode) -> Unit
onMonetModeChange: (MonetMode) -> Unit,
systemFont: Boolean,
onSystemFontChange: (Boolean) -> Unit,
) {
UnittoScreenWithLargeTopBar(
title = stringResource(R.string.theme_setting),
@ -201,7 +209,20 @@ private fun ThemesScreen(
}
}
item { Header(stringResource(R.string.color_scheme)) }
item {
UnittoListItem(
leadingContent = {
Icon(
Icons.Default.FontDownload,
stringResource(R.string.system_font_setting),
)
},
label = stringResource(R.string.system_font_setting),
supportContent = stringResource(R.string.system_font_setting_support),
switchState = systemFont,
onSwitchChange = onSystemFontChange
)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
item {
@ -283,7 +304,9 @@ private fun Preview() {
selectedColor = themmoController.currentCustomColor,
onColorChange = themmoController::setCustomColor,
monetMode = themmoController.currentMonetMode,
onMonetModeChange = themmoController::setMonetMode
onMonetModeChange = themmoController::setMonetMode,
systemFont = false,
onSystemFontChange = {}
)
}
}

View File

@ -25,6 +25,9 @@ import com.sadellie.unitto.data.userprefs.UserPreferencesRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import io.github.sadellie.themmo.MonetMode
import io.github.sadellie.themmo.ThemingMode
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject
@ -33,6 +36,14 @@ class ThemesViewModel @Inject constructor(
private val userPrefsRepository: UserPreferencesRepository
) : ViewModel() {
val systemFont = userPrefsRepository.uiPreferencesFlow
.map { it.systemFont }
.stateIn(
viewModelScope,
SharingStarted.WhileSubscribed(5000L),
false
)
/**
* @see UserPreferencesRepository.updateThemingMode
*/
@ -77,4 +88,13 @@ class ThemesViewModel @Inject constructor(
userPrefsRepository.updateMonetMode(monetMode)
}
}
/**
* @see UserPreferencesRepository.updateSystemFont
*/
fun updateSystemFont(enabled: Boolean) {
viewModelScope.launch {
userPrefsRepository.updateSystemFont(enabled)
}
}
}

View File

@ -85,7 +85,7 @@ import com.sadellie.unitto.core.ui.common.UnittoScreenWithTopBar
import com.sadellie.unitto.core.ui.common.squashable
import com.sadellie.unitto.core.ui.datetime.UnittoDateTimeFormatter
import com.sadellie.unitto.core.ui.datetime.formatLocal
import com.sadellie.unitto.core.ui.theme.AppTypography
import com.sadellie.unitto.core.ui.theme.AppTypographyUnitto
import com.sadellie.unitto.core.ui.theme.DarkThemeColors
import com.sadellie.unitto.core.ui.theme.LightThemeColors
import com.sadellie.unitto.data.model.UnittoTimeZone
@ -355,7 +355,7 @@ fun PreviewTimeZoneScreen() {
lightColorScheme = LightThemeColors,
darkColorScheme = DarkThemeColors,
),
typography = AppTypography,
typography = AppTypographyUnitto,
) {
TimeZoneScreen(
uiState = TimeZoneUIState(