From 9e5cb6cb91a3610dda22a045a454120e2a614acc Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 16 Sep 2020 22:16:28 -0700 Subject: [PATCH] Proper way to setup attr colors --- .../magisk/databinding/DataBindingAdapters.kt | 14 +++++++++++ .../topjohnwu/magisk/di/ViewModelsModule.kt | 3 +-- .../magisk/ui/safetynet/SafetynetFragment.kt | 5 +--- .../magisk/ui/safetynet/SafetynetViewModel.kt | 17 +++---------- .../res/layout/fragment_safetynet_md2.xml | 25 +++++++++++-------- 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/databinding/DataBindingAdapters.kt b/app/src/main/java/com/topjohnwu/magisk/databinding/DataBindingAdapters.kt index e6301a0cf..f1fb32263 100644 --- a/app/src/main/java/com/topjohnwu/magisk/databinding/DataBindingAdapters.kt +++ b/app/src/main/java/com/topjohnwu/magisk/databinding/DataBindingAdapters.kt @@ -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) +} diff --git a/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt b/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt index 2e3537f3c..2c364d71d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt @@ -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() } diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/safetynet/SafetynetFragment.kt b/app/src/main/java/com/topjohnwu/magisk/ui/safetynet/SafetynetFragment.kt index 10ffd746c..033da6ef0 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/safetynet/SafetynetFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/safetynet/SafetynetFragment.kt @@ -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() { override val layoutRes = R.layout.fragment_safetynet_md2 - override val viewModel by viewModel() { - parametersOf(activity) - } + override val viewModel by viewModel() override fun onStart() { super.onStart() diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/safetynet/SafetynetViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/safetynet/SafetynetViewModel.kt index a3e77fe56..b2a505460 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/safetynet/SafetynetViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/safetynet/SafetynetViewModel.kt @@ -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() diff --git a/app/src/main/res/layout/fragment_safetynet_md2.xml b/app/src/main/res/layout/fragment_safetynet_md2.xml index 438dd2dc1..60163c385 100644 --- a/app/src/main/res/layout/fragment_safetynet_md2.xml +++ b/app/src/main/res/layout/fragment_safetynet_md2.xml @@ -80,8 +80,8 @@ android:gravity="center" android:text="@{viewModel.safetyNetTitle}" android:textAppearance="@style/AppearanceFoundation.Display.OnPrimary" - android:textColor="@{viewModel.textColor}" android:textStyle="bold" + app:textColorAttr="@{viewModel.textColorAttr}" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintStart_toStartOf="parent" @@ -94,10 +94,11 @@ android:layout_height="4dp" android:layout_marginTop="@dimen/l2" app:srcCompat="@drawable/bg_divider_rounded_on_primary" - app:tint="@{viewModel.textColor}" + app:tintAttr="@{viewModel.textColorAttr}" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toBottomOf="@+id/safetynet_title" /> + app:layout_constraintTop_toBottomOf="@+id/safetynet_title" + tools:tint="?colorOnPrimary"/> + app:layout_constraintBottom_toBottomOf="@id/basic_text" + tools:tint="?colorOnPrimary"/> + app:layout_constraintBottom_toBottomOf="@id/cts_text" + tools:tint="?colorOnPrimary"/> @@ -186,7 +189,7 @@ android:text="evalType" android:textAppearance="@style/AppearanceFoundation.Body.OnPrimary" android:textStyle="bold" - android:textColor="@{viewModel.textColor}" + app:textColorAttr="@{viewModel.textColorAttr}" tools:ignore="HardcodedText" />