forked from turbolinks/turbolinks-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationController.swift
124 lines (104 loc) · 4.11 KB
/
ApplicationController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import Turbolinks
import UIKit
import WebKit
class ApplicationController: UINavigationController {
fileprivate let url = URL(string: "http://localhost:9292")!
fileprivate let webViewProcessPool = WKProcessPool()
fileprivate var application: UIApplication {
return UIApplication.shared
}
fileprivate lazy var webViewConfiguration: WKWebViewConfiguration = {
let configuration = WKWebViewConfiguration()
configuration.userContentController.add(self, name: "turbolinksDemo")
configuration.processPool = self.webViewProcessPool
configuration.applicationNameForUserAgent = "TurbolinksDemo"
return configuration
}()
fileprivate lazy var session: Session = {
let session = Session(webViewConfiguration: self.webViewConfiguration)
session.delegate = self
return session
}()
override func viewDidLoad() {
super.viewDidLoad()
// Switching this to false will prevent content from sitting beneath scrollbar
navigationBar.isTranslucent = true
presentVisitableForSession(session, url: url)
}
fileprivate func presentVisitableForSession(_ session: Session, url: URL, action: Action = .Advance) {
let visitable = DemoViewController(url: url)
if action == .Advance {
pushViewController(visitable, animated: true)
} else if action == .Replace {
popViewController(animated: false)
pushViewController(visitable, animated: false)
}
session.visit(visitable)
}
fileprivate func presentNumbersViewController() {
let viewController = NumbersViewController()
pushViewController(viewController, animated: true)
}
fileprivate func presentAuthenticationController() {
let authenticationController = AuthenticationController()
authenticationController.delegate = self
authenticationController.webViewConfiguration = webViewConfiguration
authenticationController.url = url.appendingPathComponent("sign-in")
authenticationController.title = "Sign in"
let authNavigationController = UINavigationController(rootViewController: authenticationController)
present(authNavigationController, animated: true, completion: nil)
}
}
extension ApplicationController: SessionDelegate {
func session(_ session: Session, didProposeVisitToURL URL: Foundation.URL, withAction action: Action) {
if URL.path == "/numbers" {
presentNumbersViewController()
} else {
presentVisitableForSession(session, url: URL, action: action)
}
}
func session(_ session: Session, didFailRequestForVisitable visitable: Visitable, withError error: NSError)
{
NSLog("ERROR: %@", error)
guard let demoViewController = visitable as? DemoViewController,
let errorCode = ErrorCode(rawValue: error.code)
else { return }
switch errorCode {
case .httpFailure:
let statusCode = error.userInfo["statusCode"] as! Int
switch statusCode {
case 401:
presentAuthenticationController()
case 404:
demoViewController.presentError(.HTTPNotFoundError)
default:
demoViewController.presentError(Error(HTTPStatusCode: statusCode))
}
case .networkFailure:
demoViewController.presentError(.NetworkError)
}
}
func sessionDidStartRequest(_ session: Session) {
application.isNetworkActivityIndicatorVisible = true
}
func sessionDidFinishRequest(_ session: Session) {
application.isNetworkActivityIndicatorVisible = false
}
}
extension ApplicationController: AuthenticationControllerDelegate {
func authenticationControllerDidAuthenticate(_ authenticationController: AuthenticationController) {
session.reload()
dismiss(animated: true, completion: nil)
}
}
extension ApplicationController: WKScriptMessageHandler {
func userContentController(
_ userContentController: WKUserContentController, didReceive message: WKScriptMessage
) {
if let message = message.body as? String {
let alertController = UIAlertController(title: "Turbolinks", message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alertController, animated: true, completion: nil)
}
}
}