Added central top app bar with more vertical setting icon, added basic support for toggleable dynamic theme and dark mode in Theme.kt

This commit is contained in:
Anthony La committed 2023-06-01 14:03:19 -07:00
1 parent d2530210ab
commit 0ef127a3b7
8 files changed
+141 -36

No files matched your search

@@ -4,6 +4,10 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.dp
import com.anthonyla.livewallpaper.data.settings.SettingsViewModel
import com.anthonyla.livewallpaper.themes.LiveWallpaperTheme
import com.anthonyla.livewallpaper.ui.LiveWallpaperApp
@@ -16,9 +20,27 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
LiveWallpaperTheme {
LiveWallpaperApp(settingsViewModel)
LiveWallpaperTheme (isDarkMode(), isDynamicTheming()) {
Surface(elevation = 5.dp) {
LiveWallpaperApp(settingsViewModel)
}
}
}
}
}
@Composable
private fun isDarkMode(): Boolean {
return when (settingsViewModel.getDarkMode()) {
true -> true
false -> false
null -> isSystemInDarkTheme()
}
}
private fun isDynamicTheming(): Boolean {
return when (settingsViewModel.getDynamicTheme()) {
true, null -> true
false -> false
}
}
}
@@ -17,4 +17,5 @@ sealed class BottomNavScreens(val route: String, @StringRes val resourceId: Int,
object Wallpaper : BottomNavScreens("wallpaper", R.string.wallpaper, Icons.Filled.Home)
object Library : BottomNavScreens("library", R.string.library, Icons.Filled.List)
object Configure : BottomNavScreens("configure", R.string.configure, Icons.Filled.Settings)
}
@@ -7,6 +7,8 @@ import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarDefaults
import androidx.compose.material3.NavigationBarDefaults.containerColor
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.NavigationDrawerItemDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.contentColorFor
import androidx.compose.runtime.Composable
@@ -27,7 +29,8 @@ import androidx.navigation.compose.currentBackStackEntryAsState
@Composable
fun BottomNavigationBar(
navController: NavController,
screens: List<BottomNavScreens>
screens: List<BottomNavScreens>,
modifier: Modifier = Modifier
) {
NavigationBar(
containerColor = containerColor,
@@ -40,11 +43,12 @@ fun BottomNavigationBar(
val currentDestination = navBackStackEntry?.destination
screens.forEach { screen ->
NavigationBarItem(
colors = NavigationBarItemDefaults.colors(),
selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
label = {
Text(
text = stringResource(id = screen.resourceId),
fontWeight = FontWeight.SemiBold
fontWeight = FontWeight.SemiBold,
) },
icon = {
Icon(screen.icon,
@@ -0,0 +1,41 @@
package com.anthonyla.livewallpaper.navigation
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.TopAppBarDefaults.windowInsets
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavController
import com.anthonyla.livewallpaper.R
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TopBar (
navController: NavController,
modifier: Modifier = Modifier
) {
CenterAlignedTopAppBar(
title = {},
navigationIcon = {},
windowInsets = windowInsets,
colors = TopAppBarDefaults.centerAlignedTopAppBarColors(),
scrollBehavior = null,
actions = {
IconButton(onClick = { /* doSomething() */ }) {
Icon(
imageVector = Icons.Filled.MoreVert,
contentDescription = stringResource(R.string.MoreVertIcon)
)
}
}
)
}
@@ -1,10 +1,17 @@
package com.anthonyla.livewallpaper.themes
import androidx.compose.foundation.isSystemInDarkTheme
import android.os.Build
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Typography
import androidx.compose.material3.lightColorScheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
private val LightColors = lightColorScheme(
@@ -72,19 +79,57 @@ private val DarkColors = darkColorScheme(
scrim = md_theme_dark_scrim,
)
val typography = Typography(
headlineSmall = TextStyle(
fontWeight = FontWeight.SemiBold,
fontSize = 24.sp,
lineHeight = 32.sp,
letterSpacing = 0.sp
),
titleLarge = TextStyle(
fontWeight = FontWeight.Normal,
fontSize = 18.sp,
lineHeight = 28.sp,
letterSpacing = 0.sp
),
bodyLarge = TextStyle(
fontWeight = FontWeight.Normal,
fontSize = 16.sp,
lineHeight = 24.sp,
letterSpacing = 0.15.sp
),
bodyMedium = TextStyle(
fontWeight = FontWeight.Medium,
fontSize = 14.sp,
lineHeight = 20.sp,
letterSpacing = 0.25.sp
),
labelMedium = TextStyle(
fontWeight = FontWeight.SemiBold,
fontSize = 12.sp,
lineHeight = 16.sp,
letterSpacing = 0.5.sp
)
)
@Composable
fun LiveWallpaperTheme(
useDarkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable() () -> Unit
) {
val colors = if (!useDarkTheme) {
LightColors
} else {
DarkColors
}
MaterialTheme(
colorScheme = colors,
content = content
)
useDarkTheme: Boolean,
useDynamicTheme: Boolean,
content: @Composable () -> Unit
) { // Check if dynamic theming is enabled in settings
val context = LocalContext.current
val colors = when {((Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)) && useDynamicTheme -> {
if (useDarkTheme) dynamicDarkColorScheme(context)
else dynamicLightColorScheme(context)
}
useDarkTheme -> DarkColors
else -> LightColors
}
MaterialTheme(
colorScheme = colors,
typography = typography,
content = content
)
}
@@ -18,27 +18,31 @@ import androidx.navigation.compose.rememberNavController
import com.anthonyla.livewallpaper.data.settings.SettingsViewModel
import com.anthonyla.livewallpaper.navigation.BottomNavScreens
import com.anthonyla.livewallpaper.navigation.BottomNavigationBar
import com.anthonyla.livewallpaper.navigation.TopBar
import com.anthonyla.livewallpaper.navigation.bottomNav
import com.google.accompanist.systemuicontroller.rememberSystemUiController
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LiveWallpaperApp(settingsViewModel: SettingsViewModel, modifier: Modifier = Modifier) {
val navController = rememberNavController()
SetTransparentSystemBars()
// Bottom navigation
val navController = rememberNavController()
val bottomNavOptions = listOf(
BottomNavScreens.Wallpaper,
BottomNavScreens.Library,
BottomNavScreens.Configure
)
Scaffold (
topBar = {
TopBar(navController = navController)
},
bottomBar = {
BottomNavigationBar(navController = navController, screens = bottomNavOptions)
},
) {
innerPadding -> NavHost (
) { innerPadding -> NavHost (
navController,
startDestination = "bottomNavigation",
Modifier.padding(innerPadding)
+1
View File
@@ -3,4 +3,5 @@
<string name="library">Library</string>
<string name="configure">Configure</string>
<string name="wallpaper">Wallpaper</string>
<string name="MoreVertIcon">Options</string>
</resources>