Proper way to setup attr colors

This commit is contained in:
topjohnwu
2020-09-16 22:16:28 -07:00
parent 8c19654d20
commit 9e5cb6cb91
5 changed files with 33 additions and 31 deletions
@@ -8,14 +8,11 @@ import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.arch.BaseUIFragment
import com.topjohnwu.magisk.databinding.FragmentSafetynetMd2Binding
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.parameter.parametersOf
class SafetynetFragment : BaseUIFragment<SafetynetViewModel, FragmentSafetynetMd2Binding>() {
override val layoutRes = R.layout.fragment_safetynet_md2
override val viewModel by viewModel<SafetynetViewModel>() {
parametersOf(activity)
}
override val viewModel by viewModel<SafetynetViewModel>()
override fun onStart() {
super.onStart()
@@ -1,7 +1,5 @@
package com.topjohnwu.magisk.ui.safetynet
import android.util.TypedValue
import android.view.ContextThemeWrapper
import androidx.databinding.Bindable
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R
@@ -14,7 +12,7 @@ data class SafetyNetResult(
val dismiss: Boolean = false
)
class SafetynetViewModel(context: ContextThemeWrapper) : BaseViewModel() {
class SafetynetViewModel : BaseViewModel() {
@get:Bindable
var safetyNetTitle = R.string.empty
@@ -38,21 +36,12 @@ class SafetynetViewModel(context: ContextThemeWrapper) : BaseViewModel() {
@get:Bindable
var isSuccess = false
set(value) = set(value, field, { field = it }, BR.success, BR.textColor)
set(value) = set(value, field, { field = it }, BR.success, BR.textColorAttr)
@get:Bindable
val textColor get() = if (isSuccess) colorOnPrimary else colorOnError
private val colorOnPrimary: Int
private val colorOnError: Int
val textColorAttr get() = if (isSuccess) R.attr.colorOnPrimary else R.attr.colorOnError
init {
val tv = TypedValue()
context.theme.resolveAttribute(R.attr.colorOnPrimary, tv, true)
colorOnPrimary = tv.data
context.theme.resolveAttribute(R.attr.colorOnError, tv, true)
colorOnError = tv.data
cachedResult?.also {
resolveResponse(SafetyNetResult(it))
} ?: attest()