working now, just need to re-arrange for clean code

This commit is contained in:
Henry Hiles 2023-03-31 10:55:59 -04:00
parent 211421b3e1
commit d221968137
8 changed files with 50 additions and 19 deletions

View file

@ -13,6 +13,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.WeatherApp">
<activity
android:name=".presentation.QWeatherActivity"
android:exported="true">

View file

@ -37,10 +37,6 @@ val appModule = module {
singleOf(::provideWeatherApi)
singleOf(::PreferenceManager)
factoryOf(::AppearanceSettingsScreenModel)
// single {
// LocationServices.getFusedLocationProviderClient(get<Application>())
// }
factoryOf(::WeatherScreenModel)
// factory { WeatherScreenModel(get(), get()) }
}

View file

@ -4,18 +4,30 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.Surface
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.transitions.SlideTransition
import com.henryhiles.qweather.presentation.screen.MainScreen
import com.henryhiles.qweather.presentation.screenmodel.PreferenceManager
import com.henryhiles.qweather.presentation.screenmodel.Theme
import com.henryhiles.qweather.presentation.ui.theme.WeatherAppTheme
import org.koin.android.ext.android.inject
class QWeatherActivity : ComponentActivity() {
private val prefs: PreferenceManager by inject()
@OptIn(ExperimentalAnimationApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
WeatherAppTheme {
val isDark = when (prefs.theme) {
Theme.SYSTEM -> isSystemInDarkTheme()
Theme.LIGHT -> false
Theme.DARK -> true
}
WeatherAppTheme(darkTheme = isDark, monet = prefs.monet) {
Surface {
Navigator(screen = MainScreen()) {
SlideTransition(it)

View file

@ -0,0 +1,18 @@
package com.henryhiles.qweather.presentation.components
import androidx.compose.foundation.layout.RowScope
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
@Composable
@OptIn(ExperimentalMaterial3Api::class)
fun SmallToolbar(
title: String,
actions: @Composable RowScope.() -> Unit = {},
) {
TopAppBar(
title = { Text(text = title) },
navigationIcon = { BackButton() },
actions = actions,
)
}

View file

@ -18,7 +18,7 @@ fun SettingsCategory(
destination: (() -> Screen)? = null
) {
val screen = destination?.invoke()
val nav = LocalNavigator.current
val nav = LocalNavigator.current?.parent
Box(
modifier = Modifier

View file

@ -2,14 +2,15 @@ package com.henryhiles.qweather.presentation.screenmodel
import android.content.Context
import android.content.SharedPreferences
import android.content.res.Resources
import android.os.Build
import androidx.annotation.StringRes
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.core.content.edit
import cafe.adriel.voyager.core.model.ScreenModel
import com.henryhiles.qweather.R
import kotlin.reflect.KProperty
abstract class BasePreferenceManager(
@ -120,11 +121,16 @@ abstract class BasePreferenceManager(
class PreferenceManager(context: Context) :
BasePreferenceManager(context.getSharedPreferences("prefs", Context.MODE_PRIVATE)) {
var theme by enumPreference("theme", Resources.Theme.SYSTEM)
var theme by enumPreference("theme", Theme.SYSTEM)
var monet by booleanPreference("monet", Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
}
enum class Theme(@StringRes val label: Int) {
SYSTEM(R.string.theme_system),
LIGHT(R.string.theme_light),
DARK(R.string.theme_dark);
}
class AppearanceSettingsScreenModel(
val prefs: PreferenceManager
) : ScreenModel

View file

@ -16,7 +16,7 @@ import androidx.compose.ui.res.stringResource
import cafe.adriel.voyager.navigator.tab.Tab
import cafe.adriel.voyager.navigator.tab.TabOptions
import com.henryhiles.qweather.R
import com.henryhiles.qweather.presentation.components.LargeToolbar
import com.henryhiles.qweather.presentation.components.SmallToolbar
import com.henryhiles.qweather.presentation.components.settings.SettingsCategory
import com.henryhiles.qweather.presentation.screen.AppearanceSettingsScreen
@ -39,7 +39,11 @@ object SettingsTab : Tab {
@Composable
override fun Content() {
Scaffold(
topBar = { Toolbar() },
topBar = {
SmallToolbar(
title = stringResource(R.string.tab_settings),
)
},
) {
Column(
modifier = Modifier
@ -56,11 +60,4 @@ object SettingsTab : Tab {
}
}
}
@Composable
private fun Toolbar() {
LargeToolbar(
title = stringResource(R.string.tab_settings),
)
}
}

View file

@ -35,10 +35,11 @@ private val LightColorScheme = lightColorScheme(
@Composable
fun WeatherAppTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
monet: Boolean,
content: @Composable () -> Unit
) {
val colorScheme = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
monet && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
}