UnreadMessagesSharedViewModel.kt 8.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
 * Copyright (c) 2021 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.home

import com.airbnb.mvrx.ActivityViewModelContext
import com.airbnb.mvrx.FragmentViewModelContext
import com.airbnb.mvrx.MvRxState
import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.ViewModelContext
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import im.vector.app.AppStateHandler
Valere's avatar
Valere committed
28
import im.vector.app.RoomGroupingMethod
29
30
31
import im.vector.app.core.platform.EmptyAction
import im.vector.app.core.platform.EmptyViewEvents
import im.vector.app.core.platform.VectorViewModel
Valere's avatar
Valere committed
32
import im.vector.app.features.settings.VectorPreferences
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
import org.matrix.android.sdk.api.query.ActiveSpaceFilter
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.room.RoomSortOrder
import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams
import org.matrix.android.sdk.api.session.room.summary.RoomAggregateNotificationCount
import org.matrix.android.sdk.rx.asObservable
import java.util.concurrent.TimeUnit

data class UnreadMessagesState(
        val homeSpaceUnread: RoomAggregateNotificationCount = RoomAggregateNotificationCount(0, 0),
        val otherSpacesUnread: RoomAggregateNotificationCount = RoomAggregateNotificationCount(0, 0)
) : MvRxState

data class CountInfo(
        val homeCount: RoomAggregateNotificationCount,
        val otherCount: RoomAggregateNotificationCount
)

class UnreadMessagesSharedViewModel @AssistedInject constructor(@Assisted initialState: UnreadMessagesState,
                                                                session: Session,
Valere's avatar
Valere committed
56
                                                                private val vectorPreferences: VectorPreferences,
Valere's avatar
Valere committed
57
                                                                appStateHandler: AppStateHandler)
58
59
60
61
62
63
64
65
66
67
68
69
    : VectorViewModel<UnreadMessagesState, EmptyAction, EmptyViewEvents>(initialState) {

    @AssistedFactory
    interface Factory {
        fun create(initialState: UnreadMessagesState): UnreadMessagesSharedViewModel
    }

    companion object : MvRxViewModelFactory<UnreadMessagesSharedViewModel, UnreadMessagesState> {

        @JvmStatic
        override fun create(viewModelContext: ViewModelContext, state: UnreadMessagesState): UnreadMessagesSharedViewModel? {
            val factory = when (viewModelContext) {
Valere's avatar
Valere committed
70
71
                is FragmentViewModelContext -> viewModelContext.fragment as? Factory
                is ActivityViewModelContext -> viewModelContext.activity as? Factory
72
73
74
75
76
77
78
79
80
81
82
83
            }
            return factory?.create(state) ?: error("You should let your activity/fragment implements Factory interface")
        }
    }

    override fun handle(action: EmptyAction) {}

    init {

        session.getPagedRoomSummariesLive(
                roomSummaryQueryParams {
                    this.memberships = listOf(Membership.JOIN)
84
                    this.activeSpaceFilter = ActiveSpaceFilter.ActiveSpace(null)
85
86
87
                }, sortOrder = RoomSortOrder.NONE
        ).asObservable()
                .throttleFirst(300, TimeUnit.MILLISECONDS)
Valere's avatar
Valere committed
88
                .execute {
89
90
91
                    val counts = session.getNotificationCountForRooms(
                            roomSummaryQueryParams {
                                this.memberships = listOf(Membership.JOIN)
92
                                this.activeSpaceFilter = ActiveSpaceFilter.ActiveSpace(null)
93
94
                            }
                    )
95
96
97
                    val invites = session.getRoomSummaries(
                            roomSummaryQueryParams {
                                this.memberships = listOf(Membership.INVITE)
98
                                this.activeSpaceFilter = ActiveSpaceFilter.ActiveSpace(null)
99
100
                            }
                    ).size
Valere's avatar
Valere committed
101
102
103
104
105
106
107
                    copy(
                            homeSpaceUnread = RoomAggregateNotificationCount(
                                    counts.notificationCount + invites,
                                    highlightCount = counts.highlightCount + invites
                            )
                    )
                }
108
109

        Observable.combineLatest(
110
111
                appStateHandler.selectedRoomGroupingObservable.distinctUntilChanged(),
                appStateHandler.selectedRoomGroupingObservable.switchMap {
112
113
114
115
116
117
118
119
                    session.getPagedRoomSummariesLive(
                            roomSummaryQueryParams {
                                this.memberships = Membership.activeMemberships()
                            }, sortOrder = RoomSortOrder.NONE
                    ).asObservable()
                            .throttleFirst(300, TimeUnit.MILLISECONDS)
                            .observeOn(Schedulers.computation())
                },
Valere's avatar
Valere committed
120
                { groupingMethod, _ ->
Valere's avatar
Valere committed
121
122
123
124
125
126
                    when (groupingMethod.orNull()) {
                        is RoomGroupingMethod.ByLegacyGroup -> {
                            // currently not supported
                            CountInfo(
                                    RoomAggregateNotificationCount(0, 0),
                                    RoomAggregateNotificationCount(0, 0)
127
                            )
Valere's avatar
Valere committed
128
129
130
                        }
                        is RoomGroupingMethod.BySpace       -> {
                            val selectedSpace = appStateHandler.safeActiveSpaceId()
Valere's avatar
Valere committed
131
132
133
134
135
136

                            val inviteCount = session.getRoomSummaries(roomSummaryQueryParams {
                                this.memberships = listOf(Membership.INVITE)
                            }).size

                            val totalCount = session.getNotificationCountForRooms(
Valere's avatar
Valere committed
137
138
                                    roomSummaryQueryParams {
                                        this.memberships = listOf(Membership.JOIN)
Valere's avatar
Valere committed
139
140
141
                                        this.activeSpaceFilter = ActiveSpaceFilter.ActiveSpace(null).takeIf {
                                            vectorPreferences.labsSpacesOnlyOrphansInHome()
                                        } ?: ActiveSpaceFilter.None
Valere's avatar
Valere committed
142
143
                                    }
                            )
Valere's avatar
Valere committed
144
145
146
147
148

                            val counts = RoomAggregateNotificationCount(
                                    totalCount.notificationCount + inviteCount,
                                    totalCount.highlightCount + inviteCount
                            )
Valere's avatar
Valere committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
                            val rootCounts = session.spaceService().getRootSpaceSummaries()
                                    .filter {
                                        // filter out current selection
                                        it.roomId != selectedSpace
                                    }
                            CountInfo(
                                    homeCount = counts,
                                    otherCount = RoomAggregateNotificationCount(
                                            rootCounts.fold(0, { acc, rs ->
                                                acc + rs.notificationCount
                                            }) + (counts.notificationCount.takeIf { selectedSpace != null } ?: 0),
                                            rootCounts.fold(0, { acc, rs ->
                                                acc + rs.highlightCount
                                            }) + (counts.highlightCount.takeIf { selectedSpace != null } ?: 0)
                                    )
                            )
                        }
                        null                                -> {
                            CountInfo(
                                    RoomAggregateNotificationCount(0, 0),
                                    RoomAggregateNotificationCount(0, 0)
                            )
                        }
                    }
173
174
175
176
177
178
179
180
181
                }
        ).execute {
            copy(
                    homeSpaceUnread = it.invoke()?.homeCount ?: RoomAggregateNotificationCount(0, 0),
                    otherSpacesUnread = it.invoke()?.otherCount ?: RoomAggregateNotificationCount(0, 0)
            )
        }
    }
}