Fix option selection persistence

main
h4h13 2019-09-29 23:54:47 +05:30
parent 35bf805202
commit 0d62d35ae3
5 changed files with 28 additions and 16 deletions

View File

@ -23,8 +23,9 @@ import androidx.fragment.app.DialogFragment
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceManager
import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.appthemehelper.common.prefs.supportv7.ATEPreferenceFragmentCompat
import code.name.monkey.appthemehelper.util.ATHUtil
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.preferences.*
import code.name.monkey.retromusic.util.NavigationUtil
@ -60,7 +61,7 @@ abstract class AbsSettingsFragment : ATEPreferenceFragmentCompat() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setDivider(ColorDrawable(Color.TRANSPARENT))
listView.setBackgroundColor(ThemeStore.primaryColor(requireContext()))
listView.setBackgroundColor(ATHUtil.resolveColor(requireContext(), R.attr.colorPrimary))
listView.overScrollMode = View.OVER_SCROLL_NEVER
listView.setPadding(0, 0, 0, 0)
listView.setPaddingRelative(0, 0, 0, 0)
@ -73,7 +74,6 @@ abstract class AbsSettingsFragment : ATEPreferenceFragmentCompat() {
is NowPlayingScreenPreference -> NowPlayingScreenPreferenceDialog.newInstance(preference.key)
is AlbumCoverStylePreference -> AlbumCoverStylePreferenceDialog.newInstance(preference.key)
is MaterialListPreference -> {
preference.entries
MaterialListPreferenceDialog.newInstance(preference)
}
is BlacklistPreference -> BlacklistPreferenceDialog.newInstance()

View File

@ -76,18 +76,25 @@ class MaterialListPreferenceDialog : PreferenceDialogFragmentCompat() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val materialListPreference = preference as MaterialListPreference
position = materialListPreference.findIndexOfValue(materialListPreference.value)
val entries = arguments?.getStringArrayList(EXTRA_ENTRIES)
val entriesValues = arguments?.getStringArrayList(EXTRA_ENTRIES_VALUES)
val position: Int = arguments?.getInt(EXTRA_POSITION) ?: 0
materialDialog = MaterialDialog(requireContext(), BottomSheet(LayoutMode.WRAP_CONTENT))
.title(text = materialListPreference.title.toString())
.positiveButton(R.string.set)
.cornerRadius(PreferenceUtil.getInstance(requireContext()).dialogCorner)
.listItemsSingleChoice(items = entries, initialSelection = position, waitForPositiveButton = true) { _, index, _ ->
materialListPreference.callChangeListener(entriesValues!![index])
materialListPreference.setCustomValue(entriesValues[index])
materialListPreference.summary = entries!![index]
entriesValues?.let {
materialListPreference.callChangeListener(it[index])
materialListPreference.setCustomValue(it[index])
}
entries?.let {
materialListPreference.summary = it[index]
val value = materialListPreference.entryValues[index].toString()
if (materialListPreference.callChangeListener(value)) {
materialListPreference.value = value
}
}
dismiss()
}
return materialDialog
@ -107,18 +114,22 @@ class MaterialListPreferenceDialog : PreferenceDialogFragmentCompat() {
}
companion object {
var position = 0
private const val EXTRA_KEY = "key"
private const val EXTRA_TITLE = "title"
private const val EXTRA_POSITION = "position"
private const val EXTRA_ENTRIES = "extra_entries"
private const val EXTRA_ENTRIES_VALUES = "extra_entries_values"
fun newInstance(listPreference: ListPreference): MaterialListPreferenceDialog {
val entries = listPreference.entries.toList() as ArrayList<String>
val entriesValues = listPreference.entryValues.toList() as ArrayList<String>
println("List value: ${listPreference.value}")
val position = listPreference.findIndexOfValue(listPreference.value)
val args = Bundle()
args.putString(EXTRA_KEY, listPreference.key)
args.putString(ARG_KEY, listPreference.key)
args.putString(EXTRA_TITLE, listPreference.title.toString())
args.putInt(EXTRA_POSITION, position)
args.putStringArrayList(EXTRA_ENTRIES, entries)
args.putStringArrayList(EXTRA_ENTRIES_VALUES, entriesValues)
val fragment = MaterialListPreferenceDialog()

View File

@ -792,7 +792,7 @@ public final class PreferenceUtil {
}
@LayoutRes
public int getHomeGridStyle(Context context) {
public int getHomeGridStyle(@NonNull Context context) {
int pos = Integer.parseInt(mPreferences.getString(HOME_ARTIST_GRID_STYLE, "0"));
TypedArray typedArray = context.getResources().obtainTypedArray(R.array.pref_home_grid_style_layout);
int layoutRes = typedArray.getResourceId(pos, -1);

View File

@ -34,5 +34,6 @@ dependencies {
implementation 'com.afollestad.material-dialogs:core:3.1.1'
implementation 'com.afollestad.material-dialogs:input:3.1.1'
implementation 'com.afollestad.material-dialogs:color:3.1.1'
implementation 'com.afollestad.material-dialogs:bottomsheets:3.1.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}

View File

@ -27,17 +27,17 @@ import code.name.monkey.appthemehelper.common.prefs.supportv7.dialogs.ATEPrefere
*/
abstract class ATEPreferenceFragmentCompat : PreferenceFragmentCompat() {
override fun onDisplayPreferenceDialog(preference: Preference) {
if (callbackFragment is PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback) {
(callbackFragment as PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback).onPreferenceDisplayDialog(this, preference)
if (callbackFragment is OnPreferenceDisplayDialogCallback) {
(callbackFragment as OnPreferenceDisplayDialogCallback).onPreferenceDisplayDialog(this, preference)
return
}
if (activity is PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback) {
(activity as PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback).onPreferenceDisplayDialog(this, preference)
if (activity is OnPreferenceDisplayDialogCallback) {
(activity as OnPreferenceDisplayDialogCallback).onPreferenceDisplayDialog(this, preference)
return
}
if (fragmentManager!!.findFragmentByTag("android.support.v7.preference.PreferenceFragment.DIALOG") == null) {
if (fragmentManager?.findFragmentByTag("android.support.v7.preference.PreferenceFragment.DIALOG") == null) {
val dialogFragment = onCreatePreferenceDialog(preference)
if (dialogFragment != null) {