Skip to content

Connected the counters to the cell. #9

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

Merged
merged 3 commits into from
Nov 11, 2016
Merged
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
8 changes: 6 additions & 2 deletions CounterExample/Actions/Actions.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import ReSwift

// all of the actions that can be applied to the state
struct CounterActionIncrease: Action {}
struct CounterActionDecrease: Action {}
struct CounterActionIncrease: Action {
let index: Int
}
struct CounterActionDecrease: Action {
let index: Int
}
11 changes: 9 additions & 2 deletions CounterExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@
<rect key="frame" x="0.0" y="20" width="375" height="647"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" rowHeight="68" id="nmG-E3-MuA" customClass="CounterCell" customModule="CounterExample" customModuleProvider="target">
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="CounterCell" rowHeight="68" id="nmG-E3-MuA" customClass="CounterCell" customModule="CounterExample" customModuleProvider="target">
<rect key="frame" x="0.0" y="28" width="375" height="68"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="nmG-E3-MuA" id="oKv-oX-TLv">
<rect key="frame" x="0.0" y="0.0" width="375" height="67"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<stepper opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" maximumValue="100" translatesAutoresizingMaskIntoConstraints="NO" id="pre-xb-w8s">
<stepper opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" autorepeat="NO" minimumValue="-100" maximumValue="100" translatesAutoresizingMaskIntoConstraints="NO" id="pre-xb-w8s">
<rect key="frame" x="273" y="19" width="94" height="29"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<connections>
<action selector="stepperPressed:" destination="nmG-E3-MuA" eventType="valueChanged" id="TZG-Kz-CkK"/>
</connections>
</stepper>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="rgj-3I-jgW">
<rect key="frame" x="8" y="23" width="42" height="21"/>
Expand All @@ -45,6 +48,10 @@
</label>
</subviews>
</tableViewCellContentView>
<connections>
<outlet property="label" destination="rgj-3I-jgW" id="PiX-ed-k3x"/>
<outlet property="stepper" destination="pre-xb-w8s" id="gjH-nk-iU8"/>
</connections>
</tableViewCell>
</prototypes>
<connections>
Expand Down
25 changes: 15 additions & 10 deletions CounterExample/CounterCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@
//

import UIKit
import ReSwift

class CounterCell: UITableViewCell {

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
typealias CellAction = (Bool) -> ()

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
class CounterCell: UITableViewCell {

var action: CellAction?

// Configure the view for the selected state
@IBOutlet weak var label: UILabel!
@IBOutlet weak var stepper: UIStepper!

@IBAction func stepperPressed(_ sender: UIStepper) {
if sender.value > 0 {
self.action?(true)
} else {
self.action?(false)
}
sender.value = 0
}

}
8 changes: 4 additions & 4 deletions CounterExample/Reducers/CounterReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ struct CounterReducer: Reducer {
var state = state ?? AppState()

switch action {
case _ as CounterActionIncrease:
state.counters[0] += 1
case _ as CounterActionDecrease:
state.counters[0] -= 1
case let increaseAction as CounterActionIncrease:
state.counters[increaseAction.index] += 1
case let decreaseAction as CounterActionDecrease:
state.counters[decreaseAction.index] -= 1
default:
break
}
Expand Down
2 changes: 1 addition & 1 deletion CounterExample/State/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import ReSwift
typealias Counter = Int

struct AppState: StateType {
var counters: [Counter] = []
var counters: [Counter] = [Counter(), Counter(), Counter()]
}
40 changes: 19 additions & 21 deletions CounterExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,47 @@
import UIKit
import ReSwift


class ViewController: UIViewController, StoreSubscriber {

typealias StoreSubscriberStateType = AppState

@IBOutlet weak var tableView: UITableView!


override func viewDidLoad() {
super.viewDidLoad()

// subscribe to state changes
mainStore.subscribe(self)
}
}

func newState(state: AppState) {
// when the state changes, the UI is updated to reflect the current state
tableView.reloadData()
}

// when either button is tapped, an action is dispatched to the store
// in order to update the application state
@IBAction func downTouch(_ sender: AnyObject) {
mainStore.dispatch(CounterActionDecrease());
}
@IBAction func upTouch(_ sender: AnyObject) {
mainStore.dispatch(CounterActionIncrease());
}

}

extension ViewController : UITableViewDelegate {

func numberOfSections(in tableView: UITableView) -> Int {
return 0
return 1
}
}

extension ViewController : UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
return mainStore.state.counters.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//TODO: return actual cell
return UITableViewCell()
let cell = tableView.dequeueReusableCell(withIdentifier: "CounterCell", for: indexPath) as! CounterCell
cell.label.text = "\(mainStore.state.counters[indexPath.row])"
cell.action = { didIncrease in
if didIncrease {
mainStore.dispatch(CounterActionIncrease(index: indexPath.row))
} else {
mainStore.dispatch(CounterActionDecrease(index: indexPath.row))
}

}

return cell
}

}