mirror of
https://github.com/ankidroid/Anki-Android.git
synced 2024-09-20 03:52:15 +02:00
chore: 'DialogFragmentProvider' - make optional
If a call to super is requested, `null` may be returned In this case, `super` would be called This is useful if you would optionally want to inherit from a class (say `EditTextPreference`), but also allow conditionally return a different fragment
This commit is contained in:
parent
6ab15ec3ea
commit
9175d9ec29
@ -29,6 +29,7 @@ import androidx.preference.PreferenceManager
|
||||
import androidx.preference.PreferenceManager.OnPreferenceTreeClickListener
|
||||
import com.ichi2.anki.analytics.UsageAnalytics
|
||||
import com.ichi2.preferences.DialogFragmentProvider
|
||||
import timber.log.Timber
|
||||
import java.lang.NumberFormatException
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.jvm.jvmName
|
||||
@ -91,14 +92,12 @@ abstract class SettingsFragment :
|
||||
// `getTargetFragment()`, which throws if `setTargetFragment()` isn't used before.
|
||||
// While this isn't fixed on upstream, suppress the deprecation warning
|
||||
override fun onDisplayPreferenceDialog(preference: Preference) {
|
||||
if (preference is DialogFragmentProvider) {
|
||||
val dialogFragment = preference.makeDialogFragment()
|
||||
val dialogFragment = (preference as? DialogFragmentProvider)?.makeDialogFragment()
|
||||
?: return super.onDisplayPreferenceDialog(preference)
|
||||
Timber.d("displaying custom preference: ${dialogFragment::class.simpleName}")
|
||||
dialogFragment.arguments = bundleOf("key" to preference.key)
|
||||
dialogFragment.setTargetFragment(this, 0)
|
||||
dialogFragment.show(parentFragmentManager, "androidx.preference.PreferenceFragment.DIALOG")
|
||||
} else {
|
||||
super.onDisplayPreferenceDialog(preference)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
|
@ -35,11 +35,11 @@ import kotlin.contracts.contract
|
||||
interface DialogFragmentProvider {
|
||||
|
||||
/**
|
||||
* @return A DialogFragment to show.
|
||||
* @return A DialogFragment to show or `null` to use the parent fragment
|
||||
* The dialog must have a zero-parameter constructor.
|
||||
* Any arguments set via [Fragment.setArguments] may get overridden.
|
||||
*/
|
||||
fun makeDialogFragment(): DialogFragment
|
||||
fun makeDialogFragment(): DialogFragment?
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user