-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: reusing PagingData crash [WPB-15055][WPB-15079][WPB-15064] #3758
Open
saleniuk
wants to merge
4
commits into
release/candidate
Choose a base branch
from
fix/reusing_pagingdata_crash
base: release/candidate
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import androidx.compose.runtime.setValue | |
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import androidx.paging.PagingData | ||
import androidx.paging.cachedIn | ||
import androidx.paging.insertSeparators | ||
import androidx.paging.map | ||
import com.wire.android.BuildConfig | ||
|
@@ -126,7 +127,7 @@ class ConversationListViewModelPreview( | |
class ConversationListViewModelImpl @AssistedInject constructor( | ||
@Assisted val conversationsSource: ConversationsSource, | ||
@Assisted private val usePagination: Boolean = BuildConfig.PAGINATED_CONVERSATION_LIST_ENABLED, | ||
dispatcher: DispatcherProvider, | ||
private val dispatcher: DispatcherProvider, | ||
private val updateConversationMutedStatus: UpdateConversationMutedStatusUseCase, | ||
private val getConversationsPaginated: GetConversationsFromSearchUseCase, | ||
private val observeConversationListDetailsWithEvents: ObserveConversationListDetailsWithEventsUseCase, | ||
|
@@ -161,6 +162,7 @@ class ConversationListViewModelImpl @AssistedInject constructor( | |
override val closeBottomSheet = MutableSharedFlow<Unit>() | ||
|
||
private val searchQueryFlow: MutableStateFlow<String> = MutableStateFlow("") | ||
private val isSelfUserUnderLegalHoldFlow = MutableSharedFlow<Boolean>(replay = 1) | ||
|
||
private val containsNewActivitiesSection = when (conversationsSource) { | ||
ConversationsSource.MAIN, | ||
|
@@ -174,94 +176,105 @@ class ConversationListViewModelImpl @AssistedInject constructor( | |
private val conversationsPaginatedFlow: Flow<PagingData<ConversationFolderItem>> = searchQueryFlow | ||
.debounce { if (it.isEmpty()) 0L else DEFAULT_SEARCH_QUERY_DEBOUNCE } | ||
.onStart { emit("") } | ||
.combine(isSelfUserUnderLegalHoldFlow, ::Pair) | ||
.distinctUntilChanged() | ||
.flatMapLatest { searchQuery -> | ||
.flatMapLatest { (searchQuery, isSelfUserUnderLegalHold) -> | ||
getConversationsPaginated( | ||
searchQuery = searchQuery, | ||
fromArchive = conversationsSource == ConversationsSource.ARCHIVE, | ||
conversationFilter = conversationsSource.toFilter(), | ||
onlyInteractionEnabled = false, | ||
newActivitiesOnTop = containsNewActivitiesSection, | ||
).combine(observeLegalHoldStateForSelfUser()) { conversations, selfUserLegalHoldStatus -> | ||
conversations.map { | ||
it.hideIndicatorForSelfUserUnderLegalHold(selfUserLegalHoldStatus) | ||
} | ||
}.map { | ||
it.insertSeparators { before, after -> | ||
when { | ||
// do not add separators if the list shouldn't show conversations grouped into different folders | ||
!containsNewActivitiesSection -> null | ||
|
||
before == null && after != null && after.hasNewActivitiesToShow -> | ||
// list starts with items with "new activities" | ||
ConversationFolder.Predefined.NewActivities | ||
|
||
before == null && after != null && !after.hasNewActivitiesToShow -> | ||
// list doesn't contain any items with "new activities" | ||
ConversationFolder.Predefined.Conversations | ||
|
||
before != null && before.hasNewActivitiesToShow && after != null && !after.hasNewActivitiesToShow -> | ||
// end of "new activities" section and beginning of "conversations" section | ||
ConversationFolder.Predefined.Conversations | ||
|
||
else -> null | ||
).map { pagingData -> | ||
pagingData | ||
.map { it.hideIndicatorForSelfUserUnderLegalHold(isSelfUserUnderLegalHold) } | ||
.insertSeparators { before, after -> | ||
when { | ||
// do not add separators if the list shouldn't show conversations grouped into different folders | ||
!containsNewActivitiesSection -> null | ||
|
||
before == null && after != null && after.hasNewActivitiesToShow -> | ||
// list starts with items with "new activities" | ||
ConversationFolder.Predefined.NewActivities | ||
|
||
before == null && after != null && !after.hasNewActivitiesToShow -> | ||
// list doesn't contain any items with "new activities" | ||
ConversationFolder.Predefined.Conversations | ||
|
||
before != null && before.hasNewActivitiesToShow && after != null && !after.hasNewActivitiesToShow -> | ||
// end of "new activities" section and beginning of "conversations" section | ||
ConversationFolder.Predefined.Conversations | ||
|
||
else -> null | ||
} | ||
} | ||
} | ||
} | ||
} | ||
.flowOn(dispatcher.io()) | ||
.cachedIn(viewModelScope) | ||
|
||
private var notPaginatedConversationListState by mutableStateOf(ConversationListState.NotPaginated()) | ||
override val conversationListState: ConversationListState | ||
get() = if (usePagination) { | ||
ConversationListState.Paginated( | ||
conversations = conversationsPaginatedFlow, | ||
domain = currentAccount.domain | ||
) | ||
} else { | ||
notPaginatedConversationListState | ||
override var conversationListState by mutableStateOf( | ||
when (usePagination) { | ||
true -> ConversationListState.Paginated(conversations = conversationsPaginatedFlow, domain = currentAccount.domain) | ||
false -> ConversationListState.NotPaginated() | ||
} | ||
) | ||
private set | ||
|
||
init { | ||
observeSelfUserLegalHoldState() | ||
if (!usePagination) { | ||
viewModelScope.launch { | ||
searchQueryFlow | ||
.debounce { if (it.isEmpty()) 0L else DEFAULT_SEARCH_QUERY_DEBOUNCE } | ||
.onStart { emit("") } | ||
.distinctUntilChanged() | ||
.flatMapLatest { searchQuery: String -> | ||
observeConversationListDetailsWithEvents( | ||
fromArchive = conversationsSource == ConversationsSource.ARCHIVE, | ||
conversationFilter = conversationsSource.toFilter() | ||
).combine(observeLegalHoldStateForSelfUser()) { conversations, selfUserLegalHoldStatus -> | ||
conversations.map { conversationDetails -> | ||
conversationDetails.toConversationItem( | ||
userTypeMapper = userTypeMapper, | ||
searchQuery = searchQuery, | ||
selfUserTeamId = observeSelfUser().firstOrNull()?.teamId | ||
).hideIndicatorForSelfUserUnderLegalHold(selfUserLegalHoldStatus) | ||
} to searchQuery | ||
} | ||
} | ||
.map { (conversationItems, searchQuery) -> | ||
if (searchQuery.isEmpty()) { | ||
conversationItems.withFolders(source = conversationsSource).toImmutableMap() | ||
} else { | ||
searchConversation( | ||
conversationDetails = conversationItems, | ||
searchQuery = searchQuery | ||
).withFolders(source = conversationsSource).toImmutableMap() | ||
} | ||
observeNonPaginatedSearchConversationList() | ||
} | ||
} | ||
|
||
private fun observeSelfUserLegalHoldState() { | ||
viewModelScope.launch { | ||
observeLegalHoldStateForSelfUser() | ||
.map { it is LegalHoldStateForSelfUser.Enabled } | ||
.flowOn(dispatcher.io()) | ||
.collect { isSelfUserUnderLegalHoldFlow.emit(it) } | ||
} | ||
} | ||
Comment on lines
+231
to
+238
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of having a separate
|
||
|
||
private fun observeNonPaginatedSearchConversationList() { | ||
viewModelScope.launch { | ||
searchQueryFlow | ||
.debounce { if (it.isEmpty()) 0L else DEFAULT_SEARCH_QUERY_DEBOUNCE } | ||
.onStart { emit("") } | ||
.distinctUntilChanged() | ||
.flatMapLatest { searchQuery: String -> | ||
observeConversationListDetailsWithEvents( | ||
fromArchive = conversationsSource == ConversationsSource.ARCHIVE, | ||
conversationFilter = conversationsSource.toFilter() | ||
).combine(isSelfUserUnderLegalHoldFlow) { conversations, isSelfUserUnderLegalHold -> | ||
conversations.map { conversationDetails -> | ||
conversationDetails.toConversationItem( | ||
userTypeMapper = userTypeMapper, | ||
searchQuery = searchQuery, | ||
selfUserTeamId = observeSelfUser().firstOrNull()?.teamId | ||
).hideIndicatorForSelfUserUnderLegalHold(isSelfUserUnderLegalHold) | ||
} to searchQuery | ||
} | ||
.flowOn(dispatcher.io()) | ||
.collect { | ||
notPaginatedConversationListState = notPaginatedConversationListState.copy( | ||
isLoading = false, | ||
conversations = it, | ||
domain = currentAccount.domain | ||
) | ||
} | ||
.map { (conversationItems, searchQuery) -> | ||
if (searchQuery.isEmpty()) { | ||
conversationItems.withFolders(source = conversationsSource).toImmutableMap() | ||
} else { | ||
searchConversation( | ||
conversationDetails = conversationItems, | ||
searchQuery = searchQuery | ||
).withFolders(source = conversationsSource).toImmutableMap() | ||
} | ||
} | ||
} | ||
.flowOn(dispatcher.io()) | ||
.collect { | ||
conversationListState = ConversationListState.NotPaginated( | ||
isLoading = false, | ||
conversations = it, | ||
domain = currentAccount.domain | ||
) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -446,11 +459,13 @@ private fun ConversationsSource.toFilter(): ConversationFilter = when (this) { | |
ConversationsSource.ONE_ON_ONE -> ConversationFilter.ONE_ON_ONE | ||
} | ||
|
||
private fun ConversationItem.hideIndicatorForSelfUserUnderLegalHold(selfUserLegalHoldStatus: LegalHoldStateForSelfUser) = | ||
// if self user is under legal hold then we shouldn't show legal hold indicator next to every conversation | ||
// the indication is shown in the header of the conversation list for self user in that case and it's enough | ||
when (selfUserLegalHoldStatus) { | ||
is LegalHoldStateForSelfUser.Enabled -> when (this) { | ||
/** | ||
* If self user is under legal hold then we shouldn't show legal hold indicator next to every conversation as in that case | ||
* the legal hold indication is shown in the header of the conversation list for self user in that case and it's enough. | ||
*/ | ||
private fun ConversationItem.hideIndicatorForSelfUserUnderLegalHold(isSelfUserUnderLegalHold: Boolean) = | ||
when (isSelfUserUnderLegalHold) { | ||
true -> when (this) { | ||
is ConversationItem.ConnectionConversation -> this.copy(showLegalHoldIndicator = false) | ||
is ConversationItem.GroupConversation -> this.copy(showLegalHoldIndicator = false) | ||
is ConversationItem.PrivateConversation -> this.copy(showLegalHoldIndicator = false) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Are there chances of getting an
IndexOutOfBounds
here ? Asking just in case we can make it safer (for example using a Pair instead of a List?), otherwise it looks good to me.