SchedulingFragment: Implement Material3 based ListPreference

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta 2024-06-24 13:58:30 +05:30
parent d15fe36638
commit 98c6ebab8d
2 changed files with 63 additions and 0 deletions

View file

@ -8,6 +8,8 @@ package com.stevesoltys.seedvault.settings
import android.content.SharedPreferences
import android.os.Bundle
import android.view.View
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
@ -16,6 +18,7 @@ import androidx.work.ExistingPeriodicWorkPolicy.UPDATE
import com.stevesoltys.seedvault.R
import com.stevesoltys.seedvault.permitDiskReads
import com.stevesoltys.seedvault.plugins.StoragePluginManager
import com.stevesoltys.seedvault.settings.preference.M3ListPreference
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.sharedViewModel
@ -42,6 +45,21 @@ class SchedulingFragment : PreferenceFragmentCompat(),
}
}
override fun onDisplayPreferenceDialog(preference: Preference?) {
when (preference) {
is ListPreference -> {
val dialogFragment = M3ListPreference.newInstance(preference.getKey())
dialogFragment.setTargetFragment(this, 0)
dialogFragment.show(
parentFragmentManager,
M3ListPreference.PREFERENCE_DIALOG_FRAGMENT_TAG
)
}
else -> super.onDisplayPreferenceDialog(preference)
}
}
override fun onStart() {
super.onStart()

View file

@ -0,0 +1,45 @@
/*
* SPDX-FileCopyrightText: 2024 The Calyx Institute
* SPDX-License-Identifier: Apache-2.0
*/
package com.stevesoltys.seedvault.settings.preference
import android.app.Dialog
import android.os.Bundle
import androidx.preference.ListPreferenceDialogFragmentCompat
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class M3ListPreference : ListPreferenceDialogFragmentCompat() {
companion object {
const val PREFERENCE_DIALOG_FRAGMENT_TAG = "androidx.preference.PreferenceFragment.DIALOG"
fun newInstance(key: String?): M3ListPreference {
val fragment = M3ListPreference()
val bundle = Bundle(1)
bundle.putString(ARG_KEY, key)
fragment.arguments = bundle
return fragment
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = MaterialAlertDialogBuilder(requireContext())
.setTitle(preference.dialogTitle)
.setIcon(preference.dialogIcon)
.setPositiveButton(preference.positiveButtonText, this)
.setNegativeButton(preference.negativeButtonText, this)
val contentView = onCreateDialogView(requireContext())
if (contentView != null) {
onBindDialogView(contentView)
builder.setView(contentView)
} else {
builder.setMessage(preference.dialogMessage)
}
onPrepareDialogBuilder(builder)
return builder.create()
}
}