Skip to content
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

Open
wants to merge 18 commits into
base: gucci
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
cb53995
Fix : changeColorAndAlpha(_ rectangle: ) 에서 딕셔너리 value 값을 remove로 추출
Damagucci-Juice Mar 16, 2022
c371611
Add : NotificationCenter.swift 를 추가하고 기초 설정 셋업
Damagucci-Juice Mar 16, 2022
ceff310
Fix : addRectangle 을 delegate에서 notificationCenter로 이관하려는 사전작업
Damagucci-Juice Mar 16, 2022
d27d4b1
Feat : Plane class 에 addRectangle() 에서 post를 날림
Damagucci-Juice Mar 16, 2022
3641efe
Feat : ViewController 에서 @objc made(rectangleNoti:) 를 완성
Damagucci-Juice Mar 16, 2022
dd2dfe7
Feat : notification observer 해체 조건 설정
Damagucci-Juice Mar 16, 2022
b20630d
Feat : addOberserver(... obeject: nil) 로 설정
Damagucci-Juice Mar 16, 2022
6ad2131
Chore : 사용하지 않는 주석제거
Damagucci-Juice Mar 16, 2022
e608915
Fix : 사각형 뷰가 터치가 되면 NotificationCenter를 이용하도록 변경
Damagucci-Juice Mar 16, 2022
5c84c26
Fix : 사각형 뷰의 알파값이 변경되면 NotificationCenter를 이용하도록 변경
Damagucci-Juice Mar 16, 2022
1ae7c3f
Chore : 줄 사이 불필요한 띄어쓰기 제거
Damagucci-Juice Mar 16, 2022
31dccbe
Fix : 터치를 가능하게 하는 함수명 변경
Damagucci-Juice Mar 16, 2022
04ae57a
Update : README.md 수정
Damagucci-Juice Mar 16, 2022
9499d67
Refactor : NotificationCenter.Name 과 NotificationKey 의 이름 변경
Damagucci-Juice Mar 18, 2022
b252af7
Refactor : Notification post 와 addObserver 에서 object 를 nil 에서 self, p…
Damagucci-Juice Mar 18, 2022
da788e9
Refactor : 뷰 생명주기 중 viewWillAppear() 에 NotificationCenter 를 다루는 함수를 옮김
Damagucci-Juice Mar 18, 2022
57a67ca
Refactor : showDetailOfColorAndAlpha() 에 rectangle 매개변수를 넘김
Damagucci-Juice Mar 18, 2022
7daa8fe
Refactor : @objc ... Notification 에 관련된 함수들에서 노티피케이션의 이름을 변경
Damagucci-Juice Mar 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions DrawingApp/DrawingApp/NotificationCenter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

// 노티는 행위에 가깝다.
extension Notification.Name {
static let add = Notification.Name("A rectangle is made")
static let select = Notification.Name("Select a rectangle")
static let change = Notification.Name("Change some property")
}
// 키는 변수에 가깝다.
enum NotificationKey {
case color
case alpha
case rectangle
}
32 changes: 5 additions & 27 deletions DrawingApp/DrawingApp/Plane.swift
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?
private(set) var rectangles: [Rectangle] = []

