Skip to content

Commit

Permalink
Modification after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
tevincent committed Aug 27, 2024
1 parent 635bc01 commit 3801e50
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fun MainNavHost(
)
}
composable<SettingsDestination> {
SettingsScreenWrapper(navController, windowAdaptiveInfo, isBarNavigation)
SettingsScreenWrapper(windowAdaptiveInfo)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.res.stringResource
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings.AppSettings
import com.infomaniak.multiplatform_swisstransfer.common.models.Theme
import com.infomaniak.multiplatform_swisstransfer.database.models.setting.Language
import com.infomaniak.swisstransfer.BuildConfig
import com.infomaniak.swisstransfer.R
import com.infomaniak.swisstransfer.ui.components.BrandTobAppBar
Expand All @@ -47,45 +49,12 @@ import com.infomaniak.swisstransfer.ui.theme.Margin
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import com.infomaniak.swisstransfer.ui.utils.PreviewMobile

private fun openUrl(context: Context, url: String) {
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
}

private fun goToPlayStore(context: Context) {
try {
context.startActivity(
Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=${context.packageName}"))
)
} catch (_: ActivityNotFoundException) {
context.startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=${context.packageName}")
)
)
}
}

fun openAppNotificationSettings(context: Context) {
val packageName = context.packageName
val appUid = context.applicationInfo.uid
Intent().apply {
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
}
else -> {
action = "Settings.ACTION_APP_NOTIFICATION_SETTINGS"
putExtra("app_package", packageName)
putExtra("app_uid", appUid)
}
}
}.also { context.startActivity(it) }
}

