Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Adds Dependency Injection mechanism and change protocol conformance #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions DependencyInjection/ServiceLocator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ServiceLocator.swift
//
// Created by Jesus Martin Alonso on 13/11/2019.
//

import Foundation

//Protocol to implement by ServiceLocator objects that provides all the dependencies for the app
protocol ServiceLocator {
//Repositories
var myRepository: MyRepository {get}
}
19 changes: 19 additions & 0 deletions DependencyInjection/ServiceLocatorImpl.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// ServiceLocatorImpl.swift
//
// Created by Jesus Martin Alonso on 13/11/2019.
//

import Foundation

/// ServiceLocator default implementation. We use lazy vars to avoid loading all dependencies when ServiceLocator instance is created
class ServiceLocatorImpl: ServiceLocator {

private lazy var remoteDataSource = {
MyRemoteDataSource()
}()

lazy var myRepository: MyRepository = {
MyRepositoryImpl(remoteDataSource: remoteDataSource)
}()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import UIKit

class ___VARIABLE_productName:identifier___Presenter: ___VARIABLE_productName:identifier___PresenterProtocol {
class ___VARIABLE_productName:identifier___Presenter {

weak private var view: ___VARIABLE_productName:identifier___ViewProtocol?
var interactor: ___VARIABLE_productName:identifier___InteractorProtocol?
Expand All @@ -23,3 +23,8 @@ class ___VARIABLE_productName:identifier___Presenter: ___VARIABLE_productName:id
}

}

//MARK: - ___VARIABLE_productName:identifier___PresenterProtocol
extension ___VARIABLE_productName:identifier___Presenter: ___VARIABLE_productName:identifier___PresenterProtocol {

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@

import Foundation

//MARK: Wireframe -
//MARK: - Wireframe
protocol ___VARIABLE_productName:identifier___WireframeProtocol: class {

}
//MARK: Presenter -
//MARK: - Presenter
protocol ___VARIABLE_productName:identifier___PresenterProtocol: class {

}

//MARK: Interactor -
//MARK: - Interactor
protocol ___VARIABLE_productName:identifier___InteractorProtocol: class {

var presenter: ___VARIABLE_productName:identifier___PresenterProtocol? { get set }
}

//MARK: View -
//MARK: - View
protocol ___VARIABLE_productName:identifier___ViewProtocol: class {

var presenter: ___VARIABLE_productName:identifier___PresenterProtocol? { get set }
Expand Down
10 changes: 8 additions & 2 deletions Module VIPER.xctemplate/Default/___FILEBASENAME___Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ import UIKit
class ___VARIABLE_productName:identifier___Router: ___VARIABLE_productName:identifier___WireframeProtocol {

weak var viewController: UIViewController?
let serviceLocator: ServiceLocator

static func createModule() -> UIViewController {
init(_ serviceLocator: ServiceLocator) {
self.serviceLocator = serviceLocator
}


static func createModule(serviceLocator: ServiceLocator) -> UIViewController {
// Change to get view from storyboard if not using progammatic UI
let view = ___VARIABLE_productName:identifier___ViewController(nibName: nil, bundle: nil)
let interactor = ___VARIABLE_productName:identifier___Interactor()
let router = ___VARIABLE_productName:identifier___Router()
let router = ___VARIABLE_productName:identifier___Router(serviceLocator)
let presenter = ___VARIABLE_productName:identifier___Presenter(interface: view, interactor: interactor, router: router)

view.presenter = presenter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import UIKit

class ___VARIABLE_productName:identifier___ViewController: UIViewController, ___VARIABLE_productName:identifier___ViewProtocol {
class ___VARIABLE_productName:identifier___ViewController: UIViewController {

var presenter: ___VARIABLE_productName:identifier___PresenterProtocol?

Expand All @@ -19,3 +19,8 @@ class ___VARIABLE_productName:identifier___ViewController: UIViewController, ___
}

}

//MARK: - ___VARIABLE_productName:identifier___ViewProtocol
extension ___VARIABLE_productName:identifier___ViewController: ___VARIABLE_productName:identifier___ViewProtocol{

}
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ This is an example, we're creating a Login module:
- [Default, without divide](/assets/defaultOutput.md)
- [With divided Interactor (Input & Output)](/assets/inputOutput.md)

## Depedendency injection

The `createModule` method of `Router` class receives a `ServiceLocator` object as argument. This object provides all the dependencies of the app and will be used to provides dependencies of the module.

In this way, the service locator object is passed between modules.

An example of ServiceLocator implementation can be found [here](DependencyInjection/ServiceLocatorImpl.swift)


## VIPER diagram overview
![Preview](/assets/viper_diagram.png)

Expand All @@ -48,7 +57,7 @@ Would you like decide what will be the next feature? now, you can do it [here](h
* [x] Create bash script to install more easy
* [x] Divide Interactor protocol (Input & Output) (**NEW!** Version 1.1)
* [x] Swift 4 & XCode 9 (**NEW!** Version 1.2)
* [ ] Add Dependency Injection Framework
* [x] Add Dependency Injection Framework (**NEW!** Service locator added)
* [ ] Customize name of components
* [ ] ~~Create groups in template~~ *(Only available for Project templates)*

Expand Down
7 changes: 3 additions & 4 deletions install.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ func moveTemplate(){
printInConsole("✅ Template installed succesfully 🎉. Enjoy it 🙂")

}else{

try _ = fileManager.replaceItemAt(URL(fileURLWithPath:"\(destinationPath)/\(templateName)"), withItemAt: URL(fileURLWithPath:templateName))

try fileManager.removeItem(atPath: "\(destinationPath)/\(templateName)")
try fileManager.copyItem(atPath: templateName, toPath: "\(destinationPath)/\(templateName)")
printInConsole("✅ Template already exists. So has been replaced succesfully 🎉. Enjoy it 🙂")
}
}
Expand All @@ -52,7 +51,7 @@ func shell(launchPath: String, arguments: [String]) -> String

let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: String.Encoding.utf8)!
if output.characters.count > 0 {
if output.count > 0 {
//remove newline character.
let lastIndex = output.index(before: output.endIndex)
return String(output[output.startIndex ..< lastIndex])
Expand Down