forked from Schanbro/WeekFocus
192 lines
7.3 KiB
Kotlin
192 lines
7.3 KiB
Kotlin
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
|
|
import de.schanbro.weekfocus.components.SectionTitle
|
|
import de.schanbro.weekfocus.components.ClickRow
|
|
import de.schanbro.weekfocus.components.SwitchRow
|
|
|
|
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,
|
|
onOpenAbout: () -> Unit
|
|
) {
|
|
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))
|
|
|
|
ClickRow(
|
|
title = stringResource(R.string.title_about),
|
|
summary = stringResource(R.string.settings_about_summary),
|
|
onClick = onOpenAbout
|
|
)
|
|
|
|
Spacer(Modifier.height(16.dp))
|
|
}
|
|
|
|
// Dialog, wenn "Jetzt nach Updates suchen" etwas gefunden hat
|
|
availableRelease?.let { info ->
|
|
UpdateDialog(info = info, onDismiss = { availableRelease = null })
|
|
}
|
|
} |