Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MODEL-VIEW-PRESENTER
- protocol EventHandler {
- func didTapButton()
- }
- protocol ViewProtocol: AnyObject {
- var state: String {get set}
- }
- class Presenter: EventHandler {
- weak var view: ViewProtocol?
- func didTapButton() {
- view?.state = "Hello"
- }
- }
- class View: ViewProtocol {
- let eventHandler: EventHandler
- init(eventHandler: EventHandler) {
- self.eventHandler = eventHandler
- }
- private func handleButtonTap() {
- eventHandler.didTapButton()
- }
- var state: String = "" {
- didSet {
- updateViews()
- }
- }
- private func updateViews() {
- print(state)
- }
- }
- //MODEL-VIEW-VIEWMODEL
- protocol ViewModelProtocol: AnyObject {
- func didTapButton()
- var state: String {get set}
- var stateWasChanged: (()->Void)? {get set}
- }
- class ViewModel: ViewModelProtocol {
- func didTapButton() {
- state = "Hello"
- }
- var state: String = "" {
- didSet {
- stateWasChanged?()
- }
- }
- var stateWasChanged: (() -> Void)?
- }
- class View {
- let viewModel: ViewModelProtocol
- init(viewModel: ViewModelProtocol) {
- self.viewModel = viewModel
- viewModel.stateWasChanged = { [weak self] in
- self?.updateViews()
- }
- }
- private func handleButtonTap() {
- viewModel.didTapButton()
- }
- private func updateViews() {
- print(viewModel.state)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement