diff --git a/SyncPod.xcodeproj/project.pbxproj b/SyncPod.xcodeproj/project.pbxproj index 8be50d6..2e1c7ac 100644 --- a/SyncPod.xcodeproj/project.pbxproj +++ b/SyncPod.xcodeproj/project.pbxproj @@ -22,6 +22,10 @@ 0D7FC6A020B172A400CB4169 /* ApplicationCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D7FC69F20B172A400CB4169 /* ApplicationCoordinator.swift */; }; 0D7FC6A320B1949D00CB4169 /* WelcomeCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D7FC6A220B1949D00CB4169 /* WelcomeCoordinator.swift */; }; 0D7FC6A520B1966300CB4169 /* BaseCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D7FC6A420B1966300CB4169 /* BaseCoordinator.swift */; }; + 0D7FC6AA20B1C18200CB4169 /* ViewControllerAssembly.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D7FC6A920B1C18200CB4169 /* ViewControllerAssembly.swift */; }; + 0D7FC6AC20B1C35B00CB4169 /* ViewModelAssembly.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D7FC6AB20B1C35B00CB4169 /* ViewModelAssembly.swift */; }; + 0D7FC6B020B1C6F300CB4169 /* CoordinatorAssembly.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D7FC6AF20B1C6F300CB4169 /* CoordinatorAssembly.swift */; }; + 0D7FC6B220B1C9DF00CB4169 /* DependencyManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D7FC6B120B1C9DF00CB4169 /* DependencyManager.swift */; }; 0D8BB2B62010A25700C7F6A9 /* ActionCableClient.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0D8BB2B52010A25700C7F6A9 /* ActionCableClient.framework */; }; 0D8BB2B82010A37900C7F6A9 /* Starscream.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0D8BB2B72010A37900C7F6A9 /* Starscream.framework */; }; 0DE94BFD201289A700E26582 /* YouTubePlayer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0DE94BFC201289A700E26582 /* YouTubePlayer.framework */; }; @@ -73,6 +77,10 @@ 0D7FC69F20B172A400CB4169 /* ApplicationCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApplicationCoordinator.swift; sourceTree = ""; }; 0D7FC6A220B1949D00CB4169 /* WelcomeCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeCoordinator.swift; sourceTree = ""; }; 0D7FC6A420B1966300CB4169 /* BaseCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseCoordinator.swift; sourceTree = ""; }; + 0D7FC6A920B1C18200CB4169 /* ViewControllerAssembly.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewControllerAssembly.swift; sourceTree = ""; }; + 0D7FC6AB20B1C35B00CB4169 /* ViewModelAssembly.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewModelAssembly.swift; sourceTree = ""; }; + 0D7FC6AF20B1C6F300CB4169 /* CoordinatorAssembly.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoordinatorAssembly.swift; sourceTree = ""; }; + 0D7FC6B120B1C9DF00CB4169 /* DependencyManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DependencyManager.swift; sourceTree = ""; }; 0D8BB2B52010A25700C7F6A9 /* ActionCableClient.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ActionCableClient.framework; path = Carthage/Build/iOS/ActionCableClient.framework; sourceTree = ""; }; 0D8BB2B72010A37900C7F6A9 /* Starscream.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Starscream.framework; path = Carthage/Build/iOS/Starscream.framework; sourceTree = ""; }; 0DE94BFA2012899600E26582 /* YouTubePlayer.framework.dSYM */ = {isa = PBXFileReference; lastKnownFileType = wrapper.dsym; name = YouTubePlayer.framework.dSYM; path = Carthage/Build/iOS/YouTubePlayer.framework.dSYM; sourceTree = ""; }; @@ -92,7 +100,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 0D86F07520B2AA8E00FA68C7 /* SwinjectStoryboard.framework in Frameworks */, + 0D7FC6B420B1DDA100CB4169 /* SwinjectStoryboard.framework in Frameworks */, 0DFF805820B157550087411D /* RxCocoa.framework in Frameworks */, 0DF11A0220A46E5C00D04895 /* Swinject.framework in Frameworks */, 0DF11A0020A46DDB00D04895 /* RxAlamofire.framework in Frameworks */, @@ -128,7 +136,7 @@ 0D4B438220099E540035DF1D /* Frameworks */ = { isa = PBXGroup; children = ( - 0D86F07420B2AA8E00FA68C7 /* SwinjectStoryboard.framework */, + 0D7FC6B320B1DDA100CB4169 /* SwinjectStoryboard.framework */, 0DFF805720B157550087411D /* RxCocoa.framework */, 0DFF805520B1573F0087411D /* RxCocoa.framework.dSYM */, 0DF11A0120A46E5C00D04895 /* Swinject.framework */, @@ -171,6 +179,7 @@ 0D5B826E1FC9A5D30055A72C /* SyncPod */ = { isa = PBXGroup; children = ( + 0D7FC6A620B1BD9200CB4169 /* di */, 0D7FC6A120B1945400CB4169 /* coordinator */, 0D7FC69B20B164BF00CB4169 /* viewmodel */, 0DFF805320B155CB0087411D /* view */, @@ -219,6 +228,17 @@ path = coordinator; sourceTree = ""; }; + 0D7FC6A620B1BD9200CB4169 /* di */ = { + isa = PBXGroup; + children = ( + 0D7FC6A920B1C18200CB4169 /* ViewControllerAssembly.swift */, + 0D7FC6AB20B1C35B00CB4169 /* ViewModelAssembly.swift */, + 0D7FC6AF20B1C6F300CB4169 /* CoordinatorAssembly.swift */, + 0D7FC6B120B1C9DF00CB4169 /* DependencyManager.swift */, + ); + path = di; + sourceTree = ""; + }; 0DFF805020B155890087411D /* storyboard */ = { isa = PBXGroup; children = ( @@ -411,9 +431,13 @@ files = ( 0D5B82701FC9A5D30055A72C /* AppDelegate.swift in Sources */, 0D7FC6A520B1966300CB4169 /* BaseCoordinator.swift in Sources */, + 0D7FC6B220B1C9DF00CB4169 /* DependencyManager.swift in Sources */, + 0D7FC6B020B1C6F300CB4169 /* CoordinatorAssembly.swift in Sources */, + 0D7FC6AC20B1C35B00CB4169 /* ViewModelAssembly.swift in Sources */, 0D7FC69D20B164D100CB4169 /* WelcomeViewModel.swift in Sources */, 0D7FC6A320B1949D00CB4169 /* WelcomeCoordinator.swift in Sources */, 0D7FC69A20B1636700CB4169 /* WelcomeViewController.swift in Sources */, + 0D7FC6AA20B1C18200CB4169 /* ViewControllerAssembly.swift in Sources */, 0D7FC6A020B172A400CB4169 /* ApplicationCoordinator.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/SyncPod/AppDelegate.swift b/SyncPod/AppDelegate.swift index acb8bb3..1ad2679 100644 --- a/SyncPod/AppDelegate.swift +++ b/SyncPod/AppDelegate.swift @@ -7,26 +7,29 @@ // import UIKit +import Swinject +import SwinjectStoryboard @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - lazy var applicationCoordinator = ApplicationCoordinator(window: self.window!) + lazy var coordinator = DependencyManager.getResolver().resolve(ApplicationCoordinator.self)! func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? ) -> Bool { - applicationCoordinator.start() + coordinator.start() return true } func application( - _ application: UIApplication, open url: URL, + _ application: UIApplication, + open url: URL, options: [UIApplicationOpenURLOptionsKey: Any] = [:] ) -> Bool { - applicationCoordinator.start() + coordinator.start() return true } diff --git a/SyncPod/coordinator/ApplicationCoordinator.swift b/SyncPod/coordinator/ApplicationCoordinator.swift index 8d93810..001f93e 100644 --- a/SyncPod/coordinator/ApplicationCoordinator.swift +++ b/SyncPod/coordinator/ApplicationCoordinator.swift @@ -9,12 +9,10 @@ import UIKit class ApplicationCoordinator: BaseCoordinator { - private let window: UIWindow private let welcomeCoordinator: WelcomeCoordinator - init(window: UIWindow) { - self.window = window - self.welcomeCoordinator = WelcomeCoordinator(window: window) + init(welcomeCoordinator: WelcomeCoordinator) { + self.welcomeCoordinator = welcomeCoordinator } func start() { diff --git a/SyncPod/coordinator/WelcomeCoordinator.swift b/SyncPod/coordinator/WelcomeCoordinator.swift index 85bd34d..4bb6e71 100644 --- a/SyncPod/coordinator/WelcomeCoordinator.swift +++ b/SyncPod/coordinator/WelcomeCoordinator.swift @@ -7,21 +7,18 @@ // import UIKit +import SwinjectStoryboard class WelcomeCoordinator: BaseCoordinator { + weak var delegate = UIApplication.shared.delegate as? AppDelegate private let window: UIWindow - private let storyboard: UIStoryboard + private let storyboard: SwinjectStoryboard private var nowViewController: UIViewController - init(window: UIWindow) { - self.window = window - self.storyboard = UIStoryboard(name: "Welcome", bundle: nil) + init() { + self.window = (delegate?.window)! + self.storyboard = SwinjectStoryboard.create(name: "Welcome", bundle: nil) self.nowViewController = storyboard.instantiateViewController(withIdentifier: "WelcomeView") - if let welcomeVC = nowViewController as? WelcomeViewConstoller { - let viewModel = WelcomeViewModel() - viewModel.coordinator = self - welcomeVC.viewModel = viewModel - } } func start() { diff --git a/SyncPod/di/CoordinatorAssembly.swift b/SyncPod/di/CoordinatorAssembly.swift new file mode 100644 index 0000000..f7f6eac --- /dev/null +++ b/SyncPod/di/CoordinatorAssembly.swift @@ -0,0 +1,24 @@ +// +// CoordinatorAssembly.swift +// SyncPod +// +// Created by 森篤史 on 2018/05/20. +// Copyright © 2018年 Cyder. All rights reserved. +// + +import UIKit +import Swinject + +class CoordinatorAssembly: Assembly { + func assemble(container: Container) { + container.register(ApplicationCoordinator.self) { res in + ApplicationCoordinator( + welcomeCoordinator: res.resolve(WelcomeCoordinator.self)! + ) + }.inObjectScope(.container) + + container.register(WelcomeCoordinator.self) { _ in + WelcomeCoordinator() + }.inObjectScope(.container) + } +} diff --git a/SyncPod/di/DependencyManager.swift b/SyncPod/di/DependencyManager.swift new file mode 100644 index 0000000..cd74e7a --- /dev/null +++ b/SyncPod/di/DependencyManager.swift @@ -0,0 +1,22 @@ +// +// DependencyManager.swift +// SyncPod +// +// Created by 森篤史 on 2018/05/21. +// Copyright © 2018年 Cyder. All rights reserved. +// + +import Swinject +import SwinjectStoryboard + +class DependencyManager { + static let assembler = Assembler([ + CoordinatorAssembly(), + ViewModelAssembly(), + ViewControllerAssembly() + ], container: SwinjectStoryboard.defaultContainer) + + static func getResolver() -> Resolver { + return assembler.resolver + } +} diff --git a/SyncPod/di/ViewControllerAssembly.swift b/SyncPod/di/ViewControllerAssembly.swift new file mode 100644 index 0000000..c318dfa --- /dev/null +++ b/SyncPod/di/ViewControllerAssembly.swift @@ -0,0 +1,18 @@ +// +// ViewControllerAssembly.swift +// SyncPod +// +// Created by 森篤史 on 2018/05/20. +// Copyright © 2018年 Cyder. All rights reserved. +// + +import Swinject +import SwinjectStoryboard + +class ViewControllerAssembly: Assembly { + func assemble(container: Container) { + container.storyboardInitCompleted(WelcomeViewConstoller.self) { res, viewController in + viewController.viewModel = res.resolve(WelcomeViewModel.self)! + } + } +} diff --git a/SyncPod/di/ViewModelAssembly.swift b/SyncPod/di/ViewModelAssembly.swift new file mode 100644 index 0000000..ba927ae --- /dev/null +++ b/SyncPod/di/ViewModelAssembly.swift @@ -0,0 +1,19 @@ +// +// ViewModelAssembly.swift +// SyncPod +// +// Created by 森篤史 on 2018/05/20. +// Copyright © 2018年 Cyder. All rights reserved. +// + +import Swinject + +class ViewModelAssembly: Assembly { + func assemble(container: Container) { + container.register(WelcomeViewModel.self) { res in + WelcomeViewModel( + coordinator: res.resolve(WelcomeCoordinator.self)! + ) + }.inObjectScope(.container) + } +} diff --git a/SyncPod/view/viewcontroller/WelcomeViewController.swift b/SyncPod/view/viewcontroller/WelcomeViewController.swift index 4692fec..3dff73a 100644 --- a/SyncPod/view/viewcontroller/WelcomeViewController.swift +++ b/SyncPod/view/viewcontroller/WelcomeViewController.swift @@ -12,7 +12,7 @@ import RxCocoa class WelcomeViewConstoller: UIViewController { let disposeBag = DisposeBag() - var viewModel: WelcomeViewModel? + var viewModel: WelcomeViewModel! @IBOutlet weak var signInButton: UIBarButtonItem! @IBOutlet weak var signUpButton: UIBarButtonItem! @@ -20,15 +20,17 @@ class WelcomeViewConstoller: UIViewController { override func viewDidLoad() { super.viewDidLoad() + print(viewModel) + setUpDataBinding() } private func setUpDataBinding() { signInButton.rx.tap - .subscribe({ _ in self.viewModel?.onSignInClicked() }) + .subscribe({ _ in self.viewModel.onSignInClicked() }) .disposed(by: disposeBag) signUpButton.rx.tap - .subscribe({ _ in self.viewModel?.onSignUpClicked() }) + .subscribe({ _ in self.viewModel.onSignUpClicked() }) .disposed(by: disposeBag) } } diff --git a/SyncPod/viewmodel/WelcomeViewModel.swift b/SyncPod/viewmodel/WelcomeViewModel.swift index afdec42..a519310 100644 --- a/SyncPod/viewmodel/WelcomeViewModel.swift +++ b/SyncPod/viewmodel/WelcomeViewModel.swift @@ -9,13 +9,16 @@ import Foundation class WelcomeViewModel { - var coordinator: WelcomeCoordinator? + let coordinator: WelcomeCoordinator + init(coordinator: WelcomeCoordinator) { + self.coordinator = coordinator + } func onSignInClicked() { - coordinator?.navigateToSignIn() + coordinator.navigateToSignIn() } func onSignUpClicked() { - coordinator?.navigateToSignUp() + coordinator.navigateToSignUp() } }