From d08a1314293cd38032bc5e5201852f0c83b865ca Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Wed, 22 Mar 2023 11:31:38 +0100 Subject: [PATCH] Remove named injection and provide presenter through a factory --- .../impl/addpeople/AddPeoplePresenter.kt | 10 +++--- .../impl/root/CreateRoomRootPresenter.kt | 12 ++++--- .../impl/addpeople/AddPeoplePresenterTests.kt | 16 +++++++-- .../impl/root/CreateRoomRootPresenterTests.kt | 20 +++++++---- .../api/SelectMultipleUsersPresenter.kt | 21 ------------ .../selectusers/api/SelectUsersPresenter.kt | 8 +++-- ...esenter.kt => SelectUsersPresenterArgs.kt} | 6 ++-- .../DefaultSelectMultipleUsersPresenter.kt | 33 ------------------- .../impl/DefaultSelectSingleUserPresenter.kt | 32 ------------------ .../impl/DefaultSelectUsersPresenter.kt | 18 ++++++++-- .../impl/DefaultSelectUsersPresenterTests.kt | 9 ++--- 11 files changed, 68 insertions(+), 117 deletions(-) delete mode 100644 features/selectusers/api/src/main/kotlin/io/element/android/features/selectusers/api/SelectMultipleUsersPresenter.kt rename features/selectusers/api/src/main/kotlin/io/element/android/features/selectusers/api/{SelectSingleUserPresenter.kt => SelectUsersPresenterArgs.kt} (83%) delete mode 100644 features/selectusers/impl/src/main/kotlin/io/element/android/features/selectusers/impl/DefaultSelectMultipleUsersPresenter.kt delete mode 100644 features/selectusers/impl/src/main/kotlin/io/element/android/features/selectusers/impl/DefaultSelectSingleUserPresenter.kt diff --git a/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/addpeople/AddPeoplePresenter.kt b/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/addpeople/AddPeoplePresenter.kt index 6fab3f6ae8..d16ad164fc 100644 --- a/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/addpeople/AddPeoplePresenter.kt +++ b/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/addpeople/AddPeoplePresenter.kt @@ -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 { + private val selectUsersPresenter by lazy { + selectUsersPresenterFactory.create(SelectUsersPresenterArgs(isMultiSelectionEnabled = true)) + } + @Composable override fun present(): AddPeopleState { val selectUsersState = selectUsersPresenter.present() diff --git a/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenter.kt b/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenter.kt index b7bca3a564..b30483bd61 100644 --- a/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenter.kt +++ b/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenter.kt @@ -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 { + 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) { diff --git a/features/createroom/impl/src/test/kotlin/io/element/android/features/createroom/impl/addpeople/AddPeoplePresenterTests.kt b/features/createroom/impl/src/test/kotlin/io/element/android/features/createroom/impl/addpeople/AddPeoplePresenterTests.kt index a03893f845..d9f40c1dbf 100644 --- a/features/createroom/impl/src/test/kotlin/io/element/android/features/createroom/impl/addpeople/AddPeoplePresenterTests.kt +++ b/features/createroom/impl/src/test/kotlin/io/element/android/features/createroom/impl/addpeople/AddPeoplePresenterTests.kt @@ -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 { diff --git a/features/createroom/impl/src/test/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenterTests.kt b/features/createroom/impl/src/test/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenterTests.kt index d8e171892d..5dc9ec00e9 100644 --- a/features/createroom/impl/src/test/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenterTests.kt +++ b/features/createroom/impl/src/test/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenterTests.kt @@ -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 { diff --git a/features/selectusers/api/src/main/kotlin/io/element/android/features/selectusers/api/SelectMultipleUsersPresenter.kt b/features/selectusers/api/src/main/kotlin/io/element/android/features/selectusers/api/SelectMultipleUsersPresenter.kt deleted file mode 100644 index 08228e048d..0000000000 --- a/features/selectusers/api/src/main/kotlin/io/element/android/features/selectusers/api/SelectMultipleUsersPresenter.kt +++ /dev/null @@ -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 diff --git a/features/selectusers/api/src/main/kotlin/io/element/android/features/selectusers/api/SelectUsersPresenter.kt b/features/selectusers/api/src/main/kotlin/io/element/android/features/selectusers/api/SelectUsersPresenter.kt index 913815c698..be85455d09 100644 --- a/features/selectusers/api/src/main/kotlin/io/element/android/features/selectusers/api/SelectUsersPresenter.kt +++ b/features/selectusers/api/src/main/kotlin/io/element/android/features/selectusers/api/SelectUsersPresenter.kt @@ -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 { -interface SelectUsersPresenter : Presenter + interface Factory { + fun create(args: SelectUsersPresenterArgs): SelectUsersPresenter + } +} diff --git a/features/selectusers/api/src/main/kotlin/io/element/android/features/selectusers/api/SelectSingleUserPresenter.kt b/features/selectusers/api/src/main/kotlin/io/element/android/features/selectusers/api/SelectUsersPresenterArgs.kt similarity index 83% rename from features/selectusers/api/src/main/kotlin/io/element/android/features/selectusers/api/SelectSingleUserPresenter.kt rename to features/selectusers/api/src/main/kotlin/io/element/android/features/selectusers/api/SelectUsersPresenterArgs.kt index a0755e4375..35db78eaac 100644 --- a/features/selectusers/api/src/main/kotlin/io/element/android/features/selectusers/api/SelectSingleUserPresenter.kt +++ b/features/selectusers/api/src/main/kotlin/io/element/android/features/selectusers/api/SelectUsersPresenterArgs.kt @@ -16,6 +16,6 @@ package io.element.android.features.selectusers.api -import io.element.android.libraries.architecture.Presenter - -interface SelectSingleUserPresenter : Presenter +data class SelectUsersPresenterArgs( + val isMultiSelectionEnabled: Boolean, +) diff --git a/features/selectusers/impl/src/main/kotlin/io/element/android/features/selectusers/impl/DefaultSelectMultipleUsersPresenter.kt b/features/selectusers/impl/src/main/kotlin/io/element/android/features/selectusers/impl/DefaultSelectMultipleUsersPresenter.kt deleted file mode 100644 index f2d6be0124..0000000000 --- a/features/selectusers/impl/src/main/kotlin/io/element/android/features/selectusers/impl/DefaultSelectMultipleUsersPresenter.kt +++ /dev/null @@ -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) - diff --git a/features/selectusers/impl/src/main/kotlin/io/element/android/features/selectusers/impl/DefaultSelectSingleUserPresenter.kt b/features/selectusers/impl/src/main/kotlin/io/element/android/features/selectusers/impl/DefaultSelectSingleUserPresenter.kt deleted file mode 100644 index ea538e0e39..0000000000 --- a/features/selectusers/impl/src/main/kotlin/io/element/android/features/selectusers/impl/DefaultSelectSingleUserPresenter.kt +++ /dev/null @@ -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) diff --git a/features/selectusers/impl/src/main/kotlin/io/element/android/features/selectusers/impl/DefaultSelectUsersPresenter.kt b/features/selectusers/impl/src/main/kotlin/io/element/android/features/selectusers/impl/DefaultSelectUsersPresenter.kt index 5e194adcc8..e1522e83d2 100644 --- a/features/selectusers/impl/src/main/kotlin/io/element/android/features/selectusers/impl/DefaultSelectUsersPresenter.kt +++ b/features/selectusers/impl/src/main/kotlin/io/element/android/features/selectusers/impl/DefaultSelectUsersPresenter.kt @@ -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, ) } diff --git a/features/selectusers/impl/src/test/kotlin/io/element/android/features/selectusers/impl/DefaultSelectUsersPresenterTests.kt b/features/selectusers/impl/src/test/kotlin/io/element/android/features/selectusers/impl/DefaultSelectUsersPresenterTests.kt index 4935e9d330..db4e74a5f1 100644 --- a/features/selectusers/impl/src/test/kotlin/io/element/android/features/selectusers/impl/DefaultSelectUsersPresenterTests.kt +++ b/features/selectusers/impl/src/test/kotlin/io/element/android/features/selectusers/impl/DefaultSelectUsersPresenterTests.kt @@ -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().scrollToItem(index = any()) } - val presenter = DefaultSelectUsersPresenter(isMultiSelectionEnabled = false) + val presenter = DefaultSelectUsersPresenter(SelectUsersPresenterArgs(isMultiSelectionEnabled = false)) moleculeFlow(RecompositionClock.Immediate) { presenter.present() }.test {