This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
- ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LoginModule: ModuleType { | |
var route: String = { | |
return "login" | |
}() | |
var paths: [String] = { | |
return ["/login", | |
"/logout", | |
"/payment-token"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PaymentsModuleRouter: ModuleRouter { | |
lazy var interactor = PaymentInteractor() | |
internal var route: String | |
required init(route: String) { | |
self.route = route | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum HTTPMethod: String { | |
case GET | |
case POST | |
} | |
class NetworkService { | |
func get(scheme: String? = nil, | |
host: String, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 { | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let nonCompliantModule = NonConformingModule() | |
nonCompliantModule.login(username: "myUsername", | |
password: "myPassword", | |
completion: { (bearerToken, error) in | |
print(String(describing: bearerToken)) | |
}) | |
class NonConformingModule { |