pull/298/head
parent
bc2de4a6b8
commit
9f6df2b27e
@ -0,0 +1,61 @@ |
||||
package io.legado.app.base |
||||
|
||||
import android.annotation.SuppressLint |
||||
import androidx.fragment.app.DialogFragment |
||||
import androidx.preference.* |
||||
import io.legado.app.ui.widget.prefs.EditTextPreferenceDialog |
||||
import io.legado.app.ui.widget.prefs.ListPreferenceDialog |
||||
import io.legado.app.ui.widget.prefs.MultiSelectListPreferenceDialog |
||||
|
||||
abstract class BasePreferenceFragment : PreferenceFragmentCompat() { |
||||
|
||||
private val dialogFragmentTag = "androidx.preference.PreferenceFragment.DIALOG" |
||||
|
||||
@SuppressLint("RestrictedApi") |
||||
override fun onDisplayPreferenceDialog(preference: Preference) { |
||||
|
||||
var handled = false |
||||
if (callbackFragment is OnPreferenceDisplayDialogCallback) { |
||||
handled = |
||||
(callbackFragment as OnPreferenceDisplayDialogCallback) |
||||
.onPreferenceDisplayDialog(this, preference) |
||||
} |
||||
if (!handled && activity is OnPreferenceDisplayDialogCallback) { |
||||
handled = (activity as OnPreferenceDisplayDialogCallback) |
||||
.onPreferenceDisplayDialog(this, preference) |
||||
} |
||||
|
||||
if (handled) { |
||||
return |
||||
} |
||||
|
||||
// check if dialog is already showing |
||||
if (parentFragmentManager.findFragmentByTag(dialogFragmentTag) != null) { |
||||
return |
||||
} |
||||
|
||||
val f: DialogFragment = when (preference) { |
||||
is EditTextPreference -> { |
||||
EditTextPreferenceDialog.newInstance(preference.getKey()) |
||||
} |
||||
is ListPreference -> { |
||||
ListPreferenceDialog.newInstance(preference.getKey()) |
||||
} |
||||
is MultiSelectListPreference -> { |
||||
MultiSelectListPreferenceDialog.newInstance(preference.getKey()) |
||||
} |
||||
else -> { |
||||
throw IllegalArgumentException( |
||||
"Cannot display dialog for an unknown Preference type: " |
||||
+ preference.javaClass.simpleName |
||||
+ ". Make sure to implement onPreferenceDisplayDialog() to handle " |
||||
+ "displaying a custom dialog for this Preference." |
||||
) |
||||
} |
||||
} |
||||
f.setTargetFragment(this, 0) |
||||
f.show(parentFragmentManager, dialogFragmentTag) |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,29 @@ |
||||
package io.legado.app.ui.widget.prefs |
||||
|
||||
import android.app.Dialog |
||||
import android.os.Bundle |
||||
import androidx.preference.EditTextPreferenceDialogFragmentCompat |
||||
import androidx.preference.PreferenceDialogFragmentCompat |
||||
import io.legado.app.lib.theme.ATH |
||||
|
||||
class EditTextPreferenceDialog : EditTextPreferenceDialogFragmentCompat() { |
||||
|
||||
companion object { |
||||
|
||||
fun newInstance(key: String): EditTextPreferenceDialog { |
||||
val fragment = EditTextPreferenceDialog() |
||||
val b = Bundle(1) |
||||
b.putString(PreferenceDialogFragmentCompat.ARG_KEY, key) |
||||
fragment.arguments = b |
||||
return fragment |
||||
} |
||||
|
||||
} |
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { |
||||
val dialog = super.onCreateDialog(savedInstanceState) |
||||
dialog.window?.setBackgroundDrawable(ATH.getDialogBackground()) |
||||
return dialog |
||||
} |
||||
|
||||
} |
@ -0,0 +1,29 @@ |
||||
package io.legado.app.ui.widget.prefs |
||||
|
||||
import android.app.Dialog |
||||
import android.os.Bundle |
||||
import androidx.preference.ListPreferenceDialogFragmentCompat |
||||
import androidx.preference.PreferenceDialogFragmentCompat |
||||
import io.legado.app.lib.theme.ATH |
||||
|
||||
class ListPreferenceDialog : ListPreferenceDialogFragmentCompat() { |
||||
|
||||
companion object { |
||||
|
||||
fun newInstance(key: String?): ListPreferenceDialog { |
||||
val fragment = ListPreferenceDialog() |
||||
val b = Bundle(1) |
||||
b.putString(PreferenceDialogFragmentCompat.ARG_KEY, key) |
||||
fragment.arguments = b |
||||
return fragment |
||||
} |
||||
|
||||
} |
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { |
||||
val dialog = super.onCreateDialog(savedInstanceState) |
||||
dialog.window?.setBackgroundDrawable(ATH.getDialogBackground()) |
||||
return dialog |
||||
} |
||||
|
||||
} |
@ -0,0 +1,32 @@ |
||||
package io.legado.app.ui.widget.prefs |
||||
|
||||
import android.app.Dialog |
||||
import android.os.Bundle |
||||
import androidx.preference.MultiSelectListPreferenceDialogFragmentCompat |
||||
import androidx.preference.PreferenceDialogFragmentCompat |
||||
import io.legado.app.lib.theme.ATH |
||||
|
||||
class MultiSelectListPreferenceDialog : MultiSelectListPreferenceDialogFragmentCompat() { |
||||
|
||||
companion object { |
||||
|
||||
fun newInstance(key: String?): MultiSelectListPreferenceDialog { |
||||
val fragment = |
||||
MultiSelectListPreferenceDialog() |
||||
val b = Bundle(1) |
||||
b.putString(PreferenceDialogFragmentCompat.ARG_KEY, key) |
||||
fragment.arguments = b |
||||
return fragment |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { |
||||
val dialog = super.onCreateDialog(savedInstanceState) |
||||
dialog.window?.setBackgroundDrawable(ATH.getDialogBackground()) |
||||
return dialog |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue