Skip to content

Commit

Permalink
fix: bottom sheet state management improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Garzas committed Jun 3, 2024
1 parent aee1880 commit 962d6d8
Show file tree
Hide file tree
Showing 8 changed files with 580 additions and 342 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

package com.wire.android.ui.home.conversationslist

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.text.input.TextFieldValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wire.android.appLogger
import com.wire.android.mapper.UserTypeMapper
import com.wire.android.mapper.toUIPreview
import com.wire.android.model.ImageAsset.UserAvatarAsset
import com.wire.android.model.SnackBarMessage
import com.wire.android.model.UserAvatarData
import com.wire.android.ui.common.bottomsheet.conversation.ConversationTypeDetail
import com.wire.android.ui.common.dialogs.BlockUserDialogState
import com.wire.android.ui.home.HomeSnackBarMessage
import com.wire.android.ui.home.conversations.model.UILastMessageContent
import com.wire.android.ui.home.conversations.search.DEFAULT_SEARCH_QUERY_DEBOUNCE
import com.wire.android.ui.home.conversationslist.model.BadgeEventType
import com.wire.android.ui.home.conversationslist.model.BlockState
import com.wire.android.ui.home.conversationslist.model.ConversationFolder
import com.wire.android.ui.home.conversationslist.model.ConversationInfo
import com.wire.android.ui.home.conversationslist.model.ConversationItem
import com.wire.android.ui.home.conversationslist.model.ConversationsSource
import com.wire.android.ui.home.conversationslist.model.DialogState
import com.wire.android.ui.home.conversationslist.model.GroupDialogState
import com.wire.android.ui.home.conversationslist.model.SearchQuery
import com.wire.android.ui.home.conversationslist.model.SearchQueryUpdate
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.android.util.ui.WireSessionImageLoader
import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.conversation.ConversationDetails
import com.wire.kalium.logic.data.conversation.ConversationDetails.Connection
import com.wire.kalium.logic.data.conversation.ConversationDetails.Group
import com.wire.kalium.logic.data.conversation.ConversationDetails.OneOne
import com.wire.kalium.logic.data.conversation.ConversationDetails.Self
import com.wire.kalium.logic.data.conversation.MutedConversationStatus
import com.wire.kalium.logic.data.conversation.UnreadEventCount
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.data.message.UnreadEventType
import com.wire.kalium.logic.data.user.ConnectionState
import com.wire.kalium.logic.data.user.UserAvailabilityStatus
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.feature.call.usecase.AnswerCallUseCase
import com.wire.kalium.logic.feature.call.usecase.EndCallUseCase
import com.wire.kalium.logic.feature.call.usecase.ObserveEstablishedCallsUseCase
import com.wire.kalium.logic.feature.connection.BlockUserResult
import com.wire.kalium.logic.feature.connection.BlockUserUseCase
import com.wire.kalium.logic.feature.connection.UnblockUserResult
import com.wire.kalium.logic.feature.connection.UnblockUserUseCase
import com.wire.kalium.logic.feature.conversation.ArchiveStatusUpdateResult
import com.wire.kalium.logic.feature.conversation.ClearConversationContentUseCase
import com.wire.kalium.logic.feature.conversation.ConversationUpdateStatusResult
import com.wire.kalium.logic.feature.conversation.LeaveConversationUseCase
import com.wire.kalium.logic.feature.conversation.ObserveConversationListDetailsUseCase
import com.wire.kalium.logic.feature.conversation.RefreshConversationsWithoutMetadataUseCase
import com.wire.kalium.logic.feature.conversation.RemoveMemberFromConversationUseCase
import com.wire.kalium.logic.feature.conversation.UpdateConversationArchivedStatusUseCase
import com.wire.kalium.logic.feature.conversation.UpdateConversationMutedStatusUseCase
import com.wire.kalium.logic.feature.publicuser.RefreshUsersWithoutMetadataUseCase
import com.wire.kalium.logic.feature.team.DeleteTeamConversationUseCase
import com.wire.kalium.logic.feature.team.Result
import com.wire.kalium.util.DateTimeUtil
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.collections.immutable.toImmutableMap
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.scan
import kotlinx.coroutines.launch
import java.util.Date
import javax.inject.Inject

@Suppress("MagicNumber", "TooManyFunctions", "LongParameterList")
@HiltViewModel
class ConversationListCallViewModel @Inject constructor(
private val answerCall: AnswerCallUseCase,
private val observeEstablishedCalls: ObserveEstablishedCallsUseCase,
private val endCall: EndCallUseCase,
private val refreshUsersWithoutMetadata: RefreshUsersWithoutMetadataUseCase,
private val refreshConversationsWithoutMetadata: RefreshConversationsWithoutMetadataUseCase,
) : ViewModel() {

var conversationListCallState by mutableStateOf(ConversationListCallState())

private val _infoMessage = MutableSharedFlow<SnackBarMessage>()
val infoMessage = _infoMessage.asSharedFlow()

var establishedCallConversationId: QualifiedID? = null
private var conversationId: QualifiedID? = null

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

init {
viewModelScope.launch {
println("KBX observeEstablishedCall")
observeEstablishedCall()
}
}

suspend fun refreshMissingMetadata() {
viewModelScope.launch {
refreshUsersWithoutMetadata()
refreshConversationsWithoutMetadata()
}
}

fun joinAnyway(conversationId: ConversationId, onJoined: (ConversationId) -> Unit) {
viewModelScope.launch {
establishedCallConversationId?.let {
endCall(it)
delay(DELAY_END_CALL)
}
joinOngoingCall(conversationId, onJoined)
}
}

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

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

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

companion object {
const val DELAY_END_CALL = 200L
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 962d6d8

Please sign in to comment.