From acde970f01e9a2553949b247d6a5529029a988dd Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 14 Jun 2024 12:19:07 +0200 Subject: [PATCH] Extract function and add more logs. --- .../appnav/loggedin/LoggedInPresenter.kt | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/appnav/src/main/kotlin/io/element/android/appnav/loggedin/LoggedInPresenter.kt b/appnav/src/main/kotlin/io/element/android/appnav/loggedin/LoggedInPresenter.kt index 386fe2dd5c..a2cb95ff6f 100644 --- a/appnav/src/main/kotlin/io/element/android/appnav/loggedin/LoggedInPresenter.kt +++ b/appnav/src/main/kotlin/io/element/android/appnav/loggedin/LoggedInPresenter.kt @@ -55,27 +55,7 @@ class LoggedInPresenter @Inject constructor( LaunchedEffect(isVerified) { if (isVerified) { - Timber.d("Ensure pusher is registered") - val currentPushProvider = pushService.getCurrentPushProvider() - val result = if (currentPushProvider == null) { - Timber.d("Register with the first available push provider") - val pushProvider = pushService.getAvailablePushProviders().firstOrNull() ?: return@LaunchedEffect - val distributor = pushProvider.getDistributors().firstOrNull() ?: return@LaunchedEffect - pushService.registerWith(matrixClient, pushProvider, distributor) - } else { - val currentPushDistributor = currentPushProvider.getCurrentDistributor(matrixClient) - if (currentPushDistributor == null) { - Timber.d("Register with the first available distributor") - val distributor = currentPushProvider.getDistributors().firstOrNull() ?: return@LaunchedEffect - pushService.registerWith(matrixClient, currentPushProvider, distributor) - } else { - Timber.d("Re-register with the current distributor") - pushService.registerWith(matrixClient, currentPushProvider, currentPushDistributor) - } - } - result.onFailure { - Timber.e(it, "Failed to register pusher") - } + ensurePusherIsRegistered() } else { Timber.w("Session is not verified, not registering pusher") } @@ -99,6 +79,33 @@ class LoggedInPresenter @Inject constructor( ) } + private suspend fun ensurePusherIsRegistered() { + Timber.d("Ensure pusher is registered") + val currentPushProvider = pushService.getCurrentPushProvider() + val result = if (currentPushProvider == null) { + Timber.d("Register with the first available push provider") + val pushProvider = pushService.getAvailablePushProviders().firstOrNull() + ?: return Unit.also { Timber.w("No push provider available") } + val distributor = pushProvider.getDistributors().firstOrNull() + ?: return Unit.also { Timber.w("No distributor available") } + pushService.registerWith(matrixClient, pushProvider, distributor) + } else { + val currentPushDistributor = currentPushProvider.getCurrentDistributor(matrixClient) + if (currentPushDistributor == null) { + Timber.d("Register with the first available distributor") + val distributor = currentPushProvider.getDistributors().firstOrNull() + ?: return Unit.also { Timber.w("No distributor available") } + pushService.registerWith(matrixClient, currentPushProvider, distributor) + } else { + Timber.d("Re-register with the current distributor") + pushService.registerWith(matrixClient, currentPushProvider, currentPushDistributor) + } + } + result.onFailure { + Timber.e(it, "Failed to register pusher") + } + } + private fun reportCryptoStatusToAnalytics(verificationState: SessionVerifiedStatus, recoveryState: RecoveryState) { // Update first the user property, to store the current status for that posthog user val userVerificationState = verificationState.toAnalyticsUserPropertyValue()