Skip to content

Commit

Permalink
fix: changed ViewUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
xeewii committed Jun 17, 2024
1 parent 1471bea commit 1c7a3b6
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ import com.personalizatio.stories.models.elements.ProductsElement
import com.personalizatio.stories.models.elements.TextBlockElement
import com.personalizatio.stories.viewAdapters.ProductsAdapter
import com.personalizatio.stories.views.StoriesView
import com.personalizatio.utils.ViewUtils.getColor
import com.personalizatio.utils.ViewUtils.setBackgroundColor
import com.personalizatio.utils.ViewUtils.setTextColor
import com.personalizatio.ui.utils.ViewUtils

@SuppressLint("ViewConstructor")
class StoryItemView(private val storiesView: StoriesView) : ConstraintLayout(storiesView.context) {
Expand Down Expand Up @@ -199,7 +197,7 @@ class StoryItemView(private val storiesView: StoriesView) : ConstraintLayout(sto
fun update(slide: Slide, position: Int, code: String, storyId: Int) {
slide.isPrepared = false

setBackgroundColor(getColor(context, slide.backgroundColor, android.R.color.black))
setBackgroundColor(ViewUtils.getColor(context, slide.backgroundColor, android.R.color.black))

video.visibility = GONE

Expand Down Expand Up @@ -505,8 +503,8 @@ class StoryItemView(private val storiesView: StoriesView) : ConstraintLayout(sto
button.visibility = VISIBLE
button.text = element.title

setBackgroundColor(context, button, element.background, R.color.primary)
setTextColor(context, button, element.color, R.color.white)
ViewUtils.setBackgroundColor(context, button, element.background, R.color.primary)
ViewUtils.setTextColor(context, button, element.color, R.color.white)

button.typeface = Typeface.create(
storiesView.settings.button_font_family,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import android.annotation.SuppressLint
import android.content.Context
import android.widget.FrameLayout
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.res.ResourcesCompat
import com.personalizatio.R
import com.personalizatio.stories.models.elements.TextBlockElement
import com.personalizatio.utils.ViewUtils
import com.personalizatio.ui.utils.ViewUtils

class TextBlockView(context: Context) : AppCompatTextView(context) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package com.personalizatio.utils
package com.personalizatio.ui.utils

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.Typeface
import android.os.Build
import android.widget.Button
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.FontRes
import androidx.core.content.res.ResourcesCompat
import com.personalizatio.R

object ViewUtils {

const val MAX_COLOR_CHANNEL_VALUE = 255
private const val MAX_COLOR_CHANNEL_VALUE = 255

@FontRes
fun getFontRes(fontType: String, bold: Boolean, italic: Boolean): Int {
fun getFontRes(
fontType: String,
bold: Boolean,
italic: Boolean
): Int {
return when (fontType) {
"serif" -> {
when {
Expand Down Expand Up @@ -54,7 +63,12 @@ object ViewUtils {
return MAX_COLOR_CHANNEL_VALUE * percents / 100
}

fun setBackgroundColor(view: AppCompatTextView, colorString: String, colorOpacity: Int, context: Context) {
fun setBackgroundColor(
view: TextView,
colorString: String,
colorOpacity: Int,
context: Context
) {
var correctColorString = colorString
if (!colorString.startsWith("#")) {
correctColorString = "#FFFFFF"
Expand All @@ -69,11 +83,64 @@ object ViewUtils {
view.setBackgroundColor(color)
}

fun getColor(context: Context, colorString: String, defaultColorRes: Int): Int {
fun setBackgroundColor(
context: Context,
button: Button,
colorString: String?,
@ColorRes defaultColor: Int
) {
val backgroundColor = getColor(context, colorString, defaultColor)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
button.backgroundTintList = ColorStateList.valueOf(backgroundColor)
} else {
button.setBackgroundColor(backgroundColor)
}
}

fun getColor(
context: Context,
colorString: String?,
@ColorRes defaultColorRes: Int
): Int {
return try {
android.graphics.Color.parseColor(colorString)
Color.parseColor(colorString)
} catch (e: IllegalArgumentException) {
context.getColor(defaultColorRes)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
context.getColor(defaultColorRes)
} else {
context.resources.getColor(defaultColorRes)
}
}
}

fun setTextColor(
context: Context,
textView: TextView,
colorString: String?,
@ColorRes defaultColor: Int
) {
val color = getColor(context, colorString, defaultColor)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textView.setTextColor(ColorStateList.valueOf(color))
} else {
textView.setTextColor(color)
}
}

fun setTextColor(
context: Context,
button: Button,
colorString: String?,
@ColorRes defaultColor: Int
) {
val color = getColor(context, colorString, defaultColor)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
button.setTextColor(ColorStateList.valueOf(color))
} else {
button.setTextColor(color)
}
}

Expand Down

This file was deleted.

0 comments on commit 1c7a3b6

Please sign in to comment.