Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
fix: crash when malformed color is provided (#492)
Browse files Browse the repository at this point in the history
* Set log when malformed color is provided

* fixies

* Fix message log
  • Loading branch information
danilorochazup authored Jul 10, 2020
1 parent 6653e1c commit 91b51e9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ data class Text(
override fun buildView(rootView: RootView): View {
val textView = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
viewFactory.makeTextView(rootView.getContext(), getStyleId(this.styleId))
else viewFactory.makeTextView(rootView.getContext())
else viewFactory.makeTextView(rootView.getContext())

textView.setTextWidget(this, rootView)
return textView
Expand Down Expand Up @@ -99,12 +99,10 @@ data class Text(
}
}

private fun getStyleId(styleName: String?) : Int =
BeagleEnvironment.beagleSdk.designSystem?.textStyle(styleName ?:"")?:0
private fun getStyleId(styleName: String?): Int =
BeagleEnvironment.beagleSdk.designSystem?.textStyle(styleName ?: "") ?: 0

private fun TextView.setTextColor(color: String?) {
color?.let {
this.setTextColor(color.toAndroidColor())
}
color?.toAndroidColor()?.let { androidColor -> this.setTextColor(androidColor) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ internal fun View.applyViewBackgroundAndCorner(backgroundColor: Int?, component:
}

internal fun View.applyBackgroundColor(styleWidget: StyleComponent) {
styleWidget.style?.backgroundColor?.let {
(this.background as? GradientDrawable)?.setColor(it.toAndroidColor())
styleWidget.style?.backgroundColor?.toAndroidColor()?.let { androidColor ->
(this.background as? GradientDrawable)?.setColor(androidColor)
}

}

internal fun View.applyCornerRadius(styleWidget: StyleComponent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,9 @@ internal object BeagleMessageLogs {
val errorMessage = "You are trying to use multiple expressions in a type that is not string!"
BeagleLoggerProxy.warning(errorMessage)
}

fun errorWhenMalformedColorIsProvided(color: String, ex: Exception) {
val errorMessage = "Could not parses color $color"
BeagleLoggerProxy.error(errorMessage, ex)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@

package br.com.zup.beagle.android.utils

internal fun String.toAndroidColor(): Int = ColorUtils.hexColor(this)
import br.com.zup.beagle.android.logger.BeagleLoggerProxy
import br.com.zup.beagle.android.logger.BeagleMessageLogs

internal fun String.toAndroidColor(): Int? = try {
ColorUtils.hexColor(this)
} catch (ex: Exception) {
BeagleMessageLogs.errorWhenMalformedColorIsProvided(this, ex)
null
}

internal fun String.getContextId() = this.split(".", "[")[0]

Expand Down

0 comments on commit 91b51e9

Please sign in to comment.