Skip to content

Commit

Permalink
move update version to app sync view model
Browse files Browse the repository at this point in the history
  • Loading branch information
Garzas committed Dec 3, 2024
1 parent 4733df0 commit d52f0cf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ import com.wire.kalium.logic.feature.conversation.CheckConversationInviteCodeUse
import com.wire.kalium.logic.feature.debug.SynchronizeExternalDataResult
import com.wire.kalium.logic.feature.server.GetServerConfigResult
import com.wire.kalium.logic.feature.server.GetServerConfigUseCase
import com.wire.kalium.logic.feature.server.UpdateApiVersionsUseCase
import com.wire.kalium.logic.feature.session.CurrentSessionFlowUseCase
import com.wire.kalium.logic.feature.session.CurrentSessionResult
import com.wire.kalium.logic.feature.session.DoesValidSessionExistResult
Expand Down Expand Up @@ -124,7 +123,6 @@ class WireActivityViewModel @Inject constructor(
private val globalDataStore: Lazy<GlobalDataStore>,
private val observeIfE2EIRequiredDuringLoginUseCaseProviderFactory: ObserveIfE2EIRequiredDuringLoginUseCaseProvider.Factory,
private val workManager: Lazy<WorkManager>,
private val updateApiVersions: UpdateApiVersionsUseCase
) : ViewModel() {

var globalAppState: GlobalAppState by mutableStateOf(GlobalAppState())
Expand Down Expand Up @@ -159,13 +157,6 @@ class WireActivityViewModel @Inject constructor(
observeScreenshotCensoringConfigState()
observeAppThemeState()
observeLogoutState()
updateApiVersionsOnStart()
}

private fun updateApiVersionsOnStart() {
viewModelScope.launch {
updateApiVersions()
}
}

@Suppress("TooGenericExceptionCaught")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.wire.android.appLogger
import com.wire.kalium.logic.feature.e2ei.SyncCertificateRevocationListUseCase
import com.wire.kalium.logic.feature.e2ei.usecase.ObserveCertificateRevocationForSelfClientUseCase
import com.wire.kalium.logic.feature.featureConfig.FeatureFlagsSyncWorker
import com.wire.kalium.logic.feature.server.UpdateApiVersionsUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.joinAll
Expand All @@ -38,6 +39,7 @@ class AppSyncViewModel @Inject constructor(
private val syncCertificateRevocationListUseCase: SyncCertificateRevocationListUseCase,
private val observeCertificateRevocationForSelfClient: ObserveCertificateRevocationForSelfClientUseCase,
private val featureFlagsSyncWorker: FeatureFlagsSyncWorker,
private val updateApiVersions: UpdateApiVersionsUseCase
) : ViewModel() {

private val minIntervalBetweenPulls: Duration = MIN_INTERVAL_BETWEEN_PULLS
Expand Down Expand Up @@ -73,7 +75,8 @@ class AppSyncViewModel @Inject constructor(
listOf(
viewModelScope.launch { syncCertificateRevocationListUseCase() },
viewModelScope.launch { featureFlagsSyncWorker.execute() },
viewModelScope.launch { observeCertificateRevocationForSelfClient.invoke() }
viewModelScope.launch { observeCertificateRevocationForSelfClient.invoke() },
viewModelScope.launch { updateApiVersions() },
).joinAll()
} catch (e: Exception) {
appLogger.e("Error while syncing app config", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ import com.wire.kalium.logic.feature.client.ObserveNewClientsUseCase
import com.wire.kalium.logic.feature.conversation.CheckConversationInviteCodeUseCase
import com.wire.kalium.logic.feature.server.GetServerConfigResult
import com.wire.kalium.logic.feature.server.GetServerConfigUseCase
import com.wire.kalium.logic.feature.server.UpdateApiVersionsUseCase
import com.wire.kalium.logic.feature.session.CurrentSessionFlowUseCase
import com.wire.kalium.logic.feature.session.CurrentSessionResult
import com.wire.kalium.logic.feature.session.DoesValidSessionExistResult
Expand Down Expand Up @@ -683,7 +682,6 @@ class WireActivityViewModelTest {
flowOf(false)
every { workManager.cancelAllWorkByTag(any()) } returns OperationImpl()
every { workManager.enqueueUniquePeriodicWork(any(), any(), any()) } returns OperationImpl()
coEvery { updateApiVersions() } returns Unit
}

@MockK
Expand Down Expand Up @@ -748,9 +746,6 @@ class WireActivityViewModelTest {
@MockK
lateinit var workManager: WorkManager

@MockK
lateinit var updateApiVersions: UpdateApiVersionsUseCase

@MockK
lateinit var observeEstablishedCalls: ObserveEstablishedCallsUseCase

Expand Down Expand Up @@ -784,8 +779,7 @@ class WireActivityViewModelTest {
observeScreenshotCensoringConfigUseCaseProviderFactory = observeScreenshotCensoringConfigUseCaseProviderFactory,
globalDataStore = { globalDataStore },
observeIfE2EIRequiredDuringLoginUseCaseProviderFactory = observeIfE2EIRequiredDuringLoginUseCaseProviderFactory,
workManager = { workManager },
updateApiVersions = updateApiVersions
workManager = { workManager }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.wire.android.config.CoroutineTestExtension
import com.wire.kalium.logic.feature.e2ei.SyncCertificateRevocationListUseCase
import com.wire.kalium.logic.feature.e2ei.usecase.ObserveCertificateRevocationForSelfClientUseCase
import com.wire.kalium.logic.feature.featureConfig.FeatureFlagsSyncWorker
import com.wire.kalium.logic.feature.server.UpdateApiVersionsUseCase
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
Expand All @@ -40,6 +41,7 @@ class AppSyncViewModelTest {
withObserveCertificateRevocationForSelfClient()
withFeatureFlagsSyncWorker()
withSyncCertificateRevocationListUseCase()
withUpdateApiVersions()
}

viewModel.startSyncingAppConfig()
Expand All @@ -48,6 +50,7 @@ class AppSyncViewModelTest {
coVerify { arrangement.observeCertificateRevocationForSelfClient.invoke() }
coVerify { arrangement.syncCertificateRevocationListUseCase.invoke() }
coVerify { arrangement.featureFlagsSyncWorker.execute() }
coVerify { arrangement.updateApiVersions() }
}

@Test
Expand All @@ -56,6 +59,7 @@ class AppSyncViewModelTest {
withObserveCertificateRevocationForSelfClient(1000)
withFeatureFlagsSyncWorker(1000)
withSyncCertificateRevocationListUseCase(1000)
withUpdateApiVersions(1000)
}

viewModel.startSyncingAppConfig()
Expand All @@ -66,6 +70,7 @@ class AppSyncViewModelTest {
coVerify(exactly = 1) { arrangement.observeCertificateRevocationForSelfClient.invoke() }
coVerify(exactly = 1) { arrangement.syncCertificateRevocationListUseCase.invoke() }
coVerify(exactly = 1) { arrangement.featureFlagsSyncWorker.execute() }
coVerify(exactly = 1) { arrangement.updateApiVersions() }
}

private class Arrangement {
Expand All @@ -79,14 +84,18 @@ class AppSyncViewModelTest {
@MockK
lateinit var featureFlagsSyncWorker: FeatureFlagsSyncWorker

@MockK
lateinit var updateApiVersions: UpdateApiVersionsUseCase

init {
MockKAnnotations.init(this)
}

private val viewModel = AppSyncViewModel(
syncCertificateRevocationListUseCase,
observeCertificateRevocationForSelfClient,
featureFlagsSyncWorker
featureFlagsSyncWorker,
updateApiVersions
)

@OptIn(InternalCoroutinesApi::class)
Expand All @@ -108,6 +117,12 @@ class AppSyncViewModelTest {
}
}

fun withUpdateApiVersions(delayMs: Long = 0) {
coEvery { updateApiVersions() } coAnswers {
delay(delayMs)
}
}

fun arrange(block: Arrangement.() -> Unit) = apply(block).let {
this to viewModel
}
Expand Down

0 comments on commit d52f0cf

Please sign in to comment.