Skip to content

Commit

Permalink
Added UI Settings for top bar font weight
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavesh100 committed Sep 20, 2023
1 parent 434076b commit 71125ba
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
<activity
android:name=".view.activity.SettingsActivity">
</activity>
<activity
android:name=".view.activity.UISettingsActivity">
</activity>
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
Expand Down
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
@@ -0,0 +1,21 @@
package com.lorenzovainigli.foodexpirationdates.view.activity

import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi
import com.lorenzovainigli.foodexpirationdates.view.composable.activity.UISettingsActivityLayout
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class UISettingsActivity : ComponentActivity() {

@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
UISettingsActivityLayout()
}
}
}
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 @@ -2,6 +2,7 @@ package com.lorenzovainigli.foodexpirationdates.view.composable.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.isSystemInDarkTheme
Expand Down Expand Up @@ -45,6 +46,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import com.lorenzovainigli.foodexpirationdates.R
import com.lorenzovainigli.foodexpirationdates.model.repository.PreferencesRepository
import com.lorenzovainigli.foodexpirationdates.ui.theme.FoodExpirationDatesTheme
import com.lorenzovainigli.foodexpirationdates.view.activity.UISettingsActivity
import com.lorenzovainigli.foodexpirationdates.view.composable.DateFormatDialog
import com.lorenzovainigli.foodexpirationdates.view.composable.MyTopAppBar
import com.lorenzovainigli.foodexpirationdates.view.composable.NotificationTimeBottomSheet
Expand Down Expand Up @@ -233,6 +235,19 @@ fun SettingsActivityLayout(
}
)
}
SettingsItem(
label = stringResource(R.string.ui_settings)
){
OutlinedButton(
onClick = {
val intent = Intent(context, UISettingsActivity::class.java)
context.startActivity(intent)
}
) {
Text(text = stringResource(R.string.custom_ui))
}
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package com.lorenzovainigli.foodexpirationdates.view.composable.activity

import android.app.Activity
import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.ArrowBack
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.lorenzovainigli.foodexpirationdates.R
import com.lorenzovainigli.foodexpirationdates.model.repository.PreferencesRepository
import com.lorenzovainigli.foodexpirationdates.ui.theme.FoodExpirationDatesTheme
import com.lorenzovainigli.foodexpirationdates.view.composable.MyTopAppBar
import com.lorenzovainigli.foodexpirationdates.view.composable.SettingsItem
import com.lorenzovainigli.foodexpirationdates.view.preview.DefaultPreviews
import com.lorenzovainigli.foodexpirationdates.view.preview.LanguagePreviews
import com.lorenzovainigli.foodexpirationdates.viewmodel.PreferencesViewModel

@OptIn(ExperimentalMaterial3Api::class)
@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun UISettingsActivityLayout(
context: Context = LocalContext.current,
prefsViewModel: PreferencesViewModel? = viewModel()
) {
val activity = (context as? Activity)
val topBarFontState = prefsViewModel?.getTopBarFont(context)?.collectAsState()?.value
?: PreferencesRepository.Companion.TopBarFont.NORMAL.ordinal
val darkThemeState = prefsViewModel?.getThemeMode(context)?.collectAsState()?.value
?: PreferencesRepository.Companion.ThemeMode.SYSTEM
val dynamicColorsState = prefsViewModel?.getDynamicColors(context)?.collectAsState()?.value
?: false

val isInDarkTheme = when (darkThemeState) {
PreferencesRepository.Companion.ThemeMode.LIGHT.ordinal -> false
PreferencesRepository.Companion.ThemeMode.DARK.ordinal -> true
else -> isSystemInDarkTheme()
}

FoodExpirationDatesTheme(
darkTheme = isInDarkTheme,
dynamicColor = dynamicColorsState
){
val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior()
Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
MyTopAppBar(
title = stringResource(id = R.string.ui_settings),
navigationIcon = {
IconButton(onClick = { activity?.finish() }) {
Icon(
imageVector = Icons.Outlined.ArrowBack,
contentDescription = stringResource(id = R.string.back),
tint = MaterialTheme.colorScheme.primary
)
}
},
scrollBehavior = scrollBehavior
)
}
) { padding ->
Column(
modifier = Modifier
.padding(padding)
.padding(10.dp)
.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
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) {
Button(onClick = {}) {
Text(
text = context.getString(topBarFont.label)
)
}
}
if (topBarFont.ordinal != topBarFontState) {
OutlinedButton(
onClick = {
prefsViewModel?.setTopBarFont(context, topBarFont)
},
) {
Text(
text = context.getString(topBarFont.label)
)
}
}
}
}
}
}
}
}

@RequiresApi(Build.VERSION_CODES.O)
@DefaultPreviews
@LanguagePreviews
@Composable
fun UISettingsActivityLayoutPreview() {
UISettingsActivityLayout()
}
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
7 changes: 7 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,13 @@
<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 ="custom_ui">Custom UI</string>
<string name ="top_bar_font_weight">Top Bar Font Weight</string>


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

0 comments on commit 71125ba

Please sign in to comment.