From 3ec8c77d1775c533d060203bbcf0c24a41a5163c Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Tue, 28 Jun 2022 17:12:52 +0300 Subject: [PATCH] Code review fix. --- .../location/LocationSharingFragment.kt | 41 ++++++------------- ...iveLocationLabsFlagPromotionBottomSheet.kt | 9 +++- 2 files changed, 20 insertions(+), 30 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 fa121005cb..f881e694b6 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,8 +24,7 @@ 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.fragment.app.setFragmentResultListener import androidx.lifecycle.lifecycleScope import com.airbnb.mvrx.fragmentViewModel import com.airbnb.mvrx.withState @@ -70,38 +69,22 @@ 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) } + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + setFragmentResultListener(LiveLocationLabsFlagPromotionBottomSheet.REQUEST_KEY) { _, bundle -> + val isApproved = bundle.getBoolean(LiveLocationLabsFlagPromotionBottomSheet.BUNDLE_KEY_LABS_APPROVAL) + handleLiveLocationLabsFlagPromotionResult(isApproved) + } + } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - activity?.supportFragmentManager?.registerFragmentLifecycleCallbacks(fragmentLifecycleCallbacks, false) - mapView = WeakReference(views.mapView) views.mapView.onCreate(savedInstanceState) @@ -222,8 +205,8 @@ class LocationSharingFragment @Inject constructor( } } - private fun handleLiveLocationLabsFlagPromotionResult(resultCode: Int, data: Any?) { - if (resultCode == VectorBaseBottomSheetDialogFragment.ResultListener.RESULT_OK && (data as? Boolean) == true) { + private fun handleLiveLocationLabsFlagPromotionResult(isApproved: Boolean) { + if (isApproved) { vectorPreferences.setLiveLocationLabsEnabled(isEnabled = true) startLiveLocationSharing() } 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 77928ec536..cf360ec277 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 @@ -20,6 +20,7 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.fragment.app.setFragmentResult import im.vector.app.core.platform.VectorBaseBottomSheetDialogFragment import im.vector.app.databinding.BottomSheetLiveLocationLabsFlagPromotionBinding @@ -44,12 +45,18 @@ class LiveLocationLabsFlagPromotionBottomSheet : private fun initOkButton() { views.promoteLiveLocationFlagOkButton.debouncedClicks { val enableLabsFlag = views.promoteLiveLocationFlagSwitch.isChecked - resultListener?.onBottomSheetResult(ResultListener.RESULT_OK, enableLabsFlag) + setFragmentResult(REQUEST_KEY, Bundle().apply { + putBoolean(BUNDLE_KEY_LABS_APPROVAL, enableLabsFlag) + }) dismiss() } } companion object { + + const val REQUEST_KEY = "LiveLocationLabsFlagPromotionBottomSheetRequest" + const val BUNDLE_KEY_LABS_APPROVAL = "BUNDLE_KEY_LABS_APPROVAL" + fun newInstance(): LiveLocationLabsFlagPromotionBottomSheet { return LiveLocationLabsFlagPromotionBottomSheet() }