-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Damagucci-Juice] step3 델리게이트 프로토콜로 작동하던 것을 노티피케이션 센터를 이용해서 설정 #90
base: gucci
Are you sure you want to change the base?
Changes from 13 commits
cb53995
c371611
ceff310
d27d4b1
3641efe
dd2dfe7
b20630d
6ad2131
e608915
5c84c26
1ae7c3f
31dccbe
04ae57a
9499d67
b252af7
da788e9
57a67ca
7daa8fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// NotificationCenter.swift | ||
// DrawingApp | ||
// | ||
// Created by YEONGJIN JANG on 2022/03/16. | ||
// | ||
|
||
import Foundation | ||
|
||
let notificationCenter = NotificationCenter.default | ||
|
||
extension Notification.Name { | ||
static let addRectangleView = Notification.Name("A rectangle is made") | ||
static let tappedRectangleView = Notification.Name("a rectangle view is Tapped") | ||
static let colorChange = Notification.Name("color changed") | ||
static let alphaChange = Notification.Name("alpha changed") | ||
} | ||
|
||
enum NotificationKey { | ||
case color | ||
case alpha | ||
case tappedRectangle | ||
case addedRectangle | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 노티는 행위에 가깝고, 키는 변수에 가까우니 그런 형태면 좋을 것 같고. 그외에는 겹치지 않는지. 명확한 의도를 전달하는 지 살펴보면 될 것 같습니다. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,7 @@ | ||
import Foundation | ||
|
||
protocol RectangleAddedDelegate { | ||
func made(rectangle : Rectangle) | ||
} | ||
|
||
protocol RectangleTouchedDelegate { | ||
func touched(rectangle: Rectangle) | ||
} | ||
|
||
protocol RectangleColorChangeDelegate { | ||
func didChangeColor(rectangle: Rectangle) | ||
} | ||
protocol RectangleAlaphaChangeDelegate { | ||
func didChangeAlpha(rectangle: Rectangle) | ||
} | ||
|
||
class Plane { | ||
private var rectangles: [Rectangle] = [] | ||
|
||
var addedRectangleDelegate :RectangleAddedDelegate? | ||
var rectangleTapDelegate : RectangleTouchedDelegate? | ||
var colorDelegate: RectangleColorChangeDelegate? | ||
var alphaDelegate: RectangleAlaphaChangeDelegate? | ||
|
||
var rectangleCount: Int { | ||
return rectangles.count | ||
|
@@ -31,15 +11,13 @@ class Plane { | |
func addRectangle() { | ||
let rectangle: Rectangle = Factory.createRandomRectangle() | ||
rectangles.append(rectangle) | ||
|
||
addedRectangleDelegate?.made(rectangle: rectangle) | ||
notificationCenter.post(name: .addRectangleView, object: nil, userInfo: [NotificationKey.addedRectangle : rectangle]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NotificationCenter의 object는 sender의 의미라서 보내는 객체도 항상 지정해주는 게 좋습니다. 그래야 옵저버 조건에서 매칭이 가능합니다 |
||
} | ||
|
||
subscript(index: Int) -> Rectangle { | ||
return rectangles[index] | ||
} | ||
|
||
//MARK: Bound Gate | ||
private func isTouchedOnRectangle(at point: Point) -> Rectangle? { | ||
var optionalRectangle: Rectangle? | ||
for rectangle in rectangles { | ||
|
@@ -54,18 +32,18 @@ class Plane { | |
guard let rectangle = isTouchedOnRectangle(at: point) else { | ||
return | ||
} | ||
self.rectangleTapDelegate?.touched(rectangle: rectangle) | ||
notificationCenter.post(name: Notification.Name.tappedRectangleView, object: nil, userInfo: [NotificationKey.tappedRectangle: rectangle]) | ||
} | ||
|
||
func changeColorOfRectangle(_ rectangle: Rectangle) { | ||
var oldRectangle = rectangle | ||
let newRectangle = oldRectangle.changeColor() | ||
self.colorDelegate?.didChangeColor(rectangle: newRectangle) | ||
notificationCenter.post(name: Notification.Name.colorChange, object: nil, userInfo: [NotificationKey.color: newRectangle]) | ||
} | ||
|
||
func changeAlpha(_ rectangle: Rectangle, _ alpha: Int) { | ||
var willChangeRectangle = rectangle | ||
willChangeRectangle = willChangeRectangle.changeAlpha(alpha) | ||
alphaDelegate?.didChangeAlpha(rectangle: willChangeRectangle) | ||
notificationCenter.post(name: Notification.Name.alphaChange, object: nil, userInfo: [NotificationKey.alpha: willChangeRectangle]) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,11 +40,18 @@ class ViewController: UIViewController { | |
self.plane.addRectangle() | ||
} | ||
|
||
private func declareDelegates() { | ||
plane.addedRectangleDelegate = self | ||
plane.rectangleTapDelegate = self | ||
plane.colorDelegate = self | ||
plane.alphaDelegate = self | ||
private func activateNotificationObservers() { | ||
// add rectangle | ||
notificationCenter.addObserver(self, selector: #selector(made(rectangleNoti: )), name: Notification.Name.addRectangleView, object: nil) | ||
|
||
// rectangle tapped | ||
notificationCenter.addObserver(self, selector: #selector(touched(rectangleNoti:)), name: Notification.Name.tappedRectangleView, object: nil) | ||
|
||
// color changed | ||
notificationCenter.addObserver(self, selector: #selector(didChangeColor(rectangleNoti:)), name: Notification.Name.colorChange, object: nil) | ||
|
||
// alpha changed | ||
notificationCenter.addObserver(self, selector: #selector(didChangeAlpha(rectangleNoti: )), name: Notification.Name.alphaChange, object: nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 옵저버를 등록할 때도 가능하면 object (sender)를 지정해주는 게 좋습니다. 그래야 그 타입이 보내는 그 메시지를 받을 수 있죠. |
||
} | ||
|
||
private func initDetailView() { | ||
|
@@ -58,14 +65,19 @@ class ViewController: UIViewController { | |
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
declareDelegates() | ||
|
||
activateNotificationObservers() | ||
initDetailView() | ||
touchBackgroundView() | ||
activateBackgroundTappable() | ||
} | ||
|
||
override func viewDidDisappear(_ animated: Bool) { | ||
notificationCenter.removeObserver(self) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. viewDidLoad는 뷰 컨트롤러 생성되고 한 번만 호출되지만, viewDidDisappear는 다른 뷰로 넘어가도 불리고 여러번 불릴 수 있죠. viewDidDisappear() 처럼 override 하는 함수는 꼭 super.xxx를 호출해주셔야 합니다. 그렇지 않으면 그 상태가 된 다음에 오동작을 할 수 있습니다. |
||
} | ||
} | ||
|
||
extension ViewController: UIGestureRecognizerDelegate { | ||
func touchBackgroundView() { | ||
func activateBackgroundTappable() { | ||
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:))) | ||
self.view.addGestureRecognizer(tap) | ||
} | ||
|
@@ -77,16 +89,19 @@ extension ViewController: UIGestureRecognizerDelegate { | |
} | ||
} | ||
|
||
extension ViewController: RectangleAddedDelegate { | ||
func made(rectangle: Rectangle) { | ||
extension ViewController { | ||
@objc func made(rectangleNoti : Notification) { | ||
guard let rectangle = rectangleNoti.userInfo?[NotificationKey.addedRectangle] as? Rectangle else { | ||
return | ||
} | ||
let rectView = UIView(frame: CGRect(x: rectangle.point.x, y: rectangle.point.y, width: rectangle.size.width, height: rectangle.size.height)) | ||
rectView.backgroundColor = UIColor(red: CGFloat(rectangle.color.R)/255.0, green: CGFloat(rectangle.color.G)/255.0, blue: CGFloat(rectangle.color.B)/255.0, alpha: CGFloat(rectangle.alpha.rawValue)/10.0) | ||
self.rectangleAndViewContainer[rectangle] = rectView | ||
self.view.addSubview(rectView) | ||
} | ||
} | ||
|
||
extension ViewController: RectangleTouchedDelegate { | ||
// | ||
extension ViewController { | ||
private func paintBorder(_ rectangle: Rectangle) { | ||
let touchedView = self.rectangleAndViewContainer[rectangle] | ||
touchedView?.layer.borderWidth = 3.0 | ||
|
@@ -107,7 +122,11 @@ extension ViewController: RectangleTouchedDelegate { | |
alphaSlider.value = Float(rect.alpha.rawValue) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for (key, value) in rectangleAndViewContainer 에서 view로 찾는 것은 좀 이상한데요. |
||
} | ||
} | ||
func touched(rectangle: Rectangle) { | ||
|
||
@objc func touched(rectangleNoti: Notification) { | ||
guard let rectangle = rectangleNoti.userInfo?[NotificationKey.tappedRectangle] as? Rectangle else { | ||
return | ||
} | ||
if let view = self.selectedView { | ||
view.layer.borderWidth = .zero | ||
} | ||
|
@@ -116,25 +135,30 @@ extension ViewController: RectangleTouchedDelegate { | |
} | ||
} | ||
|
||
extension ViewController: RectangleColorChangeDelegate { | ||
extension ViewController { | ||
func changeColorAndAlpha(_ rectangle: Rectangle) { | ||
let viewValue = rectangleAndViewContainer[rectangle] | ||
rectangleAndViewContainer.removeValue(forKey: rectangle) | ||
let viewValue = rectangleAndViewContainer.removeValue(forKey: rectangle) | ||
rectangleAndViewContainer[rectangle] = viewValue | ||
if let view = rectangleAndViewContainer[rectangle] { | ||
view.backgroundColor = UIColor(red: CGFloat(rectangle.color.R)/255.0, green: CGFloat(rectangle.color.G)/255.0, blue: CGFloat(rectangle.color.B)/255.0, alpha: CGFloat(rectangle.alpha.rawValue)/10.0) | ||
self.rectangleAndViewContainer.updateValue(view, forKey: rectangle) | ||
} | ||
} | ||
|
||
func didChangeColor(rectangle: Rectangle) { | ||
@objc func didChangeColor(rectangleNoti: Notification) { | ||
guard let rectangle = rectangleNoti.userInfo?[NotificationKey.color] as? Rectangle else { | ||
return | ||
} | ||
changeColorAndAlpha(rectangle) | ||
colorButton.setTitle("#\(rectangle.color.showRGBVlaue())", for: .normal) | ||
} | ||
} | ||
|
||
extension ViewController : RectangleAlaphaChangeDelegate { | ||
func didChangeAlpha(rectangle: Rectangle) { | ||
extension ViewController { | ||
@objc func didChangeAlpha(rectangleNoti: Notification) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rectangleNoti 는 notification의 이름으로 부정확한 것 같습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @objc func didChangeAlpha(from plane: Notification) { 플레인으로 부터 온 노티피케이션이라고 표현을 해보았는데 괜찮을까요? |
||
guard let rectangle = rectangleNoti.userInfo?[NotificationKey.alpha] as? Rectangle else { | ||
return | ||
} | ||
changeColorAndAlpha(rectangle) | ||
alphaSlider.value = Float(rectangle.alpha.rawValue) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NotificationCenter.default를 이렇게 줄이는 게 썩 좋은 표현이나 접근이 아닙니다.
싱글톤으로 접근하는 NotificationCenter.default 이런 값을 참조해 놓는 것이 좋지 않은 경우도 있습니다. 줄여쓰는 효과가 있는게 아니면 그대로 써도 됩니다.
NotificationCenter는 덜하지만 default가 참조하는 포인터가 바뀌는 경우도 있습니다. 그래서 이렇게 담아놓으면 다른 인스턴스를 가르킬 수도 있습니다. 싱글톤을 쓸 때는 그냥 이걸 쓰는게 더 좋습니다.