Code review fix.

This commit is contained in:
Onuray Sahin 2022-06-28 17:12:52 +03:00
parent 2fe03a0016
commit 3ec8c77d17
2 changed files with 20 additions and 30 deletions

View File

@ -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()
}

View File

@ -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()
}