diff --git a/app/src/main/kotlin/app/dapk/st/SmallTalkApplication.kt b/app/src/main/kotlin/app/dapk/st/SmallTalkApplication.kt index 8d906ef..90a3a50 100644 --- a/app/src/main/kotlin/app/dapk/st/SmallTalkApplication.kt +++ b/app/src/main/kotlin/app/dapk/st/SmallTalkApplication.kt @@ -60,13 +60,12 @@ class SmallTalkApplication : Application(), ModuleProvider { private fun onApplicationLaunch(notificationsModule: NotificationsModule, storeModule: StoreModule) { applicationScope.launch { + featureModules.homeModule.betaVersionUpgradeUseCase.waitUnitReady() + storeModule.credentialsStore().credentials()?.let { featureModules.pushModule.pushTokenRegistrar().registerCurrentToken() } runCatching { storeModule.localEchoStore.preload() } - } - - applicationScope.launch { val notificationsUseCase = notificationsModule.notificationsUseCase() notificationsUseCase.listenForNotificationChanges(this) } diff --git a/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt b/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt index 716910c..940c2f1 100644 --- a/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt +++ b/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt @@ -20,6 +20,7 @@ import app.dapk.st.directory.DirectoryModule import app.dapk.st.domain.StoreModule import app.dapk.st.engine.MatrixEngine import app.dapk.st.firebase.messaging.MessagingModule +import app.dapk.st.home.BetaVersionUpgradeUseCase import app.dapk.st.home.HomeModule import app.dapk.st.home.MainActivity import app.dapk.st.imageloader.ImageLoaderModule @@ -164,7 +165,16 @@ internal class FeatureModules internal constructor( deviceMeta, ) } - val homeModule by unsafeLazy { HomeModule(chatEngineModule.engine, storeModule.value, buildMeta) } + val homeModule by unsafeLazy { + HomeModule( + chatEngineModule.engine, + storeModule.value, + BetaVersionUpgradeUseCase( + storeModule.value.applicationStore(), + buildMeta, + ), + ) + } val settingsModule by unsafeLazy { SettingsModule( chatEngineModule.engine, diff --git a/features/home/src/main/kotlin/app/dapk/st/home/BetaVersionUpgradeUseCase.kt b/features/home/src/main/kotlin/app/dapk/st/home/BetaVersionUpgradeUseCase.kt index 7897223..4a1168a 100644 --- a/features/home/src/main/kotlin/app/dapk/st/home/BetaVersionUpgradeUseCase.kt +++ b/features/home/src/main/kotlin/app/dapk/st/home/BetaVersionUpgradeUseCase.kt @@ -3,24 +3,44 @@ package app.dapk.st.home import app.dapk.st.core.BuildMeta import app.dapk.st.domain.ApplicationPreferences import app.dapk.st.domain.ApplicationVersion +import kotlinx.coroutines.CancellableContinuation import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.suspendCancellableCoroutine +import kotlin.coroutines.resume class BetaVersionUpgradeUseCase( private val applicationPreferences: ApplicationPreferences, private val buildMeta: BuildMeta, ) { + private var _continuation: CancellableContinuation? = null + fun hasVersionChanged(): Boolean { - return runBlocking { - val previousVersion = applicationPreferences.readVersion()?.value - val currentVersion = buildMeta.versionCode - when (previousVersion) { - null -> false - else -> currentVersion > previousVersion - }.also { - applicationPreferences.setVersion(ApplicationVersion(currentVersion)) + return runBlocking { hasChangedVersion() } + } + + private suspend fun hasChangedVersion(): Boolean { + val previousVersion = applicationPreferences.readVersion()?.value + val currentVersion = buildMeta.versionCode + return when (previousVersion) { + null -> false + else -> currentVersion > previousVersion + }.also { + applicationPreferences.setVersion(ApplicationVersion(currentVersion)) + } + } + + suspend fun waitUnitReady() { + if (hasChangedVersion()) { + suspendCancellableCoroutine { continuation -> + _continuation = continuation } } } + fun notifyUpgraded() { + _continuation?.resume(Unit) + _continuation = null + } + } \ No newline at end of file diff --git a/features/home/src/main/kotlin/app/dapk/st/home/HomeModule.kt b/features/home/src/main/kotlin/app/dapk/st/home/HomeModule.kt index 210b5aa..f132898 100644 --- a/features/home/src/main/kotlin/app/dapk/st/home/HomeModule.kt +++ b/features/home/src/main/kotlin/app/dapk/st/home/HomeModule.kt @@ -1,6 +1,5 @@ package app.dapk.st.home -import app.dapk.st.core.BuildMeta import app.dapk.st.core.ProvidableModule import app.dapk.st.directory.DirectoryViewModel import app.dapk.st.domain.StoreModule @@ -11,7 +10,7 @@ import app.dapk.st.profile.ProfileViewModel class HomeModule( private val chatEngine: ChatEngine, private val storeModule: StoreModule, - private val buildMeta: BuildMeta, + val betaVersionUpgradeUseCase: BetaVersionUpgradeUseCase, ) : ProvidableModule { fun homeViewModel(directory: DirectoryViewModel, login: LoginViewModel, profileViewModel: ProfileViewModel): HomeViewModel { @@ -22,10 +21,7 @@ class HomeModule( login, profileViewModel, storeModule.cacheCleaner(), - BetaVersionUpgradeUseCase( - storeModule.applicationStore(), - buildMeta, - ), + betaVersionUpgradeUseCase, ) } diff --git a/features/home/src/main/kotlin/app/dapk/st/home/HomeViewModel.kt b/features/home/src/main/kotlin/app/dapk/st/home/HomeViewModel.kt index a18e8e8..719f41a 100644 --- a/features/home/src/main/kotlin/app/dapk/st/home/HomeViewModel.kt +++ b/features/home/src/main/kotlin/app/dapk/st/home/HomeViewModel.kt @@ -12,7 +12,6 @@ import app.dapk.st.profile.ProfileViewModel import app.dapk.st.viewmodel.DapkViewModel import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job -import kotlinx.coroutines.cancel import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach @@ -87,6 +86,7 @@ class HomeViewModel( fun clearCache() { viewModelScope.launch { cacheCleaner.cleanCache(removeCredentials = false) + betaVersionUpgradeUseCase.notifyUpgraded() _events.emit(HomeEvent.Relaunch) } }