Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
tevincent committed Sep 26, 2024
1 parent a9ce7c7 commit b4d5ea1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,23 +36,26 @@ 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()
}
}
}
}

@Composable
fun isDarkTheme(): Boolean {
fun isDarkTheme(getTheme: () -> Theme?): Boolean {
val settingsViewModel = hiltViewModel<SettingsViewModel>()
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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Theme>,
validityPeriod: GetSetCallbacks<ValidityPeriod>,
downloadLimit: GetSetCallbacks<DownloadLimit>,
emailLanguage: GetSetCallbacks<EmailLanguage>,
) {
TwoPaneScaffold<SettingsOptionScreens>(
windowAdaptiveInfo = windowAdaptiveInfo,
listPane = { ListPane(navigator = this, theme, validityPeriod, downloadLimit, emailLanguage) },
detailPane = { DetailPane(navigator = this, theme, validityPeriod, downloadLimit, emailLanguage) },
)
}

@Immutable
class GetSetCallbacks<T>(
val get: () -> T,
val set: (T) -> Unit,
)

@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
private fun ListPane(
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.swisstransfer.ui.utils

import androidx.compose.runtime.Immutable

@Immutable
class GetSetCallbacks<T>(
val get: () -> T,
val set: (T) -> Unit,
)

0 comments on commit b4d5ea1

Please sign in to comment.