-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathAppSyncViewModel.kt
More file actions
98 lines (87 loc) · 3.75 KB
/
AppSyncViewModel.kt
File metadata and controls
98 lines (87 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* 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
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wire.android.appLogger
import com.wire.kalium.logic.feature.client.MLSClientManager
import com.wire.kalium.logic.feature.conversation.keyingmaterials.KeyingMaterialsManager
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.mlsmigration.MLSMigrationManager
import com.wire.kalium.logic.feature.server.UpdateApiVersionsUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import javax.inject.Inject
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes
@HiltViewModel
class AppSyncViewModel @Inject constructor(
private val syncCertificateRevocationListUseCase: SyncCertificateRevocationListUseCase,
private val observeCertificateRevocationForSelfClient: ObserveCertificateRevocationForSelfClientUseCase,
private val featureFlagsSyncWorker: FeatureFlagsSyncWorker,
private val updateApiVersions: UpdateApiVersionsUseCase,
private val mLSMigrationManager: MLSMigrationManager,
private val keyingMaterialsManager: KeyingMaterialsManager,
private val mLSClientManager: MLSClientManager,
) : ViewModel() {
private val minIntervalBetweenPulls: Duration = MIN_INTERVAL_BETWEEN_PULLS
private var lastPullInstant: Instant? = null
private var syncDataJob: Job? = null
fun startSyncingAppConfig() {
if (isSyncing()) return
val now = Clock.System.now()
if (isPullTooRecent(now)) return
lastPullInstant = now
syncDataJob = viewModelScope.launch {
runSyncTasks()
}
}
private fun isSyncing(): Boolean {
return syncDataJob?.isActive == true
}
private fun isPullTooRecent(now: Instant): Boolean {
return lastPullInstant?.let { lastPull ->
lastPull + minIntervalBetweenPulls > now
} ?: false
}
@Suppress("TooGenericExceptionCaught")
private suspend fun runSyncTasks() {
try {
listOf(
viewModelScope.launch { syncCertificateRevocationListUseCase() },
viewModelScope.launch { featureFlagsSyncWorker.execute() },
viewModelScope.launch { observeCertificateRevocationForSelfClient.invoke() },
viewModelScope.launch { updateApiVersions() },
viewModelScope.launch { mLSClientManager() },
viewModelScope.launch { mLSMigrationManager() },
viewModelScope.launch { keyingMaterialsManager() },
).joinAll()
} catch (e: Exception) {
appLogger.e("Error while syncing app config", e)
}
}
companion object {
val MIN_INTERVAL_BETWEEN_PULLS = 60.minutes
}
}