Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
Revert "Revert "Update routes to final form (#2374)" (#2381)" (#2383)
Browse files Browse the repository at this point in the history
This reverts commit 2337cf6.
  • Loading branch information
rnystrom authored Nov 2, 2018
1 parent 2337cf6 commit 595b76c
Show file tree
Hide file tree
Showing 20 changed files with 4,143 additions and 4,102 deletions.
3 changes: 1 addition & 2 deletions Classes/Systems/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

appController.appDidFinishLaunching(with: window)
appController.setupRoutes()

// setup fabric
Fabric.with([Crashlytics.self])
Expand All @@ -50,7 +49,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
appController.handle(path: shortcutItem.type, params: shortcutItem.params)
appController.router.handle(path: shortcutItem.type, params: shortcutItem.params)
}

func applicationDidBecomeActive(_ application: UIApplication) {
Expand Down
22 changes: 0 additions & 22 deletions Classes/Systems/AppRouter/AppController+SetupRoutes.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension AppController: UNUserNotificationCenterDelegate {
case UNNotificationDismissActionIdentifier: break
case UNNotificationDefaultActionIdentifier:
if let (path, params) = response.notification.request.content.routableUserInfo {
handle(path: path, params: params)
router.handle(path: path, params: params)
}
default: print(response.actionIdentifier)
}
Expand Down
49 changes: 19 additions & 30 deletions Classes/Systems/AppRouter/AppController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ import GitHubSession
import GitHubAPI
import GitHawkRoutes

final class AppController: NSObject, LoginSplashViewControllerDelegate, GitHubSessionListener {
final class AppController: NSObject,
LoginSplashViewControllerDelegate,
GitHubSessionListener,
RouterDelegate {

public private(set) lazy var router = {
return Router(delegate: self)
}()

private var splitViewController: AppSplitViewController!
private let sessionManager = GitHubSessionManager()
private weak var loginViewController: LoginSplashViewController?
private var appClient: GithubClient?
private var settingsNavigationController: UINavigationController?
private var watchAppSync: WatchAppUserSessionSync?
private var routes = [String: (Routable & RoutePerformable).Type]()

override init() {
super.init()
Expand Down Expand Up @@ -56,34 +62,6 @@ final class AppController: NSObject, LoginSplashViewControllerDelegate, GitHubSe
appClient?.badge.fetch(application: application, handler: completion)
}

@discardableResult
func handle(url: URL) -> Bool {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
else { return false }
var params = [String: String]()
for item in components.queryItems ?? [] {
params[item.name] = item.value
}
return handle(path: url.path, params: params)
}

@discardableResult
func handle(path: String, params: [String: String]) -> Bool {
guard let routeType = routes[path],
let route = routeType.from(params: params),
let client = appClient
else { return false }
return route.perform(
sessionManager: sessionManager,
splitViewController: splitViewController,
client: client
)
}

func register<T: Routable & RoutePerformable>(route: T.Type) {
routes[T.path] = T.self
}

private func resetWatchSync(userSession: GitHubUserSession) {
watchAppSync = WatchAppUserSessionSync(userSession: userSession)
watchAppSync?.start()
Expand Down Expand Up @@ -162,4 +140,15 @@ final class AppController: NSObject, LoginSplashViewControllerDelegate, GitHubSe
showLogin(animated: trueUnlessReduceMotionEnabled)
}

// MARK: RouteHandlerDelegate

func perform(route: RoutePerformable, router: Router) -> Bool {
guard let client = appClient else { return false }
return route.perform(
sessionManager: sessionManager,
splitViewController: splitViewController,
client: client
)
}

}
59 changes: 59 additions & 0 deletions Classes/Systems/AppRouter/Router.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// Router.swift
// Freetime
//
// Created by Ryan Nystrom on 11/1/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//

import Foundation
import GitHawkRoutes

private func register<T: Routable & RoutePerformable>(
route: T.Type,
map: inout [String: (Routable & RoutePerformable).Type]
) {
map[T.path] = T.self
}

protocol RouterDelegate: class {
func perform(route: RoutePerformable, router: Router) -> Bool
}

final class Router {

private weak var delegate: RouterDelegate?
private let routes: [String: (Routable & RoutePerformable).Type]

init(delegate: RouterDelegate) {
var routes = [String: (Routable & RoutePerformable).Type]()
register(route: BookmarkShortcutRoute.self, map: &routes)
register(route: SwitchAccountShortcutRoute.self, map: &routes)
register(route: SearchShortcutRoute.self, map: &routes)
register(route: IssueRoute.self, map: &routes)
register(route: RepoRoute.self, map: &routes)
self.routes = routes
self.delegate = delegate
}

@discardableResult
func handle(url: URL) -> Bool {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
else { return false }
var params = [String: String]()
for item in components.queryItems ?? [] {
params[item.name] = item.value
}
return handle(path: url.path, params: params)
}

@discardableResult
func handle(path: String, params: [String: String]) -> Bool {
guard let routeType = routes[path],
let route = routeType.from(params: params),
let delegate = self.delegate
else { return false }
return delegate.perform(route: route, router: self)
}

}
8 changes: 4 additions & 4 deletions Freetime.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
290CA772216AE93E00DE04F8 /* SwitchAccountShortcutRoute+RoutePerformable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 290CA771216AE93E00DE04F8 /* SwitchAccountShortcutRoute+RoutePerformable.swift */; };
290CA774216AE94D00DE04F8 /* BookmarkShortcutRoute+RoutePerformable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 290CA773216AE94D00DE04F8 /* BookmarkShortcutRoute+RoutePerformable.swift */; };
290CA778216AFAE600DE04F8 /* RoutePerformable+RoutePerformable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 290CA777216AFAE600DE04F8 /* RoutePerformable+RoutePerformable.swift */; };
290CA77A216AFC1300DE04F8 /* AppController+SetupRoutes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 290CA779216AFC1300DE04F8 /* AppController+SetupRoutes.swift */; };
290D2A3D1F044CB20082E6CC /* UIViewController+SmartDeselection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 290D2A3C1F044CB20082E6CC /* UIViewController+SmartDeselection.swift */; };
290D2A421F04D3470082E6CC /* IssueStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = 290D2A411F04D3470082E6CC /* IssueStatus.swift */; };
290EF56A1F06A821006A2160 /* Notification+NotificationViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 290EF5691F06A7E1006A2160 /* Notification+NotificationViewModel.swift */; };
Expand Down Expand Up @@ -271,6 +270,7 @@
298BA0971EC947F100B01946 /* SegmentedControlCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 298BA0951EC947F100B01946 /* SegmentedControlCell.swift */; };
298BA0981EC947F100B01946 /* SegmentedControlSectionController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 298BA0961EC947F100B01946 /* SegmentedControlSectionController.swift */; };
298BA09A1EC947FC00B01946 /* SegmentedControlModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 298BA0991EC947FC00B01946 /* SegmentedControlModel.swift */; };
2990B9DB218BF23D009865EB /* Router.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2990B9DA218BF23D009865EB /* Router.swift */; };
29921BCC1EF624D400C1E848 /* UIFont+MutableTraits.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29921BCB1EF624D400C1E848 /* UIFont+MutableTraits.swift */; };
299304691FBA8A88007B9737 /* IssueManagingSectionController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 299304681FBA8A88007B9737 /* IssueManagingSectionController.swift */; };
2993046B1FBA8C04007B9737 /* IssueManagingExpansionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2993046A1FBA8C04007B9737 /* IssueManagingExpansionCell.swift */; };
Expand Down Expand Up @@ -571,7 +571,6 @@
290CA771216AE93E00DE04F8 /* SwitchAccountShortcutRoute+RoutePerformable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SwitchAccountShortcutRoute+RoutePerformable.swift"; sourceTree = "<group>"; };
290CA773216AE94D00DE04F8 /* BookmarkShortcutRoute+RoutePerformable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BookmarkShortcutRoute+RoutePerformable.swift"; sourceTree = "<group>"; };
290CA777216AFAE600DE04F8 /* RoutePerformable+RoutePerformable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RoutePerformable+RoutePerformable.swift"; sourceTree = "<group>"; };
290CA779216AFC1300DE04F8 /* AppController+SetupRoutes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppController+SetupRoutes.swift"; sourceTree = "<group>"; };
290D2A3C1F044CB20082E6CC /* UIViewController+SmartDeselection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewController+SmartDeselection.swift"; sourceTree = "<group>"; };
290D2A411F04D3470082E6CC /* IssueStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IssueStatus.swift; sourceTree = "<group>"; };
290EF5691F06A7E1006A2160 /* Notification+NotificationViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Notification+NotificationViewModel.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -819,6 +818,7 @@
298BA0951EC947F100B01946 /* SegmentedControlCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SegmentedControlCell.swift; sourceTree = "<group>"; };
298BA0961EC947F100B01946 /* SegmentedControlSectionController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SegmentedControlSectionController.swift; sourceTree = "<group>"; };
298BA0991EC947FC00B01946 /* SegmentedControlModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SegmentedControlModel.swift; sourceTree = "<group>"; };
2990B9DA218BF23D009865EB /* Router.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Router.swift; sourceTree = "<group>"; };
29921BCB1EF624D400C1E848 /* UIFont+MutableTraits.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIFont+MutableTraits.swift"; sourceTree = "<group>"; };
299304681FBA8A88007B9737 /* IssueManagingSectionController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IssueManagingSectionController.swift; sourceTree = "<group>"; };
2993046A1FBA8C04007B9737 /* IssueManagingExpansionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IssueManagingExpansionCell.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1122,13 +1122,13 @@
isa = PBXGroup;
children = (
290CA7632169799600DE04F8 /* AppController.swift */,
290CA779216AFC1300DE04F8 /* AppController+SetupRoutes.swift */,
29A10544216D9515004734A0 /* AppController+UNUserNotificationCenterDelegate.swift */,
290CA76521697A7900DE04F8 /* AppSplitViewController.swift */,
290CA773216AE94D00DE04F8 /* BookmarkShortcutRoute+RoutePerformable.swift */,
29A10540216D912F004734A0 /* IssueRoute+RoutePerformable.swift */,
2962505F217D53C000EA6164 /* RepoRoute+RoutePerformable.swift */,
290CA777216AFAE600DE04F8 /* RoutePerformable+RoutePerformable.swift */,
2990B9DA218BF23D009865EB /* Router.swift */,
290CA769216AC82700DE04F8 /* SearchShortcutRoute+RoutePerformable.swift */,
290CA771216AE93E00DE04F8 /* SwitchAccountShortcutRoute+RoutePerformable.swift */,
29A10542216D9381004734A0 /* UNMutableNotificationContent+Routable.swift */,
Expand Down Expand Up @@ -2815,6 +2815,7 @@
297DD5E11F061BBE006E7E63 /* CreateProfileViewController.swift in Sources */,
29B205FA217B8B9100E4DD9F /* PathCommitCell.swift in Sources */,
29AAB7171FB4A2AE001D5E6A /* BoundedImageSize.swift in Sources */,
2990B9DB218BF23D009865EB /* Router.swift in Sources */,
29A4768E1ED07A23005D0953 /* DateDetailsFormatter.swift in Sources */,
29AF1E8C1F8ABC5A0008A0EF /* RepositoryCodeDirectoryViewController.swift in Sources */,
291929611F3FD2960012067B /* DiffString.swift in Sources */,
Expand Down Expand Up @@ -2950,7 +2951,6 @@
DCA5ED141FAEE8030072F074 /* Bookmark.swift in Sources */,
299F4A89204CEDDC004BA4F0 /* Client+AccessToken.swift in Sources */,
2931892F1F539C0E00EF0911 /* IssueMilestoneSectionController.swift in Sources */,
290CA77A216AFC1300DE04F8 /* AppController+SetupRoutes.swift in Sources */,
29BBD82920CAC7D5004D62FE /* NotificationViewModel.swift in Sources */,
299F63E2205DE1470015D901 /* UIView+DateDetails.swift in Sources */,
9870B9031FC73EE70009719C /* Secrets.swift in Sources */,
Expand Down
13 changes: 7 additions & 6 deletions FreetimeTests/DetectShortlinkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ class DetectShortlinkTests: XCTestCase {
XCTAssertEqual(containsLink.linkText, "#345")
XCTAssertEqual(containsLink.issueNumber, 345)
XCTAssertEqual(builder.allText, testString)

testString =
"""
#345
newLine
"""

builder = setupBuilder(with: testString)
containsLink = checkForIssueLink(builder.styledTexts)[0]
XCTAssertEqual(containsLink.linkText, "#345")
Expand Down Expand Up @@ -124,18 +124,18 @@ class DetectShortlinkTests: XCTestCase {
XCTAssertEqual(containsLink.issueNumber, 4)
XCTAssertEqual(builder.allText, testString)
}

func test_ConsecutivePositiveMatches() {
var testString = "#100 #150 #200"
var builder = setupBuilder(with: testString)
var links = checkForIssueLink(builder.styledTexts)

XCTAssertEqual(links[0].issueNumber, 100)
XCTAssertEqual(links[0].linkText, "#100")

XCTAssertEqual(links[1].issueNumber, 150)
XCTAssertEqual(links[1].linkText, "#150")

XCTAssertEqual(links[2].issueNumber, 200)
XCTAssertEqual(links[2].linkText, "#200")
}
Expand All @@ -158,3 +158,4 @@ class DetectShortlinkTests: XCTestCase {
XCTAssertEqual(containsLink.count, 0)
}
}

