style: add basic black theme

Finally add black theme support to Auxio. This is abit of a janky
implementation since I had to add an extra set of accents, but it
shouldn't be as big of a problem after the styles refactoring.
Support for black android components is not implemented yet, but
will be eventually.
This commit is contained in:
OxygenCobalt
2021-06-11 09:13:20 -06:00
parent dc2157904a
commit 68bdd0d929
12 changed files with 232 additions and 25 deletions
@@ -15,6 +15,7 @@ import org.oxycblt.auxio.playback.system.PlaybackService
import org.oxycblt.auxio.settings.SettingsManager
import org.oxycblt.auxio.ui.Accent
import org.oxycblt.auxio.ui.isEdgeOn
import org.oxycblt.auxio.ui.isNight
/**
* The single [AppCompatActivity] for Auxio.
@@ -25,11 +26,7 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val settingsManager = SettingsManager.getInstance()
val newAccent = Accent.set(settingsManager.accent)
AppCompatDelegate.setDefaultNightMode(settingsManager.theme)
setTheme(newAccent.theme)
setupTheme()
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(
this, R.layout.activity_main
@@ -52,6 +49,22 @@ class MainActivity : AppCompatActivity() {
onNewIntent(intent)
}
private fun setupTheme() {
// Update the current accent and theme
val settingsManager = SettingsManager.getInstance()
AppCompatDelegate.setDefaultNightMode(settingsManager.theme)
val newAccent = Accent.set(settingsManager.accent)
// The black theme has a completely separate set of styles since style attributes cannot
// be modified at runtime.
if (isNight() && settingsManager.useBlackTheme) {
setTheme(newAccent.blackTheme)
} else {
setTheme(newAccent.theme)
}
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
@@ -18,6 +18,7 @@ import org.oxycblt.auxio.settings.blacklist.BlacklistDialog
import org.oxycblt.auxio.settings.ui.IntListPrefDialog
import org.oxycblt.auxio.settings.ui.IntListPreference
import org.oxycblt.auxio.ui.Accent
import org.oxycblt.auxio.ui.isNight
import org.oxycblt.auxio.ui.showToast
/**
@@ -82,6 +83,16 @@ class SettingsListFragment : PreferenceFragmentCompat() {
}
}
SettingsManager.KEY_BLACK_THEME -> {
onPreferenceClickListener = Preference.OnPreferenceClickListener {
if (requireContext().isNight()) {
requireActivity().recreate()
}
true
}
}
SettingsManager.KEY_ACCENT -> {
onPreferenceClickListener = Preference.OnPreferenceClickListener {
AccentDialog().show(childFragmentManager, AccentDialog.TAG)
@@ -32,6 +32,10 @@ class SettingsManager private constructor(context: Context) :
val theme: Int
get() = handleThemeCompat(sharedPrefs)
/** Whether the dark theme should be black or not */
val useBlackTheme: Boolean
get() = sharedPrefs.getBoolean(KEY_BLACK_THEME, false)
/** The current accent. */
var accent: Accent
get() = handleAccentCompat(sharedPrefs)
@@ -195,6 +199,7 @@ class SettingsManager private constructor(context: Context) :
companion object {
const val KEY_THEME = "KEY_THEME2"
const val KEY_BLACK_THEME = "KEY_BLACK_THEME"
const val KEY_ACCENT = "KEY_ACCENT2"
const val KEY_LIB_DISPLAY_MODE = "KEY_LIB_MODE"
@@ -15,34 +15,85 @@ import org.oxycblt.auxio.R
* TODO: Add custom accents
*/
val ACCENTS = arrayOf(
Accent(R.color.red, R.style.Theme_Red, R.string.color_label_red),
Accent(R.color.pink, R.style.Theme_Pink, R.string.color_label_pink),
Accent(R.color.purple, R.style.Theme_Purple, R.string.color_label_purple),
Accent(R.color.deep_purple, R.style.Theme_DeepPurple, R.string.color_label_deep_purple),
Accent(R.color.indigo, R.style.Theme_Indigo, R.string.color_label_indigo),
Accent(R.color.blue, R.style.Theme_Blue, R.string.color_label_blue),
Accent(R.color.light_blue, R.style.Theme_LightBlue, R.string.color_label_light_blue),
Accent(R.color.cyan, R.style.Theme_Cyan, R.string.color_label_cyan),
Accent(R.color.teal, R.style.Theme_Teal, R.string.color_label_teal),
Accent(R.color.green, R.style.Theme_Green, R.string.color_label_green),
Accent(R.color.light_green, R.style.Theme_LightGreen, R.string.color_label_light_green),
Accent(R.color.lime, R.style.Theme_Lime, R.string.color_label_lime),
Accent(R.color.yellow, R.style.Theme_Yellow, R.string.color_label_yellow),
Accent(R.color.orange, R.style.Theme_Orange, R.string.color_label_orange),
Accent(R.color.deep_orange, R.style.Theme_DeepOrange, R.string.color_label_deep_orange),
Accent(R.color.brown, R.style.Theme_Brown, R.string.color_label_brown),
Accent(R.color.grey, R.style.Theme_Gray, R.string.color_label_grey),
Accent(R.color.blue_grey, R.style.Theme_BlueGrey, R.string.color_label_blue_grey),
Accent(R.color.red, R.style.Theme_Red, R.style.Theme_Red_Black, R.string.color_label_red),
Accent(R.color.pink, R.style.Theme_Pink, R.style.Theme_Pink_Black, R.string.color_label_pink),
Accent(
R.color.purple,
R.style.Theme_Purple,
R.style.Theme_Purple_Black,
R.string.color_label_purple
),
Accent(
R.color.deep_purple,
R.style.Theme_DeepPurple,
R.style.Theme_DeepPurple_Black,
R.string.color_label_deep_purple
),
Accent(
R.color.indigo,
R.style.Theme_Indigo,
R.style.Theme_Indigo_Black,
R.string.color_label_indigo
),
Accent(R.color.blue, R.style.Theme_Blue, R.style.Theme_Blue_Black, R.string.color_label_blue),
Accent(
R.color.light_blue,
R.style.Theme_LightBlue,
R.style.Theme_LightBlue_Black,
R.string.color_label_light_blue
),
Accent(R.color.cyan, R.style.Theme_Cyan, R.style.Theme_Cyan_Black, R.string.color_label_cyan),
Accent(R.color.teal, R.style.Theme_Teal, R.style.Theme_Teal_Black, R.string.color_label_teal),
Accent(R.color.green, R.style.Theme_Green, R.style.Theme_Green_Black, R.string.color_label_green),
Accent(
R.color.light_green,
R.style.Theme_LightGreen,
R.style.Theme_LightGreen_Black,
R.string.color_label_light_green
),
Accent(R.color.lime, R.style.Theme_Lime, R.style.Theme_Lime_Black, R.string.color_label_lime),
Accent(
R.color.yellow,
R.style.Theme_Yellow,
R.style.Theme_Yellow_Black,
R.string.color_label_yellow
),
Accent(
R.color.orange,
R.style.Theme_Orange,
R.style.Theme_Orange_Black,
R.string.color_label_orange
),
Accent(
R.color.deep_orange,
R.style.Theme_DeepOrange,
R.style.Theme_DeepOrange_Black,
R.string.color_label_deep_orange
),
Accent(R.color.brown, R.style.Theme_Brown, R.style.Theme_Brown_Black, R.string.color_label_brown),
Accent(R.color.grey, R.style.Theme_Grey, R.style.Theme_Grey_Black, R.string.color_label_grey),
Accent(
R.color.blue_grey,
R.style.Theme_BlueGrey,
R.style.Theme_BlueGrey_Black,
R.string.color_label_blue_grey
),
)
/**
* The data object for an accent.
* @property color The color resource for this accent
* @property theme The theme resource for this accent
* @property blackTheme The black theme resource for this accent
* @property name The name of this accent
* @author OxygenCobalt
*/
data class Accent(@ColorRes val color: Int, @StyleRes val theme: Int, @StringRes val name: Int) {
data class Accent(
@ColorRes val color: Int,
@StyleRes val theme: Int,
@StyleRes val blackTheme: Int,
@StringRes val name: Int
) {
/**
* Get a [ColorStateList] of the accent
*/
@@ -78,6 +78,15 @@ fun <T : Any> Context.getSystemServiceSafe(serviceClass: KClass<T>): T {
}
}
/**
* Returns whether the current UI is in night mode or not. This will work if the theme is
* automatic as well.
*/
fun Context.isNight(): Boolean {
return resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK ==
Configuration.UI_MODE_NIGHT_YES
}
/**
* Resolve a color.
* @param context [Context] required