Last active
July 12, 2019 03:00
-
-
Save GeekTree0101/48b454be535d00428ad54d076ba51405 to your computer and use it in GitHub Desktop.
CleanSwift Example
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
class DisplayCellNode: ASCellNode { | |
// UI | |
let buttonNode = ASButtonNode() | |
// Props | |
weak var interactor: InteractorLogics! | |
private var presenter: DisplayCellPresenter = .init() | |
override func didLoad() { | |
super.didLoad() | |
buttonNode.addTarget(self, didTapButton:, .touchUpInside) | |
} | |
@objc func didTapButton() { | |
// Request -> Response | |
let response = interactor.didTapButton(id: "user-12345") | |
// Response -> ViewModel | |
let viewModel = response | |
.then { [weak self] user in | |
return self?.username(user: user) ?? brokenPromise() | |
} | |
.recover { [weak self] error in | |
return self?.errorMessage(error: error) ?? brokenPromise() | |
} | |
// ViewModel -> Rendering | |
viewModel | |
.done { [weak self] username in | |
self?.buttonNode.text = username | |
self?.setNeedsLayout() | |
} | |
} | |
} |
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
// TDD: Stub Testing | |
protocol InteractorLogics { | |
func didTapButton(id: String) -> Promise<User> | |
} | |
class Interactor: InteractorLogics { | |
private var worker: WorkerLogics | |
init(worker: WorkerLogics) { | |
self.worker = worker | |
} | |
} | |
extension Interactor { | |
func didTapButton(id: String) -> Promise<User> { | |
return worker.getUser(id: id) | |
} | |
} |
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
// TDD: Stub Testing | |
struct DisplayCellPresenter { | |
func username(user: User) -> String { | |
return user.username | |
} | |
func errorMessage(error: Error) -> String { | |
return error.errorMessage | |
} | |
} |
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 WorkerLogics { | |
func getUser(id: String) -> Promise<User> | |
} | |
class Worker: WorkerLogics { | |
func getUser(id: String) -> Promise<User> { | |
return service.get(API.user(id: userID)) | |
} | |
} |
Author
GeekTree0101
commented
Jul 12, 2019
- 복잡한 기술적 기교를 줄이고
- 직관적이고 읽기 쉬운
- 적은 코드로 많은 테스트
- 버그 추적하기 쉬운
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment