-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
184 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// | ||
// AurumBinder.swift | ||
// Bond | ||
// | ||
// Created by Nikita Arkhipov on 03.02.2020. | ||
// | ||
|
||
import Foundation | ||
import ReactiveKit | ||
import Bond | ||
|
||
public class BaseComponentData{ | ||
let forceSetNils: Bool | ||
|
||
init(forceSetNils: Bool) { | ||
self.forceSetNils = forceSetNils | ||
} | ||
|
||
func resolve<T>(_ value: T?, resolver: (T?) -> Void){ | ||
if value != nil || forceSetNils { resolver(value) } | ||
} | ||
} | ||
|
||
public protocol UIComponent { | ||
associatedtype ComponentData | ||
associatedtype ProducedData | ||
|
||
var produced: Subject<ProducedData, Never> { get } | ||
|
||
func setup() | ||
|
||
func update(data: ComponentData) | ||
} | ||
|
||
public class BindingBase<S: AurumState, C: UIComponent>{ | ||
let extractor: (S) -> C.ComponentData | ||
let component: C | ||
|
||
init(extractor: @escaping (S) -> C.ComponentData, component: C) { | ||
self.extractor = extractor | ||
self.component = component | ||
} | ||
} | ||
|
||
public typealias BindingSetup<S: AurumState, A: AurumAction> = (AurumStore<S, A>) -> Void | ||
|
||
public class Binding<S: AurumState, C: UIComponent, A: AurumAction>{//: BindingSetupable { | ||
let extractor: (S) -> C.ComponentData | ||
let component: C | ||
let action: (C.ProducedData) -> A? | ||
|
||
init(extractor: @escaping (S) -> C.ComponentData, component: C, action: @escaping (C.ProducedData) -> A?) { | ||
self.extractor = extractor | ||
self.component = component | ||
self.action = action | ||
} | ||
|
||
func setup(store: AurumStore<S, A>) { | ||
component.setup() | ||
_ = store.state.map(extractor).observeNext { [weak self] s in | ||
self?.component.update(data: s) | ||
} | ||
component.produced.map(action).ignoreNils().bind(to: store.reducer) | ||
} | ||
} | ||
|
||
public class Bindings<S: AurumState, A: AurumAction> { | ||
let bindings: [BindingSetup<S, A>] | ||
|
||
public init(_ bindings: BindingSetup<S, A>...) { | ||
self.bindings = bindings | ||
} | ||
|
||
private init(){ | ||
bindings = [] | ||
} | ||
|
||
func setup(store: AurumStore<S, A>) { | ||
bindings.forEach { $0(store) } | ||
} | ||
|
||
public static func none<S, A>() -> Bindings<S, A>{ | ||
return Bindings<S, A>() | ||
} | ||
} | ||
|
||
|
||
public func ~><S: AurumState, C: UIComponent>(left: @escaping (S) -> C.ComponentData, right: C) -> BindingBase<S, C>{ | ||
return BindingBase(extractor: left, component: right) | ||
} | ||
|
||
public func ~><S: AurumState, C: UIComponent>(left: KeyPath<S, C.ComponentData>, right: C) -> BindingBase<S, C>{ | ||
return BindingBase(extractor: { $0[keyPath: left] }, component: right) | ||
} | ||
|
||
public func ~><S: AurumState, C: UIComponent, A: AurumAction>(left: BindingBase<S, C>, right: @escaping (C.ProducedData) -> A?) -> BindingSetup<S, A>{ | ||
return Binding(extractor: left.extractor, component: left.component, action: right).setup | ||
} | ||
|
||
public func ~><S: AurumState, C: UIComponent, A: AurumAction>(left: BindingBase<S, C>, right: A) -> BindingSetup<S, A>{ | ||
return Binding(extractor: left.extractor, component: left.component, action: { _ in right }).setup | ||
} | ||
|
||
public func ~><S: AurumState, C: UIComponent, A: AurumAction>(left: BindingBase<S, C>, right: Void) -> BindingSetup<S, A>{ | ||
return Binding(extractor: left.extractor, component: left.component, action: { _ in nil }).setup | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// ButtonComponent.swift | ||
// Bond | ||
// | ||
// Created by Nikita Arkhipov on 03.02.2020. | ||
// | ||
|
||
import UIKit | ||
import ReactiveKit | ||
import Bond | ||
|
||
public class ButtonData: BaseComponentData{ | ||
let title: String? | ||
let titleColor: UIColor? | ||
let image: UIImage? | ||
let backgroundImage: UIImage? | ||
let backgroundColor: UIColor? | ||
|
||
public init(title: String? = nil, titleColor: UIColor? = nil, image: UIImage? = nil, backgroundImage: UIImage? = nil, backgroundColor: UIColor? = nil, forceSetNils: Bool = false) { | ||
self.title = title | ||
self.titleColor = titleColor | ||
self.image = image | ||
self.backgroundImage = backgroundImage | ||
self.backgroundColor = backgroundColor | ||
super.init(forceSetNils: forceSetNils) | ||
} | ||
|
||
func update(_ b: UIButton){ | ||
resolve(title) { b.setTitle($0, for: .normal) } | ||
resolve(titleColor) { b.setTitleColor($0, for: .normal) } | ||
resolve(image) { b.setImage($0, for: .normal) } | ||
resolve(backgroundImage) { b.setBackgroundImage($0, for: .normal) } | ||
resolve(backgroundColor) { b.backgroundColor = $0 } | ||
} | ||
} | ||
|
||
extension UIButton: UIComponent{ | ||
private struct AssociatedKeys { | ||
static var Subject = "Subject" | ||
} | ||
|
||
public var produced: Subject<Void, Never> { | ||
if let produced = objc_getAssociatedObject(self, &UIButton.AssociatedKeys.Subject) { | ||
return produced as! Subject<Void, Never> | ||
} else { | ||
let produced = Subject<Void, Never>() | ||
objc_setAssociatedObject(self, &UIButton.AssociatedKeys.Subject, produced, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) | ||
return produced | ||
} | ||
} | ||
|
||
public func setup() { | ||
reactive.tap.bind(to: produced) | ||
} | ||
|
||
public func update(data: ButtonData){ | ||
data.update(self) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.