Extract all Compose UI from MainActivity.kt
Split dialogs into their own files Introduced Permission integration api MapDialog moved to shared
This commit is contained in:
@@ -10,5 +10,5 @@ import java.util.concurrent.atomic.AtomicReference
|
||||
object AndroidAppContext {
|
||||
private val ref = AtomicReference<Context?>()
|
||||
fun install(appContext: Context) { ref.set(appContext.applicationContext) }
|
||||
fun get(): Context? = ref.get()
|
||||
fun get(): Context? = ref.get() // TODO: This should be an illegalstate exception if Context is null and return Context instead of Context?
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package app.klottr.platform
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.widget.Toast
|
||||
@@ -19,6 +20,21 @@ actual object AppNavigator {
|
||||
try { c.startActivity(Intent(Intent.ACTION_VIEW, geo).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) } catch (_: Exception) {}
|
||||
}
|
||||
}
|
||||
|
||||
actual fun openWalkingDirections(lat: Double?, lon: Double?, title: String?) {
|
||||
if (lat == null || lon == null) return
|
||||
val c = AndroidAppContext.get() ?: return
|
||||
val label = Uri.encode(title ?: "Plats")
|
||||
val uri = Uri.parse("google.navigation?q=$lat,$lon($label)&mode=w")
|
||||
val intent =
|
||||
Intent(Intent.ACTION_VIEW, uri).apply { setPackage("com.google.android.apps.maps") }
|
||||
try {
|
||||
c.startActivity(intent)
|
||||
} catch (_: Exception) {
|
||||
openMap(lat, lon, title)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
actual object UiFeedback {
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package app.klottr.platform
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import androidx.core.content.ContextCompat
|
||||
|
||||
|
||||
actual fun hasFgLocationPerms(): Boolean {
|
||||
val ctx = AndroidAppContext.get() ?: return false
|
||||
val fine = ContextCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_FINE_LOCATION) ==
|
||||
PackageManager.PERMISSION_GRANTED
|
||||
val coarse = ContextCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_COARSE_LOCATION) ==
|
||||
PackageManager.PERMISSION_GRANTED
|
||||
return fine || coarse
|
||||
}
|
||||
|
||||
actual fun hasBgLocationPerm(): Boolean {
|
||||
val ctx = AndroidAppContext.get() ?: return false
|
||||
return if (Build.VERSION.SDK_INT < 29) true
|
||||
else ContextCompat.checkSelfPermission(
|
||||
ctx, Manifest.permission.ACCESS_BACKGROUND_LOCATION
|
||||
) == PackageManager.PERMISSION_GRANTED
|
||||
}
|
||||
298
shared/src/commonMain/kotlin/app/klottr/MapDialog.kt
Normal file
298
shared/src/commonMain/kotlin/app/klottr/MapDialog.kt
Normal file
@@ -0,0 +1,298 @@
|
||||
package app.klottr
|
||||
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalWindowInfo
|
||||
import androidx.compose.ui.unit.dp
|
||||
import app.klottr.location.LocationServiceProvider
|
||||
import app.klottr.maps.CameraPosition
|
||||
import app.klottr.maps.LatLng as SharedLatLng
|
||||
import app.klottr.maps.MapContainer
|
||||
import app.klottr.maps.MapController
|
||||
import app.klottr.maps.MapFeature
|
||||
import app.klottr.maps.MapState
|
||||
import app.klottr.maps.MapType
|
||||
import app.klottr.maps.MapUiSettings
|
||||
import app.klottr.net.DigitaltMuseum
|
||||
import app.klottr.net.DigitaltMuseumItem
|
||||
import app.klottr.net.FornsokItem
|
||||
import app.klottr.net.FornsokShared
|
||||
import app.klottr.net.Geocache
|
||||
import app.klottr.net.NoteDTO
|
||||
import app.klottr.net.Wikipedia
|
||||
import app.klottr.net.WikipediaPage
|
||||
import app.klottr.platform.hasFgLocationPerms
|
||||
import app.klottr.util.Config
|
||||
import app.klottr.util.SettingsStore
|
||||
import app.klottr.util.distanceMeters
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.IO
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
@Composable
|
||||
fun MapDialog(
|
||||
onClose: () -> Unit,
|
||||
onKlotterClick: (id: String, lat: Double, lon: Double, label: String) -> Unit,
|
||||
onOpenUrl: (title: String, url: String) -> Unit,
|
||||
isKnownNote: (Long) -> Boolean,
|
||||
initialLat: Double? = null,
|
||||
initialLon: Double? = null
|
||||
) {
|
||||
val dialogWidth = (LocalWindowInfo.current.containerSize.width.dp * 0.98f)
|
||||
val dialogHeight = (LocalWindowInfo.current.containerSize.height.dp * 0.70f)
|
||||
|
||||
val satellite = SettingsStore.getSatelliteMap()
|
||||
val showKlotter = remember { SettingsStore.getShowKlotter() }
|
||||
val showWiki = remember { SettingsStore.getShowWikipedia() }
|
||||
val wikiRadius = remember { SettingsStore.getWikiRadiusMeters() }
|
||||
val showDm = remember { SettingsStore.getShowDigitaltMuseum() }
|
||||
val dmRadius = remember { SettingsStore.getDigitaltMuseumRadiusMeters() }
|
||||
val showFornsok = remember { SettingsStore.getShowFornsok() }
|
||||
val fornsokRadius = remember { SettingsStore.getFornsokRadiusMeters() }
|
||||
|
||||
var myLat by remember { mutableStateOf<Double?>(null) }
|
||||
var myLon by remember { mutableStateOf<Double?>(null) }
|
||||
|
||||
var klotter by remember { mutableStateOf<List<NoteDTO>>(emptyList()) }
|
||||
var wiki by remember { mutableStateOf<List<WikipediaPage>>(emptyList()) }
|
||||
var dm by remember { mutableStateOf<List<DigitaltMuseumItem>>(emptyList()) }
|
||||
var forn by remember { mutableStateOf<List<FornsokItem>>(emptyList()) }
|
||||
|
||||
// TODO: Not used? do they need to?
|
||||
var selLat by remember { mutableStateOf<Double?>(null) }
|
||||
var selLon by remember { mutableStateOf<Double?>(null) }
|
||||
var selTitle by remember { mutableStateOf<String?>(null) }
|
||||
var selUri by remember { mutableStateOf<String?>(null) }
|
||||
var selId by remember { mutableStateOf<String?>(null) } // klotter-id vid behov
|
||||
var selLayer by remember { mutableStateOf<String?>(null) } // "Klotter", "Wikipedia", "Fornsök", ...
|
||||
|
||||
// Map state for shared MapContainer
|
||||
val mapState = remember {
|
||||
MapState(
|
||||
initialCamera = CameraPosition(
|
||||
target = SharedLatLng(
|
||||
initialLat ?: 59.3293,
|
||||
initialLon ?: 18.0686
|
||||
),
|
||||
zoom = 16f
|
||||
),
|
||||
initialUi = MapUiSettings(
|
||||
compassEnabled = true,
|
||||
zoomControlsEnabled = false,
|
||||
myLocationButtonEnabled = hasFgLocationPerms(),
|
||||
mapType = if (satellite) MapType.SATELLITE else MapType.NORMAL
|
||||
)
|
||||
)
|
||||
}
|
||||
var mapController by remember { mutableStateOf<MapController?>(null) }
|
||||
|
||||
val locService = remember { LocationServiceProvider.get() }
|
||||
DisposableEffect(Unit) {
|
||||
locService.start()
|
||||
onDispose { locService.stop() }
|
||||
}
|
||||
val current = locService.location.collectAsState(initial = null)
|
||||
LaunchedEffect(current.value?.lat, current.value?.lon) {
|
||||
myLat = current.value?.lat
|
||||
myLon = current.value?.lon
|
||||
}
|
||||
//
|
||||
//
|
||||
// LaunchedEffect(Unit) {
|
||||
// val fused = GmsLocationServices.getFusedLocationProviderClient(context)
|
||||
// val loc = try {
|
||||
// fused.lastLocation.await()
|
||||
// ?: fused.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, null).await()
|
||||
// } catch (_: Exception) { null }
|
||||
// if (loc != null) {
|
||||
// myLat = loc.latitude
|
||||
// myLon = loc.longitude
|
||||
// }
|
||||
// }
|
||||
|
||||
LaunchedEffect(myLat, myLon, showKlotter, showWiki, wikiRadius, showDm, dmRadius, showFornsok, fornsokRadius) {
|
||||
val lat = myLat; val lon = myLon
|
||||
if (lat == null || lon == null) return@LaunchedEffect
|
||||
|
||||
if (showKlotter) {
|
||||
klotter = withContext(Dispatchers.IO) {
|
||||
Geocache.getNearby(lat = lat, lon = lon, radiusMeters = 300, ttlMs = 0L)
|
||||
}
|
||||
} else klotter = emptyList()
|
||||
|
||||
if (showWiki) {
|
||||
wiki = withContext(Dispatchers.IO) {
|
||||
Wikipedia.getNearby(lat = lat, lon = lon, radiusMeters = wikiRadius)
|
||||
}
|
||||
} else wiki = emptyList()
|
||||
|
||||
if (showDm) {
|
||||
dm = withContext(Dispatchers.IO) {
|
||||
DigitaltMuseum.getNearby(lat = lat, lon = lon, radiusMeters = dmRadius)
|
||||
}
|
||||
} else dm = emptyList()
|
||||
|
||||
if (showFornsok) {
|
||||
forn = withContext(Dispatchers.IO) {
|
||||
FornsokShared.getNearby(lat = lat, lon = lon, radiusMeters = fornsokRadius)
|
||||
}
|
||||
} else forn = emptyList()
|
||||
}
|
||||
|
||||
// Helper: build features for the map
|
||||
fun buildFeatures(): List<MapFeature> {
|
||||
val lat = myLat; val lon = myLon
|
||||
val out = mutableListOf<MapFeature>()
|
||||
if (showKlotter) {
|
||||
klotter.forEach { n ->
|
||||
val d = if (lat != null && lon != null)
|
||||
distanceMeters(lat, lon, n.a, n.o).roundToInt() else Int.MAX_VALUE
|
||||
val titleLine = n.t.substringBefore('\n').ifBlank { "Okänt klotter" }
|
||||
val radius = Config.DEFAULT_KLOTTER_RADIUS_M
|
||||
val visited = isKnownNote(n.i.toLong())
|
||||
val within = (d != Int.MAX_VALUE && d <= radius)
|
||||
val display = if (visited || (within && titleLine != "Okänt klotter")) titleLine else "Okänt klotter"
|
||||
out += MapFeature.Marker(
|
||||
id = "klotter:${n.i}",
|
||||
position = SharedLatLng(n.a, n.o),
|
||||
title = display
|
||||
)
|
||||
}
|
||||
}
|
||||
if (showWiki) {
|
||||
wiki.forEach { w ->
|
||||
out += MapFeature.Marker(
|
||||
id = "wiki:${w.pageId}",
|
||||
position = SharedLatLng(w.lat, w.lon),
|
||||
title = w.title
|
||||
)
|
||||
}
|
||||
}
|
||||
if (showDm) {
|
||||
dm.forEach { dmi ->
|
||||
val dmId = dmi.webUrl ?: dmi.url ?: "${dmi.lat},${dmi.lon}"
|
||||
out += MapFeature.Marker(
|
||||
id = "dm:$dmId",
|
||||
position = SharedLatLng(dmi.lat, dmi.lon),
|
||||
title = dmi.title
|
||||
)
|
||||
}
|
||||
}
|
||||
if (showFornsok) {
|
||||
forn.forEach { f ->
|
||||
val fid = f.uri ?: f.title
|
||||
out += MapFeature.Marker(
|
||||
id = "forn:$fid",
|
||||
position = SharedLatLng(f.lat, f.lon),
|
||||
title = f.title
|
||||
)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
|
||||
AlertDialog(
|
||||
onDismissRequest = onClose,
|
||||
title = { Text("Karta") },
|
||||
text = {
|
||||
Box(
|
||||
Modifier
|
||||
.widthIn(max = dialogWidth)
|
||||
.heightIn(min = 320.dp, max = dialogHeight)
|
||||
.fillMaxWidth()
|
||||
) {
|
||||
// Shared Map
|
||||
MapContainer(
|
||||
state = mapState,
|
||||
onMapClick = {
|
||||
// Clear selection
|
||||
selLat = null; selLon = null; selTitle = null; selUri = null; selId = null; selLayer = null
|
||||
},
|
||||
onMarkerClick = { id ->
|
||||
fun setSel(lat: Double, lon: Double, title: String?, uri: String?, layer: String, itemId: String? = null) {
|
||||
selLat = lat; selLon = lon; selTitle = title; selUri = uri; selLayer = layer; selId = itemId
|
||||
}
|
||||
when {
|
||||
id.startsWith("klotter:") -> {
|
||||
val nid = id.removePrefix("klotter:")
|
||||
klotter.firstOrNull { it.i.toString() == nid }?.let { n ->
|
||||
val titleLine = n.t.substringBefore('\n').ifBlank { "Okänt klotter" }
|
||||
setSel(n.a, n.o, titleLine, null, "Klotter", nid)
|
||||
onKlotterClick(nid, n.a, n.o, titleLine)
|
||||
}
|
||||
}
|
||||
id.startsWith("wiki:") -> {
|
||||
val pid = id.removePrefix("wiki:")
|
||||
wiki.firstOrNull { it.pageId.toString() == pid }?.let { w ->
|
||||
val url = "https://sv.m.wikipedia.org/?curid=${w.pageId}"
|
||||
setSel(w.lat, w.lon, w.title, url, "Wikipedia")
|
||||
onOpenUrl(w.title, url)
|
||||
}
|
||||
}
|
||||
id.startsWith("dm:") -> {
|
||||
val mid = id.removePrefix("dm:")
|
||||
dm.firstOrNull { (it.webUrl ?: it.url) == mid }?.let { dmi ->
|
||||
val url = dmi.webUrl ?: dmi.url
|
||||
setSel(dmi.lat, dmi.lon, dmi.title, url, "DigitaltMuseum")
|
||||
url?.let { onOpenUrl(dmi.title, it) }
|
||||
}
|
||||
}
|
||||
id.startsWith("forn:") -> {
|
||||
val fid = id.removePrefix("forn:")
|
||||
forn.firstOrNull { (it.uri ?: it.title) == fid }?.let { f ->
|
||||
setSel(f.lat, f.lon, f.title, f.uri, "Fornsök")
|
||||
f.uri?.let { onOpenUrl(f.title, it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
onReady = { ctrl ->
|
||||
mapController = ctrl
|
||||
ctrl.setMyLocationEnabled(hasFgLocationPerms())
|
||||
ctrl.setFeatures(buildFeatures())
|
||||
}
|
||||
)
|
||||
// Keep camera + markers in sync with state changes
|
||||
MapSyncEffects(
|
||||
controller = mapController,
|
||||
myLat = if (initialLat == null && initialLon == null) myLat else null, // don't override explicit initial cam
|
||||
myLon = if (initialLat == null && initialLon == null) myLon else null,
|
||||
featuresBuilder = { buildFeatures() },
|
||||
// everything that affects markers must be referenced here:
|
||||
showKlotter, showWiki, wikiRadius, showDm, dmRadius, showFornsok, fornsokRadius,
|
||||
klotter, wiki, dm, forn
|
||||
)
|
||||
}
|
||||
},
|
||||
confirmButton = {
|
||||
Button(onClick = onClose) { Text("Stäng") }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// Keep map in sync with state changes, camera position, enabled features.
|
||||
@Composable
|
||||
private fun MapSyncEffects(
|
||||
controller: MapController?,
|
||||
myLat: Double?, myLon: Double?,
|
||||
featuresBuilder: () -> List<MapFeature>,
|
||||
vararg featuresDeps: Any?
|
||||
) {
|
||||
LaunchedEffect(controller, myLat, myLon) {
|
||||
if (controller != null && myLat != null && myLon != null) {
|
||||
controller.animateCamera(CameraPosition(SharedLatLng(myLat, myLon), 16f))
|
||||
}
|
||||
}
|
||||
// Rebuild + apply features whenever any dependency changes
|
||||
LaunchedEffect(controller, *featuresDeps) {
|
||||
if (controller != null) {
|
||||
controller.setFeatures(featuresBuilder())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ package app.klottr.platform
|
||||
expect object AppNavigator {
|
||||
fun openUrl(url: String)
|
||||
fun openMap(lat: Double, lon: Double, label: String? = null)
|
||||
fun openWalkingDirections(lat: Double?, lon: Double?, title: String?) // Currently not used but lets keep it around
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package app.klottr.platform
|
||||
|
||||
expect fun hasFgLocationPerms() : Boolean
|
||||
|
||||
expect fun hasBgLocationPerm() : Boolean
|
||||
|
||||
@@ -16,6 +16,11 @@ actual object AppNavigator {
|
||||
val url = "http://maps.apple.com/?ll=$lat,$lon&q=$encoded"
|
||||
openUrl(url)
|
||||
}
|
||||
|
||||
actual fun openWalkingDirections(lat: Double?, lon: Double?, title: String?) {
|
||||
//TODO: Implement
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
actual object UiFeedback {
|
||||
|
||||
12
shared/src/iosMain/kotlin/app/klottr/platform/Permissions.kt
Normal file
12
shared/src/iosMain/kotlin/app/klottr/platform/Permissions.kt
Normal file
@@ -0,0 +1,12 @@
|
||||
package app.klottr.platform
|
||||
|
||||
actual fun hasBgLocationPerm(): Boolean {
|
||||
//TODO: OH: Implement
|
||||
return false
|
||||
}
|
||||
|
||||
actual fun hasFgLocationPerms(): Boolean {
|
||||
//TODO: OH: Implement
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user