restore clear buttons

This commit is contained in:
Bnyro 2022-08-13 22:51:32 +02:00
parent 7c95f5f252
commit 7d993fcd5c
6 changed files with 32 additions and 12 deletions

View File

@ -17,4 +17,7 @@ interface CustomInstanceDao {
@Delete @Delete
fun delete(customInstance: CustomInstance) fun delete(customInstance: CustomInstance)
@Query("DELETE FROM customInstance")
fun deleteAll()
} }

View File

@ -17,4 +17,7 @@ interface SearchHistoryDao {
@Delete @Delete
fun delete(searchHistoryItem: SearchHistoryItem) fun delete(searchHistoryItem: SearchHistoryItem)
@Query("DELETE FROM searchHistoryItem")
fun deleteAll()
} }

View File

@ -20,4 +20,7 @@ interface WatchHistoryDao {
@Delete @Delete
fun delete(watchHistoryItem: WatchHistoryItem) fun delete(watchHistoryItem: WatchHistoryItem)
@Query("DELETE FROM watchHistoryItem")
fun deleteAll()
} }

View File

@ -20,4 +20,7 @@ interface WatchPositionDao {
@Delete @Delete
fun delete(watchPosition: WatchPosition) fun delete(watchPosition: WatchPosition)
@Query("DELETE FROM watchPosition")
fun deleteAll()
} }

View File

@ -4,6 +4,7 @@ import android.os.Bundle
import androidx.preference.Preference import androidx.preference.Preference
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.activities.SettingsActivity import com.github.libretube.activities.SettingsActivity
import com.github.libretube.database.DatabaseHolder
import com.github.libretube.views.MaterialPreferenceFragment import com.github.libretube.views.MaterialPreferenceFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -18,33 +19,41 @@ class HistorySettings : MaterialPreferenceFragment() {
// clear search history // clear search history
val clearHistory = findPreference<Preference>(PreferenceKeys.CLEAR_SEARCH_HISTORY) val clearHistory = findPreference<Preference>(PreferenceKeys.CLEAR_SEARCH_HISTORY)
clearHistory?.setOnPreferenceClickListener { clearHistory?.setOnPreferenceClickListener {
showClearDialog(R.string.clear_history, "search_history") showClearDialog(R.string.clear_history) {
DatabaseHolder.database.searchHistoryDao().deleteAll()
}
true true
} }
// clear watch history and positions // clear watch history and positions
val clearWatchHistory = findPreference<Preference>(PreferenceKeys.CLEAR_WATCH_HISTORY) val clearWatchHistory = findPreference<Preference>(PreferenceKeys.CLEAR_WATCH_HISTORY)
clearWatchHistory?.setOnPreferenceClickListener { clearWatchHistory?.setOnPreferenceClickListener {
showClearDialog(R.string.clear_history, "watch_history") showClearDialog(R.string.clear_history) {
DatabaseHolder.database.watchHistoryDao().deleteAll()
}
true true
} }
// clear watch positions // clear watch positions
val clearWatchPositions = findPreference<Preference>(PreferenceKeys.CLEAR_WATCH_POSITIONS) val clearWatchPositions = findPreference<Preference>(PreferenceKeys.CLEAR_WATCH_POSITIONS)
clearWatchPositions?.setOnPreferenceClickListener { clearWatchPositions?.setOnPreferenceClickListener {
showClearDialog(R.string.reset_watch_positions, "watch_positions") showClearDialog(R.string.reset_watch_positions) {
DatabaseHolder.database.watchPositionDao().deleteAll()
}
true true
} }
} }
private fun showClearDialog(title: Int, preferenceKey: String) { private fun showClearDialog(title: Int, action: () -> Unit) {
MaterialAlertDialogBuilder(requireContext()) MaterialAlertDialogBuilder(requireContext())
.setTitle(title) .setTitle(title)
.setMessage(R.string.irreversible) .setMessage(R.string.irreversible)
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.okay) { _, _ -> .setPositiveButton(R.string.okay) { _, _ ->
// clear the selected preference preferences // clear the selected preference preferences
PreferenceHelper.removePreference(preferenceKey) Thread {
action()
}.start()
} }
.show() .show()
} }

View File

@ -18,6 +18,7 @@ import com.github.libretube.dialogs.CustomInstanceDialog
import com.github.libretube.dialogs.DeleteAccountDialog import com.github.libretube.dialogs.DeleteAccountDialog
import com.github.libretube.dialogs.LoginDialog import com.github.libretube.dialogs.LoginDialog
import com.github.libretube.dialogs.LogoutDialog import com.github.libretube.dialogs.LogoutDialog
import com.github.libretube.extensions.await
import com.github.libretube.obj.CustomInstance import com.github.libretube.obj.CustomInstance
import com.github.libretube.util.ImportHelper import com.github.libretube.util.ImportHelper
import com.github.libretube.util.PermissionHelper import com.github.libretube.util.PermissionHelper
@ -106,9 +107,10 @@ class InstanceSettings : MaterialPreferenceFragment() {
val clearCustomInstances = findPreference<Preference>(PreferenceKeys.CLEAR_CUSTOM_INSTANCES) val clearCustomInstances = findPreference<Preference>(PreferenceKeys.CLEAR_CUSTOM_INSTANCES)
clearCustomInstances?.setOnPreferenceClickListener { clearCustomInstances?.setOnPreferenceClickListener {
PreferenceHelper.removePreference("customInstances") Thread {
val intent = Intent(context, SettingsActivity::class.java) DatabaseHolder.database.customInstanceDao().deleteAll()
startActivity(intent) }.await()
activity?.recreate()
true true
} }
@ -162,10 +164,7 @@ class InstanceSettings : MaterialPreferenceFragment() {
var customInstances = listOf<CustomInstance>() var customInstances = listOf<CustomInstance>()
Thread { Thread {
customInstances = DatabaseHolder.database.customInstanceDao().getAll() customInstances = DatabaseHolder.database.customInstanceDao().getAll()
}.apply { }.await()
start()
join()
}
val instanceNames = arrayListOf<String>() val instanceNames = arrayListOf<String>()
val instanceValues = arrayListOf<String>() val instanceValues = arrayListOf<String>()