make theme ship schemes and utilize them in themeing util

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2022-07-29 23:30:47 +02:00
parent 58dc6a9aae
commit 0f997b433d
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B
3 changed files with 83 additions and 3 deletions

View File

@ -22,6 +22,7 @@
package com.nextcloud.talk.ui.theme
import androidx.annotation.ColorInt
import scheme.Scheme
interface ServerTheme {
@get:ColorInt
@ -50,4 +51,14 @@ interface ServerTheme {
*/
@get:ColorInt
val colorText: Int
/**
* Schema for light theme
*/
val lightScheme: Scheme
/**
* Schema for light theme
*/
val darkScheme: Scheme
}

View File

@ -26,6 +26,7 @@ package com.nextcloud.talk.ui.theme
import com.nextcloud.talk.R
import com.nextcloud.talk.models.json.capabilities.ThemingCapability
import com.nextcloud.talk.utils.ui.ColorUtil
import scheme.Scheme
internal class ServerThemeImpl(themingCapability: ThemingCapability?, colorUtil: ColorUtil) :
ServerTheme {
@ -35,6 +36,8 @@ internal class ServerThemeImpl(themingCapability: ThemingCapability?, colorUtil:
override val colorElementBright: Int
override val colorElementDark: Int
override val colorText: Int
override val lightScheme: Scheme
override val darkScheme: Scheme
init {
primaryColor = colorUtil.getNullSafeColorWithFallbackRes(themingCapability?.color, R.color.colorPrimary)
@ -44,5 +47,8 @@ internal class ServerThemeImpl(themingCapability: ThemingCapability?, colorUtil:
colorElementDark = colorUtil.getNullSafeColor(themingCapability?.colorElementDark, primaryColor)
colorText = colorUtil.getTextColor(themingCapability?.colorText, primaryColor)
lightScheme = Scheme.light(primaryColor)
darkScheme = Scheme.dark(primaryColor)
}
}

View File

@ -21,6 +21,7 @@
package com.nextcloud.talk.ui.theme
import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
@ -30,17 +31,21 @@ import android.view.View
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.RadioButton
import android.widget.SeekBar
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.SearchView.SearchAutoComplete
import androidx.appcompat.widget.SwitchCompat
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.children
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.button.MaterialButton
import com.google.android.material.card.MaterialCardView
import com.google.android.material.chip.Chip
@ -49,16 +54,26 @@ import com.google.android.material.progressindicator.LinearProgressIndicator
import com.google.android.material.tabs.TabLayout
import com.google.android.material.textfield.TextInputLayout
import com.nextcloud.talk.R
import com.nextcloud.talk.utils.DisplayUtils
import com.nextcloud.talk.utils.DrawableUtils
import com.nextcloud.talk.utils.ui.ColorUtil
import com.nextcloud.talk.utils.ui.PlatformThemeUtil.isDarkMode
import com.yarolegovich.mp.MaterialPreferenceCategory
import com.yarolegovich.mp.MaterialSwitchPreference
import scheme.Scheme
import javax.inject.Inject
@Suppress("TooManyFunctions")
class ViewThemeUtils @Inject constructor(private val theme: ServerTheme, private val colorUtil: ColorUtil) {
/**
* Scheme for painting elements
*/
fun getScheme(context: Context): Scheme = when {
isDarkMode(context) -> theme.darkScheme
else -> theme.lightScheme
}
/**
* Color for painting elements
*/
@ -71,10 +86,58 @@ class ViewThemeUtils @Inject constructor(private val theme: ServerTheme, private
block(getElementColor(view.context))
}
private fun withScheme(view: View, block: (Scheme) -> Unit) {
block(getScheme(view.context))
}
fun themeToolbar(toolbar: MaterialToolbar) {
withScheme(toolbar) { scheme ->
toolbar.setBackgroundColor(scheme.surface)
toolbar.setNavigationIconTint(scheme.onSurface)
toolbar.setTitleTextColor(scheme.onSurface)
}
}
fun themeSearchView(searchView: SearchView) {
withScheme(searchView) { scheme ->
// hacky as no default way is provided
val editText = searchView.findViewById<SearchAutoComplete>(R.id.search_src_text)
val searchPlate = searchView.findViewById<LinearLayout>(R.id.search_plate)
editText.textSize = 16f
editText.setHintTextColor(scheme.onSurfaceVariant)
editText.setTextColor(scheme.onSurface)
editText.setBackgroundColor(scheme.surface)
searchPlate.setBackgroundColor(scheme.surface)
}
}
fun themeStatusBar(activity: Activity, view: View) {
withScheme(view) { scheme ->
DisplayUtils.applyColorToStatusBar(activity, scheme.surface)
}
}
fun resetStatusBar(activity: Activity, view: View) {
DisplayUtils.applyColorToStatusBar(
activity,
ResourcesCompat.getColor(
activity.resources,
R.color.bg_default,
activity.theme
)
)
}
fun themeFAB(fab: FloatingActionButton) {
withElementColor(fab) { color ->
fab.backgroundTintList = ColorStateList.valueOf(color)
fab.imageTintList = ColorStateList.valueOf(theme.colorText)
withScheme(fab) { scheme ->
fab.backgroundTintList = ColorStateList.valueOf(scheme.primaryContainer)
fab.imageTintList = ColorStateList.valueOf(scheme.onPrimaryContainer)
}
}
fun themeCardView(cardView: MaterialCardView) {
withScheme(cardView) { scheme ->
cardView.backgroundTintList = ColorStateList.valueOf(scheme.surface)
}
}