75 lines
2.3 KiB
Kotlin
75 lines
2.3 KiB
Kotlin
package de.schanbro.weekfocus.update
|
|
|
|
import android.content.Context
|
|
import androidx.compose.foundation.layout.Column
|
|
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 de.schanbro.weekfocus.R
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.withContext
|
|
|
|
private const val KEY_LAST_SEEN = "last_seen_version"
|
|
|
|
@Composable
|
|
fun WhatsNewHandler() {
|
|
val context = LocalContext.current
|
|
val prefs = remember {
|
|
context.getSharedPreferences("WeekFocusPrefs", Context.MODE_PRIVATE)
|
|
}
|
|
val current = remember { UpdateChecker.currentVersion(context) }
|
|
var notes by remember { mutableStateOf<String?>(null) }
|
|
|
|
fun markSeen() = prefs.edit().putString(KEY_LAST_SEEN, current).apply()
|
|
|
|
LaunchedEffect(Unit) {
|
|
val lastSeen = prefs.getString(KEY_LAST_SEEN, null)
|
|
|
|
// Erster Start nach Neuinstallation: nichts anzeigen, nur merken
|
|
if (lastSeen == null) {
|
|
markSeen()
|
|
return@LaunchedEffect
|
|
}
|
|
|
|
if (UpdateChecker.isNewer(current, lastSeen)) {
|
|
val loaded = withContext(Dispatchers.IO) { ReleaseNotes.load(context) }
|
|
if (loaded != null) notes = loaded else markSeen()
|
|
}
|
|
}
|
|
|
|
notes?.let {
|
|
WhatsNewDialog(
|
|
version = current,
|
|
notes = it,
|
|
onDismiss = {
|
|
markSeen()
|
|
notes = null
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
fun WhatsNewDialog(version: String, notes: String, onDismiss: () -> Unit) {
|
|
AlertDialog(
|
|
onDismissRequest = onDismiss,
|
|
title = { Text(stringResource(R.string.whats_new_title, version)) },
|
|
text = {
|
|
Column(Modifier.verticalScroll(rememberScrollState())) {
|
|
Text(
|
|
text = ReleaseNotes.format(notes),
|
|
style = MaterialTheme.typography.bodyMedium
|
|
)
|
|
}
|
|
},
|
|
confirmButton = {
|
|
TextButton(onClick = onDismiss) {
|
|
Text(stringResource(R.string.whats_new_close))
|
|
}
|
|
}
|
|
)
|
|
} |