From 962dff81ec5953b87fffd15df161c995bf4e33c9 Mon Sep 17 00:00:00 2001 From: Ari Date: Thu, 23 Oct 2025 20:08:35 +0200 Subject: [PATCH] Get Location App --- app/build.gradle.kts | 1 + .../myapplication/LocationDataStore.kt | 47 +++++++++++++++++++ .../com/example/myapplication/MainActivity.kt | 34 ++++++++++---- gradle/libs.versions.toml | 2 + 4 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/com/example/myapplication/LocationDataStore.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 41563b3..fa8c35b 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -54,6 +54,7 @@ dependencies { implementation(libs.androidx.compose.material3) implementation(libs.play.services.location) implementation(libs.accompanist.permissions) + implementation(libs.androidx.datastore.preferences) testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) diff --git a/app/src/main/java/com/example/myapplication/LocationDataStore.kt b/app/src/main/java/com/example/myapplication/LocationDataStore.kt new file mode 100644 index 0000000..a3f0d86 --- /dev/null +++ b/app/src/main/java/com/example/myapplication/LocationDataStore.kt @@ -0,0 +1,47 @@ +package com.example.myapplication + +import android.content.Context +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.core.doublePreferencesKey +import androidx.datastore.preferences.core.edit +import androidx.datastore.preferences.preferencesDataStore +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map + +private val Context.dataStore: DataStore by preferencesDataStore(name = "location_prefs") + +class LocationDataStore(private val context: Context) { + + private object Keys { + val LATITUDE = doublePreferencesKey("latitude") + val LONGITUDE = doublePreferencesKey("longitude") + val TIMESTAMP = doublePreferencesKey("timestamp") + } + + suspend fun saveLocation(latitude: Double, longitude: Double) { + context.dataStore.edit { preferences -> + preferences[Keys.LATITUDE] = latitude + preferences[Keys.LONGITUDE] = longitude + preferences[Keys.TIMESTAMP] = System.currentTimeMillis().toDouble() + } + } + + val locationFlow: Flow = context.dataStore.data.map { preferences -> + val lat = preferences[Keys.LATITUDE] + val lon = preferences[Keys.LONGITUDE] + val timestamp = preferences[Keys.TIMESTAMP] + + if (lat != null && lon != null) { + LocationData(lat, lon, timestamp?.toLong()) + } else { + null + } + } +} + +data class LocationData( + val latitude: Double, + val longitude: Double, + val timestamp: Long? = null +) diff --git a/app/src/main/java/com/example/myapplication/MainActivity.kt b/app/src/main/java/com/example/myapplication/MainActivity.kt index 9e96556..6264d95 100644 --- a/app/src/main/java/com/example/myapplication/MainActivity.kt +++ b/app/src/main/java/com/example/myapplication/MainActivity.kt @@ -18,6 +18,7 @@ import androidx.compose.material3.Button import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -40,6 +41,7 @@ import com.google.android.gms.location.LocationServices import kotlinx.coroutines.suspendCancellableCoroutine import kotlin.coroutines.resume import kotlinx.coroutines.launch +import androidx.compose.runtime.collectAsState class MainActivity : ComponentActivity() { @@ -58,6 +60,8 @@ class MainActivity : ComponentActivity() { ) } + val locationDataStore = remember { LocationDataStore(context) } + val locationPermissions = rememberMultiplePermissionsState( permissions = listOf( Manifest.permission.ACCESS_COARSE_LOCATION, @@ -65,9 +69,9 @@ class MainActivity : ComponentActivity() { ) ) - var location by remember { - mutableStateOf(null) - } + var location by remember { mutableStateOf(null) } + + val savedLocation by locationDataStore.locationFlow.collectAsState(initial = null) val coroutineScope = rememberCoroutineScope() @@ -76,10 +80,17 @@ class MainActivity : ComponentActivity() { verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { - location?.let { - Text(text ="${it.latitude} ${it.longitude}", - fontSize = 32.sp, - fontWeight = FontWeight.Bold + // Zeige aktuelle oder gespeicherte Location + val displayLocation = location ?: savedLocation + displayLocation?.let { + Text( + text = when { + location != null -> "Aktuelle Location:\n${location!!.latitude} ${location!!.longitude}" + savedLocation != null -> "Gespeicherte Location:\n${savedLocation!!.latitude} ${savedLocation!!.longitude}" + else -> "Keine Location verfügbar" + }, + fontSize = 20.sp, + fontWeight = FontWeight.Bold ) } Button( @@ -88,12 +99,17 @@ class MainActivity : ComponentActivity() { locationPermissions.launchMultiplePermissionRequest() } else { coroutineScope.launch { - location = locationManager.getLocation() + val newLocation = locationManager.getLocation() + location = newLocation + // Speichere Location auf Disk + newLocation?.let { + locationDataStore.saveLocation(it.latitude, it.longitude) + } } } } ) { - Text("Get Location") + Text("Get & Save Location") } } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 24a758a..b28d65d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,6 @@ [versions] accompanistPermissions = "0.36.0" +datastorePreferences = "1.1.7" playServicesLocation = "21.3.0" agp = "8.13.0" kotlin = "2.0.21" @@ -13,6 +14,7 @@ composeBom = "2024.09.00" [libraries] accompanist-permissions = { module = "com.google.accompanist:accompanist-permissions", version.ref = "accompanistPermissions" } +androidx-datastore-preferences = { module = "androidx.datastore:datastore-preferences", version.ref = "datastorePreferences" } play-services-location = { module = "com.google.android.gms:play-services-location", version.ref = "playServicesLocation" } androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }