Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
Update to URLNavigator 2 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
devxoul committed Oct 14, 2017
1 parent 750efd2 commit e2db81b
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 79 deletions.
4 changes: 4 additions & 0 deletions Drrrible.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
03E1DC7F1F42FACC003E9F7A /* Matchers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03E1DC7E1F42FACC003E9F7A /* Matchers.swift */; };
03ED15691EEABA92006E1638 /* AnalyticsEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03ED15681EEABA92006E1638 /* AnalyticsEvent.swift */; };
03EE6F791F250B0600C2C6D8 /* UIGestureRecognizer+Test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03EE6F781F250B0600C2C6D8 /* UIGestureRecognizer+Test.swift */; };
03EF17891F9292130042C431 /* StubNavigator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03EF17881F9292130042C431 /* StubNavigator.swift */; };
03F727D81F643B51004FAEAB /* ShotSectionReactor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03F727D71F643B51004FAEAB /* ShotSectionReactor.swift */; };
03FE6AF41F23C835005D04DA /* ShotCellReactorSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03FE6AF31F23C835005D04DA /* ShotCellReactorSpec.swift */; };
03FE6AF61F23C96E005D04DA /* ShotTileCellSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03FE6AF51F23C96E005D04DA /* ShotTileCellSpec.swift */; };
Expand Down Expand Up @@ -272,6 +273,7 @@
03E1DC7E1F42FACC003E9F7A /* Matchers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Matchers.swift; sourceTree = "<group>"; };
03ED15681EEABA92006E1638 /* AnalyticsEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnalyticsEvent.swift; sourceTree = "<group>"; };
03EE6F781F250B0600C2C6D8 /* UIGestureRecognizer+Test.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIGestureRecognizer+Test.swift"; sourceTree = "<group>"; };
03EF17881F9292130042C431 /* StubNavigator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StubNavigator.swift; sourceTree = "<group>"; };
03F727D71F643B51004FAEAB /* ShotSectionReactor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShotSectionReactor.swift; sourceTree = "<group>"; };
03FE6AF31F23C835005D04DA /* ShotCellReactorSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShotCellReactorSpec.swift; sourceTree = "<group>"; };
03FE6AF51F23C96E005D04DA /* ShotTileCellSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShotTileCellSpec.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -357,6 +359,7 @@
036CDD8F1F239A02006C91CC /* StubAppDelegate.swift */,
036CDD901F239A02006C91CC /* StubError.swift */,
034D832E1F246BD700975F2F /* StubImageDownloader.swift */,
03EF17881F9292130042C431 /* StubNavigator.swift */,
);
path = Stubs;
sourceTree = "<group>";
Expand Down Expand Up @@ -1120,6 +1123,7 @@
037142631F64F64E0055F0C1 /* StubShotViewTitleCellDependency.swift in Sources */,
0303CAE81F4333EE00FF2E77 /* StubAnalytics.swift in Sources */,
036CDD771F236615006C91CC /* LoginViewControllerSpec.swift in Sources */,
03EF17891F9292130042C431 /* StubNavigator.swift in Sources */,
0371425F1F64F2670055F0C1 /* ShotViewImageCellSpec.swift in Sources */,
03AB28E01F49C703008D8353 /* StubShotViewReactionCellReactor.swift in Sources */,
03E1DC7F1F42FACC003E9F7A /* Matchers.swift in Sources */,
Expand Down
73 changes: 7 additions & 66 deletions Drrrible/Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,14 @@

import UIKit

import CGFloatLiteral
import Crashlytics
import Fabric
import Firebase
import Immutable
import Kingfisher
import ManualLayout
import RxCodable
import RxGesture
import RxOptional
import RxViewController
import SnapKit
import SwiftyColor
import SwiftyImage
import Then
import TouchAreaInsets
import UITextView_Placeholder
import Umbrella
import URLNavigator
import WebLinking

final class AppDelegate: UIResponder, UIApplicationDelegate {

// MARK: Properties

class var shared: AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
var dependency: AppDependency!


// MARK: UI
Expand All @@ -49,57 +29,18 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
self.configureSDKs()
self.configureAppearance()

let window = UIWindow(frame: UIScreen.main.bounds)
window.backgroundColor = .white
window.rootViewController = CompositionRoot.rootViewController()
window.makeKeyAndVisible()

self.window = window
self.dependency = self.dependency ?? CompositionRoot.resolve()
self.dependency.configureSDKs()
self.dependency.configureAppearance()
self.window = self.dependency.window
return true
}

func application(
_ app: UIApplication,
open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any] = [:]
options: [UIApplicationOpenURLOptionsKey: Any] = [:]
) -> Bool {
if Navigator.open(url) {
return true
}
if Navigator.present(url, wrap: true) != nil {
return true
}
return false
}


// MARK: SDKs

private func configureSDKs() {
self.configureFabric()
self.configureFirebase()
}

private func configureFabric() {
Fabric.with([Crashlytics.self])
}

