Refactoring following review

This commit is contained in:
Valere 2020-11-26 17:39:00 +01:00
parent 4f5632b916
commit 835a36986d
9 changed files with 36 additions and 69 deletions

View File

@ -18,6 +18,7 @@ package im.vector.app.features.home
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.os.Parcelable import android.os.Parcelable
import android.view.MenuItem import android.view.MenuItem
@ -365,14 +366,14 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable, UnknownDeviceDet
} }
} }
override fun navToMemberProfile(userId: String): Boolean { override fun navToMemberProfile(userId: String, deepLink: Uri): Boolean {
val listener = object : MatrixToBottomSheet.InteractionListener { val listener = object : MatrixToBottomSheet.InteractionListener {
override fun navigateToRoom(roomId: String) { override fun navigateToRoom(roomId: String) {
navigator.openRoom(this@HomeActivity, roomId) navigator.openRoom(this@HomeActivity, roomId)
} }
} }
// TODO check if there is already one?? // TODO check if there is already one??
MatrixToBottomSheet.withUserId(userId, listener) MatrixToBottomSheet.withLink(deepLink.toString(), listener)
.show(supportFragmentManager, "HA#MatrixToBottomSheet") .show(supportFragmentManager, "HA#MatrixToBottomSheet")
return true return true
} }

View File

@ -1460,7 +1460,7 @@ class RoomDetailFragment @Inject constructor(
return false return false
} }
override fun navToMemberProfile(userId: String): Boolean { override fun navToMemberProfile(userId: String, deepLink: Uri): Boolean {
openRoomMemberProfile(userId) openRoomMemberProfile(userId)
return true return true
} }

View File

