Add CreateRoomScope with data store

This commit is contained in:
Florian Renaud
2023-04-12 14:14:48 +02:00
parent 335eb49b6c
commit 5796789a68
12 changed files with 186 additions and 80 deletions

View File

@@ -0,0 +1,30 @@
/*
* 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.features.createroom.impl
import io.element.android.features.createroom.impl.configureroom.RoomPrivacy
import io.element.android.libraries.matrix.ui.model.MatrixUser
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
data class CreateRoomConfig(
val roomName: String? = null,
val topic: String? = null,
val avatarUrl: String? = null,
val invites: ImmutableList<MatrixUser> = persistentListOf(),
val privacy: RoomPrivacy? = null,
)

View File

@@ -0,0 +1,35 @@
/*
* 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.features.createroom.impl
import io.element.android.features.createroom.impl.di.CreateRoomScope
import io.element.android.libraries.di.SingleIn
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import javax.inject.Inject
@SingleIn(CreateRoomScope::class)
class CreateRoomDataStore @Inject constructor() {
private val createRoomConfigFlow: MutableStateFlow<CreateRoomConfig> = MutableStateFlow(CreateRoomConfig())
fun getCreateRoomConfig(): Flow<CreateRoomConfig> = createRoomConfigFlow
fun setCreateRoomConfig(createRoomConfig: CreateRoomConfig) {
createRoomConfigFlow.tryEmit(createRoomConfig)
}
}

View File

@@ -32,10 +32,13 @@ import io.element.android.anvilannotations.ContributesNode
import io.element.android.features.createroom.api.CreateRoomEntryPoint
import io.element.android.features.createroom.impl.addpeople.AddPeopleNode
import io.element.android.features.createroom.impl.configureroom.ConfigureRoomNode
import io.element.android.features.createroom.impl.di.CreateRoomComponent
import io.element.android.features.createroom.impl.root.CreateRoomRootNode
import io.element.android.libraries.architecture.BackstackNode
import io.element.android.libraries.architecture.animation.rememberDefaultTransitionHandler
import io.element.android.libraries.architecture.bindings
import io.element.android.libraries.architecture.createNode
import io.element.android.libraries.di.DaggerComponentOwner
import io.element.android.libraries.di.SessionScope
import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.ui.model.MatrixUser
@@ -45,14 +48,22 @@ import kotlinx.parcelize.Parcelize
class CreateRoomFlowNode @AssistedInject constructor(
@Assisted buildContext: BuildContext,
@Assisted plugins: List<Plugin>,
) : BackstackNode<CreateRoomFlowNode.NavTarget>(
backstack = BackStack(
initialElement = NavTarget.Root,
savedStateMap = buildContext.savedStateMap,
),
buildContext = buildContext,
plugins = plugins
) {
) : DaggerComponentOwner,
BackstackNode<CreateRoomFlowNode.NavTarget>(
backstack = BackStack(
initialElement = NavTarget.Root,
savedStateMap = buildContext.savedStateMap,
),
buildContext = buildContext,
plugins = plugins
) {
private val component by lazy {
parent!!.bindings<CreateRoomComponent.ParentBindings>().createRoomComponentBuilder().build()
}
override val daggerComponent: Any
get() = component
sealed interface NavTarget : Parcelable {
@Parcelize
@@ -62,7 +73,7 @@ class CreateRoomFlowNode @AssistedInject constructor(
object NewRoom : NavTarget
@Parcelize
data class ConfigureRoom(val users: List<MatrixUser>) : NavTarget
object ConfigureRoom : NavTarget
}
override fun resolve(navTarget: NavTarget, buildContext: BuildContext): Node {
@@ -82,13 +93,13 @@ class CreateRoomFlowNode @AssistedInject constructor(
NavTarget.NewRoom -> {
val callback = object : AddPeopleNode.Callback {
override fun onContinue(selectedUsers: List<MatrixUser>) {
backstack.push(NavTarget.ConfigureRoom(selectedUsers))
backstack.push(NavTarget.ConfigureRoom)
}
}
createNode<AddPeopleNode>(context = buildContext, plugins = listOf(callback))
}
is NavTarget.ConfigureRoom -> {
createNode<ConfigureRoomNode>(context = buildContext, plugins = listOf(ConfigureRoomNode.Inputs(navTarget.users)))
NavTarget.ConfigureRoom -> {
createNode<ConfigureRoomNode>(context = buildContext)
}
}
}

View File

@@ -25,10 +25,10 @@ import com.bumble.appyx.core.plugin.plugins
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import io.element.android.anvilannotations.ContributesNode
import io.element.android.libraries.di.SessionScope
import io.element.android.features.createroom.impl.di.CreateRoomScope
import io.element.android.libraries.matrix.ui.model.MatrixUser
@ContributesNode(SessionScope::class)
@ContributesNode(CreateRoomScope::class)
class AddPeopleNode @AssistedInject constructor(
@Assisted buildContext: BuildContext,
@Assisted plugins: List<Plugin>,

View File

@@ -17,8 +17,12 @@
package io.element.android.features.createroom.impl.addpeople
import androidx.compose.runtime.Composable
import io.element.android.features.userlist.api.SelectionMode
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import io.element.android.features.createroom.impl.CreateRoomConfig
import io.element.android.features.createroom.impl.CreateRoomDataStore
import io.element.android.features.userlist.api.MatrixUserDataSource
import io.element.android.features.userlist.api.SelectionMode
import io.element.android.features.userlist.api.UserListPresenter
import io.element.android.features.userlist.api.UserListPresenterArgs
import io.element.android.libraries.architecture.Presenter
@@ -28,6 +32,7 @@ import javax.inject.Named
class AddPeoplePresenter @Inject constructor(
private val userListPresenterFactory: UserListPresenter.Factory,
@Named("AllUsers") private val matrixUserDataSource: MatrixUserDataSource,
private val dataStore: CreateRoomDataStore,
) : Presenter<AddPeopleState> {
private val userListPresenter by lazy {
@@ -40,7 +45,10 @@ class AddPeoplePresenter @Inject constructor(
@Composable
override fun present(): AddPeopleState {
val userListState = userListPresenter.present()
val createRoomConfig = dataStore.getCreateRoomConfig().collectAsState(CreateRoomConfig())
LaunchedEffect(userListState.selectedUsers) {
dataStore.setCreateRoomConfig(createRoomConfig.value.copy(invites = userListState.selectedUsers))
}
fun handleEvents(event: AddPeopleEvents) {
// do nothing for now
}

View File

@@ -24,27 +24,15 @@ import com.bumble.appyx.core.plugin.Plugin
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import io.element.android.anvilannotations.ContributesNode
import io.element.android.libraries.architecture.NodeInputs
import io.element.android.libraries.architecture.inputs
import io.element.android.libraries.di.SessionScope
import io.element.android.libraries.matrix.ui.model.MatrixUser
import io.element.android.features.createroom.impl.di.CreateRoomScope
@ContributesNode(SessionScope::class)
@ContributesNode(CreateRoomScope::class)
class ConfigureRoomNode @AssistedInject constructor(
@Assisted buildContext: BuildContext,
@Assisted plugins: List<Plugin>,
private val presenterFactory: ConfigureRoomPresenter.Factory,
private val presenter: ConfigureRoomPresenter,
) : Node(buildContext, plugins = plugins) {
data class Inputs(
val selectedUsers: List<MatrixUser>
) : NodeInputs
private val inputs: Inputs = inputs()
private val presenter by lazy {
presenterFactory.create(ConfigureRoomPresenterArgs(inputs.selectedUsers))
}
@Composable
override fun View(modifier: Modifier) {
val state = presenter.present()

View File

@@ -16,54 +16,40 @@
package io.element.android.features.createroom.impl.configureroom
import android.net.Uri
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import io.element.android.features.createroom.impl.CreateRoomConfig
import io.element.android.features.createroom.impl.CreateRoomDataStore
import io.element.android.libraries.architecture.Presenter
import kotlinx.collections.immutable.toImmutableList
import javax.inject.Inject
class ConfigureRoomPresenter @AssistedInject constructor(
@Assisted val args: ConfigureRoomPresenterArgs,
class ConfigureRoomPresenter @Inject constructor(
private val dataStore: CreateRoomDataStore,
) : Presenter<ConfigureRoomState> {
@AssistedFactory
interface Factory {
fun create(args: ConfigureRoomPresenterArgs): ConfigureRoomPresenter
}
@Composable
override fun present(): ConfigureRoomState {
var roomName by rememberSaveable { mutableStateOf("") }
var topic by rememberSaveable { mutableStateOf("") }
var avatarUri by rememberSaveable { mutableStateOf<Uri?>(null) }
var privacy by rememberSaveable { mutableStateOf<RoomPrivacy?>(null) }
val isCreateButtonEnabled by rememberSaveable(roomName, privacy) {
val enabled = roomName.isNotEmpty() && privacy != null
val createRoomConfig = dataStore.getCreateRoomConfig().collectAsState(CreateRoomConfig())
val isCreateButtonEnabled by rememberSaveable(createRoomConfig.value.roomName, createRoomConfig.value.privacy) {
val enabled = createRoomConfig.value.roomName.isNullOrEmpty().not() && createRoomConfig.value.privacy != null
mutableStateOf(enabled)
}
fun handleEvents(event: ConfigureRoomEvents) {
when (event) {
is ConfigureRoomEvents.AvatarUriChanged -> avatarUri = event.uri
is ConfigureRoomEvents.RoomNameChanged -> roomName = event.name
is ConfigureRoomEvents.TopicChanged -> topic = event.topic
is ConfigureRoomEvents.RoomPrivacyChanged -> privacy = event.privacy
ConfigureRoomEvents.CreateRoom -> Unit // TODO
is ConfigureRoomEvents.AvatarUriChanged -> dataStore.setCreateRoomConfig(createRoomConfig.value.copy(avatarUrl = event.uri?.toString()))
is ConfigureRoomEvents.RoomNameChanged -> dataStore.setCreateRoomConfig(createRoomConfig.value.copy(roomName = event.name))
is ConfigureRoomEvents.TopicChanged -> dataStore.setCreateRoomConfig(createRoomConfig.value.copy(topic = event.topic.takeUnless { it.isEmpty() }))
is ConfigureRoomEvents.RoomPrivacyChanged -> dataStore.setCreateRoomConfig(createRoomConfig.value.copy(privacy = event.privacy))
ConfigureRoomEvents.CreateRoom -> Unit
}
}
return ConfigureRoomState(
selectedUsers = args.selectedUsers.toImmutableList(),
roomName = roomName,
topic = topic,
avatarUri = avatarUri,
privacy = privacy,
createRoomConfig.value,
isCreateButtonEnabled = isCreateButtonEnabled,
eventSink = ::handleEvents,
)

View File

@@ -16,16 +16,10 @@
package io.element.android.features.createroom.impl.configureroom
import android.net.Uri
import io.element.android.libraries.matrix.ui.model.MatrixUser
import kotlinx.collections.immutable.ImmutableList
import io.element.android.features.createroom.impl.CreateRoomConfig
data class ConfigureRoomState(
val selectedUsers: ImmutableList<MatrixUser>,
val roomName: String,
val topic: String,
val avatarUri: Uri?,
val privacy: RoomPrivacy?,
val config: CreateRoomConfig,
val isCreateButtonEnabled: Boolean,
val eventSink: (ConfigureRoomEvents) -> Unit
)

View File

@@ -17,8 +17,7 @@
package io.element.android.features.createroom.impl.configureroom
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import io.element.android.libraries.matrix.ui.components.aMatrixUserList
import kotlinx.collections.immutable.toImmutableList
import io.element.android.features.createroom.impl.CreateRoomConfig
open class ConfigureRoomStateProvider : PreviewParameterProvider<ConfigureRoomState> {
override val values: Sequence<ConfigureRoomState>
@@ -28,11 +27,7 @@ open class ConfigureRoomStateProvider : PreviewParameterProvider<ConfigureRoomSt
}
fun aConfigureRoomState() = ConfigureRoomState(
selectedUsers = aMatrixUserList().toImmutableList(),
roomName = "",
topic = "",
avatarUri = null,
privacy = null,
config = CreateRoomConfig(),
isCreateButtonEnabled = false,
eventSink = {}
)

View File

@@ -51,6 +51,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.net.toUri
import coil.compose.AsyncImage
import coil.request.ImageRequest
import io.element.android.features.createroom.impl.R
@@ -92,25 +93,25 @@ fun ConfigureRoomView(
) {
RoomNameWithAvatar(
modifier = Modifier.padding(horizontal = 16.dp),
avatarUri = state.avatarUri,
roomName = state.roomName,
avatarUri = state.config.avatarUrl?.toUri(),
roomName = state.config.roomName.orEmpty(),
onRoomNameChanged = { state.eventSink(ConfigureRoomEvents.RoomNameChanged(it)) },
)
RoomTopic(
modifier = Modifier.padding(horizontal = 16.dp),
topic = state.topic,
topic = state.config.topic.orEmpty(),
onTopicChanged = { state.eventSink(ConfigureRoomEvents.TopicChanged(it)) },
)
SelectedUsersList(
listState = selectedUsersListState,
contentPadding = PaddingValues(horizontal = 24.dp),
selectedUsers = state.selectedUsers,
selectedUsers = state.config.invites,
onUserRemoved = { }, // TODO
)
Spacer(Modifier.weight(1f))
RoomPrivacyOptions(
modifier = Modifier.padding(bottom = 40.dp),
selected = state.privacy,
selected = state.config.privacy,
onOptionSelected = { state.eventSink(ConfigureRoomEvents.RoomPrivacyChanged(it)) },
)
}

View File

@@ -0,0 +1,39 @@
/*
* 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.features.createroom.impl.di
import com.squareup.anvil.annotations.ContributesTo
import com.squareup.anvil.annotations.MergeSubcomponent
import dagger.Subcomponent
import io.element.android.libraries.architecture.NodeFactoriesBindings
import io.element.android.libraries.di.SessionScope
import io.element.android.libraries.di.SingleIn
@SingleIn(CreateRoomScope::class)
@MergeSubcomponent(CreateRoomScope::class)
interface CreateRoomComponent : NodeFactoriesBindings {
@Subcomponent.Builder
interface Builder {
fun build(): CreateRoomComponent
}
@ContributesTo(SessionScope::class)
interface ParentBindings {
fun createRoomComponentBuilder(): Builder
}
}

View File

@@ -0,0 +1,19 @@
/*
* 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.features.createroom.impl.di
abstract class CreateRoomScope private constructor()