Skip to content

Commit a9b5d83

Browse files
committed
[Swift 3] Migrate ReSwiftRouter
1 parent d8e89ff commit a9b5d83

File tree

8 files changed

+68
-62
lines changed

8 files changed

+68
-62
lines changed

ReSwiftRouter.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,11 @@
640640
};
641641
625E66CC1C1FFE280027C288 = {
642642
CreatedOnToolsVersion = 7.1.1;
643+
LastSwiftMigration = 0800;
643644
};
644645
625E66F01C2000EA0027C288 = {
645646
CreatedOnToolsVersion = 7.1.1;
647+
LastSwiftMigration = 0800;
646648
};
647649
};
648650
};
@@ -1272,6 +1274,7 @@
12721274
PRODUCT_NAME = ReSwiftRouter;
12731275
SKIP_INSTALL = YES;
12741276
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
1277+
SWIFT_VERSION = 3.0;
12751278
};
12761279
name = Debug;
12771280
};
@@ -1290,6 +1293,7 @@
12901293
PRODUCT_BUNDLE_IDENTIFIER = "de.benjamin-encz.ReSwiftRouter";
12911294
PRODUCT_NAME = ReSwiftRouter;
12921295
SKIP_INSTALL = YES;
1296+
SWIFT_VERSION = 3.0;
12931297
};
12941298
name = Release;
12951299
};
@@ -1301,6 +1305,7 @@
13011305
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
13021306
PRODUCT_BUNDLE_IDENTIFIER = "de.benjamin-encz.SwiftFlowRouterTests";
13031307
PRODUCT_NAME = "$(TARGET_NAME)";
1308+
SWIFT_VERSION = 3.0;
13041309
};
13051310
name = Debug;
13061311
};
@@ -1312,6 +1317,7 @@
13121317
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
13131318
PRODUCT_BUNDLE_IDENTIFIER = "de.benjamin-encz.SwiftFlowRouterTests";
13141319
PRODUCT_NAME = "$(TARGET_NAME)";
1320+
SWIFT_VERSION = 3.0;
13151321
};
13161322
name = Release;
13171323
};

