Skip to content
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: show incoming call screen for second account #2929

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ sealed class SwitchAccountParam {
}

sealed class SwitchAccountResult {
object Failure : SwitchAccountResult()
object SwitchedToAnotherAccount : SwitchAccountResult()
object NoOtherAccountToSwitch : SwitchAccountResult()
data object Failure : SwitchAccountResult()
data object SwitchedToAnotherAccount : SwitchAccountResult()
data object NoOtherAccountToSwitch : SwitchAccountResult()

fun callAction(actions: SwitchAccountActions) = when (this) {
NoOtherAccountToSwitch -> actions.noOtherAccountToSwitch()
Expand Down
19 changes: 13 additions & 6 deletions app/src/main/kotlin/com/wire/android/ui/calling/CallActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,23 @@ class CallActivity : AppCompatActivity() {

callNotificationManager.hideAllNotifications()

setUpScreenShootPreventionFlag()
setUpCallingFlags()

appLogger.i("$TAG Initializing proximity sensor..")
proximitySensorManager.initialize()

WindowCompat.setDecorFitsSystemWindows(window, false)

val conversationId = intent.extras?.getString(EXTRA_CONVERSATION_ID)
val screenType = intent.extras?.getString(EXTRA_SCREEN_TYPE)
val userId = intent.extras?.getString(EXTRA_USER_ID)

userId?.let {
qualifiedIdMapper.fromStringToQualifiedID(it).run {
callActivityViewModel.switchAccountIfNeeded(this)
}
}

setUpCallingFlags()
setUpScreenShootPreventionFlag()

appLogger.i("$TAG Initializing proximity sensor..")
proximitySensorManager.initialize()

setContent {
val snackbarHostState = remember { SnackbarHostState() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,29 @@ package com.wire.android.ui.calling
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wire.android.di.ObserveScreenshotCensoringConfigUseCaseProvider
import com.wire.android.feature.AccountSwitchUseCase
import com.wire.android.feature.SwitchAccountParam
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.feature.session.CurrentSessionResult
import com.wire.kalium.logic.feature.session.CurrentSessionUseCase
import com.wire.kalium.logic.feature.user.screenshotCensoring.ObserveScreenshotCensoringConfigResult
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class CallActivityViewModel @Inject constructor(
private val dispatchers: DispatcherProvider,
private val currentSession: CurrentSessionUseCase,
private val observeScreenshotCensoringConfigUseCaseProviderFactory:
ObserveScreenshotCensoringConfigUseCaseProvider.Factory
ObserveScreenshotCensoringConfigUseCaseProvider.Factory,
private val accountSwitch: AccountSwitchUseCase
) : ViewModel() {

fun isScreenshotCensoringConfigEnabled(): Deferred<Boolean> =
Expand All @@ -52,4 +58,17 @@ class CallActivityViewModel @Inject constructor(
return@async false
}
}

fun switchAccountIfNeeded(userId: UserId) {
viewModelScope.launch(Dispatchers.IO) {
val shouldSwitchAccount = when (val result = currentSession()) {
is CurrentSessionResult.Failure.Generic -> true
CurrentSessionResult.Failure.SessionNotFound -> true
is CurrentSessionResult.Success -> result.accountInfo.userId != userId
}
if (shouldSwitchAccount) {
accountSwitch(SwitchAccountParam.SwitchToAccount(userId))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package com.wire.android.ui

import com.wire.android.config.TestDispatcherProvider
import com.wire.android.di.ObserveScreenshotCensoringConfigUseCaseProvider
import com.wire.android.feature.AccountSwitchUseCase
import com.wire.android.feature.SwitchAccountResult
import com.wire.android.ui.calling.CallActivityViewModel
import com.wire.kalium.logic.data.auth.AccountInfo
import com.wire.kalium.logic.data.user.UserId
Expand All @@ -28,6 +30,7 @@ import com.wire.kalium.logic.feature.user.screenshotCensoring.ObserveScreenshotC
import com.wire.kalium.logic.feature.user.screenshotCensoring.ObserveScreenshotCensoringConfigUseCase
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.impl.annotations.MockK
import kotlinx.coroutines.flow.flowOf
Expand Down Expand Up @@ -75,6 +78,45 @@ class CallActivityViewModelTest {
assertEquals(false, result.await())
}

@Test
fun `given no session available, when trying to switch account, then try to switchAccount usecase once`() =
runTest {
val (arrangement, viewModel) = Arrangement()
.withCurrentSessionReturning(CurrentSessionResult.Failure.SessionNotFound)
.withAccountSwitch(SwitchAccountResult.Failure)
.arrange()

viewModel.switchAccountIfNeeded(userId)

coVerify(exactly = 1) { arrangement.accountSwitch(any()) }
}

@Test
fun `given userId different from currentSession, when trying to switch account, then invoke switchAccount usecase once`() =
runTest {
val (arrangement, viewModel) = Arrangement()
.withCurrentSessionReturning(CurrentSessionResult.Success(accountInfo))
.withAccountSwitch(SwitchAccountResult.SwitchedToAnotherAccount)
.arrange()

viewModel.switchAccountIfNeeded(UserId("anotherUserId", "domain"))

coVerify(exactly = 1) { arrangement.accountSwitch(any()) }
}

@Test
fun `given userId same as currentSession, when trying to switch account, then do not invoke switchAccount usecase`() =
runTest {
val (arrangement, viewModel) = Arrangement()
.withCurrentSessionReturning(CurrentSessionResult.Success(accountInfo))
.withAccountSwitch(SwitchAccountResult.SwitchedToAnotherAccount)
.arrange()

viewModel.switchAccountIfNeeded(userId)

coVerify(inverse = true) { arrangement.accountSwitch(any()) }
}

private class Arrangement {

@MockK
Expand All @@ -84,6 +126,9 @@ class CallActivityViewModelTest {
@MockK
private lateinit var currentSession: CurrentSessionUseCase

@MockK
lateinit var accountSwitch: AccountSwitchUseCase

@MockK
private lateinit var observeScreenshotCensoringConfig: ObserveScreenshotCensoringConfigUseCase

Expand All @@ -99,6 +144,7 @@ class CallActivityViewModelTest {
dispatchers = TestDispatcherProvider(),
currentSession = currentSession,
observeScreenshotCensoringConfigUseCaseProviderFactory = observeScreenshotCensoringConfigUseCaseProviderFactory,
accountSwitch = accountSwitch
)
}

Expand All @@ -108,13 +154,18 @@ class CallActivityViewModelTest {
coEvery { currentSession() } returns result
}

suspend fun withAccountSwitch(result: SwitchAccountResult) = apply {
coEvery { accountSwitch(any()) } returns result
}

suspend fun withScreenshotCensoringConfigReturning(result: ObserveScreenshotCensoringConfigResult) =
apply {
coEvery { observeScreenshotCensoringConfig() } returns flowOf(result)
}
}

companion object {
val accountInfo = AccountInfo.Valid(userId = UserId("userId", "domain"))
val userId = UserId("userId", "domain")
val accountInfo = AccountInfo.Valid(userId = userId)
}
}
Loading