Feat/auto update #36
No files matched your search
@@ -94,4 +94,5 @@ dependencies {
|
|||||||
debugImplementation(libs.androidx.compose.ui.tooling)
|
debugImplementation(libs.androidx.compose.ui.tooling)
|
||||||
implementation(libs.androidx.material.icons.extended)
|
implementation(libs.androidx.material.icons.extended)
|
||||||
implementation("androidx.core:core-splashscreen:1.0.1")
|
implementation("androidx.core:core-splashscreen:1.0.1")
|
||||||
|
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.8.7")
|
||||||
}
|
}
|
||||||
@@ -22,6 +22,7 @@ import androidx.compose.ui.res.stringResource
|
|||||||
import de.schanbro.weekfocus.ui.theme.WeekFocusTheme
|
import de.schanbro.weekfocus.ui.theme.WeekFocusTheme
|
||||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||||
import de.schanbro.weekfocus.screens.WordListScreen
|
import de.schanbro.weekfocus.screens.WordListScreen
|
||||||
|
import de.schanbro.weekfocus.update.NetworkPermissionGate
|
||||||
import de.schanbro.weekfocus.update.UpdateHandler
|
import de.schanbro.weekfocus.update.UpdateHandler
|
||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
@@ -32,10 +33,12 @@ class MainActivity : ComponentActivity() {
|
|||||||
setContent {
|
setContent {
|
||||||
WeekFocusTheme {
|
WeekFocusTheme {
|
||||||
MainScreen()
|
MainScreen()
|
||||||
|
NetworkPermissionGate {
|
||||||
UpdateHandler()
|
UpdateHandler()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
package de.schanbro.weekfocus.update
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import android.net.Uri
|
||||||
|
import android.provider.Settings
|
||||||
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
|
import androidx.compose.material3.AlertDialog
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.material3.TextButton
|
||||||
|
import androidx.compose.runtime.*
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.lifecycle.Lifecycle
|
||||||
|
import androidx.lifecycle.compose.LifecycleEventEffect
|
||||||
|
import de.schanbro.weekfocus.R
|
||||||
|
|
||||||
|
private const val KEY_HINT_DISMISSED = "network_hint_dismissed"
|
||||||
|
|
||||||
|
/** Zeigt [content] nur, wenn die App Netzwerkzugriff hat. Fragt sonst einmal danach. */
|
||||||
|
@Composable
|
||||||
|
fun NetworkPermissionGate(content: @Composable () -> Unit) {
|
||||||
|
val context = LocalContext.current
|
||||||
|
val prefs = remember {
|
||||||
|
context.getSharedPreferences("WeekFocusPrefs", Context.MODE_PRIVATE)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isGranted() = ContextCompat.checkSelfPermission(
|
||||||
|
context, Manifest.permission.INTERNET
|
||||||
|
) == PackageManager.PERMISSION_GRANTED
|
||||||
|
|
||||||
|
var granted by remember { mutableStateOf(isGranted()) }
|
||||||
|
var showDialog by remember {
|
||||||
|
mutableStateOf(!isGranted() && !prefs.getBoolean(KEY_HINT_DISMISSED, false))
|
||||||
|
}
|
||||||
|
|
||||||
|
val launcher = rememberLauncherForActivityResult(
|
||||||
|
ActivityResultContracts.RequestPermission()
|
||||||
|
) { result ->
|
||||||
|
granted = result || isGranted()
|
||||||
|
if (!granted) openAppSettings(context) // kein System-Dialog möglich → Einstellungen
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nach Rückkehr aus den Einstellungen neu prüfen
|
||||||
|
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
|
||||||
|
granted = isGranted()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (granted) {
|
||||||
|
content()
|
||||||
|
} else if (showDialog) {
|
||||||
|
AlertDialog(
|
||||||
|
onDismissRequest = {},
|
||||||
|
title = { Text(stringResource(R.string.permission_network_title)) },
|
||||||
|
text = { Text(stringResource(R.string.permission_network_message)) },
|
||||||
|
confirmButton = {
|
||||||
|
TextButton(onClick = {
|
||||||
|
showDialog = false
|
||||||
|
launcher.launch(Manifest.permission.INTERNET)
|
||||||
|
}) { Text(stringResource(R.string.permission_allow)) }
|
||||||
|
},
|
||||||
|
dismissButton = {
|
||||||
|
TextButton(onClick = {
|
||||||
|
prefs.edit().putBoolean(KEY_HINT_DISMISSED, true).apply()
|
||||||
|
showDialog = false
|
||||||
|
}) { Text(stringResource(R.string.permission_not_now)) }
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun openAppSettings(context: Context) {
|
||||||
|
val intent = Intent(
|
||||||
|
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
|
||||||
|
Uri.fromParts("package", context.packageName, null)
|
||||||
|
).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
|
context.startActivity(intent)
|
||||||
|
}
|
||||||
@@ -23,4 +23,9 @@
|
|||||||
<string name="update_later">Later</string>
|
<string name="update_later">Later</string>
|
||||||
<string name="update_failed">Update failed. Please try again later.</string>
|
<string name="update_failed">Update failed. Please try again later.</string>
|
||||||
<string name="update_permission_hint">Please allow installs from this source, then tap “Update” again.</string>
|
<string name="update_permission_hint">Please allow installs from this source, then tap “Update” again.</string>
|
||||||
|
|
||||||
|
<string name="permission_network_title">Network access needed</string>
|
||||||
|
<string name="permission_network_message">WeekFocus needs network access to check for updates. Without it the app keeps working, but won’t look for new versions.</string>
|
||||||
|
<string name="permission_allow">Allow</string>
|
||||||
|
<string name="permission_not_now">Not now</string>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -27,4 +27,10 @@
|
|||||||
<string name="update_later">Später</string>
|
<string name="update_later">Später</string>
|
||||||
<string name="update_failed">Update fehlgeschlagen. Bitte später erneut versuchen.</string>
|
<string name="update_failed">Update fehlgeschlagen. Bitte später erneut versuchen.</string>
|
||||||
<string name="update_permission_hint">Bitte die Installation aus dieser Quelle erlauben und danach erneut auf „Aktualisieren“ tippen.</string>
|
<string name="update_permission_hint">Bitte die Installation aus dieser Quelle erlauben und danach erneut auf „Aktualisieren“ tippen.</string>
|
||||||
|
|
||||||
|
<!-- Berechntigungen -->
|
||||||
|
<string name="permission_network_title">Netzwerkzugriff benötigt</string>
|
||||||
|
<string name="permission_network_message">WeekFocus braucht Netzwerkzugriff, um nach Updates zu suchen. Ohne Zugriff funktioniert die App weiterhin, sucht aber nicht nach neuen Versionen.</string>
|
||||||
|
<string name="permission_allow">Erlauben</string>
|
||||||
|
<string name="permission_not_now">Nicht jetzt</string>
|
||||||
</resources>
|
</resources>
|
||||||
Reference in new issue
Block a user