From b1772e7f612174a61ea63e812459f6c5e4453674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Mart=C3=ADn?= Date: Wed, 6 Nov 2024 10:52:53 +0100 Subject: [PATCH 1/4] Fix acceptance errors of v15.0.1 --- .../mistica/button/ProgressButton.kt | 141 ++++++++---------- 1 file changed, 59 insertions(+), 82 deletions(-) diff --git a/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt b/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt index e071ca0a3..e4405f708 100644 --- a/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt +++ b/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt @@ -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 = @@ -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 @@ -162,9 +94,7 @@ class ProgressButton : FrameLayout { ) showTextOnly() } - } - private fun setupButtonLoading() { buttonLoading.apply { id = NO_ID importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO @@ -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 { @@ -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 @@ -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() { From e7bf4e84670f1c68f3ff325b2665b30d280148bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Mart=C3=ADn?= Date: Wed, 6 Nov 2024 12:42:38 +0100 Subject: [PATCH 2/4] Remove only performClick call --- .../mistica/button/ProgressButton.kt | 141 ++++++++++-------- 1 file changed, 82 insertions(+), 59 deletions(-) diff --git a/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt b/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt index e4405f708..2600ec602 100644 --- a/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt +++ b/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt @@ -66,8 +66,72 @@ 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 = @@ -78,13 +142,17 @@ 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 @@ -94,7 +162,9 @@ class ProgressButton : FrameLayout { ) showTextOnly() } + } + private fun setupButtonLoading() { buttonLoading.apply { id = NO_ID importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO @@ -106,7 +176,9 @@ 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 { @@ -115,9 +187,12 @@ class ProgressButton : FrameLayout { importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO isIndeterminate = true indeterminateTintMode = PorterDuff.Mode.SRC_IN - visibility = View.INVISIBLE + visibility = INVISIBLE } + } + @SuppressLint("ClickableViewAccessibility") + private fun setButtonBackground() { buttonBackground.apply { id = NO_ID importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_YES @@ -127,19 +202,21 @@ class ProgressButton : FrameLayout { ) icon = null setTextColor(Color.TRANSPARENT) - visibility = View.VISIBLE + visibility = 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 @@ -148,67 +225,13 @@ class ProgressButton : FrameLayout { 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() { From 3dfb5328339888224220484ad592e5f7091b12b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Mart=C3=ADn?= Date: Wed, 6 Nov 2024 13:22:59 +0100 Subject: [PATCH 3/4] Indicate eventConsumed when MotionEvent.ACTION_UP --- .../mistica/button/ProgressButton.kt | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt b/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt index 2600ec602..ab5256bca 100644 --- a/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt +++ b/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt @@ -191,7 +191,6 @@ class ProgressButton : FrameLayout { } } - @SuppressLint("ClickableViewAccessibility") private fun setButtonBackground() { buttonBackground.apply { id = NO_ID @@ -204,29 +203,38 @@ class ProgressButton : FrameLayout { setTextColor(Color.TRANSPARENT) visibility = VISIBLE setOnTouchListener { view, event -> + var eventConsumed = false when (event.action) { MotionEvent.ACTION_DOWN -> { - buttonNormal.isPressed = true - buttonLoading.isPressed = true + setButtonPressed(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 + setButtonPressed(!outsideBounds) + } + + MotionEvent.ACTION_UP -> { + setButtonPressed(false) + view.performClick() + eventConsumed = true } - MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { - buttonNormal.isPressed = false - buttonLoading.isPressed = false + MotionEvent.ACTION_CANCEL -> { + setButtonPressed(false) } } - false + eventConsumed } } } + private fun setButtonPressed(pressed: Boolean) { + buttonNormal.isPressed = pressed + buttonLoading.isPressed = pressed + } + private fun addViews() { addView(buttonBackground) addView(buttonNormal) From cb9f8049a06795a21b53abe4ce413f593d6c0183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Mart=C3=ADn?= Date: Wed, 6 Nov 2024 13:37:19 +0100 Subject: [PATCH 4/4] Remove performClick and add SuppressLint --- .../mistica/button/ProgressButton.kt | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt b/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt index ab5256bca..2600ec602 100644 --- a/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt +++ b/library/src/main/java/com/telefonica/mistica/button/ProgressButton.kt @@ -191,6 +191,7 @@ class ProgressButton : FrameLayout { } } + @SuppressLint("ClickableViewAccessibility") private fun setButtonBackground() { buttonBackground.apply { id = NO_ID @@ -203,38 +204,29 @@ class ProgressButton : FrameLayout { setTextColor(Color.TRANSPARENT) visibility = VISIBLE setOnTouchListener { view, event -> - var eventConsumed = false when (event.action) { MotionEvent.ACTION_DOWN -> { - setButtonPressed(true) + 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 - setButtonPressed(!outsideBounds) - } - - MotionEvent.ACTION_UP -> { - setButtonPressed(false) - view.performClick() - eventConsumed = true + buttonNormal.isPressed = !outsideBounds + buttonLoading.isPressed = !outsideBounds } - MotionEvent.ACTION_CANCEL -> { - setButtonPressed(false) + MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { + buttonNormal.isPressed = false + buttonLoading.isPressed = false } } - eventConsumed + false } } } - private fun setButtonPressed(pressed: Boolean) { - buttonNormal.isPressed = pressed - buttonLoading.isPressed = pressed - } - private fun addViews() { addView(buttonBackground) addView(buttonNormal)