mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
refactor: handle MapView Lifecycle
This commit is contained in:
parent
fce97997d8
commit
0a47b8df3e
2 changed files with 25 additions and 15 deletions
|
|
@ -57,7 +57,7 @@ import com.geeksville.mesh.ui.map.components.CacheLayout
|
|||
import com.geeksville.mesh.ui.map.components.DownloadButton
|
||||
import com.geeksville.mesh.ui.map.components.EditWaypointDialog
|
||||
import com.geeksville.mesh.ui.components.IconButton
|
||||
import com.geeksville.mesh.util.EnableWakeLock
|
||||
import com.geeksville.mesh.ui.map.components.rememberMapViewWithLifecycle
|
||||
import com.geeksville.mesh.util.SqlTileWriterExt
|
||||
import com.geeksville.mesh.util.requiredZoomLevel
|
||||
import com.geeksville.mesh.util.formatAgo
|
||||
|
|
@ -147,13 +147,7 @@ fun MapView(model: UIViewModel = viewModel()) {
|
|||
|
||||
val hasGps = context.hasGps()
|
||||
|
||||
EnableWakeLock(context)
|
||||
|
||||
val map = remember {
|
||||
MapView(context).apply {
|
||||
clipToOutline = true
|
||||
}
|
||||
}
|
||||
val map = rememberMapViewWithLifecycle(context)
|
||||
|
||||
fun toggleMyLocation() {
|
||||
if (context.gpsDisabled()) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,76 @@
|
|||
package com.geeksville.mesh.ui.map.components
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.os.PowerManager
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.platform.LocalLifecycleOwner
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleEventObserver
|
||||
import com.geeksville.mesh.android.BuildUtils.errormsg
|
||||
import org.osmdroid.views.MapView
|
||||
|
||||
@SuppressLint("WakelockTimeout")
|
||||
private fun PowerManager.WakeLock.safeAcquire() {
|
||||
if (!isHeld) try {
|
||||
acquire()
|
||||
} catch (e: SecurityException) {
|
||||
errormsg("WakeLock permission exception: ${e.message}")
|
||||
} catch (e: IllegalStateException) {
|
||||
errormsg("WakeLock acquire() exception: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun PowerManager.WakeLock.safeRelease() {
|
||||
if (isHeld) try {
|
||||
release()
|
||||
} catch (e: IllegalStateException) {
|
||||
errormsg("WakeLock release() exception: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun rememberMapViewWithLifecycle(context: Context): MapView {
|
||||
val mapView = remember {
|
||||
MapView(context).apply {
|
||||
clipToOutline = true
|
||||
}
|
||||
}
|
||||
val lifecycle = LocalLifecycleOwner.current.lifecycle
|
||||
DisposableEffect(lifecycle) {
|
||||
val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
@SuppressLint("InvalidWakeLockTag")
|
||||
val wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "ScreenLock")
|
||||
|
||||
wakeLock.safeAcquire()
|
||||
|
||||
val observer = LifecycleEventObserver { _, event ->
|
||||
when (event) {
|
||||
Lifecycle.Event.ON_PAUSE -> {
|
||||
wakeLock.safeRelease()
|
||||
mapView.onPause()
|
||||
}
|
||||
|
||||
Lifecycle.Event.ON_RESUME -> {
|
||||
wakeLock.safeAcquire()
|
||||
mapView.onResume()
|
||||
}
|
||||
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
lifecycle.addObserver(observer)
|
||||
|
||||
onDispose {
|
||||
lifecycle.removeObserver(observer)
|
||||
wakeLock.safeRelease()
|
||||
mapView.onDetach()
|
||||
}
|
||||
}
|
||||
return mapView
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue