Repository: stakwork/sphinx-kotlin
Issue: Slow Network Refresh [No GooglePlay Services]

The Dashboard's call to network refresh includes updating of the push notification token. If the device does not have GooglePlay Services that push notification token retrieval runs until it times out before continuing with the rest of the network refresh.

Changed files:
sphinx/screens/dashboard/dashboard/src/main/java/chat/sphinx/dashboard/ui/DashboardViewModel.kt
sphinx/service/features/notifications/feature-service-notification-firebase/src/main/java/chat/sphinx/feature_service_notification_firebase/FirebasePushNotificationRegistrar.kt

diff --git a/sphinx/screens/dashboard/dashboard/src/main/java/chat/sphinx/dashboard/ui/DashboardViewModel.kt b/sphinx/screens/dashboard/dashboard/src/main/java/chat/sphinx/dashboard/ui/DashboardViewModel.kt
index 5baa7b26b..66e831644 100644
--- a/sphinx/screens/dashboard/dashboard/src/main/java/chat/sphinx/dashboard/ui/DashboardViewModel.kt
+++ b/sphinx/screens/dashboard/dashboard/src/main/java/chat/sphinx/dashboard/ui/DashboardViewModel.kt
@@ -351,8 +351,8 @@ internal class DashboardViewModel @Inject constructor(
     val networkStateFlow: StateFlow<LoadResponse<Boolean, ResponseError>>
         get() = _networkStateFlow.asStateFlow()
 
-    private var pushNotificationRegistrationUpdated: Boolean = false
     private var jobNetworkRefresh: Job? = null
+    private var jobPushNotificationRegistration: Job? = null
     fun networkRefresh() {
         if (jobNetworkRefresh?.isActive == true) {
             return
@@ -389,15 +389,18 @@ internal class DashboardViewModel @Inject constructor(
                 jobNetworkRefresh?.cancel()
             }
 
-            if (!pushNotificationRegistrationUpdated) {
-                pushNotificationRegistrar.register().let { response ->
-                    @Exhaustive
-                    when (response) {
-                        is Response.Error -> {
-                            // TODO: Handle on the UI
-                        }
-                        is Response.Success -> {
-                            pushNotificationRegistrationUpdated = true
+            // must occur after contacts have been retrieved such that
+            // an account owner is available, otherwise it just suspends
+            // until it is.
+            if (jobPushNotificationRegistration == null) {
+                jobPushNotificationRegistration = launch(mainImmediate) {
+                    pushNotificationRegistrar.register().let { response ->
+                        @Exhaustive
+                        when (response) {
+                            is Response.Error -> {
+                                // TODO: Handle on the UI
+                            }
+                            is Response.Success -> {}
                         }
                     }
                 }
diff --git a/sphinx/service/features/notifications/feature-service-notification-firebase/src/main/java/chat/sphinx/feature_service_notification_firebase/FirebasePushNotificationRegistrar.kt b/sphinx/service/features/notifications/feature-service-notification-firebase/src/main/java/chat/sphinx/feature_service_notification_firebase/FirebasePushNotificationRegistrar.kt
index f6e5ddcd7..c9cf8e051 100644
--- a/sphinx/service/features/notifications/feature-service-notification-firebase/src/main/java/chat/sphinx/feature_service_notification_firebase/FirebasePushNotificationRegistrar.kt
+++ b/sphinx/service/features/notifications/feature-service-notification-firebase/src/main/java/chat/sphinx/feature_service_notification_firebase/FirebasePushNotificationRegistrar.kt
@@ -61,15 +61,17 @@ internal class FirebasePushNotificationRegistrar(
             }
         } catch (e: Exception) {}
 
-        if (tokenFetchResponse.value is Response.Error) {
-            return tokenFetchResponse.value!!
-        }
-
-        return contactRepository.updateOwnerDeviceId(
-            DeviceId(
-                (tokenFetchResponse.value!! as Response.Success).value
-            )
-        )
+        return tokenFetchResponse.value?.let { response ->
+            @Exhaustive
+            when (response) {
+                is Response.Error -> {
+                    response
+                }
+                is Response.Success -> {
+                    contactRepository.updateOwnerDeviceId(DeviceId(response.value))
+                }
+            }
+        } ?: Response.Error(ResponseError("NotificationToken retrieved was null"))
     }
 
     init {