add option to clear database

This commit is contained in:
Phillip Thelen 2024-06-27 14:49:37 +02:00
parent 511fbe1cb9
commit 813d40ace1
4 changed files with 31 additions and 4 deletions

View file

@ -1531,6 +1531,7 @@
<string name="customizing_your_avatar">customizing your avatar</string>
<string name="error">Error</string>
<string name="error_loading_gems">There was an error loading gems</string>
<string name="clear_database">Clear Database</string>
<plurals name="you_x_others">
<item quantity="zero">You</item>

View file

@ -404,5 +404,10 @@
android:entries="@array/server_urls"
android:entryValues="@array/server_urls"
android:layout="@layout/preference_child_summary" />
<Preference
android:key="clear_database"
android:title="@string/clear_database"
android:layout="@layout/preference_child_summary"
app:isPreferenceVisible="false"/>
</PreferenceCategory>
</PreferenceScreen>

View file

@ -318,12 +318,16 @@ abstract class HabiticaBaseApplication : Application(), Application.ActivityLife
return context.applicationContext as? HabiticaBaseApplication
}
fun deleteDatabase(context: Context) {
val realm = Realm.getDefaultInstance()
getInstance(context)?.deleteDatabase(realm.path)
realm.close()
}
fun logout(context: Context) {
MainScope().launchCatching {
getInstance(context)?.pushNotificationManager?.removePushDeviceUsingStoredToken()
val realm = Realm.getDefaultInstance()
getInstance(context)?.deleteDatabase(realm.path)
realm.close()
deleteDatabase(context)
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
val useReminder = preferences.getBoolean("use_reminder", false)
val reminderTime = preferences.getString("reminder_time", "19:00")

View file

@ -109,6 +109,9 @@ class PreferencesFragment :
serverUrlPreference?.summary =
preferenceManager.sharedPreferences?.getString("server_url", "")
val clearDatabasePreference = findPreference("clear_database") as? Preference
clearDatabasePreference?.isVisible = false
val themePreference = findPreference("theme_name") as? ListPreference
themePreference?.summary = themePreference?.entry ?: "Default"
val themeModePreference = findPreference("theme_mode") as? ListPreference
@ -215,12 +218,23 @@ class PreferencesFragment :
)
reloadContent(true)
}
"clear_database" -> {
context?.let { context ->
HabiticaBaseApplication.deleteDatabase(context)
lifecycleScope.launchCatching {
userRepository.retrieveUser(true, true)
reloadContent(true)
}
}
}
}
return super.onPreferenceTreeClick(preference)
}
private fun reloadContent(withConfirmation: Boolean) {
lifecycleScope.launch(ExceptionHandler.coroutine()) {
lifecycleScope.launchCatching {
contentRepository.retrieveContent(true)
if (withConfirmation) {
(activity as? SnackbarActivity)?.showSnackbar(
@ -583,6 +597,9 @@ class PreferencesFragment :
if (configManager.testingLevel() == AppTestingLevel.STAFF || BuildConfig.DEBUG) {
serverUrlPreference?.isVisible = true
taskListPreference?.isVisible = true
val clearDatabasePreference = findPreference("clear_database") as? Preference
clearDatabasePreference?.isVisible = true
}
}
}