Skip to content

Commit d5c21aa

Browse files
committed
Move color parser to util class
1 parent 9060a41 commit d5c21aa

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}

core/src/main/java/io/snabble/sdk/coupons/Coupon.kt

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.os.Parcelable
55
import android.util.DisplayMetrics
66
import androidx.annotation.ColorInt
77
import com.google.gson.annotations.SerializedName
8+
import io.snabble.sdk.ColorUtils.parseColor
89
import io.snabble.sdk.Snabble
910
import kotlinx.parcelize.IgnoredOnParcel
1011
import 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

0 commit comments

Comments
 (0)