Move accent dialog to object
Move the dialog created in showAccentDialog to a dedicated object.
This commit is contained in:
parent
f5f67a0b76
commit
5d72bfa09b
6 changed files with 74 additions and 58 deletions
|
@ -23,7 +23,7 @@ class SettingsFragment : Fragment() {
|
||||||
|
|
||||||
binding.settingsToolbar.setOnMenuItemClickListener {
|
binding.settingsToolbar.setOnMenuItemClickListener {
|
||||||
if (it.itemId == R.id.action_open_about) {
|
if (it.itemId == R.id.action_open_about) {
|
||||||
AboutDialog().show(childFragmentManager, ABOUT_DIALOG_TAG)
|
AboutDialog().show(childFragmentManager, TAG_ABOUT_DIALOG)
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
|
@ -34,6 +34,6 @@ class SettingsFragment : Fragment() {
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val ABOUT_DIALOG_TAG = "TAG_ABOUT_DIALOG"
|
private const val TAG_ABOUT_DIALOG = "TAG_ABOUT_DIALOG"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,18 +8,12 @@ import androidx.preference.Preference
|
||||||
import androidx.preference.PreferenceCategory
|
import androidx.preference.PreferenceCategory
|
||||||
import androidx.preference.PreferenceFragmentCompat
|
import androidx.preference.PreferenceFragmentCompat
|
||||||
import androidx.preference.children
|
import androidx.preference.children
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
|
||||||
import coil.Coil
|
import coil.Coil
|
||||||
import com.afollestad.materialdialogs.MaterialDialog
|
|
||||||
import com.afollestad.materialdialogs.customview.customView
|
|
||||||
import com.afollestad.materialdialogs.utils.invalidateDividers
|
|
||||||
import org.oxycblt.auxio.R
|
import org.oxycblt.auxio.R
|
||||||
import org.oxycblt.auxio.logD
|
import org.oxycblt.auxio.logD
|
||||||
import org.oxycblt.auxio.playback.PlaybackViewModel
|
import org.oxycblt.auxio.playback.PlaybackViewModel
|
||||||
import org.oxycblt.auxio.recycler.DisplayMode
|
import org.oxycblt.auxio.recycler.DisplayMode
|
||||||
import org.oxycblt.auxio.settings.ui.AccentAdapter
|
import org.oxycblt.auxio.settings.ui.AccentDialog
|
||||||
import org.oxycblt.auxio.ui.ACCENTS
|
|
||||||
import org.oxycblt.auxio.ui.Accent
|
import org.oxycblt.auxio.ui.Accent
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -77,9 +71,9 @@ class SettingsListFragment : PreferenceFragmentCompat() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SettingsManager.Keys.KEY_ACCENT_OLD -> {
|
SettingsManager.Keys.KEY_ACCENT -> {
|
||||||
onPreferenceClickListener = Preference.OnPreferenceClickListener {
|
onPreferenceClickListener = Preference.OnPreferenceClickListener {
|
||||||
showAccentDialog()
|
AccentDialog().show(childFragmentManager, TAG_ACCENT_DIALOG)
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,7 +83,6 @@ class SettingsListFragment : PreferenceFragmentCompat() {
|
||||||
SettingsManager.Keys.KEY_EDGE_TO_EDGE -> {
|
SettingsManager.Keys.KEY_EDGE_TO_EDGE -> {
|
||||||
onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, _ ->
|
onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, _ ->
|
||||||
requireActivity().recreate()
|
requireActivity().recreate()
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,7 +92,6 @@ class SettingsListFragment : PreferenceFragmentCompat() {
|
||||||
|
|
||||||
onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, value ->
|
onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, value ->
|
||||||
setIcon(DisplayMode.valueOfOrFallback(value as String).iconRes)
|
setIcon(DisplayMode.valueOfOrFallback(value as String).iconRes)
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,7 +126,6 @@ class SettingsListFragment : PreferenceFragmentCompat() {
|
||||||
SettingsManager.Keys.KEY_DEBUG_SAVE -> {
|
SettingsManager.Keys.KEY_DEBUG_SAVE -> {
|
||||||
onPreferenceClickListener = Preference.OnPreferenceClickListener {
|
onPreferenceClickListener = Preference.OnPreferenceClickListener {
|
||||||
playbackModel.savePlaybackState(requireContext())
|
playbackModel.savePlaybackState(requireContext())
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -142,48 +133,7 @@ class SettingsListFragment : PreferenceFragmentCompat() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
companion object {
|
||||||
* Show the accent dialog to the user
|
const val TAG_ACCENT_DIALOG = "ACCENT_DIALOG"
|
||||||
*/
|
|
||||||
private fun showAccentDialog() {
|
|
||||||
MaterialDialog(requireActivity()).show {
|
|
||||||
title(R.string.setting_accent)
|
|
||||||
|
|
||||||
// Roll my own RecyclerView since [To no surprise whatsoever] Material Dialogs
|
|
||||||
// has a bug where ugly dividers will show with the RecyclerView even if you disable them.
|
|
||||||
// This is why I hate using third party libraries.
|
|
||||||
val recycler = RecyclerView(requireContext()).apply {
|
|
||||||
adapter = AccentAdapter { accent ->
|
|
||||||
if (accent != Accent.get()) {
|
|
||||||
settingsManager.accent = accent
|
|
||||||
|
|
||||||
requireActivity().recreate()
|
|
||||||
}
|
|
||||||
|
|
||||||
this@show.dismiss()
|
|
||||||
}
|
|
||||||
|
|
||||||
layoutManager = LinearLayoutManager(
|
|
||||||
requireContext(), LinearLayoutManager.HORIZONTAL, false
|
|
||||||
)
|
|
||||||
|
|
||||||
post {
|
|
||||||
// Combine the width of the recyclerview with the width of an item in order
|
|
||||||
// to center the currently selected accent.
|
|
||||||
val childWidth = getChildAt(0).width / 2
|
|
||||||
|
|
||||||
(layoutManager as LinearLayoutManager)
|
|
||||||
.scrollToPositionWithOffset(
|
|
||||||
ACCENTS.indexOf(Accent.get()),
|
|
||||||
(width / 2) - childWidth
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
customView(view = recycler)
|
|
||||||
invalidateDividers(showTop = false, showBottom = false)
|
|
||||||
negativeButton(android.R.string.cancel)
|
|
||||||
show()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,7 @@ class SettingsManager private constructor(context: Context) :
|
||||||
/** The current accent. */
|
/** The current accent. */
|
||||||
var accent: Accent
|
var accent: Accent
|
||||||
get() {
|
get() {
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
if (sharedPrefs.contains(Keys.KEY_ACCENT_OLD)) {
|
if (sharedPrefs.contains(Keys.KEY_ACCENT_OLD)) {
|
||||||
logD("Migrating from old accent to new accent.")
|
logD("Migrating from old accent to new accent.")
|
||||||
|
|
||||||
|
@ -233,6 +234,7 @@ class SettingsManager private constructor(context: Context) :
|
||||||
const val KEY_SEARCH_FILTER_MODE = "KEY_SEARCH"
|
const val KEY_SEARCH_FILTER_MODE = "KEY_SEARCH"
|
||||||
const val KEY_DEBUG_SAVE = "KEY_SAVE_STATE"
|
const val KEY_DEBUG_SAVE = "KEY_SAVE_STATE"
|
||||||
|
|
||||||
|
@Deprecated("Use the new KEY_ACCENT instead.")
|
||||||
const val KEY_ACCENT_OLD = "KEY_ACCENT"
|
const val KEY_ACCENT_OLD = "KEY_ACCENT"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
package org.oxycblt.auxio.settings.ui
|
||||||
|
|
||||||
|
import android.app.Dialog
|
||||||
|
import android.os.Bundle
|
||||||
|
import androidx.fragment.app.DialogFragment
|
||||||
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import com.afollestad.materialdialogs.MaterialDialog
|
||||||
|
import com.afollestad.materialdialogs.customview.customView
|
||||||
|
import com.afollestad.materialdialogs.utils.invalidateDividers
|
||||||
|
import org.oxycblt.auxio.R
|
||||||
|
import org.oxycblt.auxio.settings.SettingsManager
|
||||||
|
import org.oxycblt.auxio.ui.ACCENTS
|
||||||
|
import org.oxycblt.auxio.ui.Accent
|
||||||
|
|
||||||
|
class AccentDialog : DialogFragment() {
|
||||||
|
private val settingsManager = SettingsManager.getInstance()
|
||||||
|
|
||||||
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||||
|
// Roll my own RecyclerView since [To no surprise whatsoever] Material Dialogs
|
||||||
|
// has a bug where ugly dividers will show with the RecyclerView even if you disable them.
|
||||||
|
// This is why I hate using third party libraries.
|
||||||
|
val recycler = RecyclerView(requireContext()).apply {
|
||||||
|
adapter = AccentAdapter { accent ->
|
||||||
|
if (accent != Accent.get()) {
|
||||||
|
settingsManager.accent = accent
|
||||||
|
|
||||||
|
requireActivity().recreate()
|
||||||
|
}
|
||||||
|
|
||||||
|
dismiss()
|
||||||
|
}
|
||||||
|
|
||||||
|
layoutManager = LinearLayoutManager(
|
||||||
|
requireContext(), LinearLayoutManager.HORIZONTAL, false
|
||||||
|
)
|
||||||
|
|
||||||
|
post {
|
||||||
|
// Combine the width of the recyclerview with the width of an item in order
|
||||||
|
// to center the currently selected accent.
|
||||||
|
val childWidth = getChildAt(0).width / 2
|
||||||
|
|
||||||
|
(layoutManager as LinearLayoutManager)
|
||||||
|
.scrollToPositionWithOffset(
|
||||||
|
ACCENTS.indexOf(Accent.get()),
|
||||||
|
(width / 2) - childWidth
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return MaterialDialog(requireActivity())
|
||||||
|
.title(R.string.setting_accent)
|
||||||
|
.negativeButton(android.R.string.cancel)
|
||||||
|
.customView(view = recycler)
|
||||||
|
.noDividers()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun MaterialDialog.noDividers(): MaterialDialog {
|
||||||
|
invalidateDividers(showTop = false, showBottom = false)
|
||||||
|
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,6 +14,7 @@
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:animateLayoutChanges="true"
|
android:animateLayoutChanges="true"
|
||||||
android:gravity="center"
|
android:gravity="center"
|
||||||
|
android:padding="@dimen/padding_small"
|
||||||
android:orientation="vertical">
|
android:orientation="vertical">
|
||||||
|
|
||||||
<ProgressBar
|
<ProgressBar
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
android:icon="@drawable/ic_accent"
|
android:icon="@drawable/ic_accent"
|
||||||
android:title="@string/setting_accent"
|
android:title="@string/setting_accent"
|
||||||
app:allowDividerBelow="false"
|
app:allowDividerBelow="false"
|
||||||
app:key="KEY_ACCENT"
|
app:key="KEY_ACCENT2"
|
||||||
app:summary="@string/setting_accent_unknown" />
|
app:summary="@string/setting_accent_unknown" />
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue