Remove named injection and provide presenter through a factory

This commit is contained in:
Florian Renaud 2023-03-22 11:31:38 +01:00
parent a1fe191b64
commit d08a131429
11 changed files with 68 additions and 117 deletions

View file

@ -17,17 +17,19 @@
package io.element.android.features.createroom.impl.addpeople
import androidx.compose.runtime.Composable
import io.element.android.features.selectusers.api.MULTI_SELECTION_USERS_VARIANT
import io.element.android.features.selectusers.api.SelectUsersPresenter
import io.element.android.features.selectusers.api.SelectUsersPresenterArgs
import io.element.android.libraries.architecture.Presenter
import javax.inject.Inject
import javax.inject.Named
class AddPeoplePresenter @Inject constructor(
@Named(MULTI_SELECTION_USERS_VARIANT)
private val selectUsersPresenter: SelectUsersPresenter,
private val selectUsersPresenterFactory: SelectUsersPresenter.Factory,
) : Presenter<AddPeopleState> {
private val selectUsersPresenter by lazy {
selectUsersPresenterFactory.create(SelectUsersPresenterArgs(isMultiSelectionEnabled = true))
}
@Composable
override fun present(): AddPeopleState {
val selectUsersState = selectUsersPresenter.present()

View file

@ -17,22 +17,24 @@
package io.element.android.features.createroom.impl.root
import androidx.compose.runtime.Composable
import io.element.android.features.selectusers.api.SINGLE_SELECTION_USERS_VARIANT
import io.element.android.features.selectusers.api.SelectUsersPresenter
import io.element.android.features.selectusers.api.SelectUsersPresenterArgs
import io.element.android.libraries.architecture.Presenter
import io.element.android.libraries.matrix.ui.model.MatrixUser
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Named
class CreateRoomRootPresenter @Inject constructor(
@Named(SINGLE_SELECTION_USERS_VARIANT)
private val selectUsersPresenter: SelectUsersPresenter,
private val presenterFactory: SelectUsersPresenter.Factory,
) : Presenter<CreateRoomRootState> {
private val presenter by lazy {
presenterFactory.create(SelectUsersPresenterArgs(isMultiSelectionEnabled = false))
}
@Composable
override fun present(): CreateRoomRootState {
val selectUsersState = selectUsersPresenter.present()
val selectUsersState = presenter.present()
fun handleEvents(event: CreateRoomRootEvents) {
when (event) {

View file

@ -22,17 +22,27 @@ import app.cash.molecule.RecompositionClock
import app.cash.molecule.moleculeFlow
import app.cash.turbine.test
import com.google.common.truth.Truth.assertThat
import io.element.android.features.selectusers.impl.DefaultSelectMultipleUsersPresenter
import io.element.android.features.selectusers.api.SelectUsersPresenterArgs
import io.element.android.features.selectusers.impl.DefaultSelectUsersPresenter
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
class AddPeoplePresenterTests {
private lateinit var presenter: AddPeoplePresenter
@Before
fun setup() {
val selectUsersFactory = object : DefaultSelectUsersPresenter.DefaultSelectUsersFactory {
override fun create(args: SelectUsersPresenterArgs) = DefaultSelectUsersPresenter(args)
}
presenter = AddPeoplePresenter(selectUsersFactory)
}
@Test
fun `present - initial state`() = runTest {
val selectUsersPresenter = DefaultSelectMultipleUsersPresenter()
val presenter = AddPeoplePresenter(selectUsersPresenter)
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
}.test {

View file

@ -22,19 +22,29 @@ import app.cash.molecule.RecompositionClock
import app.cash.molecule.moleculeFlow
import app.cash.turbine.test
import com.google.common.truth.Truth.assertThat
import io.element.android.features.selectusers.impl.DefaultSelectSingleUserPresenter
import io.element.android.features.selectusers.api.SelectUsersPresenterArgs
import io.element.android.features.selectusers.impl.DefaultSelectUsersPresenter
import io.element.android.libraries.matrix.api.core.UserId
import io.element.android.libraries.matrix.ui.model.MatrixUser
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
class CreateRoomRootPresenterTests {
private lateinit var presenter: CreateRoomRootPresenter
@Before
fun setup() {
val selectUsersPresenter = object : DefaultSelectUsersPresenter.DefaultSelectUsersFactory {
override fun create(args: SelectUsersPresenterArgs) = DefaultSelectUsersPresenter(args)
}
presenter = CreateRoomRootPresenter(selectUsersPresenter)
}
@Test
fun `present - initial state`() = runTest {
val selectUsersPresenter = DefaultSelectSingleUserPresenter()
val presenter = CreateRoomRootPresenter(selectUsersPresenter)
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
}.test {
@ -45,8 +55,6 @@ class CreateRoomRootPresenterTests {
@Test
fun `present - trigger action buttons`() = runTest {
val selectUsersPresenter = DefaultSelectSingleUserPresenter()
val presenter = CreateRoomRootPresenter(selectUsersPresenter)
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
}.test {
@ -57,8 +65,6 @@ class CreateRoomRootPresenterTests {
@Test
fun `present - trigger start DM action`() = runTest {
val selectUsersPresenter = DefaultSelectSingleUserPresenter()
val presenter = CreateRoomRootPresenter(selectUsersPresenter)
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
}.test {

View file

@ -1,21 +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.features.selectusers.api
import io.element.android.libraries.architecture.Presenter
interface SelectMultipleUsersPresenter : Presenter<SelectUsersState>

View file

@ -18,7 +18,9 @@ package io.element.android.features.selectusers.api
import io.element.android.libraries.architecture.Presenter
const val SINGLE_SELECTION_USERS_VARIANT = "single_selection_users"
const val MULTI_SELECTION_USERS_VARIANT = "multi_selection_users"
interface SelectUsersPresenter : Presenter<SelectUsersState> {
interface SelectUsersPresenter : Presenter<SelectUsersState>
interface Factory {
fun create(args: SelectUsersPresenterArgs): SelectUsersPresenter
}
}

View file

@ -16,6 +16,6 @@
package io.element.android.features.selectusers.api
import io.element.android.libraries.architecture.Presenter
interface SelectSingleUserPresenter : Presenter<SelectUsersState>
data class SelectUsersPresenterArgs(
val isMultiSelectionEnabled: Boolean,
)

View file

@ -1,33 +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.features.selectusers.impl
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.features.selectusers.api.MULTI_SELECTION_USERS_VARIANT
import io.element.android.features.selectusers.api.SelectUsersPresenter
import io.element.android.libraries.di.SessionScope
import javax.inject.Inject
import javax.inject.Named
@ContributesBinding(
scope = SessionScope::class,
boundType = SelectUsersPresenter::class
)
@Named(MULTI_SELECTION_USERS_VARIANT)
class DefaultSelectMultipleUsersPresenter @Inject constructor() :
SelectUsersPresenter by DefaultSelectUsersPresenter(isMultiSelectionEnabled = true)

View file

@ -1,32 +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.features.selectusers.impl
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.features.selectusers.api.SINGLE_SELECTION_USERS_VARIANT
import io.element.android.features.selectusers.api.SelectUsersPresenter
import io.element.android.libraries.di.SessionScope
import javax.inject.Inject
import javax.inject.Named
@ContributesBinding(
scope = SessionScope::class,
boundType = SelectUsersPresenter::class
)
@Named(SINGLE_SELECTION_USERS_VARIANT)
class DefaultSelectSingleUserPresenter @Inject constructor() :
SelectUsersPresenter by DefaultSelectUsersPresenter(isMultiSelectionEnabled = false)

View file

@ -27,9 +27,15 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import com.squareup.anvil.annotations.ContributesBinding
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import io.element.android.features.selectusers.api.SelectUsersEvents
import io.element.android.features.selectusers.api.SelectUsersPresenter
import io.element.android.features.selectusers.api.SelectUsersPresenterArgs
import io.element.android.features.selectusers.api.SelectUsersState
import io.element.android.libraries.di.SessionScope
import io.element.android.libraries.matrix.api.core.MatrixPatterns
import io.element.android.libraries.matrix.api.core.UserId
import io.element.android.libraries.matrix.ui.model.MatrixUser
@ -42,7 +48,15 @@ import kotlinx.collections.immutable.toImmutableSet
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
class DefaultSelectUsersPresenter(private val isMultiSelectionEnabled: Boolean) : SelectUsersPresenter {
class DefaultSelectUsersPresenter @AssistedInject constructor(
@Assisted val args: SelectUsersPresenterArgs,
) : SelectUsersPresenter {
@AssistedFactory
@ContributesBinding(SessionScope::class)
interface DefaultSelectUsersFactory : SelectUsersPresenter.Factory {
override fun create(args: SelectUsersPresenterArgs): DefaultSelectUsersPresenter
}
@Composable
override fun present(): SelectUsersState {
@ -88,7 +102,7 @@ class DefaultSelectUsersPresenter(private val isMultiSelectionEnabled: Boolean)
selectedUsers = selectedUsers.value.reversed().toImmutableSet(),
selectedUsersListState = selectedUsersListState,
isSearchActive = isSearchActive,
isMultiSelectionEnabled = isMultiSelectionEnabled,
isMultiSelectionEnabled = args.isMultiSelectionEnabled,
eventSink = ::handleEvents,
)
}

View file

@ -22,6 +22,7 @@ import app.cash.molecule.moleculeFlow
import app.cash.turbine.test
import com.google.common.truth.Truth.assertThat
import io.element.android.features.selectusers.api.SelectUsersEvents
import io.element.android.features.selectusers.api.SelectUsersPresenterArgs
import io.element.android.libraries.matrix.api.core.UserId
import io.element.android.libraries.matrix.ui.components.aMatrixUser
import io.element.android.libraries.matrix.ui.model.MatrixUser
@ -36,7 +37,7 @@ class DefaultSelectUsersPresenterTests {
@Test
fun `present - initial state for single selection`() = runTest {
val presenter = DefaultSelectUsersPresenter(isMultiSelectionEnabled = false)
val presenter = DefaultSelectUsersPresenter(SelectUsersPresenterArgs(isMultiSelectionEnabled = false))
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
}.test {
@ -51,7 +52,7 @@ class DefaultSelectUsersPresenterTests {
@Test
fun `present - initial state for multiple selection`() = runTest {
val presenter = DefaultSelectUsersPresenter(isMultiSelectionEnabled = true)
val presenter = DefaultSelectUsersPresenter(SelectUsersPresenterArgs(isMultiSelectionEnabled = true))
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
}.test {
@ -66,7 +67,7 @@ class DefaultSelectUsersPresenterTests {
@Test
fun `present - update search query`() = runTest {
val presenter = DefaultSelectUsersPresenter(isMultiSelectionEnabled = false)
val presenter = DefaultSelectUsersPresenter(SelectUsersPresenterArgs(isMultiSelectionEnabled = false))
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
}.test {
@ -95,7 +96,7 @@ class DefaultSelectUsersPresenterTests {
mockkConstructor(LazyListState::class)
coJustRun { anyConstructed<LazyListState>().scrollToItem(index = any()) }
val presenter = DefaultSelectUsersPresenter(isMultiSelectionEnabled = false)
val presenter = DefaultSelectUsersPresenter(SelectUsersPresenterArgs(isMultiSelectionEnabled = false))
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
}.test {