From 5d557627d450fb0b647337ee39f8fdb8b3189261 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Wed, 11 Dec 2024 23:02:15 +0300 Subject: [PATCH] sync: smoother exceptions (fixes #4861) (#4864) Co-authored-by: dogi --- app/build.gradle | 4 +-- .../ui/sync/DashboardElementActivity.kt | 25 ++++++++++++++++--- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d5f47c5b5b..91d5b9712d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,8 +10,8 @@ android { applicationId "org.ole.planet.myplanet" minSdkVersion 26 targetSdkVersion 34 - versionCode 2132 - versionName "0.21.32" + versionCode 2133 + versionName "0.21.33" ndkVersion '21.3.6528147' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/sync/DashboardElementActivity.kt b/app/src/main/java/org/ole/planet/myplanet/ui/sync/DashboardElementActivity.kt index f4e213c23d..d6e2042da8 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/sync/DashboardElementActivity.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/sync/DashboardElementActivity.kt @@ -20,9 +20,12 @@ import androidx.fragment.app.FragmentManager import androidx.lifecycle.lifecycleScope import androidx.localbroadcastmanager.content.LocalBroadcastManager import com.google.android.material.bottomnavigation.BottomNavigationView +import io.realm.Realm import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.ole.planet.myplanet.R import org.ole.planet.myplanet.callback.OnRatingChangeListener import org.ole.planet.myplanet.model.RealmUserChallengeActions.Companion.createAction @@ -121,10 +124,24 @@ abstract class DashboardElementActivity : SyncActivity(), FragmentManager.OnBack } fun logSyncInSharedPrefs() { - lifecycleScope.launch { - if (isServerReachable(Utilities.getUrl())) { - startUpload("dashboard") - createAction(mRealm, "${profileDbHandler.userModel?.id}", null, "sync") + lifecycleScope.launch(Dispatchers.IO + SupervisorJob()) { + try { + val isReachable = isServerReachable(Utilities.getUrl()) + if (isReachable) { + withContext(Dispatchers.Main) { + startUpload("dashboard") + } + + withContext(Dispatchers.IO) { + Realm.getDefaultInstance().use { realm -> + realm.executeTransaction { + createAction(realm, "${profileDbHandler.userModel?.id}", null, "sync") + } + } + } + } + } catch (e: Exception) { + e.printStackTrace() } } }