Skip to content
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

fix(Button): Make Button a UIControl BREAKING CHANGE: Button.State enum has changed to UIControl.State. #79

Merged
merged 19 commits into from
Feb 16, 2021

Conversation

jmpg93
Copy link
Contributor

@jmpg93 jmpg93 commented Feb 5, 2021

Hi there!

This review is about making Button to inherit from UIControl. It will fix a few bugs we have right now regarding addTarget method (which tried to simulate UIControl usage). It will also simplify the current solution.

I have to change the current implementation to move from a single state property to a group of properties which computes the state of the Button. Meaning that we will move from this button.state = .loading to this button.isLoading = true.

Also, I've added new tests to check that the button stays the same when toggling those new properties: isEnabled, isSelected, and isLoading.

Enjoy.

@jmpg93 jmpg93 changed the title fix(Button): Make Button a UIControl BREAKING CHANGE: The existing font style has changed. fix(Button): Make Button a UIControl BREAKING CHANGE: The Button.State enum has change to UIControl.State. Feb 5, 2021
@jmpg93 jmpg93 changed the title fix(Button): Make Button a UIControl BREAKING CHANGE: The Button.State enum has change to UIControl.State. fix(Button): Make Button a UIControl BREAKING CHANGE: Button.State enum has changed to UIControl.State. Feb 5, 2021

private lazy var animator = UIViewPropertyAnimator(
duration: Constants.animationDuration,
controlPoint1: Constants.animationCurveControlPoint1,
controlPoint2: Constants.animationCurveControlPoint2
)

private lazy var backingButton = BackingButton()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for backingButton anymore.

Comment on lines +155 to +165
override open var state: UIControl.State {
if isLoading {
return .loading
} else if !isEnabled {
return .disabled
} else if isSelected || isHighlighted {
return .selected
} else {
return .normal
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

State is now a computed property.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only thing that bothers me is being able to set invalid combinations, like "isLoading=true" and "isEnabled=true"... or "isSelected=true" and "isEnabled=false", our component doesn't support it so it is what it is...

Comment on lines +167 to +171
override open func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
guard !isLoading else { return nil }
return super.hitTest(point, with: event)
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to "disable" the button when it is loading.

Comment on lines -172 to 188
override func accessibilityActivate() -> Bool {
if state.shouldBackingButtonBeEnabled {
backingButton.sendActions(for: .touchUpInside)
return true
} else {
return false
}
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed. The UIContol handles all this for us, based on the state property.

Comment on lines -270 to +283
} else if previousState == .loading {
} else if isShowingLoadingAnimation {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this isShowingLoadingAnimation to remove the concept of previousState.

@jmpg93 jmpg93 marked this pull request as ready for review February 9, 2021 09:03
pbartolome
pbartolome previously approved these changes Feb 10, 2021
}

func objc_setDisabledState() {
state = .disabled
isEnabled = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this also change "isLoading" to false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer not (same reason as here #79 (comment))

Comment on lines +155 to +165
override open var state: UIControl.State {
if isLoading {
return .loading
} else if !isEnabled {
return .disabled
} else if isSelected || isHighlighted {
return .selected
} else {
return .normal
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only thing that bothers me is being able to set invalid combinations, like "isLoading=true" and "isEnabled=true"... or "isSelected=true" and "isEnabled=false", our component doesn't support it so it is what it is...

JulianAlonso
JulianAlonso previously approved these changes Feb 10, 2021
jmbrocal
jmbrocal previously approved these changes Feb 10, 2021
Copy link
Contributor

@jmbrocal jmbrocal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

200

didSet {
didUpdateState()
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should modify the isLoading flag when setting one of these to false/true,

E.g. isEnabled=true, isLoading=true -> we set isEnabled=false -> the button won't change. In this scenario, would it make sense to se set isLoading=false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer that changing the value of one property does not affect the others. Enable and disable are different things from loading and not loading. If the button is loading and you want to stop loading, you must set the isLoading property to false, that's it. If you set the isEnabled property to false while loading, the button will be disabled as soon as you set the isLoading property back to false. I do not see anything wrong here.

However, I dot no have a strong opinion here. I will do you what you guys think is better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense, it was just something that I found weird, but it can work as you said, thanks!

Copy link
Contributor

@jmbrocal jmbrocal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

200

@jmpg93 jmpg93 merged commit 4b57222 into main Feb 16, 2021
@jmpg93 jmpg93 deleted the apps/button-to-uicontrol branch February 16, 2021 14:21
tuentisre pushed a commit that referenced this pull request Feb 17, 2021
# [9.0.0](v8.0.1...v9.0.0) (2021-02-17)

### Bug Fixes

* **Button:** Make Button a UIControl  BREAKING CHANGE: Button.State enum has changed to UIControl.State. ([#79](#79)) ([4b57222](4b57222))
* **ColorPalette:** fix backgroundPromo color of O2ClassicColorPalette ([#75](#75)) ([fd87da0](fd87da0))
* **DataCard:** fix border color of card ([cb4c3b0](cb4c3b0))
* **HighlightedCard:** add support for xib and storyboard ([5e3afa7](5e3afa7))
* **HighlightedCard:** fix showActionButton and showCloseButton so that the value of the variables makes sense in English ([b6a2921](b6a2921))
* **HighlightedCard:** nake init with parameters: `(title:subtitle:rightImage:actionButtonStyle:)` configures the correct styles. ([31f2bf6](31f2bf6))
* **HighlightedCard:** set multi lines support for both texts (title and subtitle) ([#77](#77)) ([657a3bc](657a3bc))
* **HighlightedCard,MediaCard,MediaCard,Stepper:** fix border color by replacing "divider" by "border" of the color palette and set the border size to 1 px ([66e4aae](66e4aae))
* **MediaCard:** fix border color of card and vertical spacing after headline ([f62b6f2](f62b6f2))
* **Stepper:**  Update Lottie stroke width ([#76](#76)) ([0c9a35f](0c9a35f))

### BREAKING CHANGES

* **HighlightedCard:** texts (title and subtitle) of HighlightedCard have no restrictions of number of lines and both add support multi lines.
@tuentisre
Copy link
Collaborator

🎉 This PR is included in version 9.0.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants