Address button color issues

This commit is contained in:
Rahul Patel
2024-07-29 23:53:12 +05:30
parent 8aeec0c082
commit 0a25a7e755
7 changed files with 78 additions and 11 deletions

View File

@@ -19,11 +19,14 @@
package com.aurora.extensions package com.aurora.extensions
import android.graphics.Color
import android.text.format.DateFormat import android.text.format.DateFormat
import androidx.annotation.ColorInt
import java.io.PrintWriter import java.io.PrintWriter
import java.io.StringWriter import java.io.StringWriter
import java.util.Calendar import java.util.Calendar
import java.util.Locale import java.util.Locale
import javax.annotation.Nullable
fun Long.toDate(): String { fun Long.toDate(): String {
val calendar = Calendar.getInstance(Locale.getDefault()) val calendar = Calendar.getInstance(Locale.getDefault())
@@ -42,4 +45,51 @@ fun Throwable.stackTraceToString(): String {
fun isValidPackageName(packageName: String): Boolean { fun isValidPackageName(packageName: String): Boolean {
val packageRegex = "^[a-zA-Z][a-zA-Z0-9_]*(\\.[a-zA-Z][a-zA-Z0-9_]*)*$".toRegex() val packageRegex = "^[a-zA-Z][a-zA-Z0-9_]*(\\.[a-zA-Z][a-zA-Z0-9_]*)*$".toRegex()
return packageName.matches(packageRegex) return packageName.matches(packageRegex)
}
/**
* Computes a darker color from the given color.
* @param color The color to darken.
* @param factor The factor to darken the color by.
* - higher factor values will result in a lighter color.
* @return The darker color.
*/
fun darkenColor(@ColorInt color: Int, factor: Float = 0.25f): Int {
val a = Color.alpha(color)
val r = (Color.red(color) * factor).coerceIn(0f, 255f).toInt()
val g = (Color.green(color) * factor).coerceIn(0f, 255f).toInt()
val b = (Color.blue(color) * factor).coerceIn(0f, 255f).toInt()
return Color.argb(a, r, g, b)
}
/**
* Computes a lighter color from the given color.
* @param color The color to lighten.
* @param factor The factor to lighten the color by.
* - higher factor values will result in a lighter color.
* @param alpha The alpha value to use for the lighter color.
* @return The lighter color.
*/
fun lightenColor(@ColorInt color: Int, factor: Float = 0.5f, @Nullable alpha: Int? = null): Int {
val a = alpha ?: Color.alpha(color)
val r = (Color.red(color) + (255 - Color.red(color)) * factor).toInt()
val g = (Color.green(color) + (255 - Color.green(color)) * factor).toInt()
val b = (Color.blue(color) + (255 - Color.blue(color)) * factor).toInt()
return Color.argb(a, r, g, b)
}
/**
* Computes a contrasting color from the given color.
* @param color The color to contrast.
* @return The contrasting color.
*/
fun contrastingColor(@ColorInt color: Int): Int {
val red = Color.red(color)
val green = Color.green(color)
val blue = Color.blue(color)
val yiq = ((red * 299) + (green * 587) + (blue * 114)) / 1000
return if (yiq >= 128) Color.BLACK else Color.WHITE
} }

View File

@@ -20,9 +20,13 @@
package com.aurora.store.view.custom.layouts.button package com.aurora.store.view.custom.layouts.button
import android.content.Context import android.content.Context
import android.content.res.ColorStateList
import android.util.AttributeSet import android.util.AttributeSet
import android.widget.RelativeLayout import android.widget.RelativeLayout
import com.aurora.extensions.accentColor
import com.aurora.extensions.darkenColor
import com.aurora.extensions.getString import com.aurora.extensions.getString
import com.aurora.extensions.lightenColor
import com.aurora.extensions.runOnUiThread import com.aurora.extensions.runOnUiThread
import com.aurora.store.R import com.aurora.store.R
import com.aurora.store.data.model.DownloadStatus import com.aurora.store.data.model.DownloadStatus
@@ -51,6 +55,15 @@ class UpdateButton : RelativeLayout {
private fun init(context: Context) { private fun init(context: Context) {
val view = inflate(context, R.layout.view_update_button, this) val view = inflate(context, R.layout.view_update_button, this)
binding = ViewUpdateButtonBinding.bind(view) binding = ViewUpdateButtonBinding.bind(view)
// Apply primaryColor tint to all buttons with alpha
val alphaAccent = lightenColor(context.accentColor(), alpha = 200)
binding.btnPositive.backgroundTintList = ColorStateList.valueOf(alphaAccent)
binding.btnNegative.backgroundTintList = ColorStateList.valueOf(alphaAccent)
val textColor = darkenColor(context.accentColor())
binding.btnPositive.setTextColor(textColor)
binding.btnNegative.setTextColor(textColor)
} }
fun setText(text: String) { fun setText(text: String) {
@@ -65,9 +78,8 @@ class UpdateButton : RelativeLayout {
fun updateState(downloadStatus: DownloadStatus) { fun updateState(downloadStatus: DownloadStatus) {
val displayChild = when (downloadStatus) { val displayChild = when (downloadStatus) {
DownloadStatus.QUEUED, DownloadStatus.QUEUED -> 1
DownloadStatus.DOWNLOADING -> 2 DownloadStatus.DOWNLOADING -> 2
else -> 0 else -> 0
} }

View File

@@ -25,6 +25,8 @@ import com.airbnb.epoxy.CallbackProp
import com.airbnb.epoxy.ModelProp import com.airbnb.epoxy.ModelProp
import com.airbnb.epoxy.ModelView import com.airbnb.epoxy.ModelView
import com.airbnb.epoxy.OnViewRecycled import com.airbnb.epoxy.OnViewRecycled
import com.aurora.extensions.accentColor
import com.aurora.extensions.contrastingColor
import com.aurora.store.databinding.ViewHeaderUpdateBinding import com.aurora.store.databinding.ViewHeaderUpdateBinding
@@ -46,6 +48,7 @@ class UpdateHeaderView @JvmOverloads constructor(
@ModelProp @ModelProp
fun action(action: String) { fun action(action: String) {
binding.btnAction.text = action binding.btnAction.text = action
binding.btnAction.setTextColor(contrastingColor(context.accentColor()))
} }
@CallbackProp @CallbackProp

View File

@@ -106,6 +106,7 @@
android:layout_alignTop="@id/txt_line1" android:layout_alignTop="@id/txt_line1"
android:layout_toStartOf="@id/btn_action" android:layout_toStartOf="@id/btn_action"
android:contentDescription="@string/details_changelog" android:contentDescription="@string/details_changelog"
app:iconTint="?colorControlNormal"
app:icon="@drawable/ic_arrow_down" /> app:icon="@drawable/ic_arrow_down" />
<com.aurora.store.view.custom.layouts.button.UpdateButton <com.aurora.store.view.custom.layouts.button.UpdateButton

View File

@@ -45,5 +45,6 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:text="@string/action_update_all" /> android:text="@string/action_update_all"
android:textAppearance="@style/TextAppearance.Aurora.Line1" />
</RelativeLayout> </RelativeLayout>

View File

@@ -25,14 +25,16 @@
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/btn" android:id="@+id/btn"
style="@style/Widget.Material3.Button.IconButton.Outlined" style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:textAlignment="viewStart" android:textAlignment="viewStart"
android:textColor="?colorControlNormal"
app:iconPadding="@dimen/padding_normal" app:iconPadding="@dimen/padding_normal"
app:iconTint="?colorAccent" app:iconTint="?colorControlNormal"
app:strokeColor="?colorAccent" app:strokeColor="?colorControlHighlight"
app:strokeWidth="1dp"
tools:icon="@drawable/ic_anonymous" tools:icon="@drawable/ic_anonymous"
tools:text="Button Name" /> tools:text="Button Name" />

View File

@@ -36,6 +36,7 @@
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/btnPositive" android:id="@+id/btnPositive"
style="@style/Widget.Material3.Button.TonalButton"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/action_update" android:text="@string/action_update"
@@ -48,6 +49,7 @@
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/btnQueued" android:id="@+id/btnQueued"
style="@style/Widget.Material3.Button.TonalButton"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:enabled="false" android:enabled="false"
@@ -61,15 +63,11 @@
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/btnNegative" android:id="@+id/btnNegative"
style="@style/Widget.Material3.Button.TonalButton"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/action_cancel" android:text="@string/action_cancel"
android:textAppearance="@style/TextAppearance.Aurora.Line1" /> android:textAppearance="@style/TextAppearance.Aurora.Line1" />
</RelativeLayout> </RelativeLayout>
</ViewFlipper> </ViewFlipper>
</RelativeLayout> </RelativeLayout>