-
-
Save hj56775/1bc09903de09d4204c5a9df205b0942b to your computer and use it in GitHub Desktop.
MVP-Example
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
import Foundation | |
class Amount: NSObject { | |
@objc dynamic var number: Int | |
override init() { | |
self.number = 0 | |
} | |
func increase() { | |
self.number += 1 | |
} | |
func decrease() { | |
self.number -= 1 | |
} | |
} |
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
import Foundation | |
class CountPresenter: CountViewPresenter { | |
private unowned var view: CountView | |
private var amount: Amount! | |
private var observation: NSKeyValueObservation? | |
required init(view: CountView) { | |
self.view = view | |
} | |
func viewDidLoad() { | |
self.amount = Amount() | |
self.view.onValueChange(number: 0) | |
self.observation = self.amount.observe(\.number, options: .new) { object, change in | |
guard let number = change.newValue else { return } | |
self.view.onValueChange(number: number) | |
} | |
} | |
func increaseButtonTouched() { | |
self.amount.increase() | |
} | |
func decreaseButtonTouched() { | |
self.amount.decrease() | |
} | |
} |
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
import Foundation | |
protocol CountView: AnyObject { | |
func onValueChange(number: Int) | |
} |
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
import Foundation | |
protocol CountViewPresenter { | |
init(view: CountView) | |
func viewDidLoad() | |
func increaseButtonTouched() | |
func decreaseButtonTouched() | |
} |
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
import UIKit | |
class SceneDelegate: UIResponder, UIWindowSceneDelegate { | |
var window: UIWindow? | |
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { | |
guard let scene = (scene as? UIWindowScene) else { return } | |
let view = ViewController() | |
let presenter = CountPresenter(view: view) | |
view.presenter = presenter | |
self.window = UIWindow(windowScene: scene) | |
self.window?.rootViewController = view | |
self.window?.makeKeyAndVisible() | |
} |
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
import UIKit | |
class ViewController: UIViewController { | |
private lazy var increaseButton: UIButton = { | |
let button = UIButton() | |
button.setImage(UIImage(systemName: "plus"), for: .normal) | |
button.addTarget(self, action: #selector(increaseButtonTouched(_sender:)), for: .touchUpInside) | |
return button | |
}() | |
private lazy var decreaseButton: UIButton = { | |
let button = UIButton() | |
button.setImage(UIImage(systemName: "minus"), for: .normal) | |
button.addTarget(self, action: #selector(decreaseButtonTouched(_sender:)), for: .touchUpInside) | |
return button | |
}() | |
private lazy var amountLabel: UILabel = { | |
let label = UILabel() | |
label.font = label.font.withSize(32) | |
label.text = String(0) | |
return label | |
}() | |
var presenter: CountViewPresenter? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.configureUI() | |
self.presenter?.viewDidLoad() | |
} | |
func configureUI() { | |
self.view.backgroundColor = .white | |
self.view.addSubview(self.amountLabel) | |
self.amountLabel.translatesAutoresizingMaskIntoConstraints = false | |
self.amountLabel.centerXAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerXAnchor).isActive = true | |
self.amountLabel.centerYAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerYAnchor).isActive = true | |
self.view.addSubview(self.increaseButton) | |
self.increaseButton.translatesAutoresizingMaskIntoConstraints = false | |
self.increaseButton.centerYAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerYAnchor).isActive = true | |
self.increaseButton.heightAnchor.constraint(equalToConstant: 50).isActive = true | |
self.increaseButton.widthAnchor.constraint(equalToConstant: 50).isActive = true | |
self.increaseButton.leadingAnchor.constraint(equalTo: self.amountLabel.trailingAnchor, constant: 20).isActive = true | |
self.view.addSubview(self.decreaseButton) | |
self.decreaseButton.translatesAutoresizingMaskIntoConstraints = false | |
self.decreaseButton.centerYAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerYAnchor).isActive = true | |
self.decreaseButton.heightAnchor.constraint(equalToConstant: 50).isActive = true | |
self.decreaseButton.widthAnchor.constraint(equalToConstant: 50).isActive = true | |
self.decreaseButton.trailingAnchor.constraint(equalTo: self.amountLabel.leadingAnchor, constant: -20).isActive = true | |
} | |
@objc func increaseButtonTouched(_sender: UIButton) { | |
self.presenter?.increaseButtonTouched() | |
} | |
@objc func decreaseButtonTouched(_sender: UIButton) { | |
self.presenter?.decreaseButtonTouched() | |
} | |
} | |
extension ViewController: CountView { | |
func onValueChange(number: Int) { | |
self.amountLabel.text = String(number) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment