Add theming system

Standardize theming across the app into ThemeUtils.
This commit is contained in:
OxygenCobalt
2020-09-01 07:37:49 -06:00
parent d5e6b813a9
commit 8f850f2288
13 changed files with 171 additions and 32 deletions
@@ -1,16 +1,27 @@
package org.oxycblt.auxio
import android.content.Context
import android.os.Bundle
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import org.oxycblt.auxio.theme.accent
class MainActivity : AppCompatActivity() {
override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? {
setTheme(accent.second)
return super.onCreateView(name, context, attrs)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
Log.d(this::class.simpleName, "Activity Created.")
}
@@ -15,8 +15,9 @@ import com.google.android.material.tabs.TabLayoutMediator
import org.oxycblt.auxio.databinding.FragmentMainBinding
import org.oxycblt.auxio.library.LibraryFragment
import org.oxycblt.auxio.songs.SongsFragment
import org.oxycblt.auxio.theme.getAccentTransparency
import org.oxycblt.auxio.theme.getDeselectedTransparency
import org.oxycblt.auxio.theme.accent
import org.oxycblt.auxio.theme.getInactiveAlpha
import org.oxycblt.auxio.theme.getTransparentAccent
import org.oxycblt.auxio.theme.toColor
class MainFragment : Fragment() {
@@ -27,12 +28,14 @@ class MainFragment : Fragment() {
private val songsFragment: SongsFragment by lazy { SongsFragment() }
private val colorSelected: Int by lazy {
R.color.blue.toColor(requireContext())
accent.first.toColor(requireContext())
}
private val colorDeselected: Int by lazy {
getAccentTransparency(
requireContext(), R.color.blue, getDeselectedTransparency(R.color.blue)
getTransparentAccent(
requireContext(),
accent.first,
getInactiveAlpha(accent.first)
)
}
@@ -83,10 +86,6 @@ class MainFragment : Fragment() {
}
)
binding.tabs.getTabAt(
binding.viewPager.offscreenPageLimit
)
Log.d(this::class.simpleName, "Fragment Created.")
return binding.root
@@ -4,8 +4,8 @@ import android.graphics.drawable.ColorDrawable
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView
import org.oxycblt.auxio.R
import org.oxycblt.auxio.theme.getAccentTransparency
import org.oxycblt.auxio.theme.getDayNightTransparency
import org.oxycblt.auxio.theme.getDayNightAlpha
import org.oxycblt.auxio.theme.getTransparentAccent
// Apply a custom vertical divider
fun RecyclerView.applyDivider() {
@@ -16,8 +16,8 @@ fun RecyclerView.applyDivider() {
div.setDrawable(
ColorDrawable(
getAccentTransparency(
context, R.color.divider_color, getDayNightTransparency()
getTransparentAccent(
context, R.color.divider_color, getDayNightAlpha()
)
)
)
@@ -6,18 +6,43 @@ import androidx.core.content.ContextCompat
import androidx.core.graphics.ColorUtils
import org.oxycblt.auxio.R
fun getDayNightTransparency(): Int {
// Pairs of the base accent and its theme
private val ACCENTS = listOf(
Pair(R.color.red, R.style.Theme_Red),
Pair(R.color.pink, R.style.Theme_Pink),
Pair(R.color.purple, R.style.Theme_Purple),
Pair(R.color.deep_purple, R.style.Theme_DeepPurple),
Pair(R.color.indigo, R.style.Theme_Indigo),
Pair(R.color.blue, R.style.Theme_Blue),
Pair(R.color.light_blue, R.style.Theme_Blue),
Pair(R.color.cyan, R.style.Theme_Cyan),
Pair(R.color.teal, R.style.Theme_Teal),
Pair(R.color.green, R.style.Theme_Green),
Pair(R.color.light_green, R.style.Theme_LightGreen),
Pair(R.color.lime, R.style.Theme_Lime),
Pair(R.color.yellow, R.style.Theme_Yellow),
Pair(R.color.amber, R.style.Theme_Amber),
Pair(R.color.orange, R.style.Theme_Orange),
Pair(R.color.deep_orange, R.style.Theme_DeepOrange),
Pair(R.color.brown, R.style.Theme_Brown),
Pair(R.color.grey, R.style.Theme_Gray),
Pair(R.color.blue_grey, R.style.Theme_BlueGrey)
)
val accent = ACCENTS[5]
fun getDayNightAlpha(): Int {
val isDark = AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES
// Depending on the theme use a different opacity for the divider
return if (isDark) 45 else 85
}
fun getDeselectedTransparency(color: Int): Int {
fun getInactiveAlpha(color: Int): Int {
return if (color == R.color.yellow) 100 else 150
}
fun getAccentTransparency(context: Context, color: Int, alpha: Int): Int {
fun getTransparentAccent(context: Context, color: Int, alpha: Int): Int {
return ColorUtils.setAlphaComponent(
ContextCompat.getColor(context, color),
alpha
@@ -28,6 +53,7 @@ fun Int.toColor(context: Context): Int {
return try {
ContextCompat.getColor(context, this)
} catch (e: Exception) {
ContextCompat.getColor(context, android.R.color.white)
// Default to the emergency color [Black] if the loading fails.
ContextCompat.getColor(context, android.R.color.black)
}
}