6 changes: 3 additions & 3 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ PODS:
- FMDB (2.7.2):
- FMDB/standard (= 2.7.2)
- FMDB/standard (2.7.2)
- GitHawkRoutes (0.1.0)
- GitHawkRoutes (0.1.1)
- GitHubAPI (0.1.0):
- Alamofire (~> 4.4.0)
- Apollo (~> 0.8.0)
Expand Down Expand Up @@ -170,7 +170,7 @@ CHECKOUT OPTIONS:
:commit: beb697643eea11f40bb66683d1576a2772080d9d
:git: https://github.com/GitHawkApp/FlatCache.git
GitHawkRoutes:
:commit: 1947e45203b81c1bfed2c8427a395afb7917e6ca
:commit: 26e45170b4ba06843d7a034f4a321a5797c77ec8
:git: https://github.com/GitHawkApp/GitHawkRoutes.git
Highlightr:
:commit: 4f7e90477619b8dc4b9e641efd10952c22150c5c
Expand Down Expand Up @@ -204,7 +204,7 @@ SPEC CHECKSUMS:
FlatCache: e67d3d45a0f76b93e66883b802051dcbf9d50649
FLEX: bd1a39e55b56bb413b6f1b34b3c10a0dc44ef079
FMDB: 6198a90e7b6900cfc046e6bc0ef6ebb7be9236aa
GitHawkRoutes: 6a2a20e756143cb1c87bdf1d7f33a7256cfc6722
GitHawkRoutes: 468138c117a6fbe79bdd9ba7390bf45d363bc69b
GitHubAPI: 44a907f9699210536d65179d3d0dc0dc70dde7a1
GitHubSession: 60c7bbd84fb915a0bd911a367c9661418ccfd7ae
Highlightr: 70c4df19e4aa55aa1b4387fb98182abce1dec9da
Expand Down
4 changes: 0 additions & 4 deletions Pods/GitHawkRoutes/GitHawkRoutes/IssueRoute.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Pods/GitHawkRoutes/GitHawkRoutes/RepoRoute.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion Pods/GitHawkRoutes/GitHawkRoutes/Routable.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions Pods/GitHawkRoutes/GitHawkRoutes/SearchShortcutRoute.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions Pods/GitHawkRoutes/GitHawkRoutes/UIApplication+Routable.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 595b76c

Please sign in to comment.