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 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 scope = rememberCoroutineScope() val prefs = remember { context.getSharedPreferences("WeekFocusPrefs", Context.MODE_PRIVATE) } var release by remember { mutableStateOf(null) } var progress by remember { mutableStateOf(null) } // null = kein Download var downloadJob by remember { mutableStateOf(null) } // Einmal pro Start, höchstens alle 24 Stunden LaunchedEffect(Unit) { val now = System.currentTimeMillis() if (now - prefs.getLong("last_update_check", 0L) < CHECK_INTERVAL_MS) return@LaunchedEffect val latest = UpdateChecker.fetchLatestRelease() ?: return@LaunchedEffect prefs.edit().putLong("last_update_check", now).apply() if (UpdateChecker.isNewer(latest.version, UpdateChecker.currentVersion(context))) { release = latest } } release?.let { info -> val downloading = progress != 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 } ) { Text( stringResource( if (downloading) R.string.action_cancel else R.string.update_later ) ) } } ) } }