Fixing leak on OnSymbolClickListener

This commit is contained in:
Maxime NATUREL 2022-07-27 17:11:17 +02:00
parent 74c9a71cbd
commit 4f71741107

View file

@ -35,6 +35,7 @@ import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.maps.MapboxMapOptions import com.mapbox.mapboxsdk.maps.MapboxMapOptions
import com.mapbox.mapboxsdk.maps.Style import com.mapbox.mapboxsdk.maps.Style
import com.mapbox.mapboxsdk.maps.SupportMapFragment import com.mapbox.mapboxsdk.maps.SupportMapFragment
import com.mapbox.mapboxsdk.plugins.annotation.OnSymbolClickListener
import com.mapbox.mapboxsdk.plugins.annotation.Symbol import com.mapbox.mapboxsdk.plugins.annotation.Symbol
import com.mapbox.mapboxsdk.plugins.annotation.SymbolManager import com.mapbox.mapboxsdk.plugins.annotation.SymbolManager
import com.mapbox.mapboxsdk.plugins.annotation.SymbolOptions import com.mapbox.mapboxsdk.plugins.annotation.SymbolOptions
@ -73,6 +74,7 @@ class LiveLocationMapViewFragment @Inject constructor() : VectorBaseFragment<Fra
private var mapStyle: Style? = null private var mapStyle: Style? = null
private val pendingLiveLocations = mutableListOf<UserLiveLocationViewState>() private val pendingLiveLocations = mutableListOf<UserLiveLocationViewState>()
private var isMapFirstUpdate = true private var isMapFirstUpdate = true
private var onSymbolClickListener: OnSymbolClickListener? = null
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentLiveLocationMapViewBinding { override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentLiveLocationMapViewBinding {
return FragmentLiveLocationMapViewBinding.inflate(layoutInflater, container, false) return FragmentLiveLocationMapViewBinding.inflate(layoutInflater, container, false)
@ -108,6 +110,11 @@ class LiveLocationMapViewFragment @Inject constructor() : VectorBaseFragment<Fra
setupMap() setupMap()
} }
override fun onDestroy() {
onSymbolClickListener?.let { symbolManager?.removeClickListener(it) }
super.onDestroy()
}
private fun setupMap() { private fun setupMap() {
val mapFragment = getOrCreateSupportMapFragment() val mapFragment = getOrCreateSupportMapFragment()
mapFragment.getMapAsync { mapboxMap -> mapFragment.getMapAsync { mapboxMap ->
@ -117,10 +124,10 @@ class LiveLocationMapViewFragment @Inject constructor() : VectorBaseFragment<Fra
this@LiveLocationMapViewFragment.mapboxMap = WeakReference(mapboxMap) this@LiveLocationMapViewFragment.mapboxMap = WeakReference(mapboxMap)
symbolManager = SymbolManager(mapFragment.view as MapView, mapboxMap, style).apply { symbolManager = SymbolManager(mapFragment.view as MapView, mapboxMap, style).apply {
iconAllowOverlap = true iconAllowOverlap = true
addClickListener { onSymbolClickListener = OnSymbolClickListener {
onSymbolClicked(it) onSymbolClicked(it)
true true
} }.also { addClickListener(it) }
} }
pendingLiveLocations pendingLiveLocations
.takeUnless { it.isEmpty() } .takeUnless { it.isEmpty() }