Skip to content

Commit 94ea480

Browse files
committed
Add source & demo.
1 parent b9af915 commit 94ea480

File tree

27 files changed

+3794
-0
lines changed

27 files changed

+3794
-0
lines changed

.gitmodules

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[submodule "Carthage/Checkouts/Nimble"]
2+
path = Carthage/Checkouts/Nimble
3+
url = https://github.com/Quick/Nimble.git
4+
[submodule "Carthage/Checkouts/Quick"]
5+
path = Carthage/Checkouts/Quick
6+
url = https://github.com/Quick/Quick.git
7+
[submodule "Carthage/Checkouts/SwiftTask"]
8+
path = Carthage/Checkouts/SwiftTask
9+
url = https://github.com/ReactKit/SwiftTask.git
10+
[submodule "Carthage/Checkouts/ReactKit"]
11+
path = Carthage/Checkouts/ReactKit
12+
url = https://github.com/ReactKit/ReactKit.git

Cartfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github "ReactKit/ReactKit" ~> 0.7.0

Cartfile.private

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github "Quick/Quick" ~> 0.2.2
2+
github "Quick/Nimble" ~> 0.3.0

Cartfile.resolved

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github "Quick/Nimble" "v0.3.0"
2+
github "Quick/Quick" "v0.2.2"
3+
github "ReactKit/SwiftTask" "2.6.1"
4+
github "ReactKit/ReactKit" "0.7.0"

Carthage/Checkouts/Nimble

Submodule Nimble added at aeb5da7

Carthage/Checkouts/Quick

Submodule Quick added at b0e9828

Carthage/Checkouts/ReactKit

Submodule ReactKit added at 8dabadc

Carthage/Checkouts/SwiftTask

Submodule SwiftTask added at aaebbf9

Demo/ReactKitCalculatorDemo.xcodeproj/project.pbxproj

Lines changed: 594 additions & 0 deletions
Large diffs are not rendered by default.

