Last active
February 9, 2021 16:25
-
-
Save aromal-sasidharan/e6eb31495cb0fa8823618fec29059a33 to your computer and use it in GitHub Desktop.
Clean Architecture using Swift
This file contains hidden or 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
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