Skip to content

Commit

Permalink
sync: smoother clear with coroutines (fixes #4647) (#4648)
Browse files Browse the repository at this point in the history
Co-authored-by: Kuljeet Singh Bhengura <47841620+Kuljeet1998@users.noreply.github.com>
Co-authored-by: dogi <dogi@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 16, 2024
1 parent cc57d11 commit d1e770d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 26
targetSdkVersion 34
versionCode 2033
versionName "0.20.33"
versionCode 2034
versionName "0.20.34"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/org/ole/planet/myplanet/ui/SettingActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import androidx.preference.SwitchPreference
import io.realm.Realm
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.ole.planet.myplanet.MainApplication.Companion.mRealm
import org.ole.planet.myplanet.MainApplication.Companion.setThemeMode
import org.ole.planet.myplanet.R
Expand Down Expand Up @@ -135,9 +138,11 @@ class SettingActivity : AppCompatActivity() {
preference.onPreferenceClickListener = OnPreferenceClickListener {
AlertDialog.Builder(requireActivity()).setTitle(R.string.are_you_sure)
.setPositiveButton(R.string.yes) { _: DialogInterface?, _: Int ->
clearRealmDb()
clearSharedPref()
restartApp()
CoroutineScope(Dispatchers.Main).launch {
clearRealmDb()
clearSharedPref()
restartApp()
}
}.setNegativeButton(R.string.no, null).show()
false
}
Expand Down
27 changes: 19 additions & 8 deletions app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import androidx.recyclerview.widget.RecyclerView
import com.afollestad.materialdialogs.*
import com.google.android.material.textfield.TextInputLayout
import io.realm.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
import okhttp3.ResponseBody
Expand Down Expand Up @@ -144,9 +148,11 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers
AlertDialog.Builder(this, R.style.AlertDialogTheme)
.setMessage(message)
.setPositiveButton(getString(R.string.clear_data)) { _, _ ->
clearRealmDb()
clearSharedPref()
restartApp()
CoroutineScope(Dispatchers.Main).launch {
clearRealmDb()
clearSharedPref()
restartApp()
}
}
.setNegativeButton(getString(R.string.cancel)) { _, _ ->
onCancel()
Expand Down Expand Up @@ -850,12 +856,17 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers
lateinit var cal_today: Calendar
lateinit var cal_last_Sync: Calendar

fun clearRealmDb() {
val realm = Realm.getDefaultInstance()
realm.executeTransaction { transactionRealm ->
transactionRealm.deleteAll()
suspend fun clearRealmDb() {
withContext(Dispatchers.IO) {
val realm = Realm.getDefaultInstance()
try {
realm.executeTransaction { transactionRealm ->
transactionRealm.deleteAll()
}
} finally {
realm.close()
}
}
realm.close()
}

fun clearSharedPref() {
Expand Down

0 comments on commit d1e770d

Please sign in to comment.