VancedManager/app/src/main/java/com/vanced/manager/ui/core/ThemedMaterialButton.kt

37 lines
1.2 KiB
Kotlin
Raw Normal View History

2020-12-14 21:18:58 +00:00
package com.vanced.manager.ui.core
import android.content.Context
import android.util.AttributeSet
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.ColorUtils
import com.google.android.material.button.MaterialButton
import com.vanced.manager.R
2021-01-16 17:00:38 +00:00
import com.vanced.manager.utils.accentColor
import com.vanced.manager.utils.defPrefs
2021-01-16 17:00:38 +00:00
import com.vanced.manager.utils.lifecycleOwner
import com.vanced.manager.utils.managerAccent
2020-12-14 21:18:58 +00:00
class ThemedMaterialButton @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
defStyleAttr: Int = 0
) : MaterialButton(context, attributeSet, defStyleAttr) {
init {
setBgColor(context.defPrefs.managerAccent)
2020-12-14 21:18:58 +00:00
context.lifecycleOwner()?.let { owner ->
accentColor.observe(owner) { color ->
setBgColor(color.toInt())
}
}
}
private fun setBgColor(color: Int) {
setBackgroundColor(color)
2020-12-16 07:51:31 +00:00
if (ColorUtils.calculateLuminance(color) < 0.7) {
2020-12-14 21:18:58 +00:00
setTextColor(ResourcesCompat.getColor(resources, R.color.White, null))
} else {
setTextColor(ResourcesCompat.getColor(resources, R.color.Black, null))
}
}
}