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

Feature/upgrade RAC #44

Merged
merged 52 commits into from Aug 30, 2015
Commits
File filter...
Filter file types
Jump to file or symbol
Failed to load files and symbols.
+2,428 −545
Diff settings

Always

Just for now

Copy path View file
@@ -1,3 +1,27 @@
BrewMobile.xcodeproj/xcuserdata/
BrewMobile.xcworkspace/xcuserdata/
Carthage/Build
## Build generated
build/
DerivedData

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata

## Other
*.xccheckout
*.moved-aside
*.xcuserstate
*.xcscmblueprint

## Obj-C/Swift specific
*.hmap
*.ipa

# Carthage
Carthage/Build

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -72,7 +72,8 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "692AAA8219A34C8C00224C4D"
@@ -82,6 +83,11 @@
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
<AdditionalOption
key = "NSZombieEnabled"
value = "YES"
isEnabled = "YES">
</AdditionalOption>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
@@ -90,7 +96,8 @@
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<BuildableProductRunnable>
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "692AAA8219A34C8C00224C4D"
Copy path View file
@@ -19,36 +19,37 @@ let pwmChangedEvent = "pwm_changed"
class BrewManager : NSObject {
let host = "http://brewcore-demo.herokuapp.com/"

var stopBrewCommand: RACCommand!
var syncBrewCommand: RACCommand!
let tempChangedSignal: RACSubject
let brewChangedSignal: RACSubject
let pwmChangedSignal: RACSubject
var syncBrewAction: Action<BrewState, NSData, NSError>!
var stopBrewAction: Action<Void, NSData, NSError>!
let temp = MutableProperty<Float>(0.0)
let brew = MutableProperty(BrewState())
let pwm = MutableProperty<Float>(0.0)

override init() {
tempChangedSignal = RACSubject()
brewChangedSignal = RACSubject()
pwmChangedSignal = RACSubject()

super.init()
super.init()

syncBrewCommand = RACCommand(signalBlock: { (brewObject: AnyObject!) -> RACSignal! in
let requestResult = self.requestWithBody("api/brew", method: "POST", body: JSON(brewObject))
if let request = requestResult.value as NSMutableURLRequest? {
return NSURLConnection.rac_sendAsynchronousRequest(request)
syncBrewAction = Action { brewState in
if let jsonData:AnyObject = BrewState.encode(brewState).value {
let requestResult = self.requestWithBody("api/brew", method: "POST", body: JSON(jsonData))
if let requestResultValue = requestResult.value {
return NSURLSession.sharedSession().rac_dataWithRequest(requestResultValue)
|> map { data, URLResponse in
return data
}
}
}

if let serializationError = requestResult.error {
return RACSignal.error(serializationError as NSError)
fatalError("jsonData is nil")
}

stopBrewAction = Action { brewState in
if let request = self.requestWithBody("api/brew/stop", method: "PATCH", body: "").value {
return NSURLSession.sharedSession().rac_dataWithRequest(request)
|> map { data, URLResponse in
return data
}
}

return RACSignal.empty()
})

stopBrewCommand = RACCommand(signalBlock: { Void -> RACSignal! in
let request = self.requestWithBody("api/brew/stop", method: "PATCH", body: "").value!
return NSURLConnection.rac_sendAsynchronousRequest(request)
})
fatalError("request is nil")
}
}

//Mark: HTTP
@@ -61,8 +62,8 @@ class BrewManager : NSObject {
request.HTTPMethod = method
if method == "POST" {
request.HTTPBody = body.rawData(options: .PrettyPrinted, error: &serializationError)
if serializationError != nil {
return Result.failure(serializationError!)
if let error = serializationError {
return Result.failure(error)
}
}

@@ -82,22 +83,24 @@ class BrewManager : NSObject {
}

socket.on(tempChangedEvent, callback: { (AnyObject data) -> Void in
if(count(data) > 0) {
let temp = data[0] as! NSNumber
self.tempChangedSignal.sendNext(temp)
if (count(data) > 0) {
if let temp = data[0] as? NSNumber {
self.temp.put(temp.floatValue)
}
}
})

socket.on(brewChangedEvent, callback: { (AnyObject data) -> Void in
if(count(data) > 0) {
self.brewChangedSignal.sendNext([brewChangedEvent: ContentParser.parseBrewState(JSON(data[0]))])
if (count(data) > 0) {
self.brew.put(ContentParser.parseBrewState(JSON(data[0])))
}
})

socket.on(pwmChangedEvent, callback: { (AnyObject data) -> Void in
if(count(data) > 0) {
let pwm = data[0] as! NSNumber
self.pwmChangedSignal.sendNext(pwm)
if (count(data) > 0) {
if let pwm = data[0] as? NSNumber {
self.pwm.put(pwm.floatValue)
}
}
})

Copy path View file
@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.1.1</string>
<string>1.3.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
@@ -110,7 +110,7 @@ final class BrewPhase: Equatable, JSONDecodable, JSONEncodable {
// MARK: JSONEncodable
class func encode(object: BrewPhase) -> Result<AnyObject, NSError> {
var phase = Dictionary<String, AnyObject>()
var phase = [String: AnyObject]()

phase["min"] = Int(object.min)
phase["temp"] = Float(object.temp)
@@ -7,45 +7,61 @@
//
import Foundation
import ReactiveCocoa
import Result
import SwiftyJSON

// MARK: Equatable
func == (left: BrewState, right: BrewState) -> Bool {
let phasesAreIdentical = { () -> Bool in
for i in 0...left.phases.count - 1 {
if left.phases[i] != right.phases[i] {
for i in 0...left.phases.value.count - 1 {
if left.phases.value[i] != right.phases.value[i] {
return false
}
}
return true
}()

return (left.name == right.name) && (left.startTime == right.startTime) && phasesAreIdentical && (left.paused == right.paused) && (left.inProgress == right.inProgress)
return (left.name.value == right.name.value) &&
(left.startTime.value == right.startTime.value) &&
phasesAreIdentical &&
(left.paused.value == right.paused.value) &&
(left.inProgress.value == right.inProgress.value)
}

final class BrewState: Equatable, JSONDecodable, JSONEncodable {
var name: String
var startTime: String
var phases: PhaseArray
var paused: Bool
var inProgress: Bool
var name: MutableProperty<String>
var startTime: MutableProperty<String>
var phases: MutableProperty<PhaseArray>
var paused: MutableProperty<Bool>
var inProgress: MutableProperty<Bool>

init() {
name = ""
startTime = ""
phases = PhaseArray()
paused = false
inProgress = false
name = MutableProperty("")
startTime = MutableProperty("")
phases = MutableProperty(PhaseArray())
paused = MutableProperty(false)
inProgress = MutableProperty(false)
}

init(name: String, startTime: String, phases: PhaseArray, paused: Bool, inProgress: Bool) {
self.name = MutableProperty(name)
self.startTime = MutableProperty(startTime)
self.phases = MutableProperty(phases)
self.paused = MutableProperty(paused)
self.inProgress = MutableProperty(inProgress)
}

init(name: MutableProperty<String>,
startTime: MutableProperty<String>,
phases: MutableProperty<PhaseArray>,
paused: Bool, inProgress: Bool) {
self.name = name
self.startTime = startTime
self.phases = phases
self.paused = paused
self.inProgress = inProgress
self.paused = MutableProperty(paused)
self.inProgress = MutableProperty(inProgress)
}

// MARK: JSONDecodable
@@ -65,12 +81,12 @@ final class BrewState: Equatable, JSONDecodable, JSONEncodable {
// MARK: JSONEncodable
class func encode(object: BrewState) -> Result<AnyObject, NSError> {
var brew = Dictionary<String, AnyObject>()
var brew = [String: AnyObject]()

brew["name"] = object.name
brew["startTime"] = object.startTime
brew["name"] = object.name.value
brew["startTime"] = object.startTime.value

brew["phases"] = object.phases.map { (BrewPhase phase) -> AnyObject in
brew["phases"] = object.phases.value.map { (BrewPhase phase) -> AnyObject in
return BrewPhase.encode(phase).value!
}

Copy path View file
@@ -4,29 +4,78 @@
// Created by Colin Eberhardt on 15/07/2014.
// Copyright (c) 2014 Colin Eberhardt. All rights reserved.
//
// Original source can be found at:
// https://github.com/ColinEberhardt/ReactiveTwitterSearch/blob/master/ReactiveTwitterSearch/Util/UIKitExtensions.swift
//
import Foundation
import ReactiveCocoa
import UIKit

struct AssociationKey {
static var hidden: UInt8 = 1
static var date: UInt8 = 2
static var text: UInt8 = 3
}

func lazyAssociatedProperty<T: AnyObject>(host: AnyObject, key: UnsafePointer<Void>, factory: ()->T) -> T {
return objc_getAssociatedObject(host, key) as? T ?? {
let associatedProperty = factory()
objc_setAssociatedObject(host, key, associatedProperty, UInt(OBJC_ASSOCIATION_RETAIN))
return associatedProperty
}()
}

func lazyMutableProperty<T>(host: AnyObject, key: UnsafePointer<Void>, setter: T -> (), getter: () -> T) -> MutableProperty<T> {
return lazyAssociatedProperty(host, key) {
var property = MutableProperty<T>(getter())
property.producer
.start(next: {
newValue in
setter(newValue)
})
return property
}
}

// a struct that replaces the RAC macro
struct RAC {
var target : NSObject!
var keyPath : String!
var nilValue : AnyObject!

init(_ target: NSObject!, _ keyPath: String, nilValue: AnyObject? = nil) {
self.target = target
self.keyPath = keyPath
self.nilValue = nilValue
}


func assignSignal(signal : RACSignal) {
signal.setKeyPath(self.keyPath, onObject: self.target, nilValue: self.nilValue)
}
extension UIView {
public var rac_hidden: MutableProperty<Bool> {
return lazyMutableProperty(self, &AssociationKey.hidden, { self.hidden = $0 }, { self.hidden })
}
}

infix operator ~> {}
func ~> (signal: RACSignal, rac: RAC) {
rac.assignSignal(signal)
}
extension UILabel {
public var rac_text: MutableProperty<String> {
return lazyMutableProperty(self, &AssociationKey.text, { self.text = $0 }, { self.text ?? "" })
}
}


extension UITextField {
func rac_textSignalProducer() -> SignalProducer<String, NoError> {
return self.rac_textSignal().toSignalProducer()
|> map { $0 as! String }
|> catch { _ in SignalProducer<String, NoError>.empty }
}
}

extension UITextField {
public var rac_text: MutableProperty<String> {
return lazyAssociatedProperty(self, &AssociationKey.text) {

self.addTarget(self, action: "changed", forControlEvents: UIControlEvents.EditingChanged)

var property = MutableProperty<String>(self.text ?? "")
property.producer
.start(next: {
newValue in
self.text = newValue
})
return property
}
}

func changed() {
rac_text.value = self.text
}
}

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.