feat: settings
This commit is contained in:
1 parent
6be81093e0
commit
8f65bb05b7
7 files changed
+481
-105
No files matched your search
@@ -12,6 +12,7 @@ import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import de.schanbro.weekfocus.R
|
||||
import de.schanbro.weekfocus.settings.SettingsRepository
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -21,21 +22,20 @@ private const val CHECK_INTERVAL_MS = 24L * 60 * 60 * 1000
|
||||
@Composable
|
||||
fun UpdateHandler() {
|
||||
val context = LocalContext.current
|
||||
val scope = rememberCoroutineScope()
|
||||
val settings = remember { SettingsRepository.get(context) }
|
||||
val prefs = remember {
|
||||
context.getSharedPreferences("WeekFocusPrefs", Context.MODE_PRIVATE)
|
||||
}
|
||||
|
||||
var release by remember { mutableStateOf<ReleaseInfo?>(null) }
|
||||
var progress by remember { mutableStateOf<Float?>(null) } // null = kein Download
|
||||
var downloadJob by remember { mutableStateOf<Job?>(null) }
|
||||
|
||||
// Einmal pro Start, höchstens alle 24 Stunden
|
||||
LaunchedEffect(Unit) {
|
||||
if (!settings.autoCheckUpdates) return@LaunchedEffect
|
||||
|
||||
val now = System.currentTimeMillis()
|
||||
if (now - prefs.getLong("last_update_check", 0L) < CHECK_INTERVAL_MS) return@LaunchedEffect
|
||||
|
||||
val latest = UpdateChecker.fetchLatestRelease() ?: return@LaunchedEffect
|
||||
val latest = UpdateChecker.fetchLatestRelease(settings.includePrereleases)
|
||||
?: return@LaunchedEffect
|
||||
prefs.edit().putLong("last_update_check", now).apply()
|
||||
|
||||
if (UpdateChecker.isNewer(latest.version, UpdateChecker.currentVersion(context))) {
|
||||
@@ -43,79 +43,86 @@ fun UpdateHandler() {
|
||||
}
|
||||
}
|
||||
|
||||
release?.let { info ->
|
||||
val downloading = progress != null
|
||||
release?.let { UpdateDialog(info = it, onDismiss = { release = null }) }
|
||||
}
|
||||
|
||||
AlertDialog(
|
||||
onDismissRequest = { if (!downloading) release = null },
|
||||
title = { Text(stringResource(R.string.update_title)) },
|
||||
text = {
|
||||
Column(Modifier.verticalScroll(rememberScrollState())) {
|
||||
Text(stringResource(R.string.update_message, info.version.removePrefix("v")))
|
||||
if (info.notes.isNotBlank()) {
|
||||
Spacer(Modifier.height(12.dp))
|
||||
Text(
|
||||
text = info.notes,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
progress?.let { p ->
|
||||
Spacer(Modifier.height(16.dp))
|
||||
LinearProgressIndicator(
|
||||
progress = { p },
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(
|
||||
enabled = !downloading,
|
||||
onClick = {
|
||||
if (!ApkInstaller.canInstall(context)) {
|
||||
Toast.makeText(
|
||||
context, R.string.update_permission_hint, Toast.LENGTH_LONG
|
||||
).show()
|
||||
ApkInstaller.openInstallPermissionSettings(context)
|
||||
} else {
|
||||
downloadJob = scope.launch {
|
||||
progress = 0f
|
||||
try {
|
||||
val apk = ApkInstaller.download(context, info.apkUrl) {
|
||||
progress = it
|
||||
}
|
||||
progress = null
|
||||
release = null
|
||||
ApkInstaller.install(context, apk)
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
progress = null
|
||||
Toast.makeText(
|
||||
context, R.string.update_failed, Toast.LENGTH_LONG
|
||||
).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
) { Text(stringResource(R.string.update_action)) }
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(
|
||||
onClick = {
|
||||
downloadJob?.cancel()
|
||||
progress = null
|
||||
release = null
|
||||
}
|
||||
) {
|
||||
@Composable
|
||||
fun UpdateDialog(info: ReleaseInfo, onDismiss: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val scope = rememberCoroutineScope()
|
||||
var progress by remember { mutableStateOf<Float?>(null) } // null = kein Download
|
||||
var downloadJob by remember { mutableStateOf<Job?>(null) }
|
||||
val downloading = progress != null
|
||||
|
||||
AlertDialog(
|
||||
onDismissRequest = { if (!downloading) onDismiss() },
|
||||
title = { Text(stringResource(R.string.update_title)) },
|
||||
text = {
|
||||
Column(Modifier.verticalScroll(rememberScrollState())) {
|
||||
Text(stringResource(R.string.update_message, info.version.removePrefix("v")))
|
||||
if (info.notes.isNotBlank()) {
|
||||
Spacer(Modifier.height(12.dp))
|
||||
Text(
|
||||
stringResource(
|
||||
if (downloading) R.string.action_cancel else R.string.update_later
|
||||
)
|
||||
text = info.notes,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
progress?.let { p ->
|
||||
Spacer(Modifier.height(16.dp))
|
||||
LinearProgressIndicator(
|
||||
progress = { p },
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(
|
||||
enabled = !downloading,
|
||||
onClick = {
|
||||
if (!ApkInstaller.canInstall(context)) {
|
||||
Toast.makeText(
|
||||
context, R.string.update_permission_hint, Toast.LENGTH_LONG
|
||||
).show()
|
||||
ApkInstaller.openInstallPermissionSettings(context)
|
||||
} else {
|
||||
downloadJob = scope.launch {
|
||||
progress = 0f
|
||||
try {
|
||||
val apk = ApkInstaller.download(context, info.apkUrl) {
|
||||
progress = it
|
||||
}
|
||||
progress = null
|
||||
onDismiss()
|
||||
ApkInstaller.install(context, apk)
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
progress = null
|
||||
Toast.makeText(
|
||||
context, R.string.update_failed, Toast.LENGTH_LONG
|
||||
).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
) { Text(stringResource(R.string.update_action)) }
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(
|
||||
onClick = {
|
||||
downloadJob?.cancel()
|
||||
progress = null
|
||||
onDismiss()
|
||||
}
|
||||
) {
|
||||
Text(
|
||||
stringResource(
|
||||
if (downloading) R.string.action_cancel else R.string.update_later
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user