~evilham/unchat-android

835a36986da73ea41db70bc95f900ad3c9b4d7e8 — Valere 3 years ago 4f5632b
Refactoring following review

M vector/src/main/java/im/vector/app/features/home/HomeActivity.kt => vector/src/main/java/im/vector/app/features/home/HomeActivity.kt +3 -2
@@ 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
    }

M vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt => vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt +1 -1
@@ 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
                    }

M vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheet.kt => vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheet.kt +2 -16
@@ 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

M vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheetState.kt => vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheetState.kt +1 -3
@@ 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
    )
}

M vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheetViewModel.kt => vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheetViewModel.kt +22 -41
@@ 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)
            }
        }
    }

M vector/src/main/java/im/vector/app/features/permalink/PermalinkHandler.kt => vector/src/main/java/im/vector/app/features/permalink/PermalinkHandler.kt +4 -3
@@ 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
    }
}

M vector/src/main/java/im/vector/app/features/usercode/UserCodeActivity.kt => vector/src/main/java/im/vector/app/features/usercode/UserCodeActivity.kt +1 -1
@@ 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")
                }
            }
        }

M vector/src/main/java/im/vector/app/features/usercode/UserCodeSharedViewModel.kt => vector/src/main/java/im/vector/app/features/usercode/UserCodeSharedViewModel.kt +1 -1
@@ 155,7 155,7 @@ class UserCodeSharedViewModel @AssistedInject constructor(

                    setState {
                        copy(
                                mode = UserCodeState.Mode.RESULT(user.toMatrixItem())
                                mode = UserCodeState.Mode.RESULT(user.toMatrixItem(), action.code)
                        )
                    }
                }

M vector/src/main/java/im/vector/app/features/usercode/UserCodeState.kt => vector/src/main/java/im/vector/app/features/usercode/UserCodeState.kt +1 -1
@@ 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(