Skip to content
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

Add ability to reverse actions when perform two finger tap on screen. #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions CounterExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
3C135C601D547F7700EC9FC9 /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C135C5F1D547F7700EC9FC9 /* AppState.swift */; };
3C135C651D55C92200EC9FC9 /* Actions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C135C641D55C92200EC9FC9 /* Actions.swift */; };
3C135C671D55C94300EC9FC9 /* CounterReducer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C135C661D55C94300EC9FC9 /* CounterReducer.swift */; };
D20FA0FE1DD5D11B0054EA53 /* CounterExampleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D20FA0FD1DD5D11B0054EA53 /* CounterExampleTests.swift */; };
D20FA1071DD5DF480054EA53 /* Recorder.swift in Sources */ = {isa = PBXBuildFile; fileRef = D20FA1061DD5DF480054EA53 /* Recorder.swift */; };
63DC914B1DD5D41E009A8AB0 /* ReSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3C135C451D53E94C00EC9FC9 /* ReSwift.framework */; };
D20FA0FE1DD5D11B0054EA53 /* CounterReducerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D20FA0FD1DD5D11B0054EA53 /* CounterReducerTests.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -69,6 +71,7 @@
D20FA0FB1DD5D11A0054EA53 /* CounterExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CounterExampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
D20FA0FD1DD5D11B0054EA53 /* CounterReducerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CounterReducerTests.swift; sourceTree = "<group>"; };
D20FA0FF1DD5D11B0054EA53 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D20FA1061DD5DF480054EA53 /* Recorder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Recorder.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -113,6 +116,7 @@
3C135C2E1D53E29400EC9FC9 /* CounterExample */ = {
isa = PBXGroup;
children = (
D20FA1051DD5DF380054EA53 /* Recorder */,
3C135C631D55C91800EC9FC9 /* Reducers */,
3C135C621D55C91300EC9FC9 /* State */,
3C135C611D55C90900EC9FC9 /* Actions */,
Expand Down Expand Up @@ -160,6 +164,14 @@
path = CounterExampleTests;
sourceTree = "<group>";
};
D20FA1051DD5DF380054EA53 /* Recorder */ = {
isa = PBXGroup;
children = (
D20FA1061DD5DF480054EA53 /* Recorder.swift */,
);
name = Recorder;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -274,6 +286,7 @@
3C135C671D55C94300EC9FC9 /* CounterReducer.swift in Sources */,
3C135C651D55C92200EC9FC9 /* Actions.swift in Sources */,
3C135C321D53E29400EC9FC9 /* ViewController.swift in Sources */,
D20FA1071DD5DF480054EA53 /* Recorder.swift in Sources */,
3C135C301D53E29400EC9FC9 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
15 changes: 13 additions & 2 deletions CounterExample/Actions/Actions.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import ReSwift

// all of the actions that can be applied to the state
struct CounterActionIncrease: Action {
struct CounterActionIncrease: ReversableAction {
let index: Int
func reversed() -> ReversableAction {
return CounterActionDecrease(index: index)
}
}
struct CounterActionDecrease: Action {

struct CounterActionDecrease: ReversableAction {
let index: Int
func reversed() -> ReversableAction {
return CounterActionIncrease(index: index)
}
}

protocol ReversableAction: Action {
func reversed() -> ReversableAction
}
9 changes: 9 additions & 0 deletions CounterExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@
</tableView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<gestureRecognizers/>
<constraints>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="eek-B8-JXY" secondAttribute="bottom" id="ZdA-RE-ofs"/>
<constraint firstAttribute="trailing" secondItem="eek-B8-JXY" secondAttribute="trailing" id="d7f-9z-9vc"/>
<constraint firstItem="eek-B8-JXY" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="-64" id="gwk-e7-74g"/>
<constraint firstItem="eek-B8-JXY" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="yC9-9F-pZb"/>
</constraints>
<connections>
<outletCollection property="gestureRecognizers" destination="Vcq-Uj-qPr" appends="YES" id="eM7-B0-MHJ"/>
</connections>
</view>
<navigationItem key="navigationItem" id="O1K-Td-Auc">
<barButtonItem key="rightBarButtonItem" systemItem="add" id="Zr1-8g-nfq">
Expand All @@ -80,6 +84,11 @@
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
<tapGestureRecognizer numberOfTouchesRequired="2" id="Vcq-Uj-qPr">
<connections>
<action selector="didTwoFingersTap:" destination="BYZ-38-t0r" id="LHF-Eg-ntJ"/>
</connections>
</tapGestureRecognizer>
</objects>
<point key="canvasLocation" x="1244" y="386.35682158920542"/>
</scene>
Expand Down
30 changes: 30 additions & 0 deletions CounterExample/Recorder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Recorder.swift
// CounterExample
//
// Created by Bohdan Orlov on 11/11/2016.
// Copyright © 2016 Colin Eberhardt. All rights reserved.
//

import Foundation
import ReSwift

class Recorder {
var replayDelay: TimeInterval = 0.5
var actions = [ReversableAction]()
func record(_ action: ReversableAction) {
actions.append(action)
}

func replay() {
var tmpActions = [ReversableAction]()
for (idx, action) in actions.reversed().enumerated() {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + TimeInterval(Float(replayDelay) * Float(idx)), execute: {
mainStore.dispatch(action)

})
tmpActions.append(action.reversed())
}
actions = tmpActions
}
}
13 changes: 9 additions & 4 deletions CounterExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import UIKit
import ReSwift

class ViewController: UIViewController, StoreSubscriber {

let recorder = Recorder()
typealias StoreSubscriberStateType = AppState

@IBOutlet weak var tableView: UITableView!

@IBAction func didTwoFingersTap(_ sender: Any) {
recorder.replay()
}
override func viewDidLoad() {
super.viewDidLoad()

Expand Down Expand Up @@ -48,13 +51,15 @@ extension ViewController : UITableViewDataSource {
let cell = tableView.dequeueReusableCell(withIdentifier: "CounterCell", for: indexPath) as! CounterCell
cell.label.text = "\(mainStore.state.counters[indexPath.row])"
cell.action = { actionType in

let action: ReversableAction
switch actionType {
case .increase:
mainStore.dispatch(CounterActionIncrease(index: indexPath.row))
action = CounterActionIncrease(index: indexPath.row)
case .decrease:
mainStore.dispatch(CounterActionDecrease(index: indexPath.row))
action = CounterActionDecrease(index: indexPath.row)
}
mainStore.dispatch(action)
self.recorder.record(action.reversed())
}

return cell
Expand Down