RxCombine icon indicating copy to clipboard operation
RxCombine copied to clipboard

Bridging RxSwift to SwiftUI

Open jlevine22 opened this issue 5 years ago • 5 comments

Haven't really been able to test this out as I've been fighting with Xcode 11 and just some general bugginess but here's a first stab at this:

class Bound<Value>: BindableObject {
    let bag = DisposeBag()

    var value: Value {
        didSet {
            didChange.send(value)
        }
    }
    
    let didChange = PassthroughSubject<Value, Never>()
    
    init(_ value: Value) { self.value = value }
}

extension Observable {
    func asBindableObject(initialValue: Element) -> Bound<Element> {
        let bound = Bound(initialValue)
        subscribe(onNext: { [weak bound] in bound?.value = $0 }).disposed(by: bound.bag)
        return bound
    }
}

Thoughts?

jlevine22 avatar Jun 16 '19 00:06 jlevine22

Just to follow up, I was able to use this successfully in this example project here: https://github.com/jlevine22/rxswift-to-swiftui

jlevine22 avatar Jun 16 '19 01:06 jlevine22

Hey there! Thanks, I'll take a deeper look throughout the week :)

freak4pc avatar Jun 16 '19 18:06 freak4pc

@jlevine22 Only took me 6 months to get to this :( So sorry, it's been a bit hectic. The basic concepts outlined in your repo look promising! Would you want to make a more thorough PR with these capabilities?

freak4pc avatar Jan 19 '20 19:01 freak4pc

Any updates on this?

ivnsch avatar Apr 28 '20 16:04 ivnsch

Happy to accept a more fleshed out PR to look at. I personally don't have the time to look into it ATM :)

freak4pc avatar May 04 '20 04:05 freak4pc