Skip to content

Commit 2b003fb

Browse files
LunarXKevinBoulongne
authored andcommitted
Clean and optimize menu drawers flows
1 parent cce0e3e commit 2b003fb

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/FolderController.kt

+6-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import io.realm.kotlin.query.RealmQuery
3636
import io.realm.kotlin.query.RealmResults
3737
import io.realm.kotlin.query.RealmSingleQuery
3838
import kotlinx.coroutines.flow.Flow
39-
import kotlinx.coroutines.flow.combine
4039
import kotlinx.coroutines.flow.mapNotNull
4140
import javax.inject.Inject
4241

@@ -50,13 +49,12 @@ class FolderController @Inject constructor(
5049
return getFoldersQuery(mailboxContentRealm(), withoutChildren = true).asFlow()
5150
}
5251

53-
fun getMenuDrawerFolders(): Flow<Pair<ResultsChange<Folder>, ResultsChange<Folder>>> {
54-
val defaultFoldersFlow = getDefaultFoldersQuery(mailboxContentRealm()).asFlow()
55-
val customFoldersFlow = getCustomFoldersQuery(mailboxContentRealm()).asFlow()
56-
return defaultFoldersFlow.combine(
57-
flow = customFoldersFlow,
58-
transform = { defaultFolders, customFolders -> defaultFolders to customFolders }
59-
)
52+
fun getMenuDrawerDefaultFolders(): Flow<ResultsChange<Folder>> {
53+
return getDefaultFoldersQuery(mailboxContentRealm()).asFlow()
54+
}
55+
56+
fun getMenuDrawerCustomFolders(): Flow<ResultsChange<Folder>> {
57+
return getCustomFoldersQuery(mailboxContentRealm()).asFlow()
6058
}
6159

6260
fun getMoveFolders(): RealmResults<Folder> {

app/src/main/java/com/infomaniak/mail/ui/MainViewModel.kt

+12-11
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
8383
import kotlinx.coroutines.Job
8484
import kotlinx.coroutines.delay
8585
import kotlinx.coroutines.flow.MutableStateFlow
86+
import kotlinx.coroutines.flow.combine
8687
import kotlinx.coroutines.flow.emptyFlow
88+
import kotlinx.coroutines.flow.filterNotNull
8789
import kotlinx.coroutines.flow.flatMapLatest
8890
import kotlinx.coroutines.flow.map
8991
import kotlinx.coroutines.flow.mapLatest
@@ -156,17 +158,16 @@ class MainViewModel @Inject constructor(
156158
it?.let(mailboxController::getMailbox)
157159
}.asLiveData(ioCoroutineContext)
158160

159-
val currentFoldersLive = _currentMailboxObjectId.flatMapLatest { objectId ->
160-
objectId
161-
?.let {
162-
folderController.getMenuDrawerFolders().map { (defaultFolders, customFolders) ->
163-
Pair(
164-
defaultFolders.list.flattenFolderChildren(dismissHiddenChildren = true),
165-
customFolders.list.flattenFolderChildren(dismissHiddenChildren = true),
166-
)
167-
}
168-
}
169-
?: emptyFlow()
161+
private val currentDefaultFoldersLive = _currentMailboxObjectId.filterNotNull().flatMapLatest {
162+
folderController.getMenuDrawerDefaultFolders().map { it.list.flattenFolderChildren(dismissHiddenChildren = true) }
163+
}
164+
165+
private val currentCustomFoldersLive = _currentMailboxObjectId.filterNotNull().flatMapLatest {
166+
folderController.getMenuDrawerCustomFolders().map { it.list.flattenFolderChildren(dismissHiddenChildren = true) }
167+
}
168+
169+
val currentFoldersLive = currentDefaultFoldersLive.combine(currentCustomFoldersLive) { defaultFolders, customFolders ->
170+
defaultFolders to customFolders
170171
}.asLiveData(ioCoroutineContext)
171172

172173
val currentQuotasLive = _currentMailboxObjectId.flatMapLatest {

0 commit comments

Comments
 (0)