@@ -7,20 +7,89 @@
77//
88
99import UIKit
10+import SwiftyVK
1011
1112class MainCoordinator {
1213 let window: UIWindow
1314 let mainVC = MainViewController()
15+ let loadingVC = UIViewController() // TODO: An actual loading VC
1416 let navC: UINavigationController!
1517 let secondaryCoord = SecondaryCoordinator()
1618 init(window: UIWindow) {
1719 self.window = window
20+ window.rootViewController = loadingVC
21+
22+
1823 navC = UINavigationController(rootViewController: mainVC)
19− window.rootViewController = navC
2024 updateScreens()
25+
26+ }
27+
28+ func start() {
29+ VK.configure(withAppId: appID, delegate: self)
30+ if VK.state == .authorized {
31+ authorized()
32+ } else {
33+ VK.logIn()
34+ }
2135 }
2236
2337 func updateScreens() {
2438 secondaryCoord.updateScreens()
2539 }
40+
41+ fileprivate func authorized() {
42+ print("didAuthorize")
43+ window.rootViewController = navC
44+ }
45+}
46+
47+extension MainCoordinator: VKDelegate {
48+ /** ---DEPRECATED. TOKEN NOW STORED IN KEYCHAIN--- Called when SwiftyVK need know where a token is located
49+ - returns: Path to save/read token or nil if should save token to UserDefaults*/
50+ func vkShouldUseTokenPath() -> String? {
51+ return nil
52+ }
53+
54+ func vkWillAuthorize() -> Set<VK.Scope> {
55+ //Called when SwiftyVK need authorization permissions.
56+ return permissions
57+ }
58+
59+ func vkDidAuthorizeWith(parameters: Dictionary<String, String>) {
60+ //Called when the user is log in.
61+ //Here you can start to send requests to the API.
62+ print("authorized")
63+ mainSync {
64+ self.window.rootViewController = self.navC
65+ }
66+ authorized()
67+ }
68+
69+ func vkAutorizationFailedWith(error: AuthError) {
70+ //Called when SwiftyVK could not authorize. To let the application know that something went wrong.
71+ print("failed auth with error \(error.errorCode): \(error.description)")
72+ mainSync {
73+ self.window.rootViewController = self.navC
74+ }
75+ }
76+
77+ func vkDidUnauthorize() {
78+ print("did unauthorize")
79+ mainSync {
80+ self.window.rootViewController = self.navC
81+ }
82+ //Called when user is log out.
83+ }
84+
85+ func vkWillPresentView() -> UIViewController {
86+ //Only for iOS!
87+ //Called when need to display a view from SwiftyVK.
88+ let authController = UIViewController()
89+ mainSync {
90+ self.window.rootViewController = authController
91+ }
92+ return authController //UIViewController that should present authorization view controller
93+ }
94+
2695}