var rectangleCount: Int {
return rectangles.count
Expand All @@ -31,15 +11,13 @@ class Plane {
func addRectangle() {
let rectangle: Rectangle = Factory.createRandomRectangle()
rectangles.append(rectangle)

addedRectangleDelegate?.made(rectangle: rectangle)
NotificationCenter.default.post(name: .add, object: self, userInfo: [NotificationKey.rectangle : rectangle])
}

subscript(index: Int) -> Rectangle {
return rectangles[index]
}

//MARK: Bound Gate
private func isTouchedOnRectangle(at point: Point) -> Rectangle? {
var optionalRectangle: Rectangle?
for rectangle in rectangles {
Expand All @@ -54,18 +32,18 @@ class Plane {
guard let rectangle = isTouchedOnRectangle(at: point) else {
return
}
self.rectangleTapDelegate?.touched(rectangle: rectangle)
NotificationCenter.default.post(name: Notification.Name.select, object: self, userInfo: [NotificationKey.rectangle: rectangle])
}

func changeColorOfRectangle(_ rectangle: Rectangle) {
var oldRectangle = rectangle
let newRectangle = oldRectangle.changeColor()
self.colorDelegate?.didChangeColor(rectangle: newRectangle)
NotificationCenter.default.post(name: Notification.Name.change, object: self, userInfo: [NotificationKey.color: newRectangle])
}

func changeAlpha(_ rectangle: Rectangle, _ alpha: Int) {
var willChangeRectangle = rectangle
willChangeRectangle = willChangeRectangle.changeAlpha(alpha)
alphaDelegate?.didChangeAlpha(rectangle: willChangeRectangle)
NotificationCenter.default.post(name: Notification.Name.change, object: self, userInfo: [NotificationKey.alpha: willChangeRectangle])
}
}
84 changes: 52 additions & 32 deletions DrawingApp/DrawingApp/ViewController/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.default.addObserver(self, selector: #selector(made(from: )), name: Notification.Name.add, object: plane)

// rectangle tapped
NotificationCenter.default.addObserver(self, selector: #selector(touched(from:)), name: Notification.Name.select, object: plane)

// color changed
NotificationCenter.default.addObserver(self, selector: #selector(didChangeColor(rectangleNoti:)), name: Notification.Name.change, object: plane)

// alpha changed
NotificationCenter.default.addObserver(self, selector: #selector(didChangeAlpha(from: )), name: Notification.Name.change, object: plane)
}

private func initDetailView() {
Expand All @@ -58,14 +65,23 @@ class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
declareDelegates()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
activateNotificationObservers()
initDetailView()
touchBackgroundView()
activateBackgroundTappable()
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
}

extension ViewController: UIGestureRecognizerDelegate {
func touchBackgroundView() {
func activateBackgroundTappable() {
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
self.view.addGestureRecognizer(tap)
}
Expand All @@ -77,64 +93,68 @@ extension ViewController: UIGestureRecognizerDelegate {
}
}

extension ViewController: RectangleAddedDelegate {
func made(rectangle: Rectangle) {
extension ViewController {
@objc func made(from plane : Notification) {
guard let rectangle = plane.userInfo?[NotificationKey.rectangle] 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
touchedView?.layer.borderColor = UIColor.blue.cgColor
self.selectedView = touchedView
}

private func showDetailOfColorAndAlpha() {
var rectangle : Rectangle?
for (key, value) in rectangleAndViewContainer {
if self.selectedView === value {
rectangle = key
break
}
}
if let rect = rectangle {
colorButton.setTitle("#\(rect.color.showRGBVlaue())", for: .normal)
alphaSlider.value = Float(rect.alpha.rawValue)
}
private func showDetailOfColorAndAlpha(_ rectangle: Rectangle) {
colorButton.setTitle("#\(rectangle.color.showRGBVlaue())", for: .normal)
alphaSlider.value = Float(rectangle.alpha.rawValue)
}
func touched(rectangle: Rectangle) {


@objc func touched(from plane: Notification) {
guard let rectangle = plane.userInfo?[NotificationKey.rectangle] as? Rectangle else {
return
}
if let view = self.selectedView {
view.layer.borderWidth = .zero
}
paintBorder(rectangle)
showDetailOfColorAndAlpha()
showDetailOfColorAndAlpha(rectangle)
}
}

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(from plane: Notification) {
guard let rectangle = plane.userInfo?[NotificationKey.alpha] as? Rectangle else {
return
}
changeColorAndAlpha(rectangle)
alphaSlider.value = Float(rectangle.alpha.rawValue)
}
Expand Down
Loading