From 4658b5a8f1f9ed19f2a680eb59ac275031339625 Mon Sep 17 00:00:00 2001 From: Pavel Anokhov Date: Sun, 7 Jan 2018 23:34:50 +0300 Subject: [PATCH] Router. --- Adamant-ios.xcodeproj/project.pbxproj | 13 +++++ Adamant-ios/ServiceProtocols/Router.swift | 53 +++++++++++++++++++ .../Services/AdamantLoginService.swift | 2 + Adamant-ios/Services/SwinjectedRouter.swift | 28 ++++++++++ Adamant-ios/Stories/Login/Login.storyboard | 2 +- Adamant-ios/Stories/Login/LoginRoutes.swift | 17 ++++++ Adamant-ios/SwinjectDependencies.swift | 5 +- 7 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 Adamant-ios/ServiceProtocols/Router.swift create mode 100644 Adamant-ios/Services/SwinjectedRouter.swift create mode 100644 Adamant-ios/Stories/Login/LoginRoutes.swift diff --git a/Adamant-ios.xcodeproj/project.pbxproj b/Adamant-ios.xcodeproj/project.pbxproj index 7642227a6..2c8d84da5 100644 --- a/Adamant-ios.xcodeproj/project.pbxproj +++ b/Adamant-ios.xcodeproj/project.pbxproj @@ -32,6 +32,9 @@ E9E7CD9120026FA100DFC4DB /* SwinjectDependencies.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9E7CD9020026FA100DFC4DB /* SwinjectDependencies.swift */; }; E9E7CD932002740500DFC4DB /* AdamantLoginService.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9E7CD922002740500DFC4DB /* AdamantLoginService.swift */; }; E9E7CD952002812400DFC4DB /* LoginDependencies.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9E7CD942002812400DFC4DB /* LoginDependencies.swift */; }; + E9E7CDAF2002B8A100DFC4DB /* Router.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9E7CDAE2002B8A100DFC4DB /* Router.swift */; }; + E9E7CDB32002B9FB00DFC4DB /* LoginRoutes.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9E7CDB22002B9FB00DFC4DB /* LoginRoutes.swift */; }; + E9E7CDB52002BA6900DFC4DB /* SwinjectedRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9E7CDB42002BA6900DFC4DB /* SwinjectedRouter.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -75,6 +78,9 @@ E9E7CD9020026FA100DFC4DB /* SwinjectDependencies.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwinjectDependencies.swift; sourceTree = ""; }; E9E7CD922002740500DFC4DB /* AdamantLoginService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdamantLoginService.swift; sourceTree = ""; }; E9E7CD942002812400DFC4DB /* LoginDependencies.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LoginDependencies.swift; sourceTree = ""; }; + E9E7CDAE2002B8A100DFC4DB /* Router.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Router.swift; sourceTree = ""; }; + E9E7CDB22002B9FB00DFC4DB /* LoginRoutes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginRoutes.swift; sourceTree = ""; }; + E9E7CDB42002BA6900DFC4DB /* SwinjectedRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwinjectedRouter.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -150,6 +156,7 @@ E91947AB20001A9A001362F8 /* ApiService.swift */, E9E7CD8A20026B0600DFC4DB /* LoginService.swift */, E9E7CD8C20026B6600DFC4DB /* DialogService.swift */, + E9E7CDAE2002B8A100DFC4DB /* Router.swift */, ); path = ServiceProtocols; sourceTree = ""; @@ -161,6 +168,7 @@ E9E7CD922002740500DFC4DB /* AdamantLoginService.swift */, E913C9161FFFAF76001A83F7 /* JSAdamantCore.swift */, E9E7CD8E20026CD300DFC4DB /* SwinjectedDialogService.swift */, + E9E7CDB42002BA6900DFC4DB /* SwinjectedRouter.swift */, ); path = Services; sourceTree = ""; @@ -198,6 +206,7 @@ E919479920000FFD001362F8 /* Stories */ = { isa = PBXGroup; children = ( + E9E7CDA52002AE1C00DFC4DB /* Account */, E919479A20001007001362F8 /* Login */, ); path = Stories; @@ -208,6 +217,7 @@ children = ( E919479D20001048001362F8 /* Login.storyboard */, E9E7CD942002812400DFC4DB /* LoginDependencies.swift */, + E9E7CDB22002B9FB00DFC4DB /* LoginRoutes.swift */, E919479B2000102E001362F8 /* LoginViewController.swift */, ); path = Login; @@ -383,7 +393,10 @@ E9E7CD932002740500DFC4DB /* AdamantLoginService.swift in Sources */, E91947B420002809001362F8 /* Account.swift in Sources */, E913C8F41FFFA51D001A83F7 /* ViewController.swift in Sources */, + E9E7CDB52002BA6900DFC4DB /* SwinjectedRouter.swift in Sources */, + E9E7CDAF2002B8A100DFC4DB /* Router.swift in Sources */, E913C90B1FFFA965001A83F7 /* AdamantHash.swift in Sources */, + E9E7CDB32002B9FB00DFC4DB /* LoginRoutes.swift in Sources */, E91947B22000246A001362F8 /* AdamantError.swift in Sources */, E913C8F21FFFA51D001A83F7 /* AppDelegate.swift in Sources */, E913C90D1FFFA99B001A83F7 /* Keypair.swift in Sources */, diff --git a/Adamant-ios/ServiceProtocols/Router.swift b/Adamant-ios/ServiceProtocols/Router.swift new file mode 100644 index 000000000..c21a615f2 --- /dev/null +++ b/Adamant-ios/ServiceProtocols/Router.swift @@ -0,0 +1,53 @@ +// +// Router.swift +// Adamant-ios +// +// Created by Anokhov Pavel on 07.01.2018. +// Copyright © 2018 Adamant. All rights reserved. +// + +import UIKit + +// MARK: - Adamant Story struct +struct AdamantStory: Equatable, Hashable { + let name: String + + init(_ name: String) { + self.name = name + } + + static func ==(lhs: AdamantStory, rhs: AdamantStory) -> Bool { + return lhs.name == rhs.name + } + + var hashValue: Int { + return name.hashValue &* 171717 + } +} + +// MARK: - Adamant Scene +struct AdamantScene: Equatable, Hashable { + let identifier: String + let story: AdamantStory + + init(story: AdamantStory, identifier: String) { + self.story = story + self.identifier = identifier + } + + static func ==(lhs: AdamantScene, rhs: AdamantScene) -> Bool { + return lhs.identifier == rhs.identifier + } + + var hashValue: Int { + return identifier.hashValue ^ story.hashValue &* 717171 + } +} + + + +// MARK: - Adamant Router +protocol Router { + func get(story: AdamantStory) -> UIStoryboard + func get(scene: AdamantScene) -> UIViewController +} diff --git a/Adamant-ios/Services/AdamantLoginService.swift b/Adamant-ios/Services/AdamantLoginService.swift index 6b88c43c0..f472683b0 100644 --- a/Adamant-ios/Services/AdamantLoginService.swift +++ b/Adamant-ios/Services/AdamantLoginService.swift @@ -12,6 +12,8 @@ import UIKit class AdamantLoginService: LoginService { private struct Constants { static let loginStoryboard = "Login" + + private init() {} } // MARK: - Dependencies diff --git a/Adamant-ios/Services/SwinjectedRouter.swift b/Adamant-ios/Services/SwinjectedRouter.swift new file mode 100644 index 000000000..1348cefa2 --- /dev/null +++ b/Adamant-ios/Services/SwinjectedRouter.swift @@ -0,0 +1,28 @@ +// +// SwinjectedRouter.swift +// Adamant-ios +// +// Created by Anokhov Pavel on 07.01.2018. +// Copyright © 2018 Adamant. All rights reserved. +// + +import UIKit +import SwinjectStoryboard + +class SwinjectedRouter: Router { + private var storyboards = [AdamantStory: UIStoryboard]() + + func get(story: AdamantStory) -> UIStoryboard { + if let storyboard = storyboards[story] { + return storyboard + } else { + let storyboard = SwinjectStoryboard.create(name: story.name, bundle: nil) + storyboards[story] = storyboard + return storyboard + } + } + + func get(scene: AdamantScene) -> UIViewController { + return get(story: scene.story).instantiateViewController(withIdentifier: scene.identifier) + } +} diff --git a/Adamant-ios/Stories/Login/Login.storyboard b/Adamant-ios/Stories/Login/Login.storyboard index eafaeaf8d..61f2d1844 100644 --- a/Adamant-ios/Stories/Login/Login.storyboard +++ b/Adamant-ios/Stories/Login/Login.storyboard @@ -22,7 +22,7 @@ - + diff --git a/Adamant-ios/Stories/Login/LoginRoutes.swift b/Adamant-ios/Stories/Login/LoginRoutes.swift new file mode 100644 index 000000000..d1165a47d --- /dev/null +++ b/Adamant-ios/Stories/Login/LoginRoutes.swift @@ -0,0 +1,17 @@ +// +// LoginRoutes.swift +// Adamant-ios +// +// Created by Anokhov Pavel on 07.01.2018. +// Copyright © 2018 Adamant. All rights reserved. +// + +import Foundation + +extension AdamantStory { + static let Login = AdamantStory("Login") +} + +extension AdamantScene { + static let LoginDetails = AdamantScene(story: .Login, identifier: "LoginViewController") +} diff --git a/Adamant-ios/SwinjectDependencies.swift b/Adamant-ios/SwinjectDependencies.swift index aa1bf3814..9c177eec1 100644 --- a/Adamant-ios/SwinjectDependencies.swift +++ b/Adamant-ios/SwinjectDependencies.swift @@ -12,11 +12,10 @@ import SwinjectStoryboard // MARK: Services extension Container { func registerAdamantServices(coreJsUrl core: URL, utilitiesJsUrl utils: URL) { - self.register(DialogService.self) { _ in SwinjectedDialogService() }.inObjectScope(.container) self.register(AdamantCore.self) { _ in try! JSAdamantCore(coreJsUrl: core, utilitiesJsUrl: utils) } - + self.register(DialogService.self) { _ in SwinjectedDialogService() }.inObjectScope(.container) + self.register(Router.self) { _ in SwinjectedRouter() }.inObjectScope(.container) self.register(ApiService.self) { r in AdamantApiService(adamantCore: r.resolve(AdamantCore.self)!) }.inObjectScope(.container) - self.register(LoginService.self) { r in let api = r.resolve(ApiService.self)! let dialog = r.resolve(DialogService.self)!