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
extension NSDictionary { | |
var swiftDictionary: [String : AnyObject] { | |
var swiftDictionary: [String : AnyObject] = [:] | |
let keys = self.allKeys.flatMap { $0 as? String } | |
for key in keys { | |
let keyValue = self.value(forKey: key) as AnyObject | |
swiftDictionary[key] = keyValue | |
} | |
return swiftDictionary |
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
func debounce(_ action: @escaping () -> Void) { | |
let lastFireTime = DispatchTime.now() | |
let dispatchDelay = TimeInterval(0.3) | |
DispatchQueue.main.asyncAfter(deadline: .now() + dispatchDelay) { | |
let now = DispatchTime.now() | |
let when = lastFireTime + dispatchDelay | |
if now >= when { | |
action() | |
} |
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
final class Repository: Chainable { | |
var before: (()->())? | |
private let dataProdider: DataProvider | |
init(dataProdider: DataProvider = .init()) { | |
self.dataProdider = dataProdider | |
} | |
func getItem(onSuccess: @escaping (Item)->(), onError: @escaping (Error)->()) { | |
//background async request |
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
protocol Chainable: class { | |
var before: (()->())? { get set } | |
func before(_ callback: @escaping ()->()) -> Self | |
func invoke() | |
} | |
extension Chainable { | |
@discardableResult |
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
func onViewLoaded() { | |
view?.showLoadingIndicator() | |
repository | |
.before({ [weak self] in self?.view?.hideLoadingIndicator() }) | |
.getItem( | |
onSuccess: { [weak self] item in | |
print(item.description) | |
}, | |
onError: { [weak self] error in | |
print(error.localizedDescription) |
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
func onViewLoaded() { | |
view?.showLoadingIndicator() | |
repository.getItem( | |
onSuccess: { [weak self] item in | |
self?.view?.hideLoadingIndicator() | |
print(item.description) | |
}, | |
onError: { [weak self] error in | |
self?.view?.hideLoadingIndicator() | |
print(error.localizedDescription) |
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
final class Repository { | |
private let dataProdider: DataProvider | |
init(dataProdider: DataProvider = .init()) { | |
self.dataProdider = dataProdider | |
} | |
func getItem(onSuccess: @escaping (Item)->(), onError: @escaping (Error)->()) { | |
//background async request | |
dataProvider.request.items { [weak self] data, error in | |
if let item = ItemBuilder(with: data) { |
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
func applyStyle() { | |
StyleBuilder() | |
.guide() | |
.forEach { setStyle(with: $0) } | |
} | |
private func setStyle(with guide: Guide) { | |
switch guide { | |
case .headerLabel(let closure), | |
.bodyLabel(let closure): |
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
func perform<T>(with identifier: String, closure: (T) -> Void) { | |
Mirror(reflecting: self).children.forEach { property in | |
guard | |
property.label?.contain(identifier) == true, | |
let value = property.value as? T | |
else { return } | |
//run closure | |
closure(value) | |
} | |
} |
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
func guide() -> [Guide] { | |
return [ | |
.headerLabel(headerLabel), | |
.bodyLabel(bodyLabel), | |
.loginTextField(loginTextField) | |
] | |
} |
NewerOlder