Handling map loading error in live location maximized map

This commit is contained in:
Maxime NATUREL 2022-08-01 16:05:42 +02:00
parent a30076a2ab
commit 008e07d03e
5 changed files with 38 additions and 2 deletions

View file

@ -22,4 +22,5 @@ sealed class LiveLocationMapAction : VectorViewModelAction {
data class AddMapSymbol(val key: String, val value: Long) : LiveLocationMapAction()
data class RemoveMapSymbol(val key: String) : LiveLocationMapAction()
object StopSharing : LiveLocationMapAction()
object ShowMapLoadingError : LiveLocationMapAction()
}

View file

@ -71,11 +71,13 @@ class LiveLocationMapViewFragment @Inject constructor() : VectorBaseFragment<Fra
private val viewModel: LiveLocationMapViewModel by fragmentViewModel()
private var mapboxMap: WeakReference<MapboxMap>? = null
private var mapView: MapView? = null
private var symbolManager: SymbolManager? = null
private var mapStyle: Style? = null
private val pendingLiveLocations = mutableListOf<UserLiveLocationViewState>()
private var isMapFirstUpdate = true
private var onSymbolClickListener: OnSymbolClickListener? = null
private var mapLoadingErrorListener: MapView.OnDidFailLoadingMapListener? = null
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentLiveLocationMapViewBinding {
return FragmentLiveLocationMapViewBinding.inflate(layoutInflater, container, false)
@ -106,6 +108,13 @@ class LiveLocationMapViewFragment @Inject constructor() : VectorBaseFragment<Fra
}
}
override fun onDestroyView() {
mapLoadingErrorListener?.let { mapView?.removeOnDidFailLoadingMapListener(it) }
mapLoadingErrorListener = null
mapView = null
super.onDestroyView()
}
override fun onResume() {
super.onResume()
setupMap()
@ -122,6 +131,7 @@ class LiveLocationMapViewFragment @Inject constructor() : VectorBaseFragment<Fra
private fun setupMap() {
val mapFragment = getOrCreateSupportMapFragment()
mapFragment.getMapAsync { mapboxMap ->
(mapFragment.view as? MapView)?.let(::listenMapLoadingError)
lifecycleScope.launch {
mapboxMap.setStyle(urlMapProvider.getMapUrl()) { style ->
mapStyle = style
@ -141,6 +151,13 @@ class LiveLocationMapViewFragment @Inject constructor() : VectorBaseFragment<Fra
}
}
private fun listenMapLoadingError(mapView: MapView) {
this.mapView = mapView
mapLoadingErrorListener = MapView.OnDidFailLoadingMapListener {
viewModel.handle(LocationLiveMapAction.ShowMapLoadingError)
}.also { mapView.addOnDidFailLoadingMapListener(it) }
}
private fun onSymbolClicked(symbol: Symbol?) {
symbol?.let {
mapboxMap
@ -173,7 +190,12 @@ class LiveLocationMapViewFragment @Inject constructor() : VectorBaseFragment<Fra
}
override fun invalidate() = withState(viewModel) { viewState ->
updateMap(viewState.userLocations)
if(viewState.loadingMapHasFailed) {
views.mapPreviewLoadingError.isVisible = true
} else {
views.mapPreviewLoadingError.isGone = true
updateMap(viewState.userLocations)
}
updateUserListBottomSheet(viewState.userLocations)
}

View file

@ -61,6 +61,7 @@ class LiveLocationMapViewModel @AssistedInject constructor(
is LiveLocationMapAction.AddMapSymbol -> handleAddMapSymbol(action)
is LiveLocationMapAction.RemoveMapSymbol -> handleRemoveMapSymbol(action)
LiveLocationMapAction.StopSharing -> handleStopSharing()
LiveLocationMapAction.ShowMapLoadingError -> handleShowMapLoadingError()
}
}
@ -87,6 +88,10 @@ class LiveLocationMapViewModel @AssistedInject constructor(
}
}
private fun handleShowMapLoadingError() {
setState { copy(loadingMapHasFailed = true) }
}
override fun onLocationServiceRunning(roomIds: Set<String>) {
// NOOP
}

View file

@ -27,7 +27,8 @@ data class LiveLocationMapViewState(
/**
* Map to keep track of symbol ids associated to each user Id.
*/
val mapSymbolIds: Map<String, Long> = emptyMap()
val mapSymbolIds: Map<String, Long> = emptyMap(),
val loadingMapHasFailed: Boolean = false
) : MavericksState {
constructor(liveLocationMapViewArgs: LiveLocationMapViewArgs) : this(
roomId = liveLocationMapViewArgs.roomId

View file

@ -17,6 +17,13 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />
<im.vector.app.features.location.MapLoadingErrorView
android:id="@+id/mapPreviewLoadingError"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="180dp"
android:visibility="gone" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bottomSheet"
android:layout_width="match_parent"