Extract MaterialSchemesImpl and ColorUtil to commons lib

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey 2022-08-26 17:17:57 +02:00 committed by Andy Scherzinger
parent d18fc7c9f8
commit 009634da53
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
5 changed files with 5 additions and 132 deletions

View file

@ -1,36 +0,0 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.ui.theme
import com.nextcloud.android.common.ui.theme.MaterialSchemes
import com.nextcloud.android.common.ui.theme.ServerTheme
import scheme.Scheme
class MaterialSchemesImpl(serverTheme: ServerTheme) :
MaterialSchemes {
override val lightScheme: Scheme
override val darkScheme: Scheme
init {
lightScheme = Scheme.light(serverTheme.primaryColor)
darkScheme = Scheme.dark(serverTheme.primaryColor)
}
}

View file

@ -23,11 +23,11 @@
package com.nextcloud.talk.ui.theme
import com.nextcloud.android.common.ui.color.ColorUtil
import com.nextcloud.android.common.ui.theme.MaterialSchemes
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.json.capabilities.Capabilities
import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
import com.nextcloud.talk.utils.ui.ColorUtil
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject
@ -57,7 +57,8 @@ internal class MaterialSchemesProviderImpl @Inject constructor(
}
override fun getMaterialSchemesForCapabilities(capabilities: Capabilities?): MaterialSchemes {
return MaterialSchemesImpl(ServerThemeImpl(capabilities?.themingCapability, colorUtil))
val serverTheme = ServerThemeImpl(capabilities?.themingCapability, colorUtil)
return MaterialSchemes.fromServerTheme(serverTheme)
}
companion object {

View file

@ -26,7 +26,7 @@ package com.nextcloud.talk.ui.theme
import com.nextcloud.android.common.ui.theme.ServerTheme
import com.nextcloud.talk.R
import com.nextcloud.talk.models.json.capabilities.ThemingCapability
import com.nextcloud.talk.utils.ui.ColorUtil
import com.nextcloud.android.common.ui.color.ColorUtil
internal class ServerThemeImpl(themingCapability: ThemingCapability?, colorUtil: ColorUtil) :
ServerTheme {

View file

@ -74,7 +74,7 @@ import com.nextcloud.android.common.ui.theme.MaterialSchemes
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.android.common.ui.color.ColorUtil
import com.nextcloud.talk.utils.ui.PlatformThemeUtil.isDarkMode
import com.vanniktech.emoji.EmojiTextView
import com.yarolegovich.mp.MaterialPreferenceCategory

View file

@ -1,92 +0,0 @@
/*
* Nextcloud Talk application
*
* @author Álvaro Brey
* Copyright (C) 2022 Álvaro Brey
* Copyright (C) 2022 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.utils.ui
import android.content.Context
import android.graphics.Color
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import androidx.core.graphics.ColorUtils
import com.nextcloud.talk.R
import javax.inject.Inject
import kotlin.math.roundToInt
class ColorUtil @Inject constructor(private val context: Context) {
@ColorInt
fun getNullSafeColor(color: String?, @ColorInt fallbackColor: Int): Int {
return color.parseColorOrFallback { fallbackColor }
}
@ColorInt
fun getNullSafeColorWithFallbackRes(color: String?, @ColorRes fallbackColorRes: Int): Int {
return color.parseColorOrFallback { ContextCompat.getColor(context, fallbackColorRes) }
}
@ColorInt
fun getTextColor(colorText: String?, @ColorInt backgroundColor: Int): Int {
return colorText.parseColorOrFallback { getForegroundColorForBackgroundColor(backgroundColor) }
}
@ColorInt
fun getForegroundColorForBackgroundColor(@ColorInt color: Int): Int {
val hsl = FloatArray(HSL_SIZE)
ColorUtils.RGBToHSL(Color.red(color), Color.green(color), Color.blue(color), hsl)
return if (hsl[INDEX_LIGHTNESS] < LIGHTNESS_DARK_THRESHOLD) {
Color.WHITE
} else {
ContextCompat.getColor(context, R.color.grey_900)
}
}
fun setLightness(@ColorInt color: Int, lightness: Float): Int {
require(lightness in 0.0..1.0) { "Lightness must be between 0 and 1" }
val hsl = FloatArray(HSL_SIZE)
ColorUtils.RGBToHSL(Color.red(color), Color.green(color), Color.blue(color), hsl)
hsl[INDEX_LIGHTNESS] = lightness
return ColorUtils.HSLToColor(hsl)
}
@ColorInt
private fun String?.parseColorOrFallback(fallback: () -> Int): Int {
return this?.let { Color.parseColor(this) } ?: fallback()
}
@ColorInt
fun adjustOpacity(color: Int, opacity: Float): Int {
return Color.argb(
(Color.alpha(color) * opacity).roundToInt(),
Color.red(color),
Color.green(color),
Color.blue(color)
)
}
companion object {
private const val HSL_SIZE: Int = 3
private const val INDEX_LIGHTNESS: Int = 2
private const val LIGHTNESS_DARK_THRESHOLD: Float = 0.6f
}
}