mirror of
https://github.com/YTVanced/VancedManager
synced 2025-01-07 07:51:00 +00:00
optimised lifecycleOwner getter
This commit is contained in:
parent
18cad94748
commit
1f600430ea
5 changed files with 8 additions and 15 deletions
|
@ -16,7 +16,7 @@ class ThemedMaterialButton @JvmOverloads constructor(
|
||||||
) : MaterialButton(context, attributeSet, defStyleAttr) {
|
) : MaterialButton(context, attributeSet, defStyleAttr) {
|
||||||
|
|
||||||
init {
|
init {
|
||||||
context.lifecycleOwner()?.let { owner ->
|
context.lifecycleOwner?.let { owner ->
|
||||||
accentColor.observe(owner) { color ->
|
accentColor.observe(owner) { color ->
|
||||||
setBgColor(color.toInt())
|
setBgColor(color.toInt())
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ class ThemedOutlinedMaterialButton @JvmOverloads constructor(
|
||||||
defStyleAttr: Int = 0
|
defStyleAttr: Int = 0
|
||||||
) : MaterialButton(context, attributeSet, defStyleAttr) {
|
) : MaterialButton(context, attributeSet, defStyleAttr) {
|
||||||
init {
|
init {
|
||||||
context.lifecycleOwner()?.let { owner ->
|
context.lifecycleOwner?.let { owner ->
|
||||||
accentColor.observe(owner) { color ->
|
accentColor.observe(owner) { color ->
|
||||||
applyAccent(color.toInt())
|
applyAccent(color.toInt())
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ class ThemedSwitchCompat @JvmOverloads constructor(
|
||||||
private val states = arrayOf(intArrayOf(-android.R.attr.state_checked), intArrayOf(android.R.attr.state_checked))
|
private val states = arrayOf(intArrayOf(-android.R.attr.state_checked), intArrayOf(android.R.attr.state_checked))
|
||||||
|
|
||||||
init {
|
init {
|
||||||
context.lifecycleOwner()?.let { owner ->
|
context.lifecycleOwner?.let { owner ->
|
||||||
accentColor.observe(owner) { color ->
|
accentColor.observe(owner) { color ->
|
||||||
setSwitchColors(color.toInt())
|
setSwitchColors(color.toInt())
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ class ThemedTextView @JvmOverloads constructor(
|
||||||
defStyleAttr: Int = 0
|
defStyleAttr: Int = 0
|
||||||
) : AppCompatTextView(context, attributeSet, defStyleAttr) {
|
) : AppCompatTextView(context, attributeSet, defStyleAttr) {
|
||||||
init {
|
init {
|
||||||
context.lifecycleOwner()?.let { owner ->
|
context.lifecycleOwner?.let { owner ->
|
||||||
accentColor.observe(owner) { color ->
|
accentColor.observe(owner) { color ->
|
||||||
setTextColor(color.toInt())
|
setTextColor(color.toInt())
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,17 +42,10 @@ fun String.convertToAppTheme(context: Context): String {
|
||||||
|
|
||||||
fun String.getLatestAppVersion(versions: List<String>): String = if (this == "latest") versions.reversed()[0] else this
|
fun String.getLatestAppVersion(versions: List<String>): String = if (this == "latest") versions.reversed()[0] else this
|
||||||
|
|
||||||
fun Context.lifecycleOwner(): LifecycleOwner? {
|
val Context.lifecycleOwner: LifecycleOwner? get() = when (this) {
|
||||||
var curContext = this
|
is LifecycleOwner -> this
|
||||||
var maxDepth = 20
|
!is LifecycleOwner -> (this as ContextWrapper).baseContext as LifecycleOwner
|
||||||
while (maxDepth-- > 0 && curContext !is LifecycleOwner) {
|
else -> null
|
||||||
curContext = (curContext as ContextWrapper).baseContext
|
|
||||||
}
|
|
||||||
return if (curContext is LifecycleOwner) {
|
|
||||||
curContext
|
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Int.toHex(): String = java.lang.String.format("#%06X", 0xFFFFFF and this)
|
fun Int.toHex(): String = java.lang.String.format("#%06X", 0xFFFFFF and this)
|
||||||
|
|
Loading…
Reference in a new issue