private func configureFirebase() {
FirebaseApp.configure()
}


// MARK: Appearance

private func configureAppearance() {
let navigationBarBackgroundImage = UIImage.resizable().color(.db_charcoal).image
UINavigationBar.appearance().setBackgroundImage(navigationBarBackgroundImage, for: .default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().barStyle = .black
UINavigationBar.appearance().tintColor = .db_slate
UITabBar.appearance().tintColor = .db_charcoal
return self.dependency.openURL(url, options)
}
}
78 changes: 74 additions & 4 deletions Drrrible/Sources/CompositionRoot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,45 @@
// Copyright © 2017 Suyeol Jeon. All rights reserved.
//

import UIKit

import CGFloatLiteral
import Crashlytics
import Fabric
import Firebase
import Immutable
import Kingfisher
import ManualLayout
import RxCodable
import RxGesture
import RxOptional
import RxViewController
import SnapKit
import SwiftyColor
import SwiftyImage
import Then
import TouchAreaInsets
import UITextView_Placeholder
import Umbrella
import URLNavigator
import WebLinking

struct AppDependency {
typealias OpenURLHandler = (_ url: URL, _ options: [UIApplicationOpenURLOptionsKey: Any]) -> Bool

let window: UIWindow
let navigator: Navigator
let configureSDKs: () -> Void
let configureAppearance: () -> Void
let openURL: OpenURLHandler
}

