Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.DS_Store

# Xcode
.swiftpm
build/
*.pbxuser
!default.pbxuser
Expand Down
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 0 additions & 13 deletions .travis.yml

This file was deleted.

23 changes: 23 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SimpleTwoWayBinding",
platforms: [.iOS(.v10)],
products: [
.library(
name: "SimpleTwoWayBinding",
targets: ["SimpleTwoWayBinding"]),
],
dependencies: [ ],
targets: [
.target(
name: "SimpleTwoWayBinding",
dependencies: []),
.testTarget(
name: "SimpleTwoWayBindingTests",
dependencies: ["SimpleTwoWayBinding"]),
]
)
19 changes: 0 additions & 19 deletions SimpleTwoWayBinding.podspec

This file was deleted.

17 changes: 0 additions & 17 deletions Sources/BlockBasedSelector/BlockBasedSelector.h

This file was deleted.

19 changes: 0 additions & 19 deletions Sources/BlockBasedSelector/BlockBasedSelector.m

This file was deleted.

19 changes: 0 additions & 19 deletions Sources/BlockBasedSelector/BlockBasedSelector.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,44 @@ extension Bindable where Self: NSObject {

@discardableResult
public func bind(with observable: Observable<BindingType>) -> BindingReceipt {
if let _self = self as? UIControl {
_self.addTarget(Selector, action: Selector{ [weak self] in self?.valueChanged() }, for: [.editingChanged, .valueChanged])

if let control = self as? UIControl {

let memoryAddress = String(format: "%p", unsafeBitCast(self, to: UInt.self))
let sleeve = ActionClosure { [weak self] in
self?.valueChanged()
}

control.addTarget(sleeve,
action: #selector(ActionClosure.invoke),
for: [.valueChanged, .editingChanged])

objc_setAssociatedObject(control, memoryAddress, sleeve, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}

self.binder = observable
if let val = observable.value {
self.updateValue(with: val)
}

return self.observe(for: observable) { (value) in
self.updateValue(with: value)
}
}
}
}


private extension NSObject {

@objc final class ActionClosure: NSObject {
private let closure: () -> Void

init(_ closure: @escaping () -> Void) {
self.closure = closure
}

@objc func invoke() {
closure()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

extension NSObject {
@discardableResult
public func observe<T>(for observable: Observable<T>, with: @escaping (T) -> ()) -> BindingReceipt {
public func observe<T>(for observable: Observable<T>, with: @escaping (T) -> Void) -> BindingReceipt {
observable.bind { observable, value in
DispatchQueue.main.async {
with(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,40 @@ public struct BindingReceipt: Hashable, Identifiable {

public class Observable<ObservedType> {
public typealias Observer = (_ observable: Observable<ObservedType>, ObservedType) -> Void

/// Map of receipt objects to the binding blocks those objects represent; see bind(observer:) and unbind(:)
private var observers: [BindingReceipt: Observer] = [:]
/// Map of other observers we've been bound to; see map(:) & other functional conveniences. This allows us to hold strong references to the anonymous observables generated in a chained series of calls, and break them when needed.
private var bindings: [BindingReceipt: () -> Void] = [:]

internal var paused: Bool = false

public var value: ObservedType? {
didSet { fire() }
}

/// Notify all observers with the current value if non-nil.
public func fire() {
if let value = value {
notifyObservers(value)
}
}

public init(_ value: ObservedType? = nil) {
self.value = value
}

@discardableResult
public func bind(observer: @escaping Observer) -> BindingReceipt {
let r = BindingReceipt()
observers[r] = observer
return r
}

public func setObserving(_ referenceHolder: @escaping () -> Void, receipt: BindingReceipt) {
bindings[receipt] = referenceHolder
}

public func unbind(_ r: BindingReceipt) {
guard observers[r] != nil else {
print("Warning: attempted to unbind with an invalid receipt")
Expand All @@ -58,15 +58,14 @@ public class Observable<ObservedType> {
observers[r] = nil
bindings[r] = nil
}

internal func notifyObservers(_ value: ObservedType) {
observers.values.forEach { [unowned self] observer in
guard paused == false else { return }
observer(self, value)
}
}



}



}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Ryan Forsythe on 6/15/20.
//

import Foundation
import UIKit

/// A helper class to manage pausable receipts
public class ReceiptBag {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
// Created by Manish Katoch on 11/26/17.
//

import Foundation
import UIKit

extension UITextField : Bindable {
extension UITextField: Bindable {
public typealias BindingType = String

public func observingValue() -> String? {
Expand All @@ -21,7 +21,7 @@ extension UITextField : Bindable {
}
}

extension UISwitch : Bindable {
extension UISwitch: Bindable {
public typealias BindingType = Bool

public func observingValue() -> Bool? {
Expand All @@ -33,7 +33,7 @@ extension UISwitch : Bindable {
}
}

extension UISlider : Bindable {
extension UISlider: Bindable {
public typealias BindingType = Float

public func observingValue() -> Float? {
Expand All @@ -45,7 +45,7 @@ extension UISlider : Bindable {
}
}

extension UIStepper : Bindable {
extension UIStepper: Bindable {
public typealias BindingType = Double

public func observingValue() -> Double? {
Expand All @@ -57,7 +57,7 @@ extension UIStepper : Bindable {
}
}

extension UISegmentedControl : Bindable {
extension UISegmentedControl: Bindable {
public typealias BindingType = Int

public func observingValue() -> Int? {
Expand Down
8 changes: 0 additions & 8 deletions Sources/SimpleTwoWayBindings-Bridging-Header.h

This file was deleted.