-
Notifications
You must be signed in to change notification settings - Fork 79
[SDK - 5236] [Android] Parse color fallback #895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SuriyaKannimuthu
wants to merge
7
commits into
develop
Choose a base branch
from
task/android/sdk-5236
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5f72cf3
[SDK - 5236] [Android] Handled invalid colour values in In-App displa…
SuriyaKannimuthu 3fbac2c
Update ColorUtils.kt
SuriyaKannimuthu 8066f12
Revert "Update ColorUtils.kt"
SuriyaKannimuthu c2c1cb3
Revert "[SDK - 5236] [Android] Handled invalid colour values in In-Ap…
SuriyaKannimuthu 1581380
Color handling in CTInAppNotification.kt
SuriyaKannimuthu 4d7400b
Addressd the Color object name shadowing issue.
SuriyaKannimuthu bc4ca9b
Merge branch 'develop' into task/android/sdk-5236
SuriyaKannimuthu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
clevertap-core/src/main/java/com/clevertap/android/sdk/utils/ColorUtils.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package com.clevertap.android.sdk.utils | ||
|
|
||
| import android.graphics.Color | ||
| import androidx.annotation.ColorInt | ||
| import androidx.core.graphics.toColorInt | ||
|
|
||
| /** | ||
| * Safely converts a [String] to a color integer. | ||
| * | ||
| * This function attempts to parse a color string (e.g., "#RRGGBB" or "#AARRGGBB") | ||
| * into an Android color integer. If the string is null, blank, or invalid, it returns | ||
| * the provided [defaultColor]. | ||
| * | ||
| * Example: | ||
| * ``` | ||
| * val color = "#FF0000".toColorIntOrDefault() // Returns red | ||
| * val invalid = "redcolor".toColorIntOrDefault(Color.GRAY) // Returns gray | ||
| * ``` | ||
| * | ||
| * @receiver The color string to parse (e.g., "#FFFFFF", "#80FF0000"). | ||
| * @param defaultColor The fallback color if parsing fails (default is [Color.WHITE]). | ||
| * @return The parsed color integer or [defaultColor] if parsing fails. | ||
| */ | ||
| @ColorInt | ||
| fun String?.toColorIntOrDefault(@ColorInt defaultColor: Int = Color.WHITE): Int { | ||
| // If the string is null or empty, return the default color immediately | ||
| if (this.isNullOrBlank()) return defaultColor | ||
|
|
||
| return try { | ||
| // Try converting the string to a color integer | ||
| this.toColorInt() | ||
| } catch (e: IllegalArgumentException) { | ||
| // Handles invalid color format (e.g., "abc123") | ||
| defaultColor | ||
| } catch (e: StringIndexOutOfBoundsException) { | ||
| // Handles malformed color strings (e.g., "#F") | ||
| defaultColor | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Safely validates a color string and returns a valid color string. | ||
| * | ||
| * If this string is a valid color (e.g. "#RRGGBB" or "#AARRGGBB"), it's returned as-is. | ||
| * If it's null, blank, or invalid, the provided [fallback] color string is returned instead. | ||
| * | ||
| * Example: | ||
| * ``` | ||
| * val valid = "#FF0000".toValidColorOrFallback("#FFFFFF") // "#FF0000" | ||
| * val invalid = "notacolor".toValidColorOrFallback("#FFFFFF") // "#FFFFFF" | ||
| * val empty = "".toValidColorOrFallback("#FFFFFF") // "#FFFFFF" | ||
| * ``` | ||
| * | ||
| * @receiver The color string to validate (e.g. "#FFFFFF", "#AARRGGBB") | ||
| * @param fallback The fallback color string if invalid | ||
| * @return A valid color string | ||
| */ | ||
| fun String?.toValidColorOrFallback(fallback: String): String { | ||
| if (this.isNullOrBlank()) return fallback | ||
|
|
||
| return try { | ||
| this.toColorInt() // Validate by trying to convert | ||
| this // Valid, return same string | ||
| } catch (e: IllegalArgumentException) { | ||
| fallback | ||
| } catch (e: StringIndexOutOfBoundsException) { | ||
| fallback | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Safely parses a color string into an Android color integer. | ||
| * | ||
| * This utility provides two overloads: | ||
| * - [parseColor(colorString, defaultColor)] → returns the parsed color or a provided fallback color. | ||
| * - [parseColor(colorString)] → same as above but defaults to [Color.WHITE] on failure. | ||
| * | ||
| * Supported formats: | ||
| * ``` | ||
| * #RRGGBB | ||
| * #AARRGGBB | ||
| * ``` | ||
| * | ||
| * Example: | ||
| * ``` | ||
| * val red = parseColor("#FF0000") // Returns red | ||
| * val semiTransparent = parseColor("#80FF0000") // Returns semi-transparent red | ||
| * val invalid = parseColor("notacolor", Color.GRAY) // Returns gray | ||
| * ``` | ||
| */ | ||
| object Color { | ||
|
|
||
| /** | ||
| * Safely parses a color string into a color integer. | ||
| * | ||
| * @param colorString The color string to parse (e.g. "#FFFFFF", "#AARRGGBB"). | ||
| * @param defaultColor The fallback color if parsing fails. | ||
| * @return The parsed color integer, or [defaultColor] if invalid or null. | ||
| */ | ||
| @ColorInt | ||
| fun parseColor(colorString: String?, @ColorInt defaultColor: Int): Int { | ||
| // Return default color if the string is null or blank | ||
| if (colorString.isNullOrBlank()) { | ||
| return defaultColor | ||
| } | ||
|
|
||
| return try { | ||
| // Attempt to parse the color string | ||
| colorString.toColorInt() | ||
| } catch (e: IllegalArgumentException) { | ||
| // Thrown if the string is not a valid color format | ||
| defaultColor | ||
| } catch (e: StringIndexOutOfBoundsException) { | ||
| // Thrown if the string is malformed (e.g. incomplete hex) | ||
| defaultColor | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Parses a color string, defaulting to [Color.WHITE] if parsing fails. | ||
| * | ||
| * @param colorString The color string to parse. | ||
| * @return The parsed color integer, or [Color.WHITE] if invalid or null. | ||
| */ | ||
| @ColorInt | ||
| fun parseColor(colorString: String?): Int { | ||
| return parseColor(colorString, Color.WHITE) | ||
| } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@suriya
Making default colour as white for everywhere won't work since the text might have default colour as black
Also we have defined default colours in CTInappNotification.kt
Again defining separate default colours in the extension function will be duplicate. In the future if I have to change the default colour I will have to change it in 2 places. A developer might miss this and hence could be erroneous
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay @Anush-Shand, Doing as per the comments and making changes considering the default colors in CTInappNotification.kt.