@@ -135,7 +135,7 @@ class ConversationListViewModelPreview(
135135class ConversationListViewModelImpl @AssistedInject constructor(
136136 @Assisted val conversationsSource : ConversationsSource ,
137137 @Assisted private val usePagination : Boolean = BuildConfig .PAGINATED_CONVERSATION_LIST_ENABLED ,
138- dispatcher : DispatcherProvider ,
138+ private val dispatcher : DispatcherProvider ,
139139 private val updateConversationMutedStatus : UpdateConversationMutedStatusUseCase ,
140140 private val getConversationsPaginated : GetConversationsFromSearchUseCase ,
141141 private val observeConversationListDetailsWithEvents : ObserveConversationListDetailsWithEventsUseCase ,
@@ -171,6 +171,7 @@ class ConversationListViewModelImpl @AssistedInject constructor(
171171 override val closeBottomSheet = MutableSharedFlow <Unit >()
172172
173173 private val searchQueryFlow: MutableStateFlow <String > = MutableStateFlow (" " )
174+ private val isSelfUserUnderLegalHoldFlow = MutableSharedFlow <Boolean >(replay = 1 )
174175
175176 private val containsNewActivitiesSection = when (conversationsSource) {
176177 ConversationsSource .MAIN ,
@@ -185,39 +186,38 @@ class ConversationListViewModelImpl @AssistedInject constructor(
185186 private val conversationsPaginatedFlow: Flow <PagingData <ConversationFolderItem >> = searchQueryFlow
186187 .debounce { if (it.isEmpty()) 0L else DEFAULT_SEARCH_QUERY_DEBOUNCE }
187188 .onStart { emit(" " ) }
189+ .combine(isSelfUserUnderLegalHoldFlow, ::Pair )
188190 .distinctUntilChanged()
189- .flatMapLatest { searchQuery ->
191+ .flatMapLatest { ( searchQuery, isSelfUserUnderLegalHold) ->
190192 getConversationsPaginated(
191193 searchQuery = searchQuery,
192194 fromArchive = conversationsSource == ConversationsSource .ARCHIVE ,
193195 conversationFilter = conversationsSource.toFilter(),
194196 onlyInteractionEnabled = false ,
195197 newActivitiesOnTop = containsNewActivitiesSection,
196- ).combine(observeLegalHoldStateForSelfUser()) { conversations, selfUserLegalHoldStatus ->
197- conversations.map {
198- it.hideIndicatorForSelfUserUnderLegalHold(selfUserLegalHoldStatus)
199- }
200- }.map {
201- it.insertSeparators { before, after ->
202- when {
203- // do not add separators if the list shouldn't show conversations grouped into different folders
204- ! containsNewActivitiesSection -> null
205-
206- before == null && after != null && after.hasNewActivitiesToShow ->
207- // list starts with items with "new activities"
208- ConversationFolder .Predefined .NewActivities
209-
210- before == null && after != null && ! after.hasNewActivitiesToShow ->
211- // list doesn't contain any items with "new activities"
212- ConversationFolder .Predefined .Conversations
213-
214- before != null && before.hasNewActivitiesToShow && after != null && ! after.hasNewActivitiesToShow ->
215- // end of "new activities" section and beginning of "conversations" section
216- ConversationFolder .Predefined .Conversations
217-
218- else -> null
198+ ).map { pagingData ->
199+ pagingData
200+ .map { it.hideIndicatorForSelfUserUnderLegalHold(isSelfUserUnderLegalHold) }
201+ .insertSeparators { before, after ->
202+ when {
203+ // do not add separators if the list shouldn't show conversations grouped into different folders
204+ ! containsNewActivitiesSection -> null
205+
206+ before == null && after != null && after.hasNewActivitiesToShow ->
207+ // list starts with items with "new activities"
208+ ConversationFolder .Predefined .NewActivities
209+
210+ before == null && after != null && ! after.hasNewActivitiesToShow ->
211+ // list doesn't contain any items with "new activities"
212+ ConversationFolder .Predefined .Conversations
213+
214+ before != null && before.hasNewActivitiesToShow && after != null && ! after.hasNewActivitiesToShow ->
215+ // end of "new activities" section and beginning of "conversations" section
216+ ConversationFolder .Predefined .Conversations
217+
218+ else -> null
219+ }
219220 }
220- }
221221 }
222222 }
223223 .flowOn(dispatcher.io())
@@ -232,45 +232,59 @@ class ConversationListViewModelImpl @AssistedInject constructor(
232232 private set
233233
234234 init {
235+ observeSelfUserLegalHoldState()
235236 if (! usePagination) {
236- viewModelScope.launch {
237- searchQueryFlow
238- .debounce { if (it.isEmpty()) 0L else DEFAULT_SEARCH_QUERY_DEBOUNCE }
239- .onStart { emit(" " ) }
240- .distinctUntilChanged()
241- .flatMapLatest { searchQuery: String ->
242- observeConversationListDetailsWithEvents(
243- fromArchive = conversationsSource == ConversationsSource .ARCHIVE ,
244- conversationFilter = conversationsSource.toFilter()
245- ).combine(observeLegalHoldStateForSelfUser()) { conversations, selfUserLegalHoldStatus ->
246- conversations.map { conversationDetails ->
247- conversationDetails.toConversationItem(
248- userTypeMapper = userTypeMapper,
249- searchQuery = searchQuery,
250- selfUserTeamId = observeSelfUser().firstOrNull()?.teamId
251- ).hideIndicatorForSelfUserUnderLegalHold(selfUserLegalHoldStatus)
252- } to searchQuery
253- }
254- }
255- .map { (conversationItems, searchQuery) ->
256- if (searchQuery.isEmpty()) {
257- conversationItems.withFolders(source = conversationsSource).toImmutableMap()
258- } else {
259- searchConversation(
260- conversationDetails = conversationItems,
261- searchQuery = searchQuery
262- ).withFolders(source = conversationsSource).toImmutableMap()
263- }
237+ observeNonPaginatedSearchConversationList()
238+ }
239+ }
240+
241+ private fun observeSelfUserLegalHoldState () {
242+ viewModelScope.launch {
243+ observeLegalHoldStateForSelfUser()
244+ .map { it is LegalHoldStateForSelfUser .Enabled }
245+ .flowOn(dispatcher.io())
246+ .collect { isSelfUserUnderLegalHoldFlow.emit(it) }
247+ }
248+ }
249+
250+ private fun observeNonPaginatedSearchConversationList () {
251+ viewModelScope.launch {
252+ searchQueryFlow
253+ .debounce { if (it.isEmpty()) 0L else DEFAULT_SEARCH_QUERY_DEBOUNCE }
254+ .onStart { emit(" " ) }
255+ .distinctUntilChanged()
256+ .flatMapLatest { searchQuery: String ->
257+ observeConversationListDetailsWithEvents(
258+ fromArchive = conversationsSource == ConversationsSource .ARCHIVE ,
259+ conversationFilter = conversationsSource.toFilter()
260+ ).combine(isSelfUserUnderLegalHoldFlow) { conversations, isSelfUserUnderLegalHold ->
261+ conversations.map { conversationDetails ->
262+ conversationDetails.toConversationItem(
263+ userTypeMapper = userTypeMapper,
264+ searchQuery = searchQuery,
265+ selfUserTeamId = observeSelfUser().firstOrNull()?.teamId
266+ ).hideIndicatorForSelfUserUnderLegalHold(isSelfUserUnderLegalHold)
267+ } to searchQuery
264268 }
265- .flowOn(dispatcher.io())
266- .collect {
267- conversationListState = ConversationListState .NotPaginated (
268- isLoading = false ,
269- conversations = it,
270- domain = currentAccount.domain
271- )
269+ }
270+ .map { (conversationItems, searchQuery) ->
271+ if (searchQuery.isEmpty()) {
272+ conversationItems.withFolders(source = conversationsSource).toImmutableMap()
273+ } else {
274+ searchConversation(
275+ conversationDetails = conversationItems,
276+ searchQuery = searchQuery
277+ ).withFolders(source = conversationsSource).toImmutableMap()
272278 }
273- }
279+ }
280+ .flowOn(dispatcher.io())
281+ .collect {
282+ conversationListState = ConversationListState .NotPaginated (
283+ isLoading = false ,
284+ conversations = it,
285+ domain = currentAccount.domain
286+ )
287+ }
274288 }
275289 }
276290
@@ -485,11 +499,13 @@ private fun ConversationsSource.toFilter(): ConversationFilter = when (this) {
485499 is ConversationsSource .FOLDER -> ConversationFilter .Folder (folderId = folderId, folderName = folderName)
486500}
487501
488- private fun ConversationItem.hideIndicatorForSelfUserUnderLegalHold (selfUserLegalHoldStatus : LegalHoldStateForSelfUser ) =
489- // if self user is under legal hold then we shouldn't show legal hold indicator next to every conversation
490- // the indication is shown in the header of the conversation list for self user in that case and it's enough
491- when (selfUserLegalHoldStatus) {
492- is LegalHoldStateForSelfUser .Enabled -> when (this ) {
502+ /* *
503+ * If self user is under legal hold then we shouldn't show legal hold indicator next to every conversation as in that case
504+ * the legal hold indication is shown in the header of the conversation list for self user in that case and it's enough.
505+ */
506+ private fun ConversationItem.hideIndicatorForSelfUserUnderLegalHold (isSelfUserUnderLegalHold : Boolean ) =
507+ when (isSelfUserUnderLegalHold) {
508+ true -> when (this ) {
493509 is ConversationItem .ConnectionConversation -> this .copy(showLegalHoldIndicator = false )
494510 is ConversationItem .GroupConversation -> this .copy(showLegalHoldIndicator = false )
495511 is ConversationItem .PrivateConversation -> this .copy(showLegalHoldIndicator = false )
0 commit comments