Skip to content

Commit

Permalink
[23356]
Browse files Browse the repository at this point in the history
- [TripKitUI] update ActionButtonViewModel to set button tint color from DynamicAppColor
- [TripKitUI] update CustomBinding.kt to add BindingAdapters for setting tint, background and foreground colors to imageview, layout, textview and buttons from DynamicAppColor
- [TripKitUI] update layout's TextViews, ImageViews, Buttons and Parent layouts to use bindingAdapters for DynamicAppColor implementation
- [TripGov5] update layout's TextViews, ImageViews, Buttons and Parent layouts to use bindingAdapters for DynamicAppColor implementation
  • Loading branch information
MichaelReyes committed Feb 15, 2025
1 parent 22bad48 commit 907d0f7
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import com.skedgo.tripkit.ui.R
import com.skedgo.tripkit.ui.tripresults.actionbutton.ActionButton
import com.skedgo.tripkit.ui.utils.DynamicAppColor


interface ActionButtonClickListener {
Expand Down Expand Up @@ -36,11 +37,15 @@ class ActionButtonViewModel constructor(context: Context, button: ActionButton)
this.tag = button.tag
val stateList = arrayOf(intArrayOf(android.R.attr.state_enabled), intArrayOf())

val appTintColor = DynamicAppColor.getAppColors()?.tintColor?.run {
Color.rgb(red, green, blue)
} ?: ContextCompat.getColor(context, R.color.colorPrimary)

if (button.isPrimary) {
this.iconTint.set(Color.WHITE)
this.outlineTint.set(Color.TRANSPARENT)
val backgroundColorList =
intArrayOf(ContextCompat.getColor(context, R.color.colorPrimary), 0)
intArrayOf(appTintColor, 0)
this.backgroundTint.set(ColorStateList(stateList, backgroundColorList))
this.background.set(ContextCompat.getDrawable(context, R.drawable.bg_circle_primary))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.StateListDrawable
import android.os.SystemClock
import android.util.TypedValue
Expand All @@ -18,7 +19,9 @@ import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import androidx.core.graphics.ColorUtils
import androidx.databinding.BindingAdapter
import com.afollestad.materialdialogs.utils.MDUtil.ifNotZero
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.LazyHeaders
import com.bumptech.glide.request.RequestOptions
Expand Down Expand Up @@ -309,11 +312,41 @@ fun setButtonAppTint(button: Button, @ColorInt default: Int) {
}
}

@BindingAdapter("appTintWithState")
fun setButtonStateBackground(button: Button, @ColorInt default: Int?) {
val enabledColor = DynamicAppColor.getAppColors()?.tintColor?.let {
Color.rgb(it.red, it.green, it.blue)
} ?: DynamicAppColor.getSystemColors()?.tintColor ?: default

val disabledColor = enabledColor?.let {
ColorUtils.setAlphaComponent(it, (0.3 * 255).toInt()) // 30% opacity for disabled state
} ?: Color.GRAY // Fallback to gray if everything is null

val enabledDrawable = GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
cornerRadius = 16f // Adjust as needed
setColor(enabledColor ?: Color.TRANSPARENT)
}

val disabledDrawable = GradientDrawable().apply {
shape = GradientDrawable.RECTANGLE
cornerRadius = 16f
setColor(disabledColor)
}

val stateListDrawable = StateListDrawable().apply {
addState(intArrayOf(-android.R.attr.state_enabled), disabledDrawable) // Disabled state
addState(intArrayOf(), enabledDrawable) // Default state
}

button.background = stateListDrawable
}

@BindingAdapter("appTint")
fun setImageViewAppTint(imageView: ImageView, @ColorInt default: Int?) {
val color = DynamicAppColor.getAppColors()?.tintColor?.let {
Color.rgb(it.red, it.green, it.blue)
} ?: DynamicAppColor.getSystemColors()?.tintColor ?: default
} ?: DynamicAppColor.getSystemColors()?.tintColor ?: default.takeIf { default != 0 }

color?.let {
imageView.setColorFilter(it, PorterDuff.Mode.SRC_IN) // Apply tint only to fill
Expand All @@ -331,6 +364,54 @@ fun setTextViewAppTint(textView: TextView, @ColorInt default: Int?) {
}
}

@BindingAdapter("appLayoutTint")
fun setLayoutAppTint(view: View, @ColorInt default: Int?) {
val color = DynamicAppColor.getAppColors()?.tintColor?.let {
Color.rgb(it.red, it.green, it.blue)
} ?: DynamicAppColor.getSystemColors()?.barBackground ?: default

color?.let {
view.backgroundTintList = ColorStateList.valueOf(it)
}
}

@BindingAdapter("appTextTint")
fun setButtonTextAppTint(button: Button, @ColorInt default: Int?) {
val color = DynamicAppColor.getAppColors()?.tintColor?.let {
Color.rgb(it.red, it.green, it.blue)
} ?: DynamicAppColor.getSystemColors()?.tintColor ?: default

color?.let {
button.setTextColor(it)
}
}

@BindingAdapter("appBackgroundTintAlpha")
fun setTextViewBackgroundTintAlpha(textView: TextView, @ColorInt default: Int?) {
val color = DynamicAppColor.getAppColors()?.tintColor?.let {
Color.argb((0.2 * 255).toInt(), it.red, it.green, it.blue) // Apply 20% alpha
} ?: DynamicAppColor.getSystemColors()?.tintColor?.let {
Color.argb((0.2 * 255).toInt(), Color.red(it), Color.green(it), Color.blue(it))
} ?: default?.let {
Color.argb((0.2 * 255).toInt(), Color.red(it), Color.green(it), Color.blue(it))
}

color?.let {
textView.backgroundTintList = ColorStateList.valueOf(it)
}
}

@BindingAdapter("appBackgroundTint")
fun setTextViewBackgroundTint(textView: TextView, @ColorInt default: Int?) {
val color = DynamicAppColor.getAppColors()?.tintColor?.let {
Color.rgb(it.red, it.green, it.blue)
} ?: DynamicAppColor.getSystemColors()?.tintColor ?: default

color?.let {
textView.setBackgroundColor(it)
}
}

@BindingAdapter("appForeground")
fun setTextViewAppForegroundTint(textView: TextView, @ColorInt default: Int?) {
val color = DynamicAppColor.getAppColors()?.barForeground?.let {
Expand All @@ -342,6 +423,17 @@ fun setTextViewAppForegroundTint(textView: TextView, @ColorInt default: Int?) {
}
}

@BindingAdapter("appForeground")
fun setImageViewAppForegroundTint(imageView: ImageView, @ColorInt default: Int?) {
val color = DynamicAppColor.getAppColors()?.barForeground?.let {
Color.rgb(it.red, it.green, it.blue)
} ?: DynamicAppColor.getSystemColors()?.barForeground ?: default

color?.let {
imageView.setColorFilter(it, PorterDuff.Mode.SRC_IN) // Apply tint only to fill
}
}

@BindingAdapter("appBackground")
fun setLayoutAppBackgroundTint(view: View, @ColorInt default: Int?) {
val color = DynamicAppColor.getAppColors()?.barBackground?.let {
Expand All @@ -352,3 +444,4 @@ fun setLayoutAppBackgroundTint(view: View, @ColorInt default: Int?) {
view.setBackgroundColor(it)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/toolbar"
android:background="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:padding="@dimen/spacing_small">
android:padding="@dimen/spacing_small"
app:appBackground="@{@color/colorPrimaryDark}"
tools:background="@color/colorPrimaryDark">

<LinearLayout
android:id="@+id/layoutBack"
Expand All @@ -42,15 +43,17 @@
android:src="@drawable/ic_back_button"
android:padding="@dimen/spacing_extra_small"
android:importantForAccessibility="no"
app:tint="@color/colorAccent"/>
app:appForeground="@{@color/colorAccent}"
tools:tint="@color/colorAccent"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:text="@string/back"
android:textSize="@dimen/font_size_medium"
android:padding="@dimen/spacing_small" />
android:padding="@dimen/spacing_small"
app:appForeground="@{@color/colorAccent}"
tools:textColor="@color/colorAccent"/>

</LinearLayout>

Expand Down Expand Up @@ -226,10 +229,10 @@
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="@color/colorAccent"
android:padding="@dimen/spacing_small"
android:layout_marginEnd="@dimen/spacing_small"
android:text="@string/done" />
android:text="@string/done"
app:appTint="@{@color/colorAccent}"/>

<TimePicker
android:id="@+id/time_picker"
Expand Down Expand Up @@ -259,6 +262,7 @@
android:paddingEnd="@dimen/spacing_normal"
android:text="@string/confirm"
android:textColor="@android:color/white"
app:appTintWithState="@{@color/colorAccent}"
app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,14 @@
android:padding="0dp"
android:textSize="@dimen/font_size_small"
android:textAllCaps="false"
android:backgroundTint="@color/colorAccent"
android:background="@drawable/bg_round_accent"
android:visibility="@{viewModel.quickBookingSegment != null}"
android:text="@{viewModel.quickBookingSegment.booking.title}"
app:layout_constraintTop_toBottomOf="@+id/tvTime"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
app:layout_constraintEnd_toEndOf="parent"
app:appTint="@{@color/colorAccent}"
tools:backgroundTint="@color/colorAccent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
android:layout_height="@dimen/icon_regular"
android:src="@drawable/ic_back_button"
android:padding="@dimen/spacing_extra_small"
app:appTint="@{@color/colorAccent}"
app:appForeground="@{@color/colorAccent}"
android:visibility="@{viewModel.toolbarState.showBackButton}"
android:importantForAccessibility="no" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,10 @@
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/spacing_normal"
android:text="@string/learn_more"
android:textColor="@color/colorAccent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
tools:textColor="@color/colorAccent"
app:appTint="@{@color/colorAccent}"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,13 @@
android:onClick="@{() -> viewModel.onMoreButtonClicked.perform()}"
android:singleLine="true"
android:text="@{viewModel.moreButtonText}"
android:textColor="@color/colorAccent"
android:visibility="@{viewModel.moreButtonVisible}"
android:contentDescription="@{viewModel.accessibilityLabel}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintTop_toBottomOf="@+id/footerLine"
app:appTextTint="@{@color/colorAccent}"
tools:textColor="@color/colorAccent"
tools:text="ButtonText" />

<!--
Expand Down

0 comments on commit 907d0f7

Please sign in to comment.