feat: settings
This commit is contained in:
1 parent
6be81093e0
commit
8f65bb05b7
7 files changed
+481
-105
No files matched your search
@@ -0,0 +1,236 @@
|
||||
package de.schanbro.weekfocus.screens
|
||||
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.provider.Settings
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.selection.toggleable
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalHapticFeedback
|
||||
import androidx.compose.ui.platform.LocalUriHandler
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.unit.dp
|
||||
import de.schanbro.weekfocus.R
|
||||
import de.schanbro.weekfocus.settings.SettingsRepository
|
||||
import de.schanbro.weekfocus.settings.ThemeMode
|
||||
import de.schanbro.weekfocus.update.ReleaseInfo
|
||||
import de.schanbro.weekfocus.update.UpdateChecker
|
||||
import de.schanbro.weekfocus.update.UpdateDialog
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
private const val REPO_URL = "https://git.schamagusa.de/robin/WeekFocus"
|
||||
|
||||
private enum class CheckStatus { IDLE, CHECKING, UP_TO_DATE, FAILED, AVAILABLE }
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun SettingsScreen(modifier: Modifier = Modifier) {
|
||||
val context = LocalContext.current
|
||||
val settings = remember { SettingsRepository.get(context) }
|
||||
val haptic = LocalHapticFeedback.current
|
||||
val uriHandler = LocalUriHandler.current
|
||||
val scope = rememberCoroutineScope()
|
||||
|
||||
val currentVersion = remember { UpdateChecker.currentVersion(context) }
|
||||
var status by remember { mutableStateOf(CheckStatus.IDLE) }
|
||||
var availableRelease by remember { mutableStateOf<ReleaseInfo?>(null) }
|
||||
|
||||
fun checkNow() {
|
||||
if (status == CheckStatus.CHECKING) return
|
||||
status = CheckStatus.CHECKING
|
||||
scope.launch {
|
||||
val latest = UpdateChecker.fetchLatestRelease(settings.includePrereleases)
|
||||
status = when {
|
||||
latest == null -> CheckStatus.FAILED
|
||||
UpdateChecker.isNewer(latest.version, currentVersion) -> {
|
||||
availableRelease = latest
|
||||
CheckStatus.AVAILABLE
|
||||
}
|
||||
else -> CheckStatus.UP_TO_DATE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = modifier
|
||||
.fillMaxSize()
|
||||
.verticalScroll(rememberScrollState())
|
||||
) {
|
||||
// ---------- Updates ----------
|
||||
SectionTitle(stringResource(R.string.settings_section_updates))
|
||||
|
||||
SwitchRow(
|
||||
title = stringResource(R.string.settings_auto_update_title),
|
||||
summary = stringResource(R.string.settings_auto_update_summary),
|
||||
checked = settings.autoCheckUpdates,
|
||||
onCheckedChange = {
|
||||
haptic.performHapticFeedback(
|
||||
if (it) HapticFeedbackType.ToggleOn else HapticFeedbackType.ToggleOff
|
||||
)
|
||||
settings.updateAutoCheckUpdates(it)
|
||||
}
|
||||
)
|
||||
|
||||
SwitchRow(
|
||||
title = stringResource(R.string.settings_prerelease_title),
|
||||
summary = stringResource(R.string.settings_prerelease_summary),
|
||||
checked = settings.includePrereleases,
|
||||
onCheckedChange = {
|
||||
haptic.performHapticFeedback(
|
||||
if (it) HapticFeedbackType.ToggleOn else HapticFeedbackType.ToggleOff
|
||||
)
|
||||
settings.updateIncludePrereleases(it)
|
||||
}
|
||||
)
|
||||
|
||||
ClickRow(
|
||||
title = stringResource(R.string.settings_check_now_title),
|
||||
summary = when (status) {
|
||||
CheckStatus.IDLE -> stringResource(R.string.settings_check_idle, currentVersion)
|
||||
CheckStatus.CHECKING -> stringResource(R.string.settings_check_checking)
|
||||
CheckStatus.UP_TO_DATE -> stringResource(R.string.settings_check_up_to_date)
|
||||
CheckStatus.FAILED -> stringResource(R.string.settings_check_failed)
|
||||
CheckStatus.AVAILABLE -> stringResource(R.string.settings_check_available)
|
||||
},
|
||||
onClick = { checkNow() },
|
||||
trailing = {
|
||||
if (status == CheckStatus.CHECKING) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.size(24.dp),
|
||||
strokeWidth = 3.dp
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
HorizontalDivider(Modifier.padding(vertical = 8.dp))
|
||||
|
||||
// ---------- Darstellung ----------
|
||||
SectionTitle(stringResource(R.string.settings_section_appearance))
|
||||
|
||||
ListItem(headlineContent = { Text(stringResource(R.string.settings_theme_title)) })
|
||||
val modes = listOf(
|
||||
ThemeMode.SYSTEM to R.string.settings_theme_system,
|
||||
ThemeMode.LIGHT to R.string.settings_theme_light,
|
||||
ThemeMode.DARK to R.string.settings_theme_dark
|
||||
)
|
||||
SingleChoiceSegmentedButtonRow(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 16.dp)
|
||||
) {
|
||||
modes.forEachIndexed { index, (mode, labelRes) ->
|
||||
SegmentedButton(
|
||||
selected = settings.themeMode == mode,
|
||||
onClick = {
|
||||
if (settings.themeMode != mode) {
|
||||
haptic.performHapticFeedback(HapticFeedbackType.ToggleOn)
|
||||
settings.updateThemeMode(mode)
|
||||
}
|
||||
},
|
||||
shape = SegmentedButtonDefaults.itemShape(index, modes.size)
|
||||
) {
|
||||
Text(stringResource(labelRes))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Pro-App-Sprache gibt es erst ab Android 13
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
ClickRow(
|
||||
title = stringResource(R.string.settings_language_title),
|
||||
summary = stringResource(R.string.settings_language_summary),
|
||||
onClick = {
|
||||
try {
|
||||
context.startActivity(
|
||||
Intent(
|
||||
Settings.ACTION_APP_LOCALE_SETTINGS,
|
||||
Uri.fromParts("package", context.packageName, null)
|
||||
)
|
||||
)
|
||||
} catch (_: Exception) {
|
||||
// Gerät ohne diese Einstellungsseite: nichts tun
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
HorizontalDivider(Modifier.padding(vertical = 8.dp))
|
||||
|
||||
// ---------- Info ----------
|
||||
SectionTitle(stringResource(R.string.settings_section_about))
|
||||
|
||||
ListItem(
|
||||
headlineContent = { Text(stringResource(R.string.settings_version_title)) },
|
||||
supportingContent = { Text(currentVersion) }
|
||||
)
|
||||
|
||||
ClickRow(
|
||||
title = stringResource(R.string.settings_source_title),
|
||||
summary = stringResource(R.string.settings_source_summary),
|
||||
onClick = { uriHandler.openUri(REPO_URL) }
|
||||
)
|
||||
|
||||
Spacer(Modifier.height(16.dp))
|
||||
}
|
||||
|
||||
// Dialog, wenn "Jetzt nach Updates suchen" etwas gefunden hat
|
||||
availableRelease?.let { info ->
|
||||
UpdateDialog(info = info, onDismiss = { availableRelease = null })
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun SectionTitle(text: String) {
|
||||
Text(
|
||||
text = text,
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
modifier = Modifier.padding(start = 16.dp, end = 16.dp, top = 16.dp, bottom = 4.dp)
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun SwitchRow(
|
||||
title: String,
|
||||
summary: String,
|
||||
checked: Boolean,
|
||||
onCheckedChange: (Boolean) -> Unit
|
||||
) {
|
||||
ListItem(
|
||||
headlineContent = { Text(title) },
|
||||
supportingContent = { Text(summary) },
|
||||
trailingContent = { Switch(checked = checked, onCheckedChange = null) },
|
||||
// Die ganze Zeile ist klickbar, der Switch selbst nur Anzeige (gut für TalkBack)
|
||||
modifier = Modifier.toggleable(
|
||||
value = checked,
|
||||
role = Role.Switch,
|
||||
onValueChange = onCheckedChange
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun ClickRow(
|
||||
title: String,
|
||||
summary: String,
|
||||
onClick: () -> Unit,
|
||||
trailing: (@Composable () -> Unit)? = null
|
||||
) {
|
||||
ListItem(
|
||||
headlineContent = { Text(title) },
|
||||
supportingContent = { Text(summary) },
|
||||
trailingContent = trailing,
|
||||
modifier = Modifier.clickable(onClick = onClick)
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user