Move accent dialog to object

Move the dialog created in showAccentDialog to a dedicated object.
This commit is contained in:
OxygenCobalt 2021-03-09 08:01:49 -07:00
parent f5f67a0b76
commit 5d72bfa09b
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
6 changed files with 74 additions and 58 deletions

View file

@ -23,7 +23,7 @@ class SettingsFragment : Fragment() {
binding.settingsToolbar.setOnMenuItemClickListener {
if (it.itemId == R.id.action_open_about) {
AboutDialog().show(childFragmentManager, ABOUT_DIALOG_TAG)
AboutDialog().show(childFragmentManager, TAG_ABOUT_DIALOG)
true
} else {
false
@ -34,6 +34,6 @@ class SettingsFragment : Fragment() {
}
companion object {
private const val ABOUT_DIALOG_TAG = "TAG_ABOUT_DIALOG"
private const val TAG_ABOUT_DIALOG = "TAG_ABOUT_DIALOG"
}
}

View file

@ -8,18 +8,12 @@ import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.children
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
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.logD
import org.oxycblt.auxio.playback.PlaybackViewModel
import org.oxycblt.auxio.recycler.DisplayMode
import org.oxycblt.auxio.settings.ui.AccentAdapter
import org.oxycblt.auxio.ui.ACCENTS
import org.oxycblt.auxio.settings.ui.AccentDialog
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 {
showAccentDialog()
AccentDialog().show(childFragmentManager, TAG_ACCENT_DIALOG)
true
}
@ -89,7 +83,6 @@ class SettingsListFragment : PreferenceFragmentCompat() {
SettingsManager.Keys.KEY_EDGE_TO_EDGE -> {
onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, _ ->
requireActivity().recreate()
true
}
}
@ -99,7 +92,6 @@ class SettingsListFragment : PreferenceFragmentCompat() {
onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, value ->
setIcon(DisplayMode.valueOfOrFallback(value as String).iconRes)
true
}
}
@ -134,7 +126,6 @@ class SettingsListFragment : PreferenceFragmentCompat() {
SettingsManager.Keys.KEY_DEBUG_SAVE -> {
onPreferenceClickListener = Preference.OnPreferenceClickListener {
playbackModel.savePlaybackState(requireContext())
true
}
}
@ -142,48 +133,7 @@ class SettingsListFragment : PreferenceFragmentCompat() {
}
}
/**
* Show the accent dialog to the user
*/
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()
}
companion object {
const val TAG_ACCENT_DIALOG = "ACCENT_DIALOG"
}
}

View file

@ -32,6 +32,7 @@ class SettingsManager private constructor(context: Context) :
/** The current accent. */
var accent: Accent
get() {
@Suppress("DEPRECATION")
if (sharedPrefs.contains(Keys.KEY_ACCENT_OLD)) {
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_DEBUG_SAVE = "KEY_SAVE_STATE"
@Deprecated("Use the new KEY_ACCENT instead.")
const val KEY_ACCENT_OLD = "KEY_ACCENT"
}

View file

@ -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
}
}

View file

@ -14,6 +14,7 @@
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:gravity="center"
android:padding="@dimen/padding_small"
android:orientation="vertical">
<ProgressBar

View file

@ -17,7 +17,7 @@
android:icon="@drawable/ic_accent"
android:title="@string/setting_accent"
app:allowDividerBelow="false"
app:key="KEY_ACCENT"
app:key="KEY_ACCENT2"
app:summary="@string/setting_accent_unknown" />
</PreferenceCategory>