final class CompositionRoot {
/// Builds a dependency graph and returns an entry view controller.
static func rootViewController() -> SplashViewController {
let authService = AuthService()
static func resolve() -> AppDependency {
let navigator = Navigator()

let authService = AuthService(navigator: navigator)
let networking = DrrribleNetworking(plugins: [AuthPlugin(authService: authService)])
let appStoreService = AppStoreService()
let userService = UserService(networking: networking)
Expand All @@ -21,7 +53,7 @@ final class CompositionRoot {
let analytics = DrrribleAnalytics()
analytics.register(provider: FirebaseProvider())

URLNavigationMap.initialize(authService: authService)
URLNavigationMap.initialize(navigator: navigator, authService: authService)

let productionImageOptions: ImageOptions = []

Expand Down Expand Up @@ -55,6 +87,7 @@ final class CompositionRoot {
}
let shotTileCellDependency = ShotTileCell.Dependency(
imageOptions: productionImageOptions,
navigator: navigator,
shotViewControllerFactory: { id, shot in
let reactor = ShotViewReactor(
shotID: id,
Expand Down Expand Up @@ -109,6 +142,43 @@ final class CompositionRoot {
presentLoginScreen: presentLoginScreen,
presentMainScreen: presentMainScreen
)
return splashViewController

let window = UIWindow(frame: UIScreen.main.bounds)
window.backgroundColor = .white
window.rootViewController = splashViewController
window.makeKeyAndVisible()
return AppDependency(
window: window,
navigator: navigator,
configureSDKs: self.configureSDKs,
configureAppearance: self.configureAppearance,
openURL: self.openURLFactory(navigator: navigator)
)
}

static func configureSDKs() {
Fabric.with([Crashlytics.self])
FirebaseApp.configure()
}

static func configureAppearance() {
let navigationBarBackgroundImage = UIImage.resizable().color(.db_charcoal).image
UINavigationBar.appearance().setBackgroundImage(navigationBarBackgroundImage, for: .default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().barStyle = .black
UINavigationBar.appearance().tintColor = .db_slate
UITabBar.appearance().tintColor = .db_charcoal
}

static func openURLFactory(navigator: NavigatorType) -> AppDependency.OpenURLHandler {
return { url, options -> Bool in
if navigator.open(url) {
return true
}
if navigator.present(url, wrap: UINavigationController.self) != nil {
return true
}
return false
}
}
}
7 changes: 5 additions & 2 deletions Drrrible/Sources/Services/AuthService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ final class AuthService: AuthServiceType {
fileprivate let keychain = Keychain(service: "com.drrrible.ios")
private(set) var currentAccessToken: AccessToken?

init() {
private let navigator: NavigatorType

init(navigator: NavigatorType) {
self.navigator = navigator
self.currentAccessToken = self.loadAccessToken()
log.debug("currentAccessToken exists: \(self.currentAccessToken != nil)")
}
Expand All @@ -59,7 +62,7 @@ final class AuthService: AuthServiceType {
let safariViewController = SFSafariViewController(url: url)
let navigationController = UINavigationController(rootViewController: safariViewController)
navigationController.isNavigationBarHidden = true
Navigator.present(navigationController)
self.navigator.present(navigationController)
self.currentViewController = navigationController

return self.callbackSubject
Expand Down
7 changes: 5 additions & 2 deletions Drrrible/Sources/Utils/URLNavigationMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import UIKit
import URLNavigator

final class URLNavigationMap {
static func initialize(authService: AuthServiceType) {
Navigator.map("drrrible://oauth/callback") { url, values in
static func initialize(
navigator: NavigatorType,
authService: AuthServiceType
) {
navigator.handle("drrrible://oauth/callback") { url, values, context in
guard let code = url.queryParameters["code"] else { return false }
authService.callback(code: code)
return true
Expand Down
3 changes: 2 additions & 1 deletion Drrrible/Sources/Views/ShotTileCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final class ShotTileCell: BaseCollectionViewCell, View {

struct Dependency {
let imageOptions: ImageOptions
let navigator: NavigatorType
let shotViewControllerFactory: (_ id: Int, _ shot: Shot?) -> ShotViewController
}

Expand Down Expand Up @@ -98,7 +99,7 @@ final class ShotTileCell: BaseCollectionViewCell, View {
.subscribe(onNext: { [weak reactor] _ in
guard let reactor = reactor else { return }
let viewController = dependency.shotViewControllerFactory(reactor.shot.id, reactor.shot)
Navigator.push(viewController)
dependency.navigator.push(viewController)
})
.disposed(by: self.disposeBag)
}
Expand Down
30 changes: 30 additions & 0 deletions DrrribleTests/Sources/Stubs/StubNavigator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// StubNavigator.swift
// DrrribleTests
//
// Created by Suyeol Jeon on 15/10/2017.
// Copyright © 2017 Suyeol Jeon. All rights reserved.
//

import Stubber
import URLNavigator

final class StubNavigator: NavigatorType {
let matcher = URLMatcher()

func register(_ pattern: URLPattern, _ factory: @escaping ViewControllerFactory) {
// do nothing
}

func handle(_ pattern: URLPattern, _ factory: @escaping URLOpenHandlerFactory) {
// do nothing
}

func viewController(for url: URLConvertible, context: Any?) -> UIViewController? {
return Stubber.invoke(viewController, args: (url, context), default: nil)
}

func handler(for url: URLConvertible, context: Any?) -> URLOpenHandler? {
return Stubber.invoke(handler, args: (url, context), default: nil)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extension ShotTileCell.Dependency {
}
return .init(
imageOptions: [.forceRefresh, .downloader(downloader)],
navigator: StubNavigator(),
shotViewControllerFactory: shotViewControllerFactory
)
}
Expand Down
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ target 'Drrrible' do
pod 'SwiftyColor'
pod 'SwiftyImage'
pod 'UITextView+Placeholder'
pod 'URLNavigator', :git => 'https://github.com/devxoul/URLNavigator.git', :branch => 'master'
pod 'URLNavigator', :git => 'https://github.com/devxoul/URLNavigator.git', :branch => 'urlnavigator2'
pod 'KeychainAccess'
pod 'Immutable'
pod 'Carte'
Expand Down
12 changes: 9 additions & 3 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ DEPENDENCIES:
- UITextView+Placeholder
- Umbrella
- Umbrella/Firebase
- URLNavigator
- URLNavigator (from `https://github.com/devxoul/URLNavigator.git`, branch `urlnavigator2`)
- WebLinking (from `https://github.com/devxoul/WebLinking.swift`, branch `swift-4.0`)

EXTERNAL SOURCES:
Expand All @@ -156,6 +156,9 @@ EXTERNAL SOURCES:
SectionReactor:
:branch: swift-4.0
:git: https://github.com/devxoul/SectionReactor.git
URLNavigator:
:branch: urlnavigator2
:git: https://github.com/devxoul/URLNavigator.git
WebLinking:
:branch: swift-4.0
:git: https://github.com/devxoul/WebLinking.swift
Expand All @@ -167,6 +170,9 @@ CHECKOUT OPTIONS:
SectionReactor:
:commit: 96aa587080bf5c278f554b85cdf37a36e9847a5a
:git: https://github.com/devxoul/SectionReactor.git
URLNavigator:
:commit: 3ac54a5018279478829a75be7495ff0c93fe9a60
:git: https://github.com/devxoul/URLNavigator.git
WebLinking:
:commit: 9f425b2dc9d48647d62f234be2527f962831728c
:git: https://github.com/devxoul/WebLinking.swift
Expand Down Expand Up @@ -215,9 +221,9 @@ SPEC CHECKSUMS:
UICollectionViewFlexLayout: 81ebb940737f0689ce35efd8c7cfa7c7f68d2d18
UITextView+Placeholder: 77680995fcdd07c3f52ec92fe1150874a2ac89ff
Umbrella: a4b409f07d023748ce84959363341ca7068c82ef
URLNavigator: 4a14714948dcb07461e4d6e64e9c9774c2f71b93
URLNavigator: 6d0462546e9f4b68d9aa379501e9d54218509310
WebLinking: a2ffdedbc8741b6cc490a3a06b747f7b528d6b7b

PODFILE CHECKSUM: da4cd5906802112a8c7903b89cbc81e2f24761e8
PODFILE CHECKSUM: 5ab460c0407472e4fd07b5347e3f8d10937026c7

COCOAPODS: 1.3.1

0 comments on commit e2db81b

Please sign in to comment.