Demo/ReactKitCalculatorDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// AppDelegate.swift
3+
// ReactKitCalculatorDemo
4+
//
5+
// Created by Yasuhiro Inami on 2015/02/18.
6+
// Copyright (c) 2015年 Yasuhiro Inami. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
@UIApplicationMain
12+
class AppDelegate: UIResponder, UIApplicationDelegate {
13+
14+
var window: UIWindow?
15+
16+
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
17+
18+
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
19+
self.window?.rootViewController = CalculatorViewController(nibName: "CalculatorViewController", bundle: nil)
20+
self.window?.makeKeyAndVisible()
21+
22+
return true
23+
}
24+
25+
func applicationWillResignActive(application: UIApplication) {
26+
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
27+
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
28+
}
29+
30+
func applicationDidEnterBackground(application: UIApplication) {
31+
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
32+
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
33+
}
34+
35+
func applicationWillEnterForeground(application: UIApplication) {
36+
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
37+
}
38+
39+
func applicationDidBecomeActive(application: UIApplication) {
40+
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
41+
}
42+
43+
func applicationWillTerminate(application: UIApplication) {
44+
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
45+
}
46+
47+
48+
}
49+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6214" systemVersion="14A314h" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
3+
<dependencies>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6207"/>
5+
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
6+
</dependencies>
7+
<objects>
8+
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
9+
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
10+
<view contentMode="scaleToFill" id="iN0-l3-epB">
11+
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
12+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
13+
<subviews>
14+
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" Copyright (c) 2015年 Yasuhiro Inami. All rights reserved." textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="8ie-xW-0ye">
15+
<rect key="frame" x="20" y="439" width="441" height="21"/>
16+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
17+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
18+
<nil key="highlightedColor"/>
19+
</label>
20+
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="ReactKitCalculator" textAlignment="center" lineBreakMode="middleTruncation" baselineAdjustment="alignBaselines" minimumFontSize="18" translatesAutoresizingMaskIntoConstraints="NO" id="kId-c2-rCX">
21+
<rect key="frame" x="20" y="140" width="441" height="43"/>
22+
<fontDescription key="fontDescription" type="boldSystem" pointSize="36"/>
23+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
24+
<nil key="highlightedColor"/>
25+
</label>
26+
</subviews>
27+
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
28+
<constraints>
29+
<constraint firstItem="kId-c2-rCX" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="bottom" multiplier="1/3" constant="1" id="5cJ-9S-tgC"/>
30+
<constraint firstAttribute="centerX" secondItem="kId-c2-rCX" secondAttribute="centerX" id="Koa-jz-hwk"/>
31+
<constraint firstAttribute="bottom" secondItem="8ie-xW-0ye" secondAttribute="bottom" constant="20" id="Kzo-t9-V3l"/>
32+
<constraint firstItem="8ie-xW-0ye" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="MfP-vx-nX0"/>
33+
<constraint firstAttribute="centerX" secondItem="8ie-xW-0ye" secondAttribute="centerX" id="ZEH-qu-HZ9"/>
34+
<constraint firstItem="kId-c2-rCX" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="fvb-Df-36g"/>
35+
</constraints>
36+
<nil key="simulatedStatusBarMetrics"/>
37+
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
38+
<point key="canvasLocation" x="548" y="455"/>
39+
</view>
40+
</objects>
41+
</document>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//
2+
// CalculatorViewController.swift
3+
// ReactKitCalculatorDemo
4+
//
5+
// Created by Yasuhiro Inami on 2015/02/18.
6+
// Copyright (c) 2015年 Yasuhiro Inami. All rights reserved.
7+
//
8+
9+
import UIKit
10+
import ReactKit
11+
import ReactKitCalculator
12+
13+
class CalculatorViewController: UIViewController {
14+
15+
@IBOutlet var nonClearButtons: [UIButton]!
16+
@IBOutlet var clearButton: UIButton!
17+
18+
@IBOutlet var textField: UITextField!
19+
@IBOutlet var historyLabel: UILabel!
20+
21+
var calculator: Calculator?
22+
23+
override func viewDidLoad() {
24+
super.viewDidLoad()
25+
26+
self.setupSubviews()
27+
self.setupSignals()
28+
}
29+
30+
func setupSubviews()
31+
{
32+
for subview in (self.nonClearButtons + [self.clearButton]) {
33+
subview.layer.borderWidth = 0.5
34+
subview.layer.borderColor = UIColor.blackColor().colorWithAlphaComponent(0.5).CGColor
35+
}
36+
37+
self.historyLabel.text = ""
38+
}
39+
40+
func setupSignals()
41+
{
42+
self.calculator = Calculator { mapper in
43+
44+
let alternativeMappingTuples = [
45+
("×", Calculator.Key.Multiply), // convert "×" to "*"
46+
("÷", Calculator.Key.Divide) // convert "÷" to "/"
47+
]
48+
49+
// map buttonSignal to calculator via `button.title`
50+
for button in self.nonClearButtons {
51+
let buttonTitle = button.titleForState(.Normal)
52+
let defaultMappedKey = Calculator.Key(rawValue: buttonTitle!)
53+
54+
let key = defaultMappedKey ?? (alternativeMappingTuples.filter { $0.0 == buttonTitle }.first?.1) ?? nil
55+
56+
if let key = key {
57+
mapper[key] = button.buttonSignal()
58+
}
59+
}
60+
61+
// manually map `.Clear` & `.AllClear`
62+
for clearKey in Calculator.Key.clearKeys() {
63+
mapper[clearKey] = self.clearButton.buttonSignal { button -> Bool in button?.titleForState(.Normal) == clearKey.rawValue }
64+
.filter { $0 == true }
65+
.map { _ in () as Void } // .asSignal(Void)
66+
}
67+
}
68+
69+
// REACT
70+
(self.textField, "text") <~ self.calculator!.outputSignal
71+
(self.historyLabel, "text") <~ self.calculator!.expressionSignal
72+
73+
// REACT: toggle C <-> AC
74+
self.calculator!.inputSignal ~> { [weak self] key in
75+
if contains(Calculator.Key.numKeys(), key) {
76+
self?.clearButton?.setTitle("C", forState: .Normal)
77+
78+
}
79+
else if key == .Clear {
80+
self?.clearButton?.setTitle("AC", forState: .Normal)
81+
}
82+
}
83+
}
84+
85+
}

0 commit comments

Comments
 (0)