2022-06-07 16:12:59 +05:30
|
|
|
package com.github.libretube.preferences
|
|
|
|
|
2022-06-21 11:42:40 +05:30
|
|
|
import android.content.Context
|
2022-06-07 16:12:59 +05:30
|
|
|
import android.os.Bundle
|
2022-06-07 23:46:58 +05:30
|
|
|
import android.widget.TextView
|
2022-06-07 16:12:59 +05:30
|
|
|
import androidx.preference.Preference
|
|
|
|
import androidx.preference.PreferenceFragmentCompat
|
|
|
|
import androidx.preference.PreferenceManager
|
|
|
|
import com.github.libretube.R
|
2022-06-21 11:42:40 +05:30
|
|
|
import com.github.libretube.requireMainActivityRestart
|
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
2022-06-07 16:12:59 +05:30
|
|
|
|
2022-06-07 20:22:06 +05:30
|
|
|
class AdvancedSettings : PreferenceFragmentCompat() {
|
|
|
|
val TAG = "AdvancedSettings"
|
2022-06-09 17:52:07 +05:30
|
|
|
|
2022-06-07 16:12:59 +05:30
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
2022-06-07 20:22:06 +05:30
|
|
|
setPreferencesFromResource(R.xml.advanced_settings, rootKey)
|
|
|
|
|
2022-06-08 00:52:35 +05:30
|
|
|
val topBarTextView = activity?.findViewById<TextView>(R.id.topBar_textView)
|
|
|
|
topBarTextView?.text = getString(R.string.advanced)
|
2022-06-07 23:46:58 +05:30
|
|
|
|
2022-06-07 20:22:06 +05:30
|
|
|
val clearHistory = findPreference<Preference>("clear_history")
|
|
|
|
clearHistory?.setOnPreferenceClickListener {
|
|
|
|
val sharedPreferences =
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(requireContext())
|
|
|
|
sharedPreferences.edit().remove("search_history").commit()
|
|
|
|
true
|
|
|
|
}
|
2022-06-21 11:42:40 +05:30
|
|
|
|
|
|
|
val resetSettings = findPreference<Preference>("reset_settings")
|
|
|
|
resetSettings?.setOnPreferenceClickListener {
|
|
|
|
showResetDialog()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun showResetDialog() {
|
|
|
|
MaterialAlertDialogBuilder(requireContext())
|
|
|
|
.setPositiveButton(R.string.reset) { _, _ ->
|
|
|
|
// clear default preferences
|
|
|
|
val sharedPreferences =
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(requireContext())
|
|
|
|
sharedPreferences.edit().clear().commit()
|
|
|
|
|
|
|
|
// clear login token
|
|
|
|
val sharedPrefToken =
|
|
|
|
context?.getSharedPreferences("token", Context.MODE_PRIVATE)
|
|
|
|
sharedPrefToken?.edit()?.clear()?.commit()
|
|
|
|
|
|
|
|
requireMainActivityRestart = true
|
|
|
|
activity?.recreate()
|
|
|
|
}
|
|
|
|
.setNegativeButton(getString(R.string.cancel)) { _, _ -> }
|
|
|
|
.setTitle(R.string.reset)
|
|
|
|
.setMessage(R.string.reset_message)
|
|
|
|
.show()
|
2022-06-07 16:12:59 +05:30
|
|
|
}
|
2022-06-07 16:14:22 +05:30
|
|
|
}
|