Skip to content

Commit

Permalink
Fix acceptance errors of v15.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
pmartinbTEF committed Nov 6, 2024
1 parent 9dfcaa2 commit b1772e7
Showing 1 changed file with 59 additions and 82 deletions.
141 changes: 59 additions & 82 deletions library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,72 +66,8 @@ class ProgressButton : FrameLayout {
}

@SuppressLint("ClickableViewAccessibility")
@Suppress("LongMethod")
private fun init(attrs: AttributeSet? = null, defStyleAttr: Int = 0) {
setupAttributes(attrs, defStyleAttr)
setupViewProperties()
setupButtonNormal()
setupButtonLoading()
setupProgressBar()
setButtonBackground()
addViews()
setVisibilityAndColors()
}

fun getText(): CharSequence =
buttonBackground.text

fun setText(text: CharSequence) {
buttonNormal.text = text
if (!isLoading) {
buttonBackground.text = text
}
}

fun setText(@StringRes textId: Int) {
setText(context.getString(textId))
}

fun setLoadingText(text: CharSequence) {
buttonLoading.text = text
if (isLoading) {
buttonBackground.text = text
}
}

override fun setOnClickListener(l: OnClickListener?) {
buttonBackground.setOnClickListener(l)
}

override fun setEnabled(enabled: Boolean) {
super.setEnabled(enabled)
setAlpha(enabled)
buttonBackground.isEnabled = enabled
buttonNormal.isEnabled = enabled
buttonLoading.isEnabled = enabled
progressBar.isEnabled = enabled
}

fun setIsLoading(loading: Boolean) {
if (loading) {
showLoading()
} else {
hideLoading()
}
}

fun showLoading() {
if (!isLoading) {
switchState()
}
}

fun hideLoading() {
if (isLoading) {
switchState()
}
}

private fun setupAttributes(attrs: AttributeSet?, defStyleAttr: Int) {
if (attrs != null) {
val theme = context.theme
val styledAttrs =
Expand All @@ -142,17 +78,13 @@ class ProgressButton : FrameLayout {
styledAttrs.recycle()
}
}
}

private fun setupViewProperties() {
this.importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO
isClickable = false
setPadding(0, 0, 0, 0)
setBackgroundColor(Color.TRANSPARENT)
originalTextColors = buttonNormal.textColors
}

private fun setupButtonNormal() {
buttonNormal.apply {
id = NO_ID
importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO
Expand All @@ -162,9 +94,7 @@ class ProgressButton : FrameLayout {
)
showTextOnly()
}
}

private fun setupButtonLoading() {
buttonLoading.apply {
id = NO_ID
importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO
Expand All @@ -176,9 +106,7 @@ class ProgressButton : FrameLayout {
icon = AppCompatResources.getDrawable(context, R.drawable.empty_material_button_icon)
text = ""
}
}

private fun setupProgressBar() {
progressBar.apply {
layoutParams = LayoutParams(buttonLoading.iconSize, buttonLoading.iconSize)
.apply {
Expand All @@ -187,11 +115,9 @@ class ProgressButton : FrameLayout {
importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO
isIndeterminate = true
indeterminateTintMode = PorterDuff.Mode.SRC_IN
visibility = INVISIBLE
visibility = View.INVISIBLE
}
}

private fun setButtonBackground() {
buttonBackground.apply {
id = NO_ID
importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_YES
Expand All @@ -201,37 +127,88 @@ class ProgressButton : FrameLayout {
)
icon = null
setTextColor(Color.TRANSPARENT)
visibility = VISIBLE
visibility = View.VISIBLE
setOnTouchListener { view, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
buttonNormal.isPressed = true
buttonLoading.isPressed = true
}

MotionEvent.ACTION_MOVE -> {
val outsideBounds =
event.x < 0 || event.y < 0 || event.x > view.measuredWidth || event.y > view.measuredHeight
buttonNormal.isPressed = !outsideBounds
buttonLoading.isPressed = !outsideBounds
}

MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
buttonNormal.isPressed = false
buttonLoading.isPressed = false
view.performClick()
}
}
false
}
}
}

private fun addViews() {
addView(buttonBackground)
addView(buttonNormal)
addView(buttonLoading)
addView(progressBar)

setVisibilityAndColors()
}

fun getText(): CharSequence =
buttonBackground.text

fun setText(text: CharSequence) {
buttonNormal.text = text
if (!isLoading) {
buttonBackground.text = text
}
}

fun setText(@StringRes textId: Int) {
setText(context.getString(textId))
}

fun setLoadingText(text: CharSequence) {
buttonLoading.text = text
if (isLoading) {
buttonBackground.text = text
}
}

override fun setOnClickListener(l: OnClickListener?) {
buttonBackground.setOnClickListener(l)
}

override fun setEnabled(enabled: Boolean) {
super.setEnabled(enabled)
setAlpha(enabled)
buttonBackground.isEnabled = enabled
buttonNormal.isEnabled = enabled
buttonLoading.isEnabled = enabled
progressBar.isEnabled = enabled
}

fun setIsLoading(loading: Boolean) {
if (loading) {
showLoading()
} else {
hideLoading()
}
}

fun showLoading() {
if (!isLoading) {
switchState()
}
}

fun hideLoading() {
if (isLoading) {
switchState()
}
}

private fun switchState() {
Expand Down

0 comments on commit b1772e7

Please sign in to comment.