Try mvrx mock/launcher on RoomProfileFragment

This commit is contained in:
ganfra 2022-08-24 10:48:13 +02:00
parent 45e3af9a81
commit 05d8f1b355
6 changed files with 141 additions and 39 deletions

View File

@ -37,7 +37,7 @@ import org.matrix.android.sdk.api.util.toOptional
typealias ThreadRootEvent = TimelineEvent
class FlowRoom(private val room: Room) {
class FlowRoom(val room: Room) {
fun liveRoomSummary(): Flow<Optional<RoomSummary>> {
return room.getRoomSummaryLive().asFlow()

View File

@ -121,7 +121,6 @@ import im.vector.app.features.roomdirectory.roompreview.RoomPreviewNoPreviewFrag
import im.vector.app.features.roommemberprofile.RoomMemberProfileFragment
import im.vector.app.features.roommemberprofile.devices.DeviceListFragment
import im.vector.app.features.roommemberprofile.devices.DeviceTrustInfoActionFragment
import im.vector.app.features.roomprofile.RoomProfileFragment
import im.vector.app.features.roomprofile.alias.RoomAliasFragment
import im.vector.app.features.roomprofile.banned.RoomBannedMemberListFragment
import im.vector.app.features.roomprofile.members.RoomMemberListFragment
@ -547,11 +546,6 @@ interface FragmentModule {
@FragmentKey(PublicRoomsFragment::class)
fun bindPublicRoomsFragment(fragment: PublicRoomsFragment): Fragment
@Binds
@IntoMap
@FragmentKey(RoomProfileFragment::class)
fun bindRoomProfileFragment(fragment: RoomProfileFragment): Fragment
@Binds
@IntoMap
@FragmentKey(RoomMemberListFragment::class)

View File

@ -29,8 +29,12 @@ import androidx.fragment.app.setFragmentResultListener
import androidx.lifecycle.lifecycleScope
import com.airbnb.mvrx.args
import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.mocking.MavericksViewMocks
import com.airbnb.mvrx.mocking.MockableMavericksView
import com.airbnb.mvrx.mocking.mockSingleViewModel
import com.airbnb.mvrx.withState
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.core.animations.AppBarStateChangeListener
import im.vector.app.core.animations.MatrixItemAppBarStateChangeListener
@ -52,6 +56,8 @@ import im.vector.app.features.home.room.detail.RoomDetailPendingActionStore
import im.vector.app.features.home.room.detail.upgrade.MigrateRoomBottomSheet
import im.vector.app.features.home.room.list.actions.RoomListQuickActionsSharedAction
import im.vector.app.features.home.room.list.actions.RoomListQuickActionsSharedActionViewModel
import im.vector.app.features.roomprofile.mocks.mockRoomProfileArgs
import im.vector.app.features.roomprofile.mocks.mockRoomProfileViewState
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.parcelize.Parcelize
@ -65,15 +71,17 @@ data class RoomProfileArgs(
val roomId: String
) : Parcelable
class RoomProfileFragment @Inject constructor(
private val roomProfileController: RoomProfileController,
private val avatarRenderer: AvatarRenderer,
private val roomDetailPendingActionStore: RoomDetailPendingActionStore,
) :
@AndroidEntryPoint
class RoomProfileFragment :
VectorBaseFragment<FragmentMatrixProfileBinding>(),
MockableMavericksView,
RoomProfileController.Callback,
VectorMenuProvider {
@Inject lateinit var roomProfileController: RoomProfileController
@Inject lateinit var avatarRenderer: AvatarRenderer
@Inject lateinit var roomDetailPendingActionStore: RoomDetailPendingActionStore
private lateinit var headerViews: ViewStubRoomProfileHeaderBinding
private val roomProfileArgs: RoomProfileArgs by args()
@ -83,6 +91,12 @@ class RoomProfileFragment @Inject constructor(
private var appBarStateChangeListener: AppBarStateChangeListener? = null
override fun provideMocks() = mockSingleViewModel(
viewModelReference = RoomProfileFragment::roomProfileViewModel,
defaultState = mockRoomProfileViewState,
defaultArgs = mockRoomProfileArgs
){}
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentMatrixProfileBinding {
return FragmentMatrixProfileBinding.inflate(inflater, container, false)
}
@ -91,6 +105,7 @@ class RoomProfileFragment @Inject constructor(
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
registerMockPrinter()
analyticsScreenName = MobileScreen.ScreenName.RoomDetails
setFragmentResultListener(MigrateRoomBottomSheet.REQUEST_KEY) { _, bundle ->
bundle.getString(MigrateRoomBottomSheet.BUNDLE_KEY_REPLACEMENT_ROOM)?.let { replacementRoomId ->

View File

@ -35,11 +35,13 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.getRoom
import org.matrix.android.sdk.api.session.room.Room
import org.matrix.android.sdk.api.session.room.getStateEvent
import org.matrix.android.sdk.api.session.room.members.roomMemberQueryParams
import org.matrix.android.sdk.api.session.room.model.Membership
@ -67,18 +69,20 @@ class RoomProfileViewModel @AssistedInject constructor(
companion object : MavericksViewModelFactory<RoomProfileViewModel, RoomProfileViewState> by hiltMavericksViewModelFactory()
private val room = session.getRoom(initialState.roomId)!!
private val room = session.getRoom(initialState.roomId)
init {
val flowRoom = room.flow()
observeRoomSummary(flowRoom)
observeRoomCreateContent(flowRoom)
observeBannedRoomMembers(flowRoom)
observePermissions()
observePowerLevels()
room?.also { room ->
val flowRoom = room.flow()
observeRoomSummary(flowRoom)
observeRoomCreateContent(flowRoom)
observeBannedRoomMembers(flowRoom)
observePermissions(room)
observePowerLevels(room)
}
}
private fun observePowerLevels() {
private fun observePowerLevels(room: Room) {
val powerLevelsContentLive = PowerLevelsFlowFactory(room).createFlow()
powerLevelsContentLive
.onEach {
@ -98,10 +102,10 @@ class RoomProfileViewModel @AssistedInject constructor(
copy(
roomCreateContent = async,
// This is a shortcut, we should do the next lines elsewhere, but keep it like that for the moment.
recommendedRoomVersion = room.roomVersionService().getRecommendedVersion(),
isUsingUnstableRoomVersion = room.roomVersionService().isUsingUnstableRoomVersion(),
canUpgradeRoom = room.roomVersionService().userMayUpgradeRoom(session.myUserId),
isTombstoned = room.getStateEvent(EventType.STATE_ROOM_TOMBSTONE, QueryStringValue.IsEmpty) != null
recommendedRoomVersion = flowRoom.room.roomVersionService().getRecommendedVersion(),
isUsingUnstableRoomVersion = flowRoom.room.roomVersionService().isUsingUnstableRoomVersion(),
canUpgradeRoom = flowRoom.room.roomVersionService().userMayUpgradeRoom(session.myUserId),
isTombstoned = flowRoom.room.getStateEvent(EventType.STATE_ROOM_TOMBSTONE, QueryStringValue.IsEmpty) != null
)
}
}
@ -121,7 +125,7 @@ class RoomProfileViewModel @AssistedInject constructor(
}
}
private fun observePermissions() {
private fun observePermissions(room: Room) {
PowerLevelsFlowFactory(room)
.createFlow()
.setOnEach {
@ -134,21 +138,22 @@ class RoomProfileViewModel @AssistedInject constructor(
}
override fun handle(action: RoomProfileAction) {
val room = this.room ?: return
when (action) {
is RoomProfileAction.EnableEncryption -> handleEnableEncryption()
RoomProfileAction.LeaveRoom -> handleLeaveRoom()
is RoomProfileAction.ChangeRoomNotificationState -> handleChangeNotificationMode(action)
is RoomProfileAction.EnableEncryption -> handleEnableEncryption(room)
RoomProfileAction.LeaveRoom -> handleLeaveRoom(room)
is RoomProfileAction.ChangeRoomNotificationState -> handleChangeNotificationMode(room, action)
is RoomProfileAction.ShareRoomProfile -> handleShareRoomProfile()
RoomProfileAction.CreateShortcut -> handleCreateShortcut()
RoomProfileAction.RestoreEncryptionState -> restoreEncryptionState()
RoomProfileAction.RestoreEncryptionState -> restoreEncryptionState(room)
}
}
fun isPublicRoom(): Boolean {
return room.stateService().isPublic()
return room?.stateService()?.isPublic().orFalse()
}
private fun handleEnableEncryption() {
private fun handleEnableEncryption(room: Room) {
postLoading(true)
viewModelScope.launch {
@ -168,15 +173,14 @@ class RoomProfileViewModel @AssistedInject constructor(
private fun handleCreateShortcut() {
viewModelScope.launch(Dispatchers.IO) {
withState { state ->
state.roomSummary()
?.let { shortcutCreator.create(it) }
?.let { _viewEvents.post(RoomProfileViewEvents.OnShortcutReady(it)) }
}
val state = awaitState()
state.roomSummary()
?.let { shortcutCreator.create(it) }
?.let { _viewEvents.post(RoomProfileViewEvents.OnShortcutReady(it)) }
}
}
private fun handleChangeNotificationMode(action: RoomProfileAction.ChangeRoomNotificationState) {
private fun handleChangeNotificationMode(room: Room, action: RoomProfileAction.ChangeRoomNotificationState) {
viewModelScope.launch {
try {
room.roomPushRuleService().setRoomNotificationState(action.notificationState)
@ -186,7 +190,7 @@ class RoomProfileViewModel @AssistedInject constructor(
}
}
private fun handleLeaveRoom() {
private fun handleLeaveRoom(room: Room) {
_viewEvents.post(RoomProfileViewEvents.Loading(stringProvider.getString(R.string.room_profile_leaving_room)))
viewModelScope.launch {
try {
@ -212,7 +216,7 @@ class RoomProfileViewModel @AssistedInject constructor(
}
}
private fun restoreEncryptionState() {
private fun restoreEncryptionState(room: Room) {
_viewEvents.post(RoomProfileViewEvents.Loading())
session.coroutineScope.launch {
try {

View File

@ -0,0 +1,6 @@
package im.vector.app.features.roomprofile.mocks
import im.vector.app.features.roomprofile.RoomProfileArgs
import kotlin.String
val mockRoomProfileArgs by lazy { RoomProfileArgs(roomId = "!CWLUCoEWXSFyTCOtfL:matrix.org") }

View File

@ -0,0 +1,83 @@
package im.vector.app.features.roomprofile.mocks
import com.airbnb.mvrx.Success
import im.vector.app.features.roomprofile.RoomProfileViewState
import kotlin.Boolean
import kotlin.Int
import kotlin.Long
import kotlin.String
import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.events.model.UnsignedData
import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.model.RoomEncryptionAlgorithm
import org.matrix.android.sdk.api.session.room.model.RoomJoinRules
import org.matrix.android.sdk.api.session.room.model.RoomSummary
import org.matrix.android.sdk.api.session.room.model.VersioningState
import org.matrix.android.sdk.api.session.room.model.create.Predecessor
import org.matrix.android.sdk.api.session.room.model.create.RoomCreateContent
import org.matrix.android.sdk.api.session.room.sender.SenderInfo
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
val mockRoomProfileViewState by lazy { RoomProfileViewState(
roomId = "!CWLUCoEWXSFyTCOtfL:matrix.org",
roomSummary = Success(
value = RoomSummary(
roomId = "!CWLUCoEWXSFyTCOtfL:matrix.org",
displayName = "Megolm test (E2E Encryption Testfest)",
name = "Megolm test (E2E Encryption Testfest)",
topic = "Test room! Please discuss issues at #e2e:matrix.org. Requires synapse 0.18.0 or later. (0.18.4 for encrypted images); Latest version is recommended.",
avatarUrl = "",
canonicalAlias = "#megolm:matrix.org",
joinRules = RoomJoinRules.PUBLIC,
joinedMembersCount = 199,
latestPreviewableEvent = TimelineEvent(
root = Event(
type = "m.room.encrypted",
eventId = "\$zo2MIeXtdl9dWNhz1cbY3YT55rlpQLG8XpYBTFN-JXI",
content = mutableMapOf("algorithm" to "m.megolm.v1.aes-sha2","ciphertext" to "AwgAEoADprWrq3ZJ4BLD/w0+Lx9H/3mXZpnEQdgbF2FoWOPB1fuki6/1w7vikL1Sb6rP5AwMuA4TPJQ14Pl++/TCGCUzRoZE9m8TsYe0nQXKb2VWRDB6u7i1ib27l3epbtQAIOYfKuCg6MnbMmadef9fEA1K4lHl3xxf0SdWF9PQKGq3nf9rhcZwa3AZW+FDepI094NO98bTHmssKJ5YTp1ChStjALy4FwdcfanlD5WWPHbKqij/RIC63FNAspIOUeb3AkBmcAdoUctBrxXml+NpJKKCGKxXvFudeSkiqHMG","device_id" to "QBOXOIATIX","sender_key" to "dGWOgnrcFYOdsYAfwvKJQcDr4dwuZ0R743B3ZlGSMWU","session_id" to "Eg1c1yyhczbhGt2baH4m6qI4TpJxN0Kb59TxZ3JY5lE"),
originServerTs = 1661232614808,
senderId = "@DatseMultimedia:matrix.org",
roomId = "!CWLUCoEWXSFyTCOtfL:matrix.org",
unsignedData = UnsignedData(age = 7112957)
),
localId = 133,
eventId = "\$zo2MIeXtdl9dWNhz1cbY3YT55rlpQLG8XpYBTFN-JXI",
displayIndex = 11,
senderInfo = SenderInfo(
userId = "@DatseMultimedia:matrix.org",
displayName = "Jigme Datse (they/them)",
isUniqueDisplayName = true,
avatarUrl = "mxc://matrix.org/aIPbYElyzjKrOWPaWkxbWEqy"
)
),
otherMemberIds = listOf(
"@zocker1999:matrix.org",
"@denisea:element.io",
"@+:jae.fi"
),
membership = Membership.JOIN,
versioningState = VersioningState.NONE,
readMarkerId = "\$-wLehwJjk4gGG8igOfp-giNzGQruvlZMI-Vti1YYxHA",
isEncrypted = true,
encryptionEventTs = 1574382534883,
typingUsers = listOf(
),
roomEncryptionTrustLevel = RoomEncryptionTrustLevel.Default,
roomEncryptionAlgorithm = RoomEncryptionAlgorithm.Megolm
)
),
roomCreateContent = Success(
value = RoomCreateContent(
creator = "@abuse:matrix.org",
roomVersion = "5",
predecessor = Predecessor(roomId = "!UCnwUWwIKhcpaPTHtR:sw1v.org",eventId = "\$1574382534206ivbfT:matrix.org")
)
),
bannedMembership = Success(value = listOf(
)),
actionPermissions = RoomProfileViewState.ActionPermissions(),
recommendedRoomVersion = "9"
) }