Skip to content

Commit

Permalink
πŸ”€ Merge pull request #63 from bhavesh100/customize-ui
Browse files Browse the repository at this point in the history
Added UI Settings for top bar font weight
  • Loading branch information
lorenzovngl committed Sep 28, 2023
2 parents 1db2623 + 33c5f6c commit 4caa2fc
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class PreferencesRepository {
const val keyNotificationTimeHour = "notification_time_hour"
const val keyNotificationTimeMinute = "notification_time_minute"
const val keyThemeMode = "theme_mode"
const val keyTopBarFont = "top_bar_font"
const val keyDynamicColors = "dynamic_colors"
private val availLocaleDateFormats = arrayOf(DateFormat.SHORT, DateFormat.MEDIUM)
private val availOtherDateFormats =
Expand All @@ -35,6 +36,12 @@ class PreferencesRepository {
DARK(R.string.dark)
}

enum class TopBarFont(val label: Int) {
NORMAL(R.string.normal),
BOLD(R.string.bold),
EXTRA_BOLD(R.string.extra_bold)
}

fun getAvailLocaleDateFormats(): List<String> {
return availLocaleDateFormats.map {
(DateFormat.getDateInstance(
Expand Down Expand Up @@ -113,6 +120,28 @@ class PreferencesRepository {
.edit().putInt(keyThemeMode, themeMode.ordinal).apply()
}

fun getTopBarFont(
context: Context,
sharedPrefs: String = sharedPrefsName,
): Int{
try {
return context.getSharedPreferences(sharedPrefs, ComponentActivity.MODE_PRIVATE)
.getInt(keyTopBarFont,TopBarFont.NORMAL.ordinal)
} catch (e: Exception){
e.printStackTrace()
}
return TopBarFont.NORMAL.ordinal
}

fun setTopBarFont(
context: Context,
sharedPrefs: String = sharedPrefsName,
topBarFont: TopBarFont
) {
return context.getSharedPreferences(sharedPrefs, ComponentActivity.MODE_PRIVATE)
.edit().putInt(keyTopBarFont, topBarFont.ordinal).apply()
}

fun getDynamicColors(
context: Context,
sharedPrefs: String = sharedPrefsName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,31 @@ import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.lorenzovainigli.foodexpirationdates.model.repository.PreferencesRepository
import com.lorenzovainigli.foodexpirationdates.ui.theme.FoodExpirationDatesTheme
import com.lorenzovainigli.foodexpirationdates.viewmodel.PreferencesViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyTopAppBar(
title: String,
actions: @Composable RowScope.() -> Unit = {},
navigationIcon: @Composable () -> Unit = {},
scrollBehavior: TopAppBarScrollBehavior? = null
scrollBehavior: TopAppBarScrollBehavior? = null,
prefsViewModel: PreferencesViewModel? = viewModel()
) {
val context = LocalContext.current
val topBarFontState = prefsViewModel?.getTopBarFont(context)?.collectAsState()?.value
?: PreferencesRepository.Companion.TopBarFont.NORMAL.ordinal

LargeTopAppBar(
modifier = Modifier
.padding(bottom = 4.dp)
Expand All @@ -30,7 +41,13 @@ fun MyTopAppBar(
title = {
Text(
text = title,
color = MaterialTheme.colorScheme.primary
color = MaterialTheme.colorScheme.primary,
fontWeight = when(topBarFontState){
PreferencesRepository.Companion.TopBarFont.NORMAL.ordinal -> FontWeight.Normal
PreferencesRepository.Companion.TopBarFont.BOLD.ordinal -> FontWeight.Medium
PreferencesRepository.Companion.TopBarFont.EXTRA_BOLD.ordinal-> FontWeight.Bold
else -> null
}
)
},
actions = actions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ fun SettingsActivityLayout(
?: PreferencesRepository.Companion.ThemeMode.SYSTEM.ordinal
val dynamicColorsState = prefsViewModel?.getDynamicColors(context)?.collectAsState()?.value
?: false
val topBarFontState = prefsViewModel?.getTopBarFont(context)?.collectAsState()?.value
?: PreferencesRepository.Companion.TopBarFont.NORMAL.ordinal

val dateFormat = prefsViewModel?.getDateFormat(context)?.collectAsState()?.value
?: PreferencesRepository.getAvailOtherDateFormats()[0]
Expand Down Expand Up @@ -152,6 +154,10 @@ fun SettingsActivityLayout(
.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Text(
text = stringResource(R.string.behaviour),
style = MaterialTheme.typography.labelLarge
)
SettingsItem(
label = stringResource(id = R.string.date_format)
){
Expand Down Expand Up @@ -185,6 +191,10 @@ fun SettingsActivityLayout(
}
)
}
Text(
text = stringResource(R.string.appearance),
style = MaterialTheme.typography.labelLarge
)
SettingsItem(
label = stringResource(R.string.theme)
){
Expand Down Expand Up @@ -233,6 +243,35 @@ fun SettingsActivityLayout(
}
)
}
SettingsItem(
label = stringResource(R.string.top_bar_font_weight)
) {
PreferencesRepository.Companion.TopBarFont.values().forEach { topBarFont->
Spacer(
modifier = Modifier
.fillMaxHeight()
.weight(0.1f)
)
if (topBarFont.ordinal != topBarFontState) {
OutlinedButton(
onClick = {
prefsViewModel?.setTopBarFont(context, topBarFont)
},
) {
Text(
text = context.getString(topBarFont.label)
)
}
}
if (topBarFont.ordinal == topBarFontState) {
Button(onClick = {}) {
Text(
text = context.getString(topBarFont.label)
)
}
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ class PreferencesViewModel @Inject constructor(): ViewModel() {

private var _themeMode = MutableStateFlow(0)
private var themeMode = _themeMode.asStateFlow()

private var _dynamicColors = MutableStateFlow(false)
private var dynamicColors = _dynamicColors.asStateFlow()

private var _topBarFont = MutableStateFlow(0)
private var topbarFont = _topBarFont.asStateFlow()

fun getDateFormat(context: Context): StateFlow<String> {
viewModelScope.launch {
_dateFormat.value = PreferencesRepository.getUserDateFormat(context)
Expand Down Expand Up @@ -88,6 +90,23 @@ class PreferencesViewModel @Inject constructor(): ViewModel() {
_themeMode.value = theme.ordinal
}

fun getTopBarFont(context: Context):StateFlow<Int> {
viewModelScope.launch {
_topBarFont.value = PreferencesRepository.getTopBarFont(context)
}
return topbarFont
}

fun setTopBarFont(context: Context, topBarFont: PreferencesRepository.Companion.TopBarFont) {
viewModelScope.launch {
PreferencesRepository.setTopBarFont(
context = context,
topBarFont = topBarFont
)
}
_topBarFont.value = topBarFont.ordinal
}

fun getDynamicColors(context: Context): StateFlow<Boolean> {
viewModelScope.launch {
_dynamicColors.value = PreferencesRepository.getDynamicColors(context)
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
<string name="dark">Dark</string>
<string name="x_deleted">%1$s deleted</string>
<string name="undo">Undo</string>
<string name ="normal">Normal</string>
<string name ="bold">Bold</string>
<string name ="extra_bold">Extra Bold</string>
<string name ="ui_settings">UI Settings</string>
<string name ="top_bar_font_weight">Top Bar Font Weight</string>
<string name ="behaviour">Behaviour</string>
<string name ="appearance">Appearance</string>


<string-array name="features">
<item>Display a list with food expiration dates in ascending time order.</item>
Expand Down

0 comments on commit 4caa2fc

Please sign in to comment.