Improving code in Fragment

This commit is contained in:
Maxime NATUREL 2022-05-23 11:30:57 +02:00
parent 721d7cb6a0
commit b331521e93
1 changed files with 10 additions and 9 deletions

View File

@ -26,7 +26,7 @@ import com.mapbox.mapboxsdk.maps.MapboxMapOptions
import com.mapbox.mapboxsdk.maps.SupportMapFragment import com.mapbox.mapboxsdk.maps.SupportMapFragment
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.extensions.addFragment import im.vector.app.core.extensions.addChildFragment
import im.vector.app.core.platform.VectorBaseFragment import im.vector.app.core.platform.VectorBaseFragment
import im.vector.app.databinding.FragmentLiveLocationMapBinding import im.vector.app.databinding.FragmentLiveLocationMapBinding
import im.vector.app.features.location.UrlMapProvider import im.vector.app.features.location.UrlMapProvider
@ -53,14 +53,7 @@ class LocationLiveMapViewFragment : VectorBaseFragment<FragmentLiveLocationMapBi
} }
private fun setupMap() { private fun setupMap() {
val mapFragment: SupportMapFragment = val mapFragment = getOrCreateSupportMapFragment()
parentFragmentManager.findFragmentByTag(MAP_FRAGMENT_TAG) as? SupportMapFragment
?: run {
val options = MapboxMapOptions.createFromAttributes(requireContext(), null)
val fragment = SupportMapFragment.newInstance(options)
addFragment(R.id.liveLocationMapContainer, fragment, tag = MAP_FRAGMENT_TAG)
fragment
}
mapFragment.getMapAsync { mapBoxMap -> mapFragment.getMapAsync { mapBoxMap ->
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
@ -69,6 +62,14 @@ class LocationLiveMapViewFragment : VectorBaseFragment<FragmentLiveLocationMapBi
} }
} }
private fun getOrCreateSupportMapFragment() =
childFragmentManager.findFragmentByTag(MAP_FRAGMENT_TAG) as? SupportMapFragment
?: run {
val options = MapboxMapOptions.createFromAttributes(requireContext(), null)
SupportMapFragment.newInstance(options)
.also { addChildFragment(R.id.liveLocationMapContainer, it, tag = MAP_FRAGMENT_TAG) }
}
companion object { companion object {
private const val MAP_FRAGMENT_TAG = "im.vector.app.features.location.live.map" private const val MAP_FRAGMENT_TAG = "im.vector.app.features.location.live.map"
} }