Skip to content

Instantly share code, notes, and snippets.

@poksi592
poksi592 / ApplicationRouter.swift
Last active May 15, 2018 08:05
Application Router
public typealias ModuleCallback = ([String: Any]?, Data?, URLResponse?, Error?) -> Void
/**
Protocol defines application router, which function is
- register application modules
- access/open the modules
- provide the callback, result of the access
*/
protocol ApplicationRouterType: class {
@poksi592
poksi592 / Module.swift
Created May 14, 2018 13:42
Module infrastructure
public typealias ModuleParameters = [String: String]
/**
Application module represents a group off all the classes that implement a certain functionality of the module, like:
- Storyboard
- View Controllers
- Views, specific to the module
- Presenters, View Models and other Client architecture classes
- ...
class LoginModule: ModuleType {
var route: String = {
return "login"
}()
var paths: [String] = {
return ["/login",
"/logout",
"/payment-token"]
class ApplicationServices {
// ApplicationServices is a singleton, because it makes it easier to be accessed from anywhere to access its functions/services
static let shared = ApplicationServices()
let appRouter = ApplicationRouter()
func pay(amount: Double,
username: String,
password: String,
completion: @escaping (() -> Void)) {
/**
This interface defines few simple steps:
- store the module route, so it can be accessed many times
- route the request sent to the module with parameters to desired functionality which is encapsulated in the module
*/
protocol ModuleRouter {
var route: String { get set }
init(route: String)
class LoginInteractor {
func getPaymentToken(parameters: ModuleParameters?,
completion: @escaping (String?, HTTPURLResponse?, Error?) -> Void) {
let service = MockLoginNetworkService()
guard let parameters = parameters,
let username = parameters[LoginModuleParameters.username.rawValue],
let password = parameters[LoginModuleParameters.password.rawValue] else {
return
class PaymentsModuleRouter: ModuleRouter {
lazy var interactor = PaymentInteractor()
internal var route: String
required init(route: String) {
self.route = route
}
enum HTTPMethod: String {
case GET
case POST
}
class NetworkService {
func get(scheme: String? = nil,
host: String,
@objc class URLRouter: URLProtocol, URLSessionDataDelegate, URLSessionTaskDelegate {
// TODO: This is synchronisyng only write access, which might be inadequate in many cases
// Need to be replaced with proper full generic implementation of synchronized collection
private (set) var moduleQueue = DispatchQueue(label: "com.tandem.module.queue")
// MARK: URLProtocol methods overriding
override class func canInit(with task: URLSessionTask) -> Bool {
let nonCompliantModule = NonConformingModule()
nonCompliantModule.login(username: "myUsername",
password: "myPassword",
completion: { (bearerToken, error) in
print(String(describing: bearerToken))
})
class NonConformingModule {