CustomizeFragment.kt 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package io.r_a_d.radio2.preferences
  2. import android.content.Intent
  3. import android.net.Uri
  4. import android.os.Bundle
  5. import android.util.Log
  6. import androidx.appcompat.app.AlertDialog
  7. import androidx.preference.*
  8. import io.r_a_d.radio2.R
  9. import io.r_a_d.radio2.preferenceStore
  10. import io.r_a_d.radio2.ui.songs.request.Requestor
  11. class CustomizeFragment : PreferenceFragmentCompat() {
  12. override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
  13. setPreferencesFromResource(R.xml.customize_preferences, rootKey)
  14. val userNamePref = preferenceScreen.findPreference<EditTextPreference>("userName")
  15. userNamePref?.summaryProvider = EditTextPreference.SimpleSummaryProvider.getInstance()
  16. userNamePref?.setOnPreferenceChangeListener { _, newValue ->
  17. val name = newValue as String
  18. Requestor.instance.initFavorites(name) // need to be as parameter cause the callback is called BEFORE PARAMETER SET
  19. true
  20. }
  21. val snackbarPersistent = preferenceScreen.findPreference<SwitchPreferenceCompat>("snackbarPersistent")
  22. snackbarPersistent!!.summary = if (preferenceStore.getBoolean("snackbarPersistent", true))
  23. getString(R.string.snackbarPersistent)
  24. else
  25. getString(R.string.snackbarNonPersistent)
  26. snackbarPersistent.setOnPreferenceChangeListener { preference, newValue ->
  27. if (newValue as Boolean)
  28. preference.setSummary(R.string.snackbarPersistent)
  29. else
  30. preference.setSummary(R.string.snackbarNonPersistent)
  31. true
  32. }
  33. val splitLayout = preferenceScreen.findPreference<SwitchPreferenceCompat>("splitLayout")
  34. splitLayout!!.summary = if (preferenceStore.getBoolean("splitLayout", true))
  35. getString(R.string.split_layout)
  36. else
  37. getString(R.string.not_split_layout)
  38. splitLayout.setOnPreferenceChangeListener { preference, newValue ->
  39. if (newValue as Boolean)
  40. preference.setSummary(R.string.split_layout)
  41. else
  42. preference.setSummary(R.string.not_split_layout)
  43. true
  44. }
  45. val helpFavorites = preferenceScreen.findPreference<Preference>("helpFavorites")
  46. helpFavorites?.setOnPreferenceClickListener { _ ->
  47. val url = getString(R.string.github_url_wiki_irc_for_favorites)
  48. val i = Intent(Intent.ACTION_VIEW)
  49. i.data = Uri.parse(url)
  50. startActivity(i)
  51. true
  52. }
  53. val fetchPeriod = preferenceScreen.findPreference<ListPreference>("fetchPeriod")
  54. fetchPeriod?.summaryProvider = ListPreference.SimpleSummaryProvider.getInstance()
  55. fetchPeriod?.setOnPreferenceChangeListener { _, newValue ->
  56. val builder1 = AlertDialog.Builder(context!!)
  57. if (Integer.parseInt(newValue as String) == 0)
  58. builder1.setMessage(R.string.fetch_disabled_restart_the_app)
  59. else
  60. builder1.setMessage(R.string.restart_the_app)
  61. builder1.setCancelable(true)
  62. builder1.setPositiveButton("Close" ) { dialog, _ ->
  63. dialog.cancel()
  64. }
  65. val alert11 = builder1.create()
  66. alert11.show()
  67. true
  68. }
  69. }
  70. }