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

View File

@@ -297,3 +297,17 @@ fun CardView.setCardBackgroundColorAttr(attr: Int) {
fun ImageView.setTint(color: Int) {
ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(color))
}
@BindingAdapter("tintAttr")
fun ImageView.setTintAttr(attr: Int) {
val tv = TypedValue()
context.theme.resolveAttribute(attr, tv, true)
ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(tv.data))
}
@BindingAdapter("textColorAttr")
fun TextView.setTextColorAttr(attr: Int) {
val tv = TypedValue()
context.theme.resolveAttribute(attr, tv, true)
setTextColor(tv.data)
}

View File

@@ -1,6 +1,5 @@
package com.topjohnwu.magisk.di
import android.view.ContextThemeWrapper
import com.topjohnwu.magisk.ui.MainViewModel
import com.topjohnwu.magisk.ui.flash.FlashFragmentArgs
import com.topjohnwu.magisk.ui.flash.FlashViewModel
@@ -22,7 +21,7 @@ val viewModelModules = module {
viewModel { HomeViewModel(get()) }
viewModel { LogViewModel(get()) }
viewModel { ModuleViewModel(get(), get(), get()) }
viewModel { (context: ContextThemeWrapper) -> SafetynetViewModel(context) }
viewModel { SafetynetViewModel() }
viewModel { SettingsViewModel(get()) }
viewModel { SuperuserViewModel(get(), get()) }
viewModel { ThemeViewModel() }

View File

@@ -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()

View File

@@ -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()