From 2fe03a0016d268d79e1492b9c3b3d265c7e2e0aa Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Tue, 28 Jun 2022 15:06:42 +0300 Subject: [PATCH] Code review fixes. --- .../location/LocationSharingFragment.kt | 35 +++++++++++++++---- ...iveLocationLabsFlagPromotionBottomSheet.kt | 6 ++-- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt index 1deb9fade2..fa121005cb 100644 --- a/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt +++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt @@ -24,6 +24,8 @@ import android.view.View import android.view.ViewGroup import androidx.core.content.ContextCompat import androidx.core.view.isGone +import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentManager import androidx.lifecycle.lifecycleScope import com.airbnb.mvrx.fragmentViewModel import com.airbnb.mvrx.withState @@ -68,6 +70,29 @@ class LocationSharingFragment @Inject constructor( private var hasRenderedUserAvatar = false + private val liveLocationLabsFlagPromotionListener = object : VectorBaseBottomSheetDialogFragment.ResultListener { + override fun onBottomSheetResult(resultCode: Int, data: Any?) { + handleLiveLocationLabsFlagPromotionResult(resultCode, data) + } + } + + private val fragmentLifecycleCallbacks = object : FragmentManager.FragmentLifecycleCallbacks() { + override fun onFragmentResumed(fm: FragmentManager, f: Fragment) { + if (f is LiveLocationLabsFlagPromotionBottomSheet) { + f.resultListener = liveLocationLabsFlagPromotionListener + } + super.onFragmentResumed(fm, f) + + } + + override fun onFragmentPaused(fm: FragmentManager, f: Fragment) { + if (f is LiveLocationLabsFlagPromotionBottomSheet) { + f.resultListener = null + } + super.onFragmentPaused(fm, f) + } + } + override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentLocationSharingBinding { return FragmentLocationSharingBinding.inflate(inflater, container, false) } @@ -75,6 +100,8 @@ class LocationSharingFragment @Inject constructor( override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + activity?.supportFragmentManager?.registerFragmentLifecycleCallbacks(fragmentLifecycleCallbacks, false) + mapView = WeakReference(views.mapView) views.mapView.onCreate(savedInstanceState) @@ -195,12 +222,6 @@ class LocationSharingFragment @Inject constructor( } } - private val liveLocationLabsFlagPromotionListener = object : VectorBaseBottomSheetDialogFragment.ResultListener { - override fun onBottomSheetResult(resultCode: Int, data: Any?) { - handleLiveLocationLabsFlagPromotionResult(resultCode, data) - } - } - private fun handleLiveLocationLabsFlagPromotionResult(resultCode: Int, data: Any?) { if (resultCode == VectorBaseBottomSheetDialogFragment.ResultListener.RESULT_OK && (data as? Boolean) == true) { vectorPreferences.setLiveLocationLabsEnabled(isEnabled = true) @@ -212,7 +233,7 @@ class LocationSharingFragment @Inject constructor( if (vectorPreferences.labsEnableLiveLocation()) { startLiveLocationSharing() } else { - LiveLocationLabsFlagPromotionBottomSheet.newInstance(liveLocationLabsFlagPromotionListener) + LiveLocationLabsFlagPromotionBottomSheet.newInstance() .show(requireActivity().supportFragmentManager, "DISPLAY_LIVE_LOCATION_LABS_FLAG_PROMOTION") } } diff --git a/vector/src/main/java/im/vector/app/features/location/live/LiveLocationLabsFlagPromotionBottomSheet.kt b/vector/src/main/java/im/vector/app/features/location/live/LiveLocationLabsFlagPromotionBottomSheet.kt index 5c11a26d6d..77928ec536 100644 --- a/vector/src/main/java/im/vector/app/features/location/live/LiveLocationLabsFlagPromotionBottomSheet.kt +++ b/vector/src/main/java/im/vector/app/features/location/live/LiveLocationLabsFlagPromotionBottomSheet.kt @@ -50,10 +50,8 @@ class LiveLocationLabsFlagPromotionBottomSheet : } companion object { - fun newInstance(resultListener: ResultListener): LiveLocationLabsFlagPromotionBottomSheet { - return LiveLocationLabsFlagPromotionBottomSheet().also { - it.resultListener = resultListener - } + fun newInstance(): LiveLocationLabsFlagPromotionBottomSheet { + return LiveLocationLabsFlagPromotionBottomSheet() } } }