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:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.WeatherApp"> android:theme="@style/Theme.WeatherApp">
<activity <activity
android:name=".presentation.QWeatherActivity" android:name=".presentation.QWeatherActivity"
android:exported="true"> android:exported="true">

View file

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

View file

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

View file

@ -2,14 +2,15 @@ package com.henryhiles.qweather.presentation.screenmodel
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.res.Resources
import android.os.Build import android.os.Build
import androidx.annotation.StringRes
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.core.content.edit import androidx.core.content.edit
import cafe.adriel.voyager.core.model.ScreenModel import cafe.adriel.voyager.core.model.ScreenModel
import com.henryhiles.qweather.R
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
abstract class BasePreferenceManager( abstract class BasePreferenceManager(
@ -120,11 +121,16 @@ abstract class BasePreferenceManager(
class PreferenceManager(context: Context) : class PreferenceManager(context: Context) :
BasePreferenceManager(context.getSharedPreferences("prefs", Context.MODE_PRIVATE)) { BasePreferenceManager(context.getSharedPreferences("prefs", Context.MODE_PRIVATE)) {
var theme by enumPreference("theme", Theme.SYSTEM)
var theme by enumPreference("theme", Resources.Theme.SYSTEM)
var monet by booleanPreference("monet", Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) 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( class AppearanceSettingsScreenModel(
val prefs: PreferenceManager val prefs: PreferenceManager
) : ScreenModel ) : 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.Tab
import cafe.adriel.voyager.navigator.tab.TabOptions import cafe.adriel.voyager.navigator.tab.TabOptions
import com.henryhiles.qweather.R 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.components.settings.SettingsCategory
import com.henryhiles.qweather.presentation.screen.AppearanceSettingsScreen import com.henryhiles.qweather.presentation.screen.AppearanceSettingsScreen
@ -39,7 +39,11 @@ object SettingsTab : Tab {
@Composable @Composable
override fun Content() { override fun Content() {
Scaffold( Scaffold(
topBar = { Toolbar() }, topBar = {
SmallToolbar(
title = stringResource(R.string.tab_settings),
)
},
) { ) {
Column( Column(
modifier = Modifier 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 @Composable
fun WeatherAppTheme( fun WeatherAppTheme(
darkTheme: Boolean = isSystemInDarkTheme(), darkTheme: Boolean = isSystemInDarkTheme(),
monet: Boolean,
content: @Composable () -> Unit content: @Composable () -> Unit
) { ) {
val colorScheme = when { val colorScheme = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { monet && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
val context = LocalContext.current val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
} }