Skip to content

Commit

Permalink
SchedulingFragment: Implement Material3 based ListPreference
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Gupta <[email protected]>
  • Loading branch information
theimpulson committed Jun 24, 2024
1 parent a160ff7 commit afcffe1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
7 changes: 1 addition & 6 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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()
}
}

0 comments on commit afcffe1

Please sign in to comment.