From b4d5ea1885f67fd7df6c81be10b8c8bc6e9c4211 Mon Sep 17 00:00:00 2001 From: Vincent TE Date: Thu, 26 Sep 2024 14:32:44 +0200 Subject: [PATCH] Code review --- .../swisstransfer/ui/MainActivity.kt | 14 +++++----- .../ui/screen/main/settings/SettingsScreen.kt | 1 + .../main/settings/SettingsScreenWrapper.kt | 10 +------ .../swisstransfer/ui/utils/GetSetCallbacks.kt | 26 +++++++++++++++++++ 4 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/com/infomaniak/swisstransfer/ui/utils/GetSetCallbacks.kt diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/MainActivity.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/MainActivity.kt index 41a7cf229..e05e1ba08 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/MainActivity.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/MainActivity.kt @@ -21,11 +21,10 @@ import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge +import androidx.activity.viewModels import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.saveable.rememberSaveable import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.infomaniak.multiplatform_swisstransfer.common.models.Theme @@ -37,11 +36,14 @@ import dagger.hilt.android.AndroidEntryPoint @AndroidEntryPoint class MainActivity : ComponentActivity() { + private val settingsViewModel: SettingsViewModel by viewModels() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { - SwissTransferTheme(isDarkTheme = isDarkTheme()) { + val appSettings by settingsViewModel.appSettingsFlow.collectAsStateWithLifecycle(null) + SwissTransferTheme(isDarkTheme = isDarkTheme(getTheme = { appSettings?.theme })) { MainScreen() } } @@ -49,11 +51,11 @@ class MainActivity : ComponentActivity() { } @Composable -fun isDarkTheme(): Boolean { +fun isDarkTheme(getTheme: () -> Theme?): Boolean { val settingsViewModel = hiltViewModel() val appSettings by settingsViewModel.appSettingsFlow.collectAsStateWithLifecycle(null) - return appSettings?.let { - if (it.theme == Theme.SYSTEM) isSystemInDarkTheme() else it.theme == Theme.DARK + return getTheme()?.let { + if (it == Theme.SYSTEM) isSystemInDarkTheme() else it== Theme.DARK } ?: isSystemInDarkTheme() } diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/settings/SettingsScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/settings/SettingsScreen.kt index df06f4d42..ee21aa194 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/settings/SettingsScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/settings/SettingsScreen.kt @@ -46,6 +46,7 @@ import com.infomaniak.swisstransfer.ui.screen.main.settings.components.SettingIt import com.infomaniak.swisstransfer.ui.screen.main.settings.components.SettingTitle import com.infomaniak.swisstransfer.ui.theme.Margin import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme +import com.infomaniak.swisstransfer.ui.utils.GetSetCallbacks import com.infomaniak.swisstransfer.ui.utils.PreviewSmallWindow @Composable diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/settings/SettingsScreenWrapper.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/settings/SettingsScreenWrapper.kt index c80be976d..395d59c36 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/settings/SettingsScreenWrapper.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/settings/SettingsScreenWrapper.kt @@ -60,32 +60,24 @@ fun SettingsScreenWrapper( val emailLanguage = GetSetCallbacks(get = { safeAppSettings.emailLanguage }, set = { settingsViewModel.setEmailLanguage(it) }) - SettingsScreenWrapper(windowAdaptiveInfo, theme, validityPeriod, downloadLimit, emailLanguage) + SettingsScreenWrapper(theme, validityPeriod, downloadLimit, emailLanguage) } } @OptIn(ExperimentalMaterial3AdaptiveApi::class) @Composable fun SettingsScreenWrapper( - windowAdaptiveInfo: WindowAdaptiveInfo = currentWindowAdaptiveInfo(), theme: GetSetCallbacks, validityPeriod: GetSetCallbacks, downloadLimit: GetSetCallbacks, emailLanguage: GetSetCallbacks, ) { TwoPaneScaffold( - windowAdaptiveInfo = windowAdaptiveInfo, listPane = { ListPane(navigator = this, theme, validityPeriod, downloadLimit, emailLanguage) }, detailPane = { DetailPane(navigator = this, theme, validityPeriod, downloadLimit, emailLanguage) }, ) } -@Immutable -class GetSetCallbacks( - val get: () -> T, - val set: (T) -> Unit, -) - @OptIn(ExperimentalMaterial3AdaptiveApi::class) @Composable private fun ListPane( diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/GetSetCallbacks.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/GetSetCallbacks.kt new file mode 100644 index 000000000..28ed3b914 --- /dev/null +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/GetSetCallbacks.kt @@ -0,0 +1,26 @@ +/* + * Infomaniak SwissTransfer - Android + * Copyright (C) 2024 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.infomaniak.swisstransfer.ui.utils + +import androidx.compose.runtime.Immutable + +@Immutable +class GetSetCallbacks( + val get: () -> T, + val set: (T) -> Unit, +)