kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
refactor: handle MapView Lifecycle
rodzic
fce97997d8
commit
0a47b8df3e
|
@ -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()) {
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
package com.geeksville.mesh.util
|
||||
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() {
|
||||
|
@ -29,23 +31,35 @@ private fun PowerManager.WakeLock.safeRelease() {
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressLint("InvalidWakeLockTag")
|
||||
@Composable
|
||||
fun EnableWakeLock(context: Context) {
|
||||
fun rememberMapViewWithLifecycle(context: Context): MapView {
|
||||
val mapView = remember {
|
||||
MapView(context).apply {
|
||||
clipToOutline = true
|
||||
}
|
||||
}
|
||||
val lifecycle = LocalLifecycleOwner.current.lifecycle
|
||||
|
||||
DisposableEffect(Unit) {
|
||||
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()
|
||||
Lifecycle.Event.ON_RESUME -> wakeLock.safeAcquire()
|
||||
Lifecycle.Event.ON_PAUSE -> {
|
||||
wakeLock.safeRelease()
|
||||
mapView.onPause()
|
||||
}
|
||||
|
||||
Lifecycle.Event.ON_RESUME -> {
|
||||
wakeLock.safeAcquire()
|
||||
mapView.onResume()
|
||||
}
|
||||
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
@ -55,6 +69,8 @@ fun EnableWakeLock(context: Context) {
|
|||
onDispose {
|
||||
lifecycle.removeObserver(observer)
|
||||
wakeLock.safeRelease()
|
||||
mapView.onDetach()
|
||||
}
|
||||
}
|
||||
return mapView
|
||||
}
|
Ładowanie…
Reference in New Issue