@Composable
fun SettingsScreen(onItemClick: (SettingsOptionScreens) -> Unit, getSelectedSetting: () -> SettingsOptionScreens?) {
fun SettingsScreen(
onItemClick: (SettingsOptionScreens) -> Unit,
getSelectedSetting: () -> SettingsOptionScreens?,
appSettings: AppSettings? = null
) {
val selectedSetting = getSelectedSetting()

Scaffold(topBar = { BrandTobAppBar() }) { paddingsValue ->
Expand All @@ -106,7 +75,7 @@ fun SettingsScreen(onItemClick: (SettingsOptionScreens) -> Unit, getSelectedSett
titleRes = R.string.settingsOptionTheme,
isSelected = { selectedSetting == THEME },
icon = AppIcons.PaintbrushPalette,
description = "TODO",
description = (appSettings?.theme ?: Theme.SYSTEM).getString(),
CHEVRON
) {
onItemClick(THEME)
Expand Down Expand Up @@ -173,6 +142,31 @@ fun SettingsScreen(onItemClick: (SettingsOptionScreens) -> Unit, getSelectedSett
}
}

@Composable
private fun Theme.getString(): String {
return when (this) {
Theme.SYSTEM -> stringResource(R.string.settingsOptionThemeSystem)
Theme.DARK -> stringResource(R.string.settingsOptionThemeDark)
Theme.LIGHT -> stringResource(R.string.settingsOptionThemeLight)
}
}

@Composable
private fun getValidityPeriodString(validityPeriod: Int): String {
return pluralStringResource(R.plurals.settingsValidityPeriodValue, validityPeriod, validityPeriod)
}

@Composable
private fun Language.getEmailLanguageString(): String {
return when (this) {
Language.ENGLISH -> stringResource(R.string.settingsEmailLanguageValueEnglish)
Language.FRENCH -> stringResource(R.string.settingsEmailLanguageValueFrench)
Language.GERMAN -> stringResource(R.string.settingsEmailLanguageValueGerman)
Language.ITALIAN -> stringResource(R.string.settingsEmailLanguageValueItalian)
Language.SPANISH -> stringResource(R.string.settingsEmailLanguageValueSpanish)
}
}

enum class SettingsOptionScreens {
THEME, NOTIFICATIONS,
VALIDITY_PERIOD, DOWNLOAD_LIMIT, EMAIL_LANGUAGE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings.AppSettings
import com.infomaniak.multiplatform_swisstransfer.common.models.Theme
import com.infomaniak.swisstransfer.R
import com.infomaniak.swisstransfer.extensions.goToPlayStore
import com.infomaniak.swisstransfer.extensions.openAppNotificationSettings
Expand All @@ -49,17 +53,21 @@ import com.infomaniak.swisstransfer.ui.utils.PreviewTablet

@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
fun SettingsScreenWrapper(windowAdaptiveInfo: WindowAdaptiveInfo = currentWindowAdaptiveInfo()) {
fun SettingsScreenWrapper(
windowAdaptiveInfo: WindowAdaptiveInfo = currentWindowAdaptiveInfo(),
settingsViewModel: SettingsViewModel = hiltViewModel<SettingsViewModel>(),
) {
val appSettings by settingsViewModel.appSettingsFlow.collectAsStateWithLifecycle(null)
TwoPaneScaffold<SettingsOptionScreens>(
windowAdaptiveInfo,
listPane = { ListPane(this) },
detailPane = { DetailPane(this) }
listPane = { ListPane(this, appSettings) },
detailPane = { DetailPane(settingsViewModel, this, appSettings) }
)
}

@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
private fun ListPane(navigator: ThreePaneScaffoldNavigator<SettingsOptionScreens>) {
private fun ListPane(navigator: ThreePaneScaffoldNavigator<SettingsOptionScreens>, appSettings: AppSettings?) {
val context = LocalContext.current
val aboutURL = stringResource(R.string.urlAbout)
val userReportURL = stringResource(R.string.urlUserReportAndroid)
Expand All @@ -78,12 +86,17 @@ private fun ListPane(navigator: ThreePaneScaffoldNavigator<SettingsOptionScreens
}
},
getSelectedSetting = { navigator.currentDestination?.content },
appSettings
)
}

@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
private fun DetailPane(navigator: ThreePaneScaffoldNavigator<SettingsOptionScreens>) {
private fun DetailPane(
settingsViewModel: SettingsViewModel,
navigator: ThreePaneScaffoldNavigator<SettingsOptionScreens>,
appSettings: AppSettings?
) {
var lastSelectedScreen by rememberSaveable { mutableStateOf<SettingsOptionScreens?>(null) }

val destination = navigator.currentDestination?.content ?: lastSelectedScreen
Expand All @@ -93,7 +106,9 @@ private fun DetailPane(navigator: ThreePaneScaffoldNavigator<SettingsOptionScree
val navigateBack: (() -> Unit)? = if (navigator.canNavigateBack()) navigateBackCallback else null

when (destination) {
THEME -> SettingsThemeScreen(navigateBack)
THEME -> SettingsThemeScreen(appSettings?.theme ?: Theme.SYSTEM, navigateBack) {
settingsViewModel.setTheme(it)
}
VALIDITY_PERIOD -> SettingsValidityPeriodScreen(navigateBack)
DOWNLOAD_LIMIT -> SettingsDownloadsLimitScreen(navigateBack)
EMAIL_LANGUAGE -> SettingsEmailLanguageScreen(navigateBack)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import com.infomaniak.swisstransfer.ui.utils.PreviewMobile

@Composable
fun SettingsThemeScreen(navigateBack: (() -> Unit)?) {
fun SettingsThemeScreen(theme: Theme, navigateBack: (() -> Unit)?, onThemeUpdate: (Theme) -> Unit) {
Scaffold(topBar = {
val canDisplayBackButton = navigateBack?.let { TopAppBarButton.backButton(navigateBack) }
SwissTransferTobAppBar(R.string.settingsOptionTheme, navigationMenu = canDisplayBackButton)
Expand All @@ -59,8 +59,12 @@ fun SettingsThemeScreen(navigateBack: (() -> Unit)?) {
) {
SettingTitle(titleRes = R.string.settingsThemeTitle)

var selectedItem by rememberSaveable { mutableIntStateOf(0) } // TODO: Use DataStore or Realm
SingleSelectOptions(ThemeOption.entries, { selectedItem }, { selectedItem = it })
var selectedItem by rememberSaveable { mutableIntStateOf(theme.indexOf()) }
SingleSelectOptions(ThemeOption.entries, { selectedItem }, {
selectedItem = it
val selectedTheme = Theme.entries[it]
onThemeUpdate(selectedTheme)
})
}
}
}
Expand Down

0 comments on commit 3801e50

Please sign in to comment.