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

Avoid API misusage #17

Open
amarcadet opened this issue Jan 24, 2018 · 4 comments
Open

Avoid API misusage #17

amarcadet opened this issue Jan 24, 2018 · 4 comments

Comments

@amarcadet
Copy link

First of all, congrats for this nice helper library, I once try to build something pretty similar but didn't make it, yours looks really great, simple and powerful. 👍

After using it on a recent project I have faced some issues by misusing the API.

I was looking to do a simple fade between two views instead of just hidding.

I first wrote this, but got caught by a runtime issue because type was nil here.

someObservable.bind(animated: myView.rx.animated.alpha).disposed(by: disposeBag)

After looking at your sample code I realized I wasn't using any of the default animations helpers, I just wanted a simple fade, without fade, flip, tick or animation. The .alpha sink on its own was right for my needs.

The current .animated property on its own does nothing if not used in junction with one of the default animated sink listed above.

What do you think about adding something like this in replacement (or not) of the current .animated property sink?

extension Reactive where Base: UIView {
    
    /// default animated sink on `UIView`
    public func animated(_ duration: TimeInterval) -> AnimatedSink<Base> {
        let type = AnimationType<Base>(type: RxAnimationType.animation, duration: duration, animations: nil)
        return AnimatedSink<Base>(base: self.base, type: type)
    }
}

Which give:

someObservable.bind(animated: myView.rx.animated(0.2).alpha).disposed(by: disposeBag)

I might have missed something in the API design, if that's the case sorry for inconvenience!

@icanzilb
Copy link
Member

I was thinking in a similar direction some time ago but couldn't make the auto-completion in Xcode work (even if the code was compiling and all was good) so I deviated towards the current API structure.

Do you have a working example of what you're proposing? I'd be curious to look at it

@amarcadet
Copy link
Author

Unfortunately this is private project, all I can do is extract a small chunk of code like above or create a sample project if that can help. I might have more time later this week to provide a working sample or I can fork and PR, what do you prefer?

Code completion was hard to get, Xcode failed until I figured it out how I should write it (even with version 0.4.1). I'm using Xcode 9.2 on Sierra.

@icanzilb
Copy link
Member

If you have the time to grab that piece of code and shove it into an Xcode project I'll have a look 👍

@DanielAsher
Copy link

Hi,
@icanzilb I just ran into the same API misuse, also wanting to simply animate alpha, briefly baffled by the fatal error caused by the forced unwrap in:

public struct AnimatedSink<Base> {
    public var type: AnimationType<Base>!
    public var base: Base
    public init(base: Base, type: AnimationType<Base>? = nil) {
        self.base = base
        self.type = type
    }
}

@amarcadet's solution works for me and with Xcode 9.4 code completion is working well. I would recommend adding this.

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

No branches or pull requests

3 participants