128 lines
4.8 KiB
Kotlin
128 lines
4.8 KiB
Kotlin
package de.schanbro.weekfocus.update
|
|
|
|
import android.content.Context
|
|
import android.widget.Toast
|
|
import androidx.compose.foundation.layout.*
|
|
import androidx.compose.foundation.rememberScrollState
|
|
import androidx.compose.foundation.verticalScroll
|
|
import androidx.compose.material3.*
|
|
import androidx.compose.runtime.*
|
|
import androidx.compose.ui.Modifier
|
|
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
|
|
|
|
private const val CHECK_INTERVAL_MS = 24L * 60 * 60 * 1000
|
|
|
|
@Composable
|
|
fun UpdateHandler() {
|
|
val context = LocalContext.current
|
|
val settings = remember { SettingsRepository.get(context) }
|
|
val prefs = remember {
|
|
context.getSharedPreferences("WeekFocusPrefs", Context.MODE_PRIVATE)
|
|
}
|
|
var release by remember { mutableStateOf<ReleaseInfo?>(null) }
|
|
|
|
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(settings.includePrereleases)
|
|
?: return@LaunchedEffect
|
|
prefs.edit().putLong("last_update_check", now).apply()
|
|
|
|
if (UpdateChecker.isNewer(latest.version, UpdateChecker.currentVersion(context))) {
|
|
release = latest
|
|
}
|
|
}
|
|
|
|
release?.let { UpdateDialog(info = it, onDismiss = { 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(
|
|
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
|
|
)
|
|
)
|
|
}
|
|
}
|
|
)
|
|
} |