Skip to content

Instantly share code, notes, and snippets.

@KhyeK
Forked from aromal-sasidharan/CleanArc.swift
Created February 9, 2021 16:25
Show Gist options
  • Save KhyeK/da6ae007675a30e08d81904c6464ed69 to your computer and use it in GitHub Desktop.
Save KhyeK/da6ae007675a30e08d81904c6464ed69 to your computer and use it in GitHub Desktop.
Clean Architecture using Swift
protocol M {
func convert(stringInt: [String]) -> [Int]
}
protocol W {
func fetchDays() -> [String]
}
protocol I {
var worker: W? {get set}
var output: IO? {get set}
func calculateTotal()
}
protocol IO {
func onTotalcalculated(total: Int)
}
protocol PO {
func onTotalcalculated(total: String)
}
class ViewController: PO {
func onTotalcalculated(total: String) {
print("result \n", total)
}
var output: I?
func viewDidLoad() {
output?.calculateTotal()
}
}
class Presenter: IO {
var output: PO?
func onTotalcalculated(total: Int) {
let result = "total sum is \(total)"
output?.onTotalcalculated(total: result)
}
init(output: PO?) {
self.output = output
}
}
class Mapper: M {
func convert(stringInt: [String]) -> [Int] {
let ints = stringInt.map({Int($0)}).compactMap({$0})
return ints
}
}
class Interactor: I {
func calculateTotal() {
let daysString = worker?.fetchDays() ?? []
let days = mapper?.convert(stringInt: daysString)
let sum:Int = days?
// .map({$0*2})
.reduce(0, +) ?? 0
output?.onTotalcalculated(total: sum)
}
var output: IO?
var worker: W?
var mapper: M?
init(worker: W, mapper: M, output: IO?) {
self.worker = worker
self.output = output
self.mapper = mapper
}
}
class Worker: W {
func fetchDays() -> [String] {
return ["1","2","3","7","10","20"]
}
}
// configuration
let worker: W = Worker()
let mapper: M = Mapper()
var viewController: ViewController = ViewController()
let presenter: IO = Presenter(output: viewController)
let interactor: I = Interactor(worker: worker, mapper: mapper, output: presenter)
viewController.output = interactor
viewController.viewDidLoad()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment