Skip to content

Commit

Permalink
fix: Rejoining call can't be completed (WPB-5111) (#2407)
Browse files Browse the repository at this point in the history
  • Loading branch information
ohassine committed Nov 13, 2023
1 parent 5900c33 commit f548d83
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ data class ConversationListState(
val allMentions: ImmutableList<ConversationItem> = persistentListOf(),
val newActivityCount: Long = 0,
val missedCallsCount: Long = 0,
val unreadMentionsCount: Long = 0,
val hasEstablishedCall: Boolean = false,
val shouldShowJoinAnywayDialog: Boolean = false,
val shouldShowCallingPermissionDialog: Boolean = false
val unreadMentionsCount: Long = 0
) {
fun findConversationById(conversationId: ConversationId): ConversationItem? =
foldersWithConversations.values.flatten().firstOrNull { it.conversationId == conversationId }
foldersWithConversations.values.flatten()
.firstOrNull { it.conversationId == conversationId }
}

data class ConversationListCallState(
val hasEstablishedCall: Boolean = false,
val shouldShowJoinAnywayDialog: Boolean = false,
val shouldShowCallingPermissionDialog: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.collections.immutable.toImmutableMap
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
Expand Down Expand Up @@ -118,6 +119,7 @@ class ConversationListViewModel @Inject constructor(
) : ViewModel() {

var conversationListState by mutableStateOf(ConversationListState())
var conversationListCallState by mutableStateOf(ConversationListCallState())

val homeSnackBarState = MutableSharedFlow<HomeSnackbarState>()

Expand Down Expand Up @@ -148,17 +150,19 @@ class ConversationListViewModel @Inject constructor(
var establishedCallConversationId: QualifiedID? = null
private var conversationId: QualifiedID? = null

private fun observeEstablishedCall() = viewModelScope.launch {
private suspend fun observeEstablishedCall() {
observeEstablishedCalls()
.distinctUntilChanged()
.collect {
.collectLatest {
val hasEstablishedCall = it.isNotEmpty()
conversationListCallState = conversationListCallState.copy(
hasEstablishedCall = hasEstablishedCall
)
establishedCallConversationId = if (it.isNotEmpty()) {
it.first().conversationId
} else {
null
}
conversationListState = conversationListState.copy(hasEstablishedCall = hasEstablishedCall)
}
}

Expand All @@ -168,7 +172,9 @@ class ConversationListViewModel @Inject constructor(
}
viewModelScope.launch {
searchQueryFlow.flatMapLatest { searchQuery ->
observeConversationListDetails(fromArchive = searchQuery.source == ConversationsSource.ARCHIVE)
observeConversationListDetails(
fromArchive = searchQuery.source == ConversationsSource.ARCHIVE
)
.map {
it.map { conversationDetails ->
conversationDetails.toConversationItem(
Expand Down Expand Up @@ -205,21 +211,39 @@ class ConversationListViewModel @Inject constructor(
}

fun showCallingPermissionDialog() {
conversationListState = conversationListState.copy(shouldShowCallingPermissionDialog = true)
conversationListCallState = conversationListCallState.copy(
shouldShowCallingPermissionDialog = true
)
}

fun dismissCallingPermissionDialog() {
conversationListState = conversationListState.copy(shouldShowCallingPermissionDialog = false)
conversationListCallState = conversationListCallState.copy(
shouldShowCallingPermissionDialog = false
)
}

// Mateusz : First iteration, just filter stuff
// next iteration : SQL- query ?
private fun searchConversation(conversationDetails: List<ConversationItem>, searchQuery: String): List<ConversationItem> {
private fun searchConversation(
conversationDetails: List<ConversationItem>,
searchQuery: String
): List<ConversationItem> {
val matchingConversations = conversationDetails.filter { details ->
when (details) {
is ConversationItem.ConnectionConversation -> details.conversationInfo.name.contains(searchQuery, true)
is ConversationItem.GroupConversation -> details.groupName.contains(searchQuery, true)
is ConversationItem.PrivateConversation -> details.conversationInfo.name.contains(searchQuery, true)
is ConversationItem.ConnectionConversation -> details.conversationInfo.name.contains(
searchQuery,
true
)

is ConversationItem.GroupConversation -> details.groupName.contains(
searchQuery,
true
)

is ConversationItem.PrivateConversation -> details.conversationInfo.name.contains(
searchQuery,
true
)
}
}
return matchingConversations
Expand All @@ -237,7 +261,10 @@ class ConversationListViewModel @Inject constructor(
return when (source) {
ConversationsSource.ARCHIVE -> {
buildMap {
if (this@withFolders.isNotEmpty()) put(ConversationFolder.WithoutHeader, this@withFolders)
if (this@withFolders.isNotEmpty()) put(
ConversationFolder.WithoutHeader,
this@withFolders
)
}
}

Expand All @@ -257,12 +284,13 @@ class ConversationListViewModel @Inject constructor(
BadgeEventType.UnreadReply -> true
}

MutedConversationStatus.OnlyMentionsAndRepliesAllowed -> when (it.badgeEventType) {
BadgeEventType.UnreadReply -> true
BadgeEventType.UnreadMention -> true
BadgeEventType.ReceivedConnectionRequest -> true
else -> false
}
MutedConversationStatus.OnlyMentionsAndRepliesAllowed ->
when (it.badgeEventType) {
BadgeEventType.UnreadReply -> true
BadgeEventType.UnreadMention -> true
BadgeEventType.ReceivedConnectionRequest -> true
else -> false
}

MutedConversationStatus.AllMuted -> false
} || (it is ConversationItem.GroupConversation && it.hasOnGoingCall)
Expand All @@ -271,20 +299,39 @@ class ConversationListViewModel @Inject constructor(
val remainingConversations = this - unreadConversations.toSet()

buildMap {
if (unreadConversations.isNotEmpty()) put(ConversationFolder.Predefined.NewActivities, unreadConversations)
if (remainingConversations.isNotEmpty()) put(ConversationFolder.Predefined.Conversations, remainingConversations)
if (unreadConversations.isNotEmpty()) put(
ConversationFolder.Predefined.NewActivities,
unreadConversations
)
if (remainingConversations.isNotEmpty()) put(
ConversationFolder.Predefined.Conversations,
remainingConversations
)
}
}
}
}

fun muteConversation(conversationId: ConversationId?, mutedConversationStatus: MutedConversationStatus) {
fun muteConversation(
conversationId: ConversationId?,
mutedConversationStatus: MutedConversationStatus
) {
conversationId?.let {
viewModelScope.launch {
when (updateConversationMutedStatus(conversationId, mutedConversationStatus, Date().time)) {
ConversationUpdateStatusResult.Failure -> homeSnackBarState.emit(HomeSnackbarState.MutingOperationError)
when (updateConversationMutedStatus(
conversationId,
mutedConversationStatus,
Date().time
)) {
ConversationUpdateStatusResult.Failure -> homeSnackBarState.emit(
HomeSnackbarState.MutingOperationError
)

ConversationUpdateStatusResult.Success ->
appLogger.d("MutedStatus changed for conversation: $conversationId to $mutedConversationStatus")
appLogger.d(
"MutedStatus changed for conversation: " +
"$conversationId to $mutedConversationStatus"
)
}
}
}
Expand All @@ -302,23 +349,25 @@ class ConversationListViewModel @Inject constructor(

fun joinOngoingCall(conversationId: ConversationId, onJoined: (ConversationId) -> Unit) {
this.conversationId = conversationId
viewModelScope.launch {
if (conversationListState.hasEstablishedCall) {
showJoinCallAnywayDialog()
} else {
dismissJoinCallAnywayDialog()
if (conversationListCallState.hasEstablishedCall) {
showJoinCallAnywayDialog()
} else {
dismissJoinCallAnywayDialog()
viewModelScope.launch {
answerCall(conversationId = conversationId)
onJoined(conversationId)
}
onJoined(conversationId)
}
}

private fun showJoinCallAnywayDialog() {
conversationListState = conversationListState.copy(shouldShowJoinAnywayDialog = true)
conversationListCallState =
conversationListCallState.copy(shouldShowJoinAnywayDialog = true)
}

fun dismissJoinCallAnywayDialog() {
conversationListState = conversationListState.copy(shouldShowJoinAnywayDialog = false)
conversationListCallState =
conversationListCallState.copy(shouldShowJoinAnywayDialog = false)
}

fun blockUser(blockUserState: BlockUserDialogState) {
Expand All @@ -331,7 +380,10 @@ class ConversationListViewModel @Inject constructor(
}

is BlockUserResult.Failure -> {
appLogger.d("Error while blocking user ${blockUserState.userId} ; Error ${result.coreFailure}")
appLogger.d(
"Error while blocking user ${blockUserState.userId} ;" +
" Error ${result.coreFailure}"
)
HomeSnackbarState.BlockingUserOperationError
}
}
Expand All @@ -350,7 +402,10 @@ class ConversationListViewModel @Inject constructor(
}

is UnblockUserResult.Failure -> {
appLogger.e("Error while unblocking user $userId ; Error ${result.coreFailure}")
appLogger.e(
"Error while unblocking user $userId ;" +
" Error ${result.coreFailure}"
)
homeSnackBarState.emit(HomeSnackbarState.UnblockingUserOperationError)
}
}
Expand Down Expand Up @@ -410,7 +465,10 @@ class ConversationListViewModel @Inject constructor(
fun moveConversationToFolder(id: String = "") {
}

fun moveConversationToArchive(dialogState: DialogState, timestamp: Long = DateTimeUtil.currentInstant().toEpochMilliseconds()) =
fun moveConversationToArchive(
dialogState: DialogState,
timestamp: Long = DateTimeUtil.currentInstant().toEpochMilliseconds()
) =
with(dialogState) {
viewModelScope.launch {
val isArchiving = !isArchived
Expand All @@ -425,11 +483,19 @@ class ConversationListViewModel @Inject constructor(
requestInProgress = false
when (result) {
is ArchiveStatusUpdateResult.Failure -> {
homeSnackBarState.emit(HomeSnackbarState.UpdateArchivingStatusError(isArchiving))
homeSnackBarState.emit(
HomeSnackbarState.UpdateArchivingStatusError(
isArchiving
)
)
}

is ArchiveStatusUpdateResult.Success -> {
homeSnackBarState.emit(HomeSnackbarState.UpdateArchivingStatusSuccess(isArchiving))
homeSnackBarState.emit(
HomeSnackbarState.UpdateArchivingStatusSuccess(
isArchiving
)
)
}
}
}
Expand Down Expand Up @@ -561,9 +627,17 @@ private fun ConversationDetails.toConversationItem(
}

private fun parseConnectionEventType(connectionState: ConnectionState) =
if (connectionState == ConnectionState.SENT) BadgeEventType.SentConnectRequest else BadgeEventType.ReceivedConnectionRequest
if (connectionState == ConnectionState.SENT) {
BadgeEventType.SentConnectRequest
} else {
BadgeEventType.ReceivedConnectionRequest
}

fun parsePrivateConversationEventType(connectionState: ConnectionState, isDeleted: Boolean, eventType: BadgeEventType) =
fun parsePrivateConversationEventType(
connectionState: ConnectionState,
isDeleted: Boolean,
eventType: BadgeEventType
) =
if (connectionState == ConnectionState.BLOCKED) {
BadgeEventType.Blocked
} else if (isDeleted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fun ConversationRouterHomeBridge(
}

MicrophoneBTPermissionsDeniedDialog(
shouldShow = viewModel.conversationListState.shouldShowCallingPermissionDialog,
shouldShow = viewModel.conversationListCallState.shouldShowCallingPermissionDialog,
onDismiss = viewModel::dismissCallingPermissionDialog,
onOpenSettings = {
context.openAppInfoScreen()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fun AllConversationScreenContent(
val lazyListState = rememberLazyListState()
val callConversationIdToJoin = remember { mutableStateOf(ConversationId("", "")) }

if (viewModel.conversationListState.shouldShowJoinAnywayDialog) {
if (viewModel.conversationListCallState.shouldShowJoinAnywayDialog) {
appLogger.i("$TAG showing showJoinAnywayDialog..")
JoinAnywayDialog(
onDismiss = viewModel::dismissJoinCallAnywayDialog,
Expand Down

0 comments on commit f548d83

Please sign in to comment.