Skip to content

Commit ede14ed

Browse files
Okuro3499dogi
andauthored
settings: smoother clear data (fixes #3755) (#3756)
Co-authored-by: dogi <[email protected]>
1 parent d09464b commit ede14ed

File tree

4 files changed

+57
-58
lines changed

4 files changed

+57
-58
lines changed

app/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
applicationId "org.ole.planet.myplanet"
1010
minSdkVersion 21
1111
targetSdkVersion 34
12-
versionCode 1644
13-
versionName "0.16.44"
12+
versionCode 1645
13+
versionName "0.16.45"
1414
ndkVersion '21.3.6528147'
1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616
vectorDrawables.useSupportLibrary = true

app/src/main/java/org/ole/planet/myplanet/ui/SettingActivity.kt

+6-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import org.ole.planet.myplanet.service.UserProfileDbHandler
2525
import org.ole.planet.myplanet.ui.dashboard.DashboardActivity
2626
import org.ole.planet.myplanet.ui.dashboard.DashboardFragment
2727
import org.ole.planet.myplanet.ui.sync.LoginActivity
28+
import org.ole.planet.myplanet.ui.sync.SyncActivity.Companion.clearRealmDb
29+
import org.ole.planet.myplanet.ui.sync.SyncActivity.Companion.clearSharedPref
30+
import org.ole.planet.myplanet.ui.sync.SyncActivity.Companion.restartApp
2831
import org.ole.planet.myplanet.utilities.Constants.PREFS_NAME
2932
import org.ole.planet.myplanet.utilities.DialogUtils
3033
import org.ole.planet.myplanet.utilities.FileUtils.availableOverTotalMemoryFormattedString
@@ -100,15 +103,9 @@ class SettingActivity : AppCompatActivity() {
100103
preference.onPreferenceClickListener = OnPreferenceClickListener {
101104
AlertDialog.Builder(requireActivity()).setTitle(R.string.are_you_sure)
102105
.setPositiveButton(R.string.yes) { _: DialogInterface?, _: Int ->
103-
BaseResourceFragment.settings?.edit()?.clear()?.apply()
104-
mRealm.executeTransactionAsync(
105-
Realm.Transaction { realm: Realm -> realm.deleteAll() },
106-
Realm.Transaction.OnSuccess {
107-
Utilities.toast(requireActivity(), R.string.data_cleared.toString())
108-
startActivity(Intent(requireActivity(), LoginActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK))
109-
openDashboard = false
110-
requireActivity().finish()
111-
})
106+
clearRealmDb()
107+
clearSharedPref()
108+
restartApp()
112109
}.setNegativeButton(R.string.no, null).show()
113110
false
114111
}

app/src/main/java/org/ole/planet/myplanet/ui/dashboard/BaseDashboardFragment.kt

+19-18
Original file line numberDiff line numberDiff line change
@@ -262,25 +262,26 @@ open class BaseDashboardFragment : BaseDashboardFragmentPlugin(), NotificationCa
262262

263263
private fun setUpMyLife(userId: String?) {
264264
val realm = DatabaseService(requireContext()).realmInstance
265-
val realmObjects = RealmMyLife.getMyLifeByUserId(mRealm, settings)
266-
if (realmObjects.isEmpty()) {
267-
if (!realm.isInTransaction) {
268-
realm.beginTransaction()
269-
}
270-
val myLifeListBase = getMyLifeListBase(userId)
271-
var ml: RealmMyLife
272-
var weight = 1
273-
for (item in myLifeListBase) {
274-
ml = realm.createObject(RealmMyLife::class.java, UUID.randomUUID().toString())
275-
ml.title = item.title
276-
ml.imageId = item.imageId
277-
ml.weight = weight
278-
ml.userId = item.userId
279-
ml.isVisible = true
280-
weight++
265+
realm.executeTransactionAsync({ realmTransaction ->
266+
val realmObjects = RealmMyLife.getMyLifeByUserId(realmTransaction, settings)
267+
if (realmObjects.isEmpty()) {
268+
val myLifeListBase = getMyLifeListBase(userId)
269+
var weight = 1
270+
for (item in myLifeListBase) {
271+
val ml = realmTransaction.createObject(RealmMyLife::class.java, UUID.randomUUID().toString())
272+
ml.title = item.title
273+
ml.imageId = item.imageId
274+
ml.weight = weight
275+
ml.userId = item.userId
276+
ml.isVisible = true
277+
weight++
278+
}
281279
}
282-
realm.commitTransaction()
283-
}
280+
}, {
281+
realm.close()
282+
}, {
283+
realm.close()
284+
})
284285
}
285286

286287
private fun myLibraryItemClickAction(textView: TextView, items: RealmMyLibrary?) {

app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.kt

+30-29
Original file line numberDiff line numberDiff line change
@@ -134,35 +134,6 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers
134134
.show()
135135
}
136136

137-
private fun clearSharedPref() {
138-
val keysToKeep = setOf(prefData.FIRSTLAUNCH)
139-
val tempStorage = HashMap<String, Boolean>()
140-
for (key in keysToKeep) {
141-
tempStorage[key] = settings.getBoolean(key, false)
142-
}
143-
editor.clear().commit()
144-
val editor = editor
145-
for ((key, value) in tempStorage) {
146-
editor.putBoolean(key, value)
147-
}
148-
editor.commit()
149-
}
150-
151-
private fun clearRealmDb(){
152-
val realm = Realm.getDefaultInstance()
153-
realm.executeTransaction { transactionRealm ->
154-
transactionRealm.deleteAll()
155-
}
156-
realm.close()
157-
}
158-
159-
private fun restartApp() {
160-
val intent = packageManager.getLaunchIntentForPackage(packageName)
161-
val mainIntent = Intent.makeRestartActivityTask(intent?.component)
162-
startActivity(mainIntent)
163-
Runtime.getRuntime().exit(0)
164-
}
165-
166137
private fun clearInternalStorage() {
167138
val myDir = File(Utilities.SD_PATH)
168139
if (myDir.isDirectory) {
@@ -763,5 +734,35 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers
763734
companion object {
764735
lateinit var cal_today: Calendar
765736
lateinit var cal_last_Sync: Calendar
737+
738+
fun clearRealmDb() {
739+
val realm = Realm.getDefaultInstance()
740+
realm.executeTransaction { transactionRealm ->
741+
transactionRealm.deleteAll()
742+
}
743+
realm.close()
744+
}
745+
746+
fun clearSharedPref() {
747+
val settings = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
748+
val editor = settings.edit()
749+
val keysToKeep = setOf(SharedPrefManager(context).FIRSTLAUNCH)
750+
val tempStorage = HashMap<String, Boolean>()
751+
for (key in keysToKeep) {
752+
tempStorage[key] = settings.getBoolean(key, false)
753+
}
754+
editor.clear().commit()
755+
for ((key, value) in tempStorage) {
756+
editor.putBoolean(key, value)
757+
}
758+
editor.commit()
759+
}
760+
761+
fun restartApp() {
762+
val intent = context.packageManager.getLaunchIntentForPackage(context.packageName)
763+
val mainIntent = Intent.makeRestartActivityTask(intent?.component)
764+
context.startActivity(mainIntent)
765+
Runtime.getRuntime().exit(0)
766+
}
766767
}
767768
}

0 commit comments

Comments
 (0)