From c74cdb9540cbf34b711dde2313ea1e043b174a18 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL <46314705+mnaturel@users.noreply.github.com> Date: Wed, 22 Feb 2023 10:44:55 +0100 Subject: [PATCH] Observe the spaces invites --- .../features/home/NewHomeDetailViewModel.kt | 19 +++++++++- .../app/features/spaces/GetSpacesUseCase.kt | 37 +++++++++++++++++++ .../app/features/spaces/SpaceListViewModel.kt | 8 ++-- 3 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/features/spaces/GetSpacesUseCase.kt diff --git a/vector/src/main/java/im/vector/app/features/home/NewHomeDetailViewModel.kt b/vector/src/main/java/im/vector/app/features/home/NewHomeDetailViewModel.kt index 6f3ba79cf8..7f68accd90 100644 --- a/vector/src/main/java/im/vector/app/features/home/NewHomeDetailViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/NewHomeDetailViewModel.kt @@ -25,16 +25,20 @@ import im.vector.app.core.di.hiltMavericksViewModelFactory import im.vector.app.core.platform.EmptyAction import im.vector.app.core.platform.EmptyViewEvents import im.vector.app.core.platform.VectorViewModel -import im.vector.app.features.roomprofile.polls.RoomPollsViewModel -import im.vector.app.features.roomprofile.polls.RoomPollsViewState +import im.vector.app.features.spaces.GetSpacesUseCase import im.vector.app.features.spaces.notification.GetNotificationCountForSpacesUseCase import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach +import org.matrix.android.sdk.api.query.QueryStringValue import org.matrix.android.sdk.api.query.SpaceFilter +import org.matrix.android.sdk.api.session.room.model.Membership +import org.matrix.android.sdk.api.session.room.spaceSummaryQueryParams +// TODO add unit tests class NewHomeDetailViewModel @AssistedInject constructor( @Assisted initialState: NewHomeDetailViewState, private val getNotificationCountForSpacesUseCase: GetNotificationCountForSpacesUseCase, + private val getSpacesUseCase: GetSpacesUseCase, ) : VectorViewModel(initialState) { @AssistedFactory @@ -46,6 +50,7 @@ class NewHomeDetailViewModel @AssistedInject constructor( init { observeSpacesNotificationCount() + observeSpacesInvite() } private fun observeSpacesNotificationCount() { @@ -54,6 +59,16 @@ class NewHomeDetailViewModel @AssistedInject constructor( .launchIn(viewModelScope) } + private fun observeSpacesInvite() { + val params = spaceSummaryQueryParams { + memberships = listOf(Membership.INVITE) + displayName = QueryStringValue.IsNotEmpty + } + getSpacesUseCase.execute(params) + .onEach { setState { copy(hasPendingSpaceInvites = it.isNotEmpty()) } } + .launchIn(viewModelScope) + } + override fun handle(action: EmptyAction) { // do nothing } diff --git a/vector/src/main/java/im/vector/app/features/spaces/GetSpacesUseCase.kt b/vector/src/main/java/im/vector/app/features/spaces/GetSpacesUseCase.kt new file mode 100644 index 0000000000..d3f462f9f9 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/spaces/GetSpacesUseCase.kt @@ -0,0 +1,37 @@ +/* + * 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 im.vector.app.features.spaces + +import im.vector.app.core.di.ActiveSessionHolder +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.emptyFlow +import org.matrix.android.sdk.api.session.room.model.RoomSummary +import org.matrix.android.sdk.api.session.space.SpaceSummaryQueryParams +import org.matrix.android.sdk.flow.flow +import javax.inject.Inject + +// TODO add unit tests +class GetSpacesUseCase @Inject constructor( + private val activeSessionHolder: ActiveSessionHolder, +) { + + fun execute(params: SpaceSummaryQueryParams): Flow> { + val session = activeSessionHolder.getSafeActiveSession() + + return session?.flow()?.liveSpaceSummaries(params) ?: emptyFlow() + } +} diff --git a/vector/src/main/java/im/vector/app/features/spaces/SpaceListViewModel.kt b/vector/src/main/java/im/vector/app/features/spaces/SpaceListViewModel.kt index e5ebce0c11..b2776e5f87 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/SpaceListViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/SpaceListViewModel.kt @@ -29,7 +29,6 @@ import im.vector.app.core.di.hiltMavericksViewModelFactory import im.vector.app.core.platform.VectorViewModel import im.vector.app.features.analytics.AnalyticsTracker import im.vector.app.features.analytics.plan.Interaction -import im.vector.app.features.invite.AutoAcceptInvites import im.vector.app.features.session.coroutineScope import im.vector.app.features.settings.VectorPreferences import im.vector.app.features.spaces.notification.GetNotificationCountForSpacesUseCase @@ -53,16 +52,15 @@ import org.matrix.android.sdk.api.session.space.SpaceOrderUtils import org.matrix.android.sdk.api.session.space.model.SpaceOrderContent import org.matrix.android.sdk.api.session.space.model.TopLevelSpaceComparator import org.matrix.android.sdk.api.util.toMatrixItem -import org.matrix.android.sdk.flow.flow class SpaceListViewModel @AssistedInject constructor( @Assisted initialState: SpaceListViewState, private val spaceStateHandler: SpaceStateHandler, private val session: Session, private val vectorPreferences: VectorPreferences, - private val autoAcceptInvites: AutoAcceptInvites, private val analyticsTracker: AnalyticsTracker, - private val getNotificationCountForSpacesUseCase: GetNotificationCountForSpacesUseCase, + getNotificationCountForSpacesUseCase: GetNotificationCountForSpacesUseCase, + private val getSpacesUseCase: GetSpacesUseCase, ) : VectorViewModel(initialState) { @AssistedFactory @@ -238,7 +236,7 @@ class SpaceListViewModel @AssistedInject constructor( } combine( - session.flow().liveSpaceSummaries(params), + getSpacesUseCase.execute(params), session.accountDataService() .getLiveRoomAccountDataEvents(setOf(RoomAccountDataTypes.EVENT_TYPE_SPACE_ORDER)) .asFlow()