Cleanup: Use extension, use setArguments() and avoid direct usage of Mavericks.KEY_ARG

This commit is contained in:
Benoit Marty 2021-11-19 17:03:18 +01:00
parent d0f5a10c76
commit 292c09b176

View File

@ -24,7 +24,6 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.airbnb.mvrx.Mavericks
import com.airbnb.mvrx.fragmentViewModel import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.withState import com.airbnb.mvrx.withState
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -33,6 +32,7 @@ import im.vector.app.R
import im.vector.app.core.extensions.commitTransaction import im.vector.app.core.extensions.commitTransaction
import im.vector.app.core.extensions.exhaustive import im.vector.app.core.extensions.exhaustive
import im.vector.app.core.extensions.registerStartForActivityResult import im.vector.app.core.extensions.registerStartForActivityResult
import im.vector.app.core.extensions.toMvRxBundle
import im.vector.app.core.platform.VectorBaseActivity import im.vector.app.core.platform.VectorBaseActivity
import im.vector.app.core.platform.VectorBaseBottomSheetDialogFragment import im.vector.app.core.platform.VectorBaseBottomSheetDialogFragment
import im.vector.app.databinding.BottomSheetVerificationBinding import im.vector.app.databinding.BottomSheetVerificationBinding
@ -178,36 +178,37 @@ class VerificationBottomSheet : VectorBaseBottomSheetDialogFragment<BottomSheetV
} }
if (state.quadSHasBeenReset) { if (state.quadSHasBeenReset) {
showFragment(VerificationConclusionFragment::class, Bundle().apply { showFragment(
putParcelable(Mavericks.KEY_ARG, VerificationConclusionFragment.Args( VerificationConclusionFragment::class,
isSuccessFull = true, VerificationConclusionFragment.Args(
isMe = true, isSuccessFull = true,
cancelReason = null isMe = true,
)) cancelReason = null
}) ))
return@withState return@withState
} }
if (state.userThinkItsNotHim) { if (state.userThinkItsNotHim) {
views.otherUserNameText.text = getString(R.string.dialog_title_warning) views.otherUserNameText.text = getString(R.string.dialog_title_warning)
showFragment(VerificationNotMeFragment::class, Bundle()) showFragment(VerificationNotMeFragment::class)
return@withState return@withState
} }
if (state.userWantsToCancel) { if (state.userWantsToCancel) {
views.otherUserNameText.text = getString(R.string.are_you_sure) views.otherUserNameText.text = getString(R.string.are_you_sure)
showFragment(VerificationCancelFragment::class, Bundle()) showFragment(VerificationCancelFragment::class)
return@withState return@withState
} }
if (state.selfVerificationMode && state.verifyingFrom4S) { if (state.selfVerificationMode && state.verifyingFrom4S) {
showFragment(QuadSLoadingFragment::class, Bundle()) showFragment(QuadSLoadingFragment::class)
return@withState return@withState
} }
if (state.selfVerificationMode && state.verifiedFromPrivateKeys) { if (state.selfVerificationMode && state.verifiedFromPrivateKeys) {
showFragment(VerificationConclusionFragment::class, Bundle().apply { showFragment(
putParcelable(Mavericks.KEY_ARG, VerificationConclusionFragment.Args(true, null, state.isMe)) VerificationConclusionFragment::class,
}) VerificationConclusionFragment.Args(true, null, state.isMe)
)
return@withState return@withState
} }
@ -229,23 +230,27 @@ class VerificationBottomSheet : VectorBaseBottomSheetDialogFragment<BottomSheetV
is VerificationTxState.SendingMac, is VerificationTxState.SendingMac,
is VerificationTxState.MacSent, is VerificationTxState.MacSent,
is VerificationTxState.Verifying -> { is VerificationTxState.Verifying -> {
showFragment(VerificationEmojiCodeFragment::class, Bundle().apply { showFragment(
putParcelable(Mavericks.KEY_ARG, VerificationArgs( VerificationEmojiCodeFragment::class,
state.otherUserMxItem?.id ?: "", VerificationArgs(
// If it was outgoing it.transaction id would be null, but the pending request state.otherUserMxItem?.id ?: "",
// would be updated (from localId to txId) // If it was outgoing it.transaction id would be null, but the pending request
state.pendingRequest.invoke()?.transactionId ?: state.transactionId)) // would be updated (from localId to txId)
}) state.pendingRequest.invoke()?.transactionId ?: state.transactionId
)
)
} }
is VerificationTxState.Verified -> { is VerificationTxState.Verified -> {
showFragment(VerificationConclusionFragment::class, Bundle().apply { showFragment(
putParcelable(Mavericks.KEY_ARG, VerificationConclusionFragment.Args(true, null, state.isMe)) VerificationConclusionFragment::class,
}) VerificationConclusionFragment.Args(true, null, state.isMe)
)
} }
is VerificationTxState.Cancelled -> { is VerificationTxState.Cancelled -> {
showFragment(VerificationConclusionFragment::class, Bundle().apply { showFragment(
putParcelable(Mavericks.KEY_ARG, VerificationConclusionFragment.Args(false, state.sasTransactionState.cancelCode.value, state.isMe)) VerificationConclusionFragment::class,
}) VerificationConclusionFragment.Args(false, state.sasTransactionState.cancelCode.value, state.isMe)
)
} }
} }
@ -254,29 +259,32 @@ class VerificationBottomSheet : VectorBaseBottomSheetDialogFragment<BottomSheetV
when (state.qrTransactionState) { when (state.qrTransactionState) {
is VerificationTxState.QrScannedByOther -> { is VerificationTxState.QrScannedByOther -> {
showFragment(VerificationQrScannedByOtherFragment::class, Bundle()) showFragment(VerificationQrScannedByOtherFragment::class)
return@withState return@withState
} }
is VerificationTxState.Started, is VerificationTxState.Started,
is VerificationTxState.WaitingOtherReciprocateConfirm -> { is VerificationTxState.WaitingOtherReciprocateConfirm -> {
showFragment(VerificationQRWaitingFragment::class, Bundle().apply { showFragment(
putParcelable(Mavericks.KEY_ARG, VerificationQRWaitingFragment.Args( VerificationQRWaitingFragment::class,
isMe = state.isMe, VerificationQRWaitingFragment.Args(
otherUserName = state.otherUserMxItem?.getBestName() ?: "" isMe = state.isMe,
)) otherUserName = state.otherUserMxItem?.getBestName() ?: ""
}) )
)
return@withState return@withState
} }
is VerificationTxState.Verified -> { is VerificationTxState.Verified -> {
showFragment(VerificationConclusionFragment::class, Bundle().apply { showFragment(
putParcelable(Mavericks.KEY_ARG, VerificationConclusionFragment.Args(true, null, state.isMe)) VerificationConclusionFragment::class,
}) VerificationConclusionFragment.Args(true, null, state.isMe)
)
return@withState return@withState
} }
is VerificationTxState.Cancelled -> { is VerificationTxState.Cancelled -> {
showFragment(VerificationConclusionFragment::class, Bundle().apply { showFragment(
putParcelable(Mavericks.KEY_ARG, VerificationConclusionFragment.Args(false, state.qrTransactionState.cancelCode.value, state.isMe)) VerificationConclusionFragment::class,
}) VerificationConclusionFragment.Args(false, state.qrTransactionState.cancelCode.value, state.isMe)
)
return@withState return@withState
} }
else -> Unit else -> Unit
@ -288,12 +296,14 @@ class VerificationBottomSheet : VectorBaseBottomSheetDialogFragment<BottomSheetV
if (state.pendingRequest.invoke()?.cancelConclusion != null) { if (state.pendingRequest.invoke()?.cancelConclusion != null) {
// The request has been declined, we should dismiss // The request has been declined, we should dismiss
views.otherUserNameText.text = getString(R.string.verification_cancelled) views.otherUserNameText.text = getString(R.string.verification_cancelled)
showFragment(VerificationConclusionFragment::class, Bundle().apply { showFragment(
putParcelable(Mavericks.KEY_ARG, VerificationConclusionFragment.Args( VerificationConclusionFragment::class,
false, VerificationConclusionFragment.Args(
state.pendingRequest.invoke()?.cancelConclusion?.value ?: CancelCode.User.value, isSuccessFull = false,
state.isMe)) cancelReason = state.pendingRequest.invoke()?.cancelConclusion?.value ?: CancelCode.User.value,
}) isMe = state.isMe
)
)
return@withState return@withState
} }
@ -303,36 +313,44 @@ class VerificationBottomSheet : VectorBaseBottomSheetDialogFragment<BottomSheetV
if (state.pendingRequest.invoke()?.isReady == true) { if (state.pendingRequest.invoke()?.isReady == true) {
Timber.v("## SAS show bottom sheet for outgoing and ready request") Timber.v("## SAS show bottom sheet for outgoing and ready request")
// Show choose method fragment with waiting // Show choose method fragment with waiting
showFragment(VerificationChooseMethodFragment::class, Bundle().apply { showFragment(
putParcelable(Mavericks.KEY_ARG, VerificationArgs(state.otherUserMxItem?.id VerificationChooseMethodFragment::class,
?: "", state.pendingRequest.invoke()?.transactionId)) VerificationArgs(
}) otherUserId = state.otherUserMxItem?.id ?: "",
verificationId = state.pendingRequest.invoke()?.transactionId
)
)
} else { } else {
// Stay on the start fragment // Stay on the start fragment
showFragment(VerificationRequestFragment::class, Bundle().apply { showFragment(
putParcelable(Mavericks.KEY_ARG, VerificationArgs( VerificationRequestFragment::class,
state.otherUserMxItem?.id ?: "", VerificationArgs(
state.pendingRequest.invoke()?.transactionId, otherUserId = state.otherUserMxItem?.id ?: "",
state.roomId)) verificationId = state.pendingRequest.invoke()?.transactionId,
}) verificationLocalId = state.roomId
)
)
} }
} else if (state.pendingRequest.invoke()?.isIncoming == true) { } else if (state.pendingRequest.invoke()?.isIncoming == true) {
Timber.v("## SAS show bottom sheet for Incoming request") Timber.v("## SAS show bottom sheet for Incoming request")
// For incoming we can switch to choose method because ready is being sent or already sent // For incoming we can switch to choose method because ready is being sent or already sent
showFragment(VerificationChooseMethodFragment::class, Bundle().apply { showFragment(
putParcelable(Mavericks.KEY_ARG, VerificationArgs(state.otherUserMxItem?.id VerificationChooseMethodFragment::class,
?: "", state.pendingRequest.invoke()?.transactionId)) VerificationArgs(
}) otherUserId = state.otherUserMxItem?.id ?: "",
verificationId = state.pendingRequest.invoke()?.transactionId
)
)
} }
super.invalidate() super.invalidate()
} }
private fun showFragment(fragmentClass: KClass<out Fragment>, bundle: Bundle) { private fun showFragment(fragmentClass: KClass<out Fragment>, parcelable: Parcelable? = null) {
if (childFragmentManager.findFragmentByTag(fragmentClass.simpleName) == null) { if (childFragmentManager.findFragmentByTag(fragmentClass.simpleName) == null) {
childFragmentManager.commitTransaction { childFragmentManager.commitTransaction {
replace(R.id.bottomSheetFragmentContainer, replace(R.id.bottomSheetFragmentContainer,
fragmentClass.java, fragmentClass.java,
bundle, parcelable?.toMvRxBundle(),
fragmentClass.simpleName fragmentClass.simpleName
) )
} }
@ -342,37 +360,31 @@ class VerificationBottomSheet : VectorBaseBottomSheetDialogFragment<BottomSheetV
companion object { companion object {
fun withArgs(roomId: String?, otherUserId: String, transactionId: String? = null): VerificationBottomSheet { fun withArgs(roomId: String?, otherUserId: String, transactionId: String? = null): VerificationBottomSheet {
return VerificationBottomSheet().apply { return VerificationBottomSheet().apply {
arguments = Bundle().apply { setArguments(VerificationArgs(
putParcelable(Mavericks.KEY_ARG, VerificationArgs( otherUserId = otherUserId,
otherUserId = otherUserId, roomId = roomId,
roomId = roomId, verificationId = transactionId,
verificationId = transactionId, selfVerificationMode = false
selfVerificationMode = false ))
))
}
} }
} }
fun forSelfVerification(session: Session): VerificationBottomSheet { fun forSelfVerification(session: Session): VerificationBottomSheet {
return VerificationBottomSheet().apply { return VerificationBottomSheet().apply {
arguments = Bundle().apply { setArguments(VerificationArgs(
putParcelable(Mavericks.KEY_ARG, VerificationArgs( otherUserId = session.myUserId,
otherUserId = session.myUserId, selfVerificationMode = true
selfVerificationMode = true ))
))
}
} }
} }
fun forSelfVerification(session: Session, outgoingRequest: String): VerificationBottomSheet { fun forSelfVerification(session: Session, outgoingRequest: String): VerificationBottomSheet {
return VerificationBottomSheet().apply { return VerificationBottomSheet().apply {
arguments = Bundle().apply { setArguments(VerificationArgs(
putParcelable(Mavericks.KEY_ARG, VerificationArgs( otherUserId = session.myUserId,
otherUserId = session.myUserId, selfVerificationMode = true,
selfVerificationMode = true, verificationId = outgoingRequest
verificationId = outgoingRequest ))
))
}
} }
} }