@ -41,8 +41,7 @@ class MatrixToBottomSheet : VectorBaseBottomSheetDialogFragment() {
@Parcelize @Parcelize
data class MatrixToArgs( data class MatrixToArgs(
val matrixToLink: String?, val matrixToLink: String
val userId: String?
) : Parcelable ) : Parcelable
@Inject lateinit var avatarRenderer: AvatarRenderer @Inject lateinit var avatarRenderer: AvatarRenderer
@ -136,20 +135,7 @@ class MatrixToBottomSheet : VectorBaseBottomSheetDialogFragment() {
return MatrixToBottomSheet().apply { return MatrixToBottomSheet().apply {
arguments = Bundle().apply { arguments = Bundle().apply {
putParcelable(MvRx.KEY_ARG, MatrixToBottomSheet.MatrixToArgs( putParcelable(MvRx.KEY_ARG, MatrixToBottomSheet.MatrixToArgs(
matrixToLink = matrixToLink, matrixToLink = matrixToLink
userId = null
))
}
interactionListener = listener
}
}
fun withUserId(userId: String, listener: InteractionListener?): MatrixToBottomSheet {
return MatrixToBottomSheet().apply {
arguments = Bundle().apply {
putParcelable(MvRx.KEY_ARG, MatrixToBottomSheet.MatrixToArgs(
matrixToLink = null,
userId = userId
)) ))
} }
interactionListener = listener interactionListener = listener

View File

@ -22,14 +22,12 @@ import com.airbnb.mvrx.Uninitialized
import org.matrix.android.sdk.api.util.MatrixItem import org.matrix.android.sdk.api.util.MatrixItem
data class MatrixToBottomSheetState( data class MatrixToBottomSheetState(
val userId: String? = null, val deepLink: String,
val deepLink: String? = null,
val matrixItem: Async<MatrixItem> = Uninitialized, val matrixItem: Async<MatrixItem> = Uninitialized,
val startChattingState: Async<Unit> = Uninitialized val startChattingState: Async<Unit> = Uninitialized
) : MvRxState { ) : MvRxState {
constructor(args: MatrixToBottomSheet.MatrixToArgs) : this( constructor(args: MatrixToBottomSheet.MatrixToArgs) : this(
userId = args.userId,
deepLink = args.matrixToLink deepLink = args.matrixToLink
) )
} }

View File

@ -65,10 +65,8 @@ class MatrixToBottomSheetViewModel @AssistedInject constructor(
} }
private suspend fun resolveLink(initialState: MatrixToBottomSheetState) { private suspend fun resolveLink(initialState: MatrixToBottomSheetState) {
when { val permalinkData = PermalinkParser.parse(initialState.deepLink)
initialState.deepLink != null -> { if (permalinkData is PermalinkData.FallbackLink) {
val linkedId = PermalinkParser.parse(initialState.deepLink)
if (linkedId is PermalinkData.FallbackLink) {
setState { setState {
copy( copy(
matrixItem = Fail(IllegalArgumentException(stringProvider.getString(R.string.permalink_malformed))), matrixItem = Fail(IllegalArgumentException(stringProvider.getString(R.string.permalink_malformed))),
@ -78,9 +76,9 @@ class MatrixToBottomSheetViewModel @AssistedInject constructor(
return return
} }
when (linkedId) { when (permalinkData) {
is PermalinkData.UserLink -> { is PermalinkData.UserLink -> {
val user = resolveUser(linkedId.userId) val user = resolveUser(permalinkData.userId)
setState { setState {
copy( copy(
matrixItem = Success(user.toMatrixItem()), matrixItem = Success(user.toMatrixItem()),
@ -90,31 +88,14 @@ class MatrixToBottomSheetViewModel @AssistedInject constructor(
} }
is PermalinkData.RoomLink -> { is PermalinkData.RoomLink -> {
// not yet supported // not yet supported
_viewEvents.post(MatrixToViewEvents.Dismiss)
} }
is PermalinkData.GroupLink -> { is PermalinkData.GroupLink -> {
// not yet supported // not yet supported
_viewEvents.post(MatrixToViewEvents.Dismiss)
} }
is PermalinkData.FallbackLink -> { is PermalinkData.FallbackLink -> {
} _viewEvents.post(MatrixToViewEvents.Dismiss)
}
}
initialState.userId != null -> {
val user = resolveUser(initialState.userId)
setState {
copy(
matrixItem = Success(user.toMatrixItem()),
startChattingState = Success(Unit)
)
}
}
else -> {
setState {
copy(
matrixItem = Fail(IllegalArgumentException(stringProvider.getString(R.string.unexpected_error))),
startChattingState = Uninitialized
)
}
} }
} }
} }

View File

@ -63,13 +63,14 @@ class PermalinkHandler @Inject constructor(private val activeSessionHolder: Acti
.subscribeOn(Schedulers.computation()) .subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.flatMap { permalinkData -> .flatMap { permalinkData ->
handlePermalink(permalinkData, context, navigationInterceptor, buildTask) handlePermalink(permalinkData, deepLink, context, navigationInterceptor, buildTask)
} }
.onErrorReturnItem(false) .onErrorReturnItem(false)
} }
private fun handlePermalink( private fun handlePermalink(
permalinkData: PermalinkData, permalinkData: PermalinkData,
rawLink: Uri,
context: Context, context: Context,
navigationInterceptor: NavigationInterceptor?, navigationInterceptor: NavigationInterceptor?,
buildTask: Boolean buildTask: Boolean
@ -96,7 +97,7 @@ class PermalinkHandler @Inject constructor(private val activeSessionHolder: Acti
Single.just(true) Single.just(true)
} }
is PermalinkData.UserLink -> { is PermalinkData.UserLink -> {
if (navigationInterceptor?.navToMemberProfile(permalinkData.userId) != true) { if (navigationInterceptor?.navToMemberProfile(permalinkData.userId, rawLink) != true) {
navigator.openRoomMemberProfile(userId = permalinkData.userId, roomId = null, context = context, buildTask = buildTask) navigator.openRoomMemberProfile(userId = permalinkData.userId, roomId = null, context = context, buildTask = buildTask)
} }
Single.just(true) Single.just(true)
@ -175,7 +176,7 @@ interface NavigationInterceptor {
/** /**
* Return true if the navigation has been intercepted * Return true if the navigation has been intercepted
*/ */
fun navToMemberProfile(userId: String): Boolean { fun navToMemberProfile(userId: String, deepLink: Uri): Boolean {
return false return false
} }
} }

View File

@ -71,7 +71,7 @@ class UserCodeActivity
UserCodeState.Mode.SCAN -> showFragment(ScanUserCodeFragment::class, Bundle.EMPTY) UserCodeState.Mode.SCAN -> showFragment(ScanUserCodeFragment::class, Bundle.EMPTY)
is UserCodeState.Mode.RESULT -> { is UserCodeState.Mode.RESULT -> {
showFragment(ShowUserCodeFragment::class, Bundle.EMPTY) showFragment(ShowUserCodeFragment::class, Bundle.EMPTY)
MatrixToBottomSheet.withUserId(mode.matrixItem.id, this).show(supportFragmentManager, "MatrixToBottomSheet") MatrixToBottomSheet.withLink(mode.rawLink, this).show(supportFragmentManager, "MatrixToBottomSheet")
} }
} }
} }

View File

@ -155,7 +155,7 @@ class UserCodeSharedViewModel @AssistedInject constructor(
setState { setState {
copy( copy(
mode = UserCodeState.Mode.RESULT(user.toMatrixItem()) mode = UserCodeState.Mode.RESULT(user.toMatrixItem(), action.code)
) )
} }
} }

View File

@ -28,7 +28,7 @@ data class UserCodeState(
sealed class Mode { sealed class Mode {
object SHOW : Mode() object SHOW : Mode()
object SCAN : Mode() object SCAN : Mode()
data class RESULT(val matrixItem: MatrixItem) : Mode() data class RESULT(val matrixItem: MatrixItem, val rawLink: String) : Mode()
} }
constructor(args: UserCodeActivity.Args) : this( constructor(args: UserCodeActivity.Args) : this(