Skip to content

Commit

Permalink
fix: Crash after fresh install 🍒 (#2967)
Browse files Browse the repository at this point in the history
Co-authored-by: Oussama Hassine <[email protected]>
  • Loading branch information
github-actions[bot] and ohassine authored May 3, 2024
1 parent 0c82442 commit 3ed5453
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
20 changes: 14 additions & 6 deletions app/src/main/kotlin/com/wire/android/ui/WireActivityViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.wire.android.ui

import android.content.Intent
import androidx.annotation.VisibleForTesting
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
Expand Down Expand Up @@ -60,7 +61,6 @@ import com.wire.kalium.logic.data.logout.LogoutReason
import com.wire.kalium.logic.data.sync.SyncState
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.feature.appVersioning.ObserveIfAppUpdateRequiredUseCase
import com.wire.kalium.logic.feature.call.usecase.ObserveEstablishedCallsUseCase
import com.wire.kalium.logic.feature.client.ClearNewClientsForUserUseCase
import com.wire.kalium.logic.feature.client.NewClientResult
import com.wire.kalium.logic.feature.client.ObserveNewClientsUseCase
Expand All @@ -78,6 +78,7 @@ import com.wire.kalium.logic.feature.user.webSocketStatus.ObservePersistentWebSo
import com.wire.kalium.util.DateTimeUtil.toIsoDateTimeString
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
Expand Down Expand Up @@ -117,8 +118,7 @@ class WireActivityViewModel @Inject constructor(
private val observeScreenshotCensoringConfigUseCaseProviderFactory: ObserveScreenshotCensoringConfigUseCaseProvider.Factory,
private val globalDataStore: GlobalDataStore,
private val observeIfE2EIRequiredDuringLoginUseCaseProviderFactory: ObserveIfE2EIRequiredDuringLoginUseCaseProvider.Factory,
private val workManager: WorkManager,
private val observeEstablishedCalls: ObserveEstablishedCallsUseCase
private val workManager: WorkManager
) : ViewModel() {

var globalAppState: GlobalAppState by mutableStateOf(GlobalAppState())
Expand Down Expand Up @@ -257,7 +257,15 @@ class WireActivityViewModel @Inject constructor(
return intent?.action == Intent.ACTION_SEND || intent?.action == Intent.ACTION_SEND_MULTIPLE
}

private suspend fun canLoginThroughDeepLinks() = observeEstablishedCalls().first().isEmpty()
@VisibleForTesting
internal suspend fun canLoginThroughDeepLinks() = viewModelScope.async {
coreLogic.getGlobalScope().session.currentSession().takeIf {
it is CurrentSessionResult.Success
}?.let {
val currentUserId = (it as CurrentSessionResult.Success).accountInfo.userId
coreLogic.getSessionScope(currentUserId).calls.establishedCall().first().isEmpty()
} ?: true
}

@Suppress("ComplexMethod")
fun handleDeepLink(
Expand All @@ -276,15 +284,15 @@ class WireActivityViewModel @Inject constructor(
val result = intent?.data?.let { deepLinkProcessor(it) }
when {
result is DeepLinkResult.SSOLogin -> {
if (canLoginThroughDeepLinks()) {
if (canLoginThroughDeepLinks().await()) {
onResult(result)
} else {
onCannotLoginDuringACall()
}
}
result is DeepLinkResult.MigrationLogin -> onResult(result)
result is DeepLinkResult.CustomServerConfig -> {
if (canLoginThroughDeepLinks()) {
if (canLoginThroughDeepLinks().await()) {
onCustomServerConfig(result)
} else {
onCannotLoginDuringACall()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class SharedCallingViewModel @AssistedInject constructor(
if (callState.isCameraOn) {
flipToFrontCamera(conversationId)
}
if (callState.isCameraOn || callState.isSpeakerOn) {
if (callState.isSpeakerOn) {
turnLoudSpeakerOff()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,44 @@ class WireActivityViewModelTest {
assertEquals(ThemeOption.DARK, viewModel.globalAppState.themeOption)
}

@Test
fun `given no active session, when canLoginThroughDeepLinks is called, then return true`() =
runTest {
val (_, viewModel) = Arrangement()
.withNoCurrentSession()
.arrange()

val result = viewModel.canLoginThroughDeepLinks()

result.await() `should be equal to` true
}

@Test
fun `given an established call, when canLoginThroughDeepLinks is called, then return false`() =
runTest {
val (_, viewModel) = Arrangement()
.withSomeCurrentSession()
.withOngoingCall()
.arrange()

val result = viewModel.canLoginThroughDeepLinks()

result.await() `should be equal to` false
}

@Test
fun `given no established call, when canLoginThroughDeepLinks is called, then return true`() =
runTest {
val (_, viewModel) = Arrangement()
.withNoCurrentSession()
.withNoOngoingCall()
.arrange()

val result = viewModel.canLoginThroughDeepLinks()

result.await() `should be equal to` true
}

private class Arrangement {

init {
Expand Down Expand Up @@ -722,8 +760,7 @@ class WireActivityViewModelTest {
observeScreenshotCensoringConfigUseCaseProviderFactory = observeScreenshotCensoringConfigUseCaseProviderFactory,
globalDataStore = globalDataStore,
observeIfE2EIRequiredDuringLoginUseCaseProviderFactory = observeIfE2EIRequiredDuringLoginUseCaseProviderFactory,
workManager = workManager,
observeEstablishedCalls = observeEstablishedCalls
workManager = workManager
)
}

Expand All @@ -746,10 +783,12 @@ class WireActivityViewModelTest {
}

fun withNoOngoingCall(): Arrangement {
coEvery { coreLogic.getSessionScope(any()).calls.establishedCall } returns observeEstablishedCalls
coEvery { observeEstablishedCalls() } returns flowOf(emptyList())
return this
}
fun withOngoingCall(): Arrangement {
coEvery { coreLogic.getSessionScope(any()).calls.establishedCall } returns observeEstablishedCalls
coEvery { observeEstablishedCalls() } returns flowOf(listOf(ongoingCall))
return this
}
Expand Down

0 comments on commit 3ed5453

Please sign in to comment.