ReSwiftRouter/NavigationActions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public struct SetRouteAction: StandardActionConvertible {
3131
public func toStandardAction() -> StandardAction {
3232
return StandardAction(
3333
type: SetRouteAction.type,
34-
payload: ["route": route, "animated": animated],
34+
payload: ["route": route as AnyObject, "animated": animated as AnyObject],
3535
isTypedAction: true
3636
)
3737
}

ReSwiftRouter/NavigationReducer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import ReSwift
1616
*/
1717
public struct NavigationReducer {
1818

19-
public static func handleAction(action: Action, state: NavigationState?) -> NavigationState {
19+
public static func handleAction(_ action: Action, state: NavigationState?) -> NavigationState {
2020
let state = state ?? NavigationState()
2121

2222
switch action {
@@ -31,7 +31,7 @@ public struct NavigationReducer {
3131
return state
3232
}
3333

34-
static func setRoute(state: NavigationState, setRouteAction: SetRouteAction) -> NavigationState {
34+
static func setRoute(_ state: NavigationState, setRouteAction: SetRouteAction) -> NavigationState {
3535
var state = state
3636

3737
state.route = setRouteAction.route
@@ -41,7 +41,7 @@ public struct NavigationReducer {
4141
}
4242

4343
static func setRouteSpecificData(
44-
state: NavigationState,
44+
_ state: NavigationState,
4545
route: Route,
4646
data: Any) -> NavigationState{
4747
let routeHash = RouteHash(route: route)

ReSwiftRouter/NavigationState.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public struct RouteHash: Hashable {
1717
let routeHash: String
1818

1919
public init(route: Route) {
20-
self.routeHash = route.joinWithSeparator("/")
20+
self.routeHash = route.joined(separator: "/")
2121
}
2222

2323
public var hashValue: Int { return self.routeHash.hashValue }
@@ -36,7 +36,7 @@ public struct NavigationState {
3636
}
3737

3838
extension NavigationState {
39-
public func getRouteSpecificState<T>(route: Route) -> T? {
39+
public func getRouteSpecificState<T>(_ route: Route) -> T? {
4040
let hash = RouteHash(route: route)
4141

4242
return self.routeSpecificState[hash] as? T

ReSwiftRouter/Routable.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ public typealias RoutingCompletionHandler = () -> Void
1111
public protocol Routable {
1212

1313
func pushRouteSegment(
14-
routeElementIdentifier: RouteElementIdentifier,
14+
_ routeElementIdentifier: RouteElementIdentifier,
1515
animated: Bool,
1616
completionHandler: RoutingCompletionHandler) -> Routable
1717

1818
func popRouteSegment(
19-
routeElementIdentifier: RouteElementIdentifier,
19+
_ routeElementIdentifier: RouteElementIdentifier,
2020
animated: Bool,
2121
completionHandler: RoutingCompletionHandler)
2222

2323
func changeRouteSegment(
24-
from: RouteElementIdentifier,
24+
_ from: RouteElementIdentifier,
2525
to: RouteElementIdentifier,
2626
animated: Bool,
2727
completionHandler: RoutingCompletionHandler) -> Routable
@@ -31,21 +31,21 @@ public protocol Routable {
3131
extension Routable {
3232

3333
public func pushRouteSegment(
34-
routeElementIdentifier: RouteElementIdentifier,
34+
_ routeElementIdentifier: RouteElementIdentifier,
3535
animated: Bool,
3636
completionHandler: RoutingCompletionHandler) -> Routable {
3737
fatalError("This routable cannot change segments. You have not implemented it.")
3838
}
3939

4040
public func popRouteSegment(
41-
routeElementIdentifier: RouteElementIdentifier,
41+
_ routeElementIdentifier: RouteElementIdentifier,
4242
animated: Bool,
4343
completionHandler: RoutingCompletionHandler) {
4444
fatalError("This routable cannot change segments. You have not implemented it.")
4545
}
4646

4747
public func changeRouteSegment(
48-
from: RouteElementIdentifier,
48+
_ from: RouteElementIdentifier,
4949
to: RouteElementIdentifier,
5050
animated: Bool,
5151
completionHandler: RoutingCompletionHandler) -> Routable {

ReSwiftRouter/Router.swift

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,81 +9,81 @@
99
import Foundation
1010
import ReSwift
1111

12-
public class Router<State: StateType>: StoreSubscriber {
12+
open class Router<State: StateType>: StoreSubscriber {
1313

1414
public typealias NavigationStateSelector = (State) -> NavigationState
1515

1616
var store: Store<State>
1717
var lastNavigationState = NavigationState()
1818
var routables: [Routable] = []
19-
let waitForRoutingCompletionQueue = dispatch_queue_create("WaitForRoutingCompletionQueue", nil)
19+
let waitForRoutingCompletionQueue = DispatchQueue(label: "WaitForRoutingCompletionQueue", attributes: [])
2020

21-
public init(store: Store<State>, rootRoutable: Routable, stateSelector: NavigationStateSelector) {
21+
public init(store: Store<State>, rootRoutable: Routable, stateSelector: @escaping NavigationStateSelector) {
2222
self.store = store
2323
self.routables.append(rootRoutable)
2424

2525
self.store.subscribe(self, selector: stateSelector)
2626
}
2727

28-
public func newState(state: NavigationState) {
28+
open func newState(state: NavigationState) {
2929
let routingActions = Router.routingActionsForTransitionFrom(
3030
lastNavigationState.route, newRoute: state.route)
3131

3232
routingActions.forEach { routingAction in
3333

34-
let semaphore = dispatch_semaphore_create(0)
34+
let semaphore = DispatchSemaphore(value: 0)
3535

3636
// Dispatch all routing actions onto this dedicated queue. This will ensure that
3737
// only one routing action can run at any given time. This is important for using this
3838
// Router with UI frameworks. Whenever a navigation action is triggered, this queue will
3939
// block (using semaphore_wait) until it receives a callback from the Routable
4040
// indicating that the navigation action has completed
41-
dispatch_async(waitForRoutingCompletionQueue) {
41+
waitForRoutingCompletionQueue.async {
4242
switch routingAction {
4343

44-
case let .Pop(responsibleRoutableIndex, segmentToBePopped):
45-
dispatch_async(dispatch_get_main_queue()) {
44+
case let .pop(responsibleRoutableIndex, segmentToBePopped):
45+
DispatchQueue.main.async {
4646
self.routables[responsibleRoutableIndex]
4747
.popRouteSegment(
4848
segmentToBePopped,
4949
animated: state.changeRouteAnimated) {
50-
dispatch_semaphore_signal(semaphore)
50+
semaphore.signal()
5151
}
5252

53-
self.routables.removeAtIndex(responsibleRoutableIndex + 1)
53+
self.routables.remove(at: responsibleRoutableIndex + 1)
5454
}
5555

56-
case let .Change(responsibleRoutableIndex, segmentToBeReplaced, newSegment):
57-
dispatch_async(dispatch_get_main_queue()) {
56+
case let .change(responsibleRoutableIndex, segmentToBeReplaced, newSegment):
57+
DispatchQueue.main.async {
5858
self.routables[responsibleRoutableIndex + 1] =
5959
self.routables[responsibleRoutableIndex]
6060
.changeRouteSegment(
6161
segmentToBeReplaced,
6262
to: newSegment,
6363
animated: state.changeRouteAnimated) {
64-
dispatch_semaphore_signal(semaphore)
64+
semaphore.signal()
6565
}
6666
}
6767

68-
case let .Push(responsibleRoutableIndex, segmentToBePushed):
69-
dispatch_async(dispatch_get_main_queue()) {
68+
case let .push(responsibleRoutableIndex, segmentToBePushed):
69+
DispatchQueue.main.async {
7070
self.routables.append(
7171
self.routables[responsibleRoutableIndex]
7272
.pushRouteSegment(
7373
segmentToBePushed,
7474
animated: state.changeRouteAnimated) {
75-
dispatch_semaphore_signal(semaphore)
75+
semaphore.signal()
7676
}
7777
)
7878
}
7979
}
8080

81-
let waitUntil = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * Double(NSEC_PER_SEC)))
81+
let waitUntil = DispatchTime.now() + Double(Int64(3 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
8282

83-
let result = dispatch_semaphore_wait(semaphore, waitUntil)
83+
let result = semaphore.wait(timeout: waitUntil)
8484

85-
if result != 0 {
86-
print("[SwiftFlowRouter]: Router is stuck waiting for a" +
85+
if case .timedOut = result {
86+
print("[ReSwiftRouter]: Router is stuck waiting for a" +
8787
" completion handler to be called. Ensure that you have called the" +
8888
" completion handler in each Routable element.")
8989
print("Set a symbolic breakpoint for the `ReSwiftRouterStuck` symbol in order" +
@@ -99,7 +99,7 @@ public class Router<State: StateType>: StoreSubscriber {
9999

100100
// MARK: Route Transformation Logic
101101

102-
static func largestCommonSubroute(oldRoute: Route, newRoute: Route) -> Int {
102+
static func largestCommonSubroute(_ oldRoute: Route, newRoute: Route) -> Int {
103103
var largestCommonSubroute = -1
104104

105105
while largestCommonSubroute + 1 < newRoute.count &&
@@ -115,11 +115,11 @@ public class Router<State: StateType>: StoreSubscriber {
115115
// is not represented in the route, e.g.
116116
// route = ["tabBar"]
117117
// routables = [RootRoutable, TabBarRoutable]
118-
static func routableIndexForRouteSegment(segment: Int) -> Int {
118+
static func routableIndexForRouteSegment(_ segment: Int) -> Int {
119119
return segment + 1
120120
}
121121

122-
static func routingActionsForTransitionFrom(oldRoute: Route,
122+
static func routingActionsForTransitionFrom(_ oldRoute: Route,
123123
newRoute: Route) -> [RoutingActions] {
124124

125125
var routingActions: [RoutingActions] = []
@@ -147,7 +147,7 @@ public class Router<State: StateType>: StoreSubscriber {
147147
while routeBuildingIndex > commonSubroute + 1 {
148148
let routeSegmentToPop = oldRoute[routeBuildingIndex]
149149

150-
let popAction = RoutingActions.Pop(
150+
let popAction = RoutingActions.pop(
151151
responsibleRoutableIndex: routableIndexForRouteSegment(routeBuildingIndex - 1),
152152
segmentToBePopped: routeSegmentToPop
153153
)
@@ -160,7 +160,7 @@ public class Router<State: StateType>: StoreSubscriber {
160160
// "The old route had an element after the commonSubroute and the new route does not
161161
// we need to pop the route segment after the commonSubroute"
162162
if oldRoute.count > newRoute.count {
163-
let popAction = RoutingActions.Pop(
163+
let popAction = RoutingActions.pop(
164164
responsibleRoutableIndex: routableIndexForRouteSegment(routeBuildingIndex - 1),
165165
segmentToBePopped: oldRoute[routeBuildingIndex]
166166
)
@@ -172,7 +172,7 @@ public class Router<State: StateType>: StoreSubscriber {
172172
// "The new route has a different element after the commonSubroute, we need to replace
173173
// the old route element with the new one"
174174
else if oldRoute.count > (commonSubroute + 1) && newRoute.count > (commonSubroute + 1) {
175-
let changeAction = RoutingActions.Change(
175+
let changeAction = RoutingActions.change(
176176
responsibleRoutableIndex: routableIndexForRouteSegment(commonSubroute),
177177
segmentToBeReplaced: oldRoute[commonSubroute + 1],
178178
newSegment: newRoute[commonSubroute + 1])
@@ -189,7 +189,7 @@ public class Router<State: StateType>: StoreSubscriber {
189189
while routeBuildingIndex < newRouteIndex {
190190
let routeSegmentToPush = newRoute[routeBuildingIndex + 1]
191191

192-
let pushAction = RoutingActions.Push(
192+
let pushAction = RoutingActions.push(
193193
responsibleRoutableIndex: routableIndexForRouteSegment(routeBuildingIndex),
194194
segmentToBePushed: routeSegmentToPush
195195
)
@@ -206,8 +206,8 @@ public class Router<State: StateType>: StoreSubscriber {
206206
func ReSwiftRouterStuck() {}
207207

208208
enum RoutingActions {
209-
case Push(responsibleRoutableIndex: Int, segmentToBePushed: RouteElementIdentifier)
210-
case Pop(responsibleRoutableIndex: Int, segmentToBePopped: RouteElementIdentifier)
211-
case Change(responsibleRoutableIndex: Int, segmentToBeReplaced: RouteElementIdentifier,
209+
case push(responsibleRoutableIndex: Int, segmentToBePushed: RouteElementIdentifier)
210+
case pop(responsibleRoutableIndex: Int, segmentToBePopped: RouteElementIdentifier)
211+
case change(responsibleRoutableIndex: Int, segmentToBeReplaced: RouteElementIdentifier,
212212
newSegment: RouteElementIdentifier)
213213
}

ReSwiftRouterTests/ReSwiftRouterIntegrationTests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class MockRoutable: Routable {
2222
)] = []
2323

2424
func pushRouteSegment(
25-
routeElementIdentifier: RouteElementIdentifier,
25+
_ routeElementIdentifier: RouteElementIdentifier,
2626
animated: Bool,
2727
completionHandler: RoutingCompletionHandler) -> Routable {
2828
callsToPushRouteSegment.append(
@@ -33,7 +33,7 @@ class MockRoutable: Routable {
3333
}
3434

3535
func popRouteSegment(
36-
routeElementIdentifier: RouteElementIdentifier,
36+
_ routeElementIdentifier: RouteElementIdentifier,
3737
animated: Bool,
3838
completionHandler: RoutingCompletionHandler) {
3939
callsToPopRouteSegment.append(
@@ -43,7 +43,7 @@ class MockRoutable: Routable {
4343
}
4444

4545
func changeRouteSegment(
46-
from: RouteElementIdentifier,
46+
_ from: RouteElementIdentifier,
4747
to: RouteElementIdentifier,
4848
animated: Bool,
4949
completionHandler: RoutingCompletionHandler) -> Routable {
@@ -93,7 +93,7 @@ class SwiftFlowRouterIntegrationTests: QuickSpec {
9393
class FakeRootRoutable: Routable {
9494
var called = false
9595

96-
func pushRouteSegment(routeElementIdentifier: RouteElementIdentifier,
96+
func pushRouteSegment(_ routeElementIdentifier: RouteElementIdentifier,
9797
completionHandler: RoutingCompletionHandler) -> Routable {
9898
called = true
9999
return MockRoutable()
@@ -118,12 +118,12 @@ class SwiftFlowRouterIntegrationTests: QuickSpec {
118118
class FakeRootRoutable: Routable {
119119
var calledWithIdentifier: (RouteElementIdentifier?) -> Void
120120

121-
init(calledWithIdentifier: (RouteElementIdentifier?) -> Void) {
121+
init(calledWithIdentifier: @escaping (RouteElementIdentifier?) -> Void) {
122122
self.calledWithIdentifier = calledWithIdentifier
123123
}
124124

125125
func pushRouteSegment(
126-
routeSegment: RouteElementIdentifier,
126+
_ routeSegment: RouteElementIdentifier,
127127
animated: Bool,
128128
completionHandler: RoutingCompletionHandler) -> Routable {
129129
calledWithIdentifier(routeSegment)
@@ -157,12 +157,12 @@ class SwiftFlowRouterIntegrationTests: QuickSpec {
157157
class FakeChildRoutable: Routable {
158158
var calledWithIdentifier: (RouteElementIdentifier?) -> Void
159159

160-
init(calledWithIdentifier: (RouteElementIdentifier?) -> Void) {
160+
init(calledWithIdentifier: @escaping (RouteElementIdentifier?) -> Void) {
161161
self.calledWithIdentifier = calledWithIdentifier
162162
}
163163

164164
func pushRouteSegment(
165-
routeSegment: RouteElementIdentifier,
165+
_ routeSegment: RouteElementIdentifier,
166166
animated: Bool,
167167
completionHandler: RoutingCompletionHandler) -> Routable {
168168
calledWithIdentifier(routeSegment)
@@ -187,7 +187,7 @@ class SwiftFlowRouterIntegrationTests: QuickSpec {
187187
}
188188

189189
func pushRouteSegment(
190-
routeElementIdentifier: RouteElementIdentifier,
190+
_ routeElementIdentifier: RouteElementIdentifier,
191191
animated: Bool,
192192
completionHandler: RoutingCompletionHandler) -> Routable {
193193
completionHandler()

0 commit comments

Comments
 (0)