Skip to content

Commit

Permalink
refactor(map): move Marker updates to Composable
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir committed Sep 5, 2023
1 parent 8372215 commit b034f37
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions app/src/main/java/com/geeksville/mesh/ui/map/MapFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.content.ContextCompat
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.viewmodel.compose.viewModel
import com.geeksville.mesh.BuildConfig
Expand Down Expand Up @@ -64,7 +63,6 @@ import com.geeksville.mesh.waypoint
import com.google.accompanist.themeadapter.appcompat.AppCompatTheme
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import org.osmdroid.bonuspack.utils.BonusPackHelper.getBitmapFromVectorDrawable
import org.osmdroid.config.Configuration
import org.osmdroid.events.MapEventsReceiver
Expand Down Expand Up @@ -112,6 +110,16 @@ class MapFragment : ScreenFragment("Map Fragment"), Logging {
}
}

@Composable
private fun MapView.UpdateMarkers(
nodeMarkers: List<MarkerWithLabel>,
waypointMarkers: List<MarkerWithLabel>,
) {
debug("Showing on map: ${nodeMarkers.size} nodes ${waypointMarkers.size} waypoints")
overlays.removeAll(overlays.filterIsInstance<MarkerWithLabel>())
overlays.addAll(nodeMarkers + waypointMarkers)
}

@Composable
fun MapView(model: UIViewModel = viewModel()) {

Expand Down Expand Up @@ -437,21 +445,11 @@ fun MapView(model: UIViewModel = viewModel()) {
addCopyright() // Copyright is required for certain map sources
createLatLongGrid(false)

overlays.removeAll(overlays.filterIsInstance<MarkerWithLabel>())
val nodeMarkers = onNodesChanged(nodes.values)
val waypointMarkers = onWaypointChanged(waypoints.values)
debug("Showing on map: ${nodeMarkers.size} nodes ${waypointMarkers.size} waypoints")

overlays.addAll(nodeMarkers + waypointMarkers)
invalidate()
}

// FIXME workaround to 'nodes.observeAsState' going stale after MapFragment enters onPause state
LaunchedEffect(Unit) {
while (true) {
if (model.isConnected() && downloadRegionBoundingBox == null) map.drawOverlays()
delay(30000L)
}
with(map) {
UpdateMarkers(onNodesChanged(nodes.values), onWaypointChanged(waypoints.values))
}

// private fun addWeatherLayer() {
Expand Down Expand Up @@ -623,9 +621,7 @@ fun MapView(model: UIViewModel = viewModel()) {
}
},
modifier = Modifier.fillMaxSize(),
update = { map ->
if (downloadRegionBoundingBox == null) map.drawOverlays()
},
update = { map -> map.drawOverlays() },
)
if (downloadRegionBoundingBox != null) CacheLayout(
cacheEstimate = cacheEstimate,
Expand Down

0 comments on commit b034f37

Please sign in to comment.