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

Allow subclassing AnimatedSwitch and Button #1431

Merged
merged 1 commit into from Dec 9, 2021
Merged

Conversation

jandamm
Copy link
Contributor

@jandamm jandamm commented Dec 9, 2021

In order to create a AnimatedSwitch which applies the
traitCollection either a subclass of AnimatedControl and
reimplementing all the switch logic or creating a wrapper view and
exposing the AnimatedControl has to be done.
Same is true for AnimatedButton.

final class ThemedAnimatedSwitch: UIView {
    let toggle: AnimatedSwitch
    let redValueProvider = ColorValueProvider(Color(r: 1, g: 0.2, b: 0.3, a: 1))

    init(frame: CGRect) {
        toggle = .init(animation: animation)
        super.init(frame: frame)

        toggle.setValueProvider(valueProvider, keypath: AnimationKeypath(keypath: "**.Fill 1.Color"))
    }

    required init?(coder: NSCoder) { ... }
}

Then use animatedSwitch.toggle.


final class TestSwitch: AnimatedSwitch {
    let redValueProvider = ColorValueProvider(Color(r: 1, g: 0.2, b: 0.3, a: 1))

    convenience override init() {
        self.init(animation: animation)

        setValueProvider(valueProvider, keypath: AnimationKeypath(keypath: "**.Fill 1.Color"))
    }
}

Then use animatedSwitch directly.

Since the public interface of AnimatedSwitch is public instead of
open functions like func setIsOn(_ isOn: Bool, animated: Bool, shouldFireHaptics: Bool = true) cannot be overridden.

Supersedes #1359

In order to create a `AnimatedSwitch` which applies the
`traitCollection` either a subclass of `AnimatedControl` and
reimplementing all the switch logic or creating a wrapper view and
exposing the `AnimatedControl` has to be done.
Same is true for `AnimatedButton`.

```swift
final class ThemedAnimatedSwitch: UIView {
    let toggle: AnimatedSwitch
    let redValueProvider = ColorValueProvider(Color(r: 1, g: 0.2, b: 0.3, a: 1))

    init(frame: CGRect) {
        toggle = .init(animation: animation)
        super.init(frame: frame)

        toggle.setValueProvider(valueProvider, keypath: AnimationKeypath(keypath: "**.Fill 1.Color"))
    }

    required init?(coder: NSCoder) { ... }
}
```

Then use `animatedSwitch.toggle`.

---

```swift
final class TestSwitch: AnimatedSwitch {
    let redValueProvider = ColorValueProvider(Color(r: 1, g: 0.2, b: 0.3, a: 1))

    convenience override init() {
        self.init(animation: animation)

        setValueProvider(valueProvider, keypath: AnimationKeypath(keypath: "**.Fill 1.Color"))
    }
}
```

Then use `animatedSwitch` directly.

Since the public interface of `AnimatedSwitch` is `public` instead of
`open` functions like `func setIsOn(_ isOn: Bool, animated: Bool,
shouldFireHaptics: Bool = true)` cannot be overridden.

Supersedes airbnb#1359
@jandamm
Copy link
Contributor Author

jandamm commented Dec 9, 2021

@calda please have a look :)

Copy link
Member

@calda calda left a comment

Choose a reason for hiding this comment

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

Thanks!

@calda calda merged commit b0260e6 into airbnb:master Dec 9, 2021
calda pushed a commit that referenced this pull request Nov 28, 2022
calda pushed a commit that referenced this pull request Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants