File tree Expand file tree Collapse file tree 2 files changed +26
-18
lines changed
core/src/main/java/io/snabble/sdk Expand file tree Collapse file tree 2 files changed +26
-18
lines changed Original file line number Diff line number Diff line change 1+ package io.snabble.sdk
2+
3+ import android.graphics.Color
4+ import androidx.annotation.ColorInt
5+
6+ object ColorUtils {
7+ @JvmStatic
8+ fun parseColor (color : String? , @ColorInt default : Int ) =
9+ color?.let {
10+ Color .parseColor(when {
11+ " ^[0-9a-fA-F]{6}(?:[0-9a-fA-F]{2})?$" .toRegex().matches(color) -> {
12+ // add missing prefix
13+ " #$color "
14+ }
15+ " ^#?[0-9a-fA-F]{3}$" .toRegex().matches(color) -> {
16+ // convert 3 digit color to 6 digits
17+ color.removePrefix(" #" ).toCharArray()
18+ .joinToString(separator = " " , prefix = " #" ) { " $it$it " }
19+ }
20+ else -> {
21+ color
22+ }
23+ })
24+ } ? : default
25+ }
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import android.os.Parcelable
55import android.util.DisplayMetrics
66import androidx.annotation.ColorInt
77import com.google.gson.annotations.SerializedName
8+ import io.snabble.sdk.ColorUtils.parseColor
89import io.snabble.sdk.Snabble
910import kotlinx.parcelize.IgnoredOnParcel
1011import kotlinx.parcelize.Parcelize
@@ -49,24 +50,6 @@ data class Coupon (
4950 @IgnoredOnParcel
5051 val textColor
5152 get() = parseColor(colors?.get(" foreground" ), Color .BLACK )
52-
53- fun parseColor (color : String? , @ColorInt default : Int ) =
54- color?.let {
55- Color .parseColor(when {
56- " ^[0-9a-fA-F]{6}(?:[0-9a-fA-F]{2})?$" .toRegex().matches(color) -> {
57- // add missing prefix
58- " #$color "
59- }
60- " ^#?[0-9a-fA-F]{3}$" .toRegex().matches(color) -> {
61- // convert 3 digit color to 6 digits
62- color.removePrefix(" #" ).toCharArray()
63- .joinToString(separator = " " , prefix = " #" ) { " $it$it " }
64- }
65- else -> {
66- color
67- }
68- })
69- } ? : default
7053}
7154
7255@Parcelize
You can’t perform that action at this time.
0 commit comments