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) { fun ImageView.setTint(color: Int) {
ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(color)) 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 package com.topjohnwu.magisk.di
import android.view.ContextThemeWrapper
import com.topjohnwu.magisk.ui.MainViewModel import com.topjohnwu.magisk.ui.MainViewModel
import com.topjohnwu.magisk.ui.flash.FlashFragmentArgs import com.topjohnwu.magisk.ui.flash.FlashFragmentArgs
import com.topjohnwu.magisk.ui.flash.FlashViewModel import com.topjohnwu.magisk.ui.flash.FlashViewModel
@ -22,7 +21,7 @@ val viewModelModules = module {
viewModel { HomeViewModel(get()) } viewModel { HomeViewModel(get()) }
viewModel { LogViewModel(get()) } viewModel { LogViewModel(get()) }
viewModel { ModuleViewModel(get(), get(), get()) } viewModel { ModuleViewModel(get(), get(), get()) }
viewModel { (context: ContextThemeWrapper) -> SafetynetViewModel(context) } viewModel { SafetynetViewModel() }
viewModel { SettingsViewModel(get()) } viewModel { SettingsViewModel(get()) }
viewModel { SuperuserViewModel(get(), get()) } viewModel { SuperuserViewModel(get(), get()) }
viewModel { ThemeViewModel() } viewModel { ThemeViewModel() }

View File

@ -8,14 +8,11 @@ import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.arch.BaseUIFragment import com.topjohnwu.magisk.arch.BaseUIFragment
import com.topjohnwu.magisk.databinding.FragmentSafetynetMd2Binding import com.topjohnwu.magisk.databinding.FragmentSafetynetMd2Binding
import org.koin.androidx.viewmodel.ext.android.viewModel import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.parameter.parametersOf
class SafetynetFragment : BaseUIFragment<SafetynetViewModel, FragmentSafetynetMd2Binding>() { class SafetynetFragment : BaseUIFragment<SafetynetViewModel, FragmentSafetynetMd2Binding>() {
override val layoutRes = R.layout.fragment_safetynet_md2 override val layoutRes = R.layout.fragment_safetynet_md2
override val viewModel by viewModel<SafetynetViewModel>() { override val viewModel by viewModel<SafetynetViewModel>()
parametersOf(activity)
}
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()

View File

@ -1,7 +1,5 @@
package com.topjohnwu.magisk.ui.safetynet package com.topjohnwu.magisk.ui.safetynet
import android.util.TypedValue
import android.view.ContextThemeWrapper
import androidx.databinding.Bindable import androidx.databinding.Bindable
import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
@ -14,7 +12,7 @@ data class SafetyNetResult(
val dismiss: Boolean = false val dismiss: Boolean = false
) )
class SafetynetViewModel(context: ContextThemeWrapper) : BaseViewModel() { class SafetynetViewModel : BaseViewModel() {
@get:Bindable @get:Bindable
var safetyNetTitle = R.string.empty var safetyNetTitle = R.string.empty
@ -38,21 +36,12 @@ class SafetynetViewModel(context: ContextThemeWrapper) : BaseViewModel() {
@get:Bindable @get:Bindable
var isSuccess = false 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 @get:Bindable
val textColor get() = if (isSuccess) colorOnPrimary else colorOnError val textColorAttr get() = if (isSuccess) R.attr.colorOnPrimary else R.attr.colorOnError
private val colorOnPrimary: Int
private val colorOnError: Int
init { 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 { cachedResult?.also {
resolveResponse(SafetyNetResult(it)) resolveResponse(SafetyNetResult(it))
} ?: attest() } ?: attest()

View File

@ -80,8 +80,8 @@
android:gravity="center" android:gravity="center"
android:text="@{viewModel.safetyNetTitle}" android:text="@{viewModel.safetyNetTitle}"
android:textAppearance="@style/AppearanceFoundation.Display.OnPrimary" android:textAppearance="@style/AppearanceFoundation.Display.OnPrimary"
android:textColor="@{viewModel.textColor}"
android:textStyle="bold" android:textStyle="bold"
app:textColorAttr="@{viewModel.textColorAttr}"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
@ -94,10 +94,11 @@
android:layout_height="4dp" android:layout_height="4dp"
android:layout_marginTop="@dimen/l2" android:layout_marginTop="@dimen/l2"
app:srcCompat="@drawable/bg_divider_rounded_on_primary" app:srcCompat="@drawable/bg_divider_rounded_on_primary"
app:tint="@{viewModel.textColor}" app:tintAttr="@{viewModel.textColorAttr}"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/safetynet_title" /> app:layout_constraintTop_toBottomOf="@+id/safetynet_title"
tools:tint="?colorOnPrimary"/>
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/checkbox_layout" android:id="@+id/checkbox_layout"
@ -117,8 +118,8 @@
android:text="basicIntegrity" android:text="basicIntegrity"
android:textAppearance="@style/AppearanceFoundation.Body.OnPrimary" android:textAppearance="@style/AppearanceFoundation.Body.OnPrimary"
android:textStyle="bold" android:textStyle="bold"
android:textColor="@{viewModel.textColor}"
android:layout_marginTop="@dimen/l2" android:layout_marginTop="@dimen/l2"
app:textColorAttr="@{viewModel.textColorAttr}"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="@id/snet_barrier" app:layout_constraintEnd_toEndOf="@id/snet_barrier"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
@ -133,8 +134,8 @@
android:text="ctsProfile" android:text="ctsProfile"
android:textAppearance="@style/AppearanceFoundation.Body.OnPrimary" android:textAppearance="@style/AppearanceFoundation.Body.OnPrimary"
android:textStyle="bold" android:textStyle="bold"
android:textColor="@{viewModel.textColor}"
android:layout_marginTop="@dimen/l2" android:layout_marginTop="@dimen/l2"
app:textColorAttr="@{viewModel.textColorAttr}"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="@id/snet_barrier" app:layout_constraintEnd_toEndOf="@id/snet_barrier"
app:layout_constraintTop_toBottomOf="@id/basic_text" app:layout_constraintTop_toBottomOf="@id/basic_text"
@ -152,20 +153,22 @@
isSelected="@{viewModel.basicIntegrityState}" isSelected="@{viewModel.basicIntegrityState}"
android:layout_gravity="center" android:layout_gravity="center"
app:srcCompat="@drawable/ic_check_circle_md2" app:srcCompat="@drawable/ic_check_circle_md2"
app:tint="@{viewModel.textColor}" app:tintAttr="@{viewModel.textColorAttr}"
app:layout_constraintStart_toEndOf="@id/snet_barrier" app:layout_constraintStart_toEndOf="@id/snet_barrier"
app:layout_constraintTop_toTopOf="@id/basic_text" app:layout_constraintTop_toTopOf="@id/basic_text"
app:layout_constraintBottom_toBottomOf="@id/basic_text"/> app:layout_constraintBottom_toBottomOf="@id/basic_text"
tools:tint="?colorOnPrimary"/>
<ImageView <ImageView
style="@style/WidgetFoundation.Icon.OnPrimary" style="@style/WidgetFoundation.Icon.OnPrimary"
isSelected="@{viewModel.ctsState}" isSelected="@{viewModel.ctsState}"
android:layout_gravity="center" android:layout_gravity="center"
app:srcCompat="@drawable/ic_check_circle_md2" app:srcCompat="@drawable/ic_check_circle_md2"
app:tint="@{viewModel.textColor}" app:tintAttr="@{viewModel.textColorAttr}"
app:layout_constraintStart_toEndOf="@id/snet_barrier" app:layout_constraintStart_toEndOf="@id/snet_barrier"
app:layout_constraintTop_toTopOf="@id/cts_text" app:layout_constraintTop_toTopOf="@id/cts_text"
app:layout_constraintBottom_toBottomOf="@id/cts_text"/> app:layout_constraintBottom_toBottomOf="@id/cts_text"
tools:tint="?colorOnPrimary"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
@ -186,7 +189,7 @@
android:text="evalType" android:text="evalType"
android:textAppearance="@style/AppearanceFoundation.Body.OnPrimary" android:textAppearance="@style/AppearanceFoundation.Body.OnPrimary"
android:textStyle="bold" android:textStyle="bold"
android:textColor="@{viewModel.textColor}" app:textColorAttr="@{viewModel.textColorAttr}"
tools:ignore="HardcodedText" /> tools:ignore="HardcodedText" />
<TextView <TextView
@ -195,9 +198,9 @@
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:textAppearance="@style/AppearanceFoundation.Body.OnPrimary" android:textAppearance="@style/AppearanceFoundation.Body.OnPrimary"
android:textStyle="bold" android:textStyle="bold"
android:textColor="@{viewModel.textColor}"
android:layout_marginStart="@dimen/l1" android:layout_marginStart="@dimen/l1"
android:text="@{viewModel.evalType}" android:text="@{viewModel.evalType}"
app:textColorAttr="@{viewModel.textColorAttr}"
tools:text="HARDWARE"/> tools:text="HARDWARE"/>
</LinearLayout> </LinearLayout>