fix #92 - make all node movements instantly show on map

This commit is contained in:
geeksville 2020-07-07 14:55:01 -07:00
parent c217c16d24
commit 3e7457245a

View file

@ -6,6 +6,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import com.geeksville.android.GeeksvilleApplication
import com.geeksville.android.Logging
import com.geeksville.mesh.NodeInfo
@ -130,7 +131,12 @@ class MapFragment : ScreenFragment("Map"), Logging {
}
var mapView: MapView? = null
var mapboxMap: MapboxMap? = null
/**
* Mapbox native code can crash painfully if you ever call a mapbox view function while the view is not actively being show
*/
private val isViewVisible: Boolean
get() = view != null && isResumed
override fun onViewCreated(viewIn: View, savedInstanceState: Bundle?) {
super.onViewCreated(viewIn, savedInstanceState)
@ -145,7 +151,6 @@ class MapFragment : ScreenFragment("Map"), Logging {
// Each time the pane is shown start fetching new map info (we do this here instead of
// onCreate because getMapAsync can die in native code if the view goes away)
v.getMapAsync { map ->
mapboxMap = map
if (view != null) { // it might have gone away by now
// val markerIcon = BitmapFactory.decodeResource(context.resources, R.drawable.ic_twotone_person_pin_24)
@ -161,7 +166,19 @@ class MapFragment : ScreenFragment("Map"), Logging {
//map.uiSettings.isScrollGesturesEnabled = true
//map.uiSettings.isZoomGesturesEnabled = true
// Provide initial positions
model.nodeDB.nodes.value?.let { nodes ->
onNodesChanged(map, nodes.values)
}
}
// Any times nodes change update our map
model.nodeDB.nodes.observe(viewLifecycleOwner, Observer { nodes ->
debug("Nodes updated! map visible = $isViewVisible")
if (isViewVisible)
onNodesChanged(map, nodes.values)
})
}
}
}
@ -185,16 +202,6 @@ class MapFragment : ScreenFragment("Map"), Logging {
override fun onResume() {
super.onResume()
mapView?.onResume()
// FIXME: for now we just set the node positions when the user pages to the map - because
// otherwise we try to update in the background which breaks mapbox native code (when view is not shown
mapboxMap?.let { map ->
// model.nodeDB.nodes.observe(viewLifecycleOwner, Observer { nodes ->
model.nodeDB.nodes.value?.values?.let {
onNodesChanged(map, it)
}
//})
}
}
override fun onDestroy() {