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.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Parcelable
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 {
override fun navigateToRoom(roomId: String) {
navigator.openRoom(this@HomeActivity, roomId)
}
}
// TODO check if there is already one??
MatrixToBottomSheet.withUserId(userId, listener)
MatrixToBottomSheet.withLink(deepLink.toString(), listener)
.show(supportFragmentManager, "HA#MatrixToBottomSheet")
return true
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -71,7 +71,7 @@ class UserCodeActivity
UserCodeState.Mode.SCAN -> showFragment(ScanUserCodeFragment::class, Bundle.EMPTY)
is UserCodeState.Mode.RESULT -> {
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 {
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 {
object SHOW : 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(