Skip to content

Commit

Permalink
add option to clear database
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipthelen committed Jun 27, 2024
1 parent 511fbe1 commit 813d40a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions Habitica/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
5 changes: 5 additions & 0 deletions Habitica/res/xml/preferences_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit 813d40a

Please sign in to comment.