Some clean up on room details
This commit is contained in:
parent
32b09f2e61
commit
105fce0b90
7 changed files with 37 additions and 71 deletions
|
|
@ -18,8 +18,8 @@ package io.element.android.appnav
|
||||||
|
|
||||||
import android.os.Parcelable
|
import android.os.Parcelable
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.DisposableEffect
|
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.bumble.appyx.core.composable.Children
|
import com.bumble.appyx.core.composable.Children
|
||||||
import com.bumble.appyx.core.lifecycle.subscribe
|
import com.bumble.appyx.core.lifecycle.subscribe
|
||||||
import com.bumble.appyx.core.modality.BuildContext
|
import com.bumble.appyx.core.modality.BuildContext
|
||||||
|
|
@ -45,6 +45,7 @@ import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.flow.filter
|
import kotlinx.coroutines.flow.filter
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.parcelize.Parcelize
|
import kotlinx.parcelize.Parcelize
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
|
|
@ -56,7 +57,6 @@ class RoomFlowNode @AssistedInject constructor(
|
||||||
private val roomDetailsEntryPoint: RoomDetailsEntryPoint,
|
private val roomDetailsEntryPoint: RoomDetailsEntryPoint,
|
||||||
private val appNavigationStateService: AppNavigationStateService,
|
private val appNavigationStateService: AppNavigationStateService,
|
||||||
roomMembershipObserver: RoomMembershipObserver,
|
roomMembershipObserver: RoomMembershipObserver,
|
||||||
coroutineScope: CoroutineScope,
|
|
||||||
) : BackstackNode<RoomFlowNode.NavTarget>(
|
) : BackstackNode<RoomFlowNode.NavTarget>(
|
||||||
backstack = BackStack(
|
backstack = BackStack(
|
||||||
initialElement = NavTarget.Messages,
|
initialElement = NavTarget.Messages,
|
||||||
|
|
@ -76,9 +76,6 @@ class RoomFlowNode @AssistedInject constructor(
|
||||||
) : NodeInputs
|
) : NodeInputs
|
||||||
|
|
||||||
private val inputs: Inputs = inputs()
|
private val inputs: Inputs = inputs()
|
||||||
private val timeline = inputs.room.timeline()
|
|
||||||
|
|
||||||
private val roomFlowPresenter = RoomFlowPresenter(inputs.room)
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
lifecycle.subscribe(
|
lifecycle.subscribe(
|
||||||
|
|
@ -95,22 +92,34 @@ class RoomFlowNode @AssistedInject constructor(
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
lifecycleScope.fetchRoomMembers()
|
||||||
roomMembershipObserver.updates
|
roomMembershipObserver.updates
|
||||||
.filter { update -> update.roomId == inputs.room.roomId && !update.isUserInRoom }
|
.filter { update -> update.roomId == inputs.room.roomId && !update.isUserInRoom }
|
||||||
.onEach {
|
.onEach {
|
||||||
navigateUp()
|
navigateUp()
|
||||||
}
|
}
|
||||||
.launchIn(coroutineScope)
|
.launchIn(lifecycleScope)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun CoroutineScope.fetchRoomMembers() = launch {
|
||||||
|
val room = inputs.room
|
||||||
|
room.fetchMembers()
|
||||||
|
.onFailure {
|
||||||
|
Timber.e(it, "Fail to fetch members for room ${room.roomId}")
|
||||||
|
}.onSuccess {
|
||||||
|
Timber.v("Success fetching members for room ${room.roomId}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun resolve(navTarget: NavTarget, buildContext: BuildContext): Node {
|
override fun resolve(navTarget: NavTarget, buildContext: BuildContext): Node {
|
||||||
return when (navTarget) {
|
return when (navTarget) {
|
||||||
NavTarget.Messages -> {
|
NavTarget.Messages -> {
|
||||||
messagesEntryPoint.createNode(this, buildContext, object : MessagesEntryPoint.Callback {
|
val callback = object : MessagesEntryPoint.Callback {
|
||||||
override fun onRoomDetailsClicked() {
|
override fun onRoomDetailsClicked() {
|
||||||
backstack.push(NavTarget.RoomDetails)
|
backstack.push(NavTarget.RoomDetails)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
messagesEntryPoint.createNode(this, buildContext, callback)
|
||||||
}
|
}
|
||||||
NavTarget.RoomDetails -> {
|
NavTarget.RoomDetails -> {
|
||||||
roomDetailsEntryPoint.createNode(this, buildContext, emptyList())
|
roomDetailsEntryPoint.createNode(this, buildContext, emptyList())
|
||||||
|
|
@ -128,7 +137,6 @@ class RoomFlowNode @AssistedInject constructor(
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
override fun View(modifier: Modifier) {
|
override fun View(modifier: Modifier) {
|
||||||
roomFlowPresenter.present()
|
|
||||||
Children(
|
Children(
|
||||||
navModel = backstack,
|
navModel = backstack,
|
||||||
modifier = modifier,
|
modifier = modifier,
|
||||||
|
|
|
||||||
|
|
@ -1,46 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2023 New Vector Ltd
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package io.element.android.appnav
|
|
||||||
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
import io.element.android.libraries.architecture.Presenter
|
|
||||||
import io.element.android.libraries.matrix.api.room.MatrixRoom
|
|
||||||
import timber.log.Timber
|
|
||||||
|
|
||||||
class RoomFlowPresenter(
|
|
||||||
private val room: MatrixRoom,
|
|
||||||
) : Presenter<RoomFlowState> {
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
override fun present(): RoomFlowState {
|
|
||||||
// Preload room members so we can quickly detect if the room is a DM room
|
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
room.fetchMembers()
|
|
||||||
.onFailure {
|
|
||||||
Timber.e(it, "Fail to fetch members for room ${room.roomId}")
|
|
||||||
}.onSuccess {
|
|
||||||
Timber.v("Success fetching members for room ${room.roomId}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return RoomFlowState
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// At first the return type was Unit, but detekt complained about it
|
|
||||||
object RoomFlowState
|
|
||||||
|
|
@ -56,19 +56,16 @@ class RoomDetailsFlowNode @AssistedInject constructor(
|
||||||
object RoomMemberList : NavTarget
|
object RoomMemberList : NavTarget
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Callback : Plugin {
|
|
||||||
fun openRoomMemberList()
|
|
||||||
}
|
|
||||||
|
|
||||||
val callback = object : Callback {
|
|
||||||
override fun openRoomMemberList() {
|
|
||||||
backstack.push(NavTarget.RoomMemberList)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun resolve(navTarget: NavTarget, buildContext: BuildContext): Node {
|
override fun resolve(navTarget: NavTarget, buildContext: BuildContext): Node {
|
||||||
return when (navTarget) {
|
return when (navTarget) {
|
||||||
NavTarget.RoomDetails -> createNode<RoomDetailsNode>(buildContext, listOf(callback))
|
NavTarget.RoomDetails -> {
|
||||||
|
val callback = object : RoomDetailsNode.Callback {
|
||||||
|
override fun openRoomMemberList() {
|
||||||
|
backstack.push(NavTarget.RoomMemberList)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
createNode<RoomDetailsNode>(buildContext, listOf(callback))
|
||||||
|
}
|
||||||
NavTarget.RoomMemberList -> createNode<RoomMemberListNode>(buildContext)
|
NavTarget.RoomMemberList -> createNode<RoomMemberListNode>(buildContext)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,11 @@ class RoomDetailsNode @AssistedInject constructor(
|
||||||
private val room: MatrixRoom,
|
private val room: MatrixRoom,
|
||||||
) : Node(buildContext, plugins = plugins) {
|
) : Node(buildContext, plugins = plugins) {
|
||||||
|
|
||||||
private val callback = plugins<RoomDetailsFlowNode.Callback>().firstOrNull()
|
interface Callback : Plugin {
|
||||||
|
fun openRoomMemberList()
|
||||||
|
}
|
||||||
|
|
||||||
|
private val callback = plugins<Callback>().firstOrNull()
|
||||||
|
|
||||||
private fun openRoomMemberList() {
|
private fun openRoomMemberList() {
|
||||||
callback?.openRoomMemberList()
|
callback?.openRoomMemberList()
|
||||||
|
|
|
||||||
|
|
@ -56,5 +56,5 @@ interface MatrixRoom: Closeable {
|
||||||
|
|
||||||
suspend fun redactEvent(eventId: EventId, reason: String? = null): Result<Unit>
|
suspend fun redactEvent(eventId: EventId, reason: String? = null): Result<Unit>
|
||||||
|
|
||||||
fun leave(): Result<Unit>
|
suspend fun leave(): Result<Unit>
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,7 @@ class RustMatrixRoom(
|
||||||
coroutineDispatchers = coroutineDispatchers
|
coroutineDispatchers = coroutineDispatchers
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun close() {
|
override fun close() {
|
||||||
innerRoom.destroy()
|
innerRoom.destroy()
|
||||||
slidingSyncRoom.destroy()
|
slidingSyncRoom.destroy()
|
||||||
|
|
@ -183,7 +184,9 @@ class RustMatrixRoom(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun leave(): Result<Unit> {
|
override suspend fun leave(): Result<Unit> = withContext(coroutineDispatchers.io) {
|
||||||
return runCatching { innerRoom.leave() }
|
runCatching {
|
||||||
|
innerRoom.leave()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -117,7 +117,7 @@ class FakeMatrixRoom(
|
||||||
return Result.success(Unit)
|
return Result.success(Unit)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun leave(): Result<Unit> = leaveRoomError?.let { Result.failure(it) } ?: Result.success(Unit)
|
override suspend fun leave(): Result<Unit> = leaveRoomError?.let { Result.failure(it) } ?: Result.success(Unit)
|
||||||
|
|
||||||
override fun close() = Unit
|
override fun close() = Unit
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue