mirror of
https://github.com/YTVanced/VancedManager
synced 2025-01-03 14:10:59 +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) {
|
||||
|
||||
init {
|
||||
context.lifecycleOwner()?.let { owner ->
|
||||
context.lifecycleOwner?.let { owner ->
|
||||
accentColor.observe(owner) { color ->
|
||||
setBgColor(color.toInt())
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ class ThemedOutlinedMaterialButton @JvmOverloads constructor(
|
|||
defStyleAttr: Int = 0
|
||||
) : MaterialButton(context, attributeSet, defStyleAttr) {
|
||||
init {
|
||||
context.lifecycleOwner()?.let { owner ->
|
||||
context.lifecycleOwner?.let { owner ->
|
||||
accentColor.observe(owner) { color ->
|
||||
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))
|
||||
|
||||
init {
|
||||
context.lifecycleOwner()?.let { owner ->
|
||||
context.lifecycleOwner?.let { owner ->
|
||||
accentColor.observe(owner) { color ->
|
||||
setSwitchColors(color.toInt())
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ class ThemedTextView @JvmOverloads constructor(
|
|||
defStyleAttr: Int = 0
|
||||
) : AppCompatTextView(context, attributeSet, defStyleAttr) {
|
||||
init {
|
||||
context.lifecycleOwner()?.let { owner ->
|
||||
context.lifecycleOwner?.let { owner ->
|
||||
accentColor.observe(owner) { color ->
|
||||
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 Context.lifecycleOwner(): LifecycleOwner? {
|
||||
var curContext = this
|
||||
var maxDepth = 20
|
||||
while (maxDepth-- > 0 && curContext !is LifecycleOwner) {
|
||||
curContext = (curContext as ContextWrapper).baseContext
|
||||
}
|
||||
return if (curContext is LifecycleOwner) {
|
||||
curContext
|
||||
} else {
|
||||
null
|
||||
}
|
||||
val Context.lifecycleOwner: LifecycleOwner? get() = when (this) {
|
||||
is LifecycleOwner -> this
|
||||
!is LifecycleOwner -> (this as ContextWrapper).baseContext as LifecycleOwner
|
||||
else -> null
|
||||
}
|
||||
|
||||
fun Int.toHex(): String = java.lang.String.format("#%06X", 0xFFFFFF and this)
|
||||
|
|
Loading…
Reference in a new issue