diff --git a/DrawingApp/DrawingApp.xcodeproj/project.pbxproj b/DrawingApp/DrawingApp.xcodeproj/project.pbxproj index 17fa0277..dcdf512d 100644 --- a/DrawingApp/DrawingApp.xcodeproj/project.pbxproj +++ b/DrawingApp/DrawingApp.xcodeproj/project.pbxproj @@ -7,20 +7,11 @@ objects = { /* Begin PBXBuildFile section */ - 8D1AB31A27D9CFB50024AD38 /* Plane.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D5DF31927D0F88300F5E9C5 /* Plane.swift */; }; - 8D1AB31B27D9D0130024AD38 /* Size.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D69083627CE08E900F78A51 /* Size.swift */; }; - 8D1AB31C27D9D01B0024AD38 /* Point.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D69083827CE08F000F78A51 /* Point.swift */; }; - 8D1AB31E27D9D0370024AD38 /* RGB.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D69083C27CE249900F78A51 /* RGB.swift */; }; - 8D1AB31F27D9D0400024AD38 /* UIColor++.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DBC282A27D393A8007422D6 /* UIColor++.swift */; }; - 8D1AB32027D9D0680024AD38 /* Alpha.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D69083A27CE249200F78A51 /* Alpha.swift */; }; - 8D1AB32227D9D09F0024AD38 /* ID.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D69084E27CF4D9F00F78A51 /* ID.swift */; }; - 8D1AB32327D9D0A20024AD38 /* IDFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D69084C27CF4D4200F78A51 /* IDFactory.swift */; }; 8D41426027DB2AB30070FDCB /* ImageButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D41425F27DB2AB30070FDCB /* ImageButton.swift */; }; 8D5DF31A27D0F88300F5E9C5 /* Plane.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D5DF31927D0F88300F5E9C5 /* Plane.swift */; }; 8D5DF31C27D1A88900F5E9C5 /* DetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D5DF31B27D1A88900F5E9C5 /* DetailView.swift */; }; - 8D5DF32227D3107800F5E9C5 /* DetailViewDelgate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D5DF32127D3107800F5E9C5 /* DetailViewDelgate.swift */; }; + 8D5DF32227D3107800F5E9C5 /* DetailViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D5DF32127D3107800F5E9C5 /* DetailViewDelegate.swift */; }; 8D67432C27D72AD100397D68 /* RectangleButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D67432B27D72AD100397D68 /* RectangleButton.swift */; }; - 8D67432E27D7312200397D68 /* PlaneRectangleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D67432D27D7312200397D68 /* PlaneRectangleView.swift */; }; 8D6907F927CCBEF100F78A51 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D6907F827CCBEF100F78A51 /* AppDelegate.swift */; }; 8D6907FB27CCBEF100F78A51 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D6907FA27CCBEF100F78A51 /* SceneDelegate.swift */; }; 8D6907FD27CCBEF100F78A51 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D6907FC27CCBEF100F78A51 /* MainViewController.swift */; }; @@ -36,18 +27,10 @@ 8D7593A727DEDBAF00F9D8B4 /* Rectangleable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D7593A627DEDBAF00F9D8B4 /* Rectangleable.swift */; }; 8D7593A927DEDC2E00F9D8B4 /* PlaneRectangle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D7593A827DEDC2E00F9D8B4 /* PlaneRectangle.swift */; }; 8D7593AB27DEDC4100F9D8B4 /* ImageRectangle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D7593AA27DEDC4100F9D8B4 /* ImageRectangle.swift */; }; - 8D7593AD27DEDD1E00F9D8B4 /* RectangleCreator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D7593AC27DEDD1E00F9D8B4 /* RectangleCreator.swift */; }; 8D7593AF27DEDD5100F9D8B4 /* RectangleFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D7593AE27DEDD5100F9D8B4 /* RectangleFactory.swift */; }; 8D7593B127DEDE2000F9D8B4 /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D7593B027DEDE2000F9D8B4 /* Image.swift */; }; - 8D7593B627E0AC8A00F9D8B4 /* DrawingAppTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D1AB31327D884550024AD38 /* DrawingAppTests.swift */; }; - 8D7593B827E0AD0F00F9D8B4 /* PlaneRectangle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D7593A827DEDC2E00F9D8B4 /* PlaneRectangle.swift */; }; - 8D7593B927E0BBE300F9D8B4 /* RectangleCreator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D7593AC27DEDD1E00F9D8B4 /* RectangleCreator.swift */; }; - 8D7593BA27E0BBEE00F9D8B4 /* Rectangleable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D7593A627DEDBAF00F9D8B4 /* Rectangleable.swift */; }; - 8D7593BB27E0BBF200F9D8B4 /* RectangleFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D7593AE27DEDD5100F9D8B4 /* RectangleFactory.swift */; }; - 8D7593BD27E0CAC800F9D8B4 /* RectanlgeViewFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D7593BC27E0CAC800F9D8B4 /* RectanlgeViewFactory.swift */; }; - 8D7593BF27E0CAF100F9D8B4 /* RectangleViewCreator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D7593BE27E0CAF100F9D8B4 /* RectangleViewCreator.swift */; }; - 8D7593C327E189AA00F9D8B4 /* RectangleViewable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D7593C227E189AA00F9D8B4 /* RectangleViewable.swift */; }; - 8D7593C527E18B9D00F9D8B4 /* ImageRectangleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D7593C427E18B9D00F9D8B4 /* ImageRectangleView.swift */; }; + 8D7593BD27E0CAC800F9D8B4 /* RectangleViewFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D7593BC27E0CAC800F9D8B4 /* RectangleViewFactory.swift */; }; + 8D75A59F27E324280091EEC4 /* DrawingAppTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D1AB31327D884550024AD38 /* DrawingAppTests.swift */; }; 8DBC282B27D393A8007422D6 /* UIColor++.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DBC282A27D393A8007422D6 /* UIColor++.swift */; }; /* End PBXBuildFile section */ @@ -67,9 +50,8 @@ 8D41425F27DB2AB30070FDCB /* ImageButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageButton.swift; sourceTree = ""; }; 8D5DF31927D0F88300F5E9C5 /* Plane.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Plane.swift; sourceTree = ""; }; 8D5DF31B27D1A88900F5E9C5 /* DetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailView.swift; sourceTree = ""; }; - 8D5DF32127D3107800F5E9C5 /* DetailViewDelgate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailViewDelgate.swift; sourceTree = ""; }; + 8D5DF32127D3107800F5E9C5 /* DetailViewDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailViewDelegate.swift; sourceTree = ""; }; 8D67432B27D72AD100397D68 /* RectangleButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RectangleButton.swift; sourceTree = ""; }; - 8D67432D27D7312200397D68 /* PlaneRectangleView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlaneRectangleView.swift; sourceTree = ""; }; 8D6907F527CCBEF100F78A51 /* DrawingApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DrawingApp.app; sourceTree = BUILT_PRODUCTS_DIR; }; 8D6907F827CCBEF100F78A51 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 8D6907FA27CCBEF100F78A51 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; @@ -87,13 +69,9 @@ 8D7593A627DEDBAF00F9D8B4 /* Rectangleable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Rectangleable.swift; sourceTree = ""; }; 8D7593A827DEDC2E00F9D8B4 /* PlaneRectangle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaneRectangle.swift; sourceTree = ""; }; 8D7593AA27DEDC4100F9D8B4 /* ImageRectangle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageRectangle.swift; sourceTree = ""; }; - 8D7593AC27DEDD1E00F9D8B4 /* RectangleCreator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RectangleCreator.swift; sourceTree = ""; }; 8D7593AE27DEDD5100F9D8B4 /* RectangleFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RectangleFactory.swift; sourceTree = ""; }; 8D7593B027DEDE2000F9D8B4 /* Image.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Image.swift; sourceTree = ""; }; - 8D7593BC27E0CAC800F9D8B4 /* RectanlgeViewFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RectanlgeViewFactory.swift; sourceTree = ""; }; - 8D7593BE27E0CAF100F9D8B4 /* RectangleViewCreator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RectangleViewCreator.swift; sourceTree = ""; }; - 8D7593C227E189AA00F9D8B4 /* RectangleViewable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RectangleViewable.swift; sourceTree = ""; }; - 8D7593C427E18B9D00F9D8B4 /* ImageRectangleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageRectangleView.swift; sourceTree = ""; }; + 8D7593BC27E0CAC800F9D8B4 /* RectangleViewFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RectangleViewFactory.swift; sourceTree = ""; }; 8DBC282A27D393A8007422D6 /* UIColor++.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIColor++.swift"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -205,7 +183,7 @@ children = ( 8D69084C27CF4D4200F78A51 /* IDFactory.swift */, 8D7593AE27DEDD5100F9D8B4 /* RectangleFactory.swift */, - 8D7593BC27E0CAC800F9D8B4 /* RectanlgeViewFactory.swift */, + 8D7593BC27E0CAC800F9D8B4 /* RectangleViewFactory.swift */, ); path = Factory; sourceTree = ""; @@ -216,8 +194,6 @@ 8D67432B27D72AD100397D68 /* RectangleButton.swift */, 8D41425F27DB2AB30070FDCB /* ImageButton.swift */, 8D5DF31B27D1A88900F5E9C5 /* DetailView.swift */, - 8D67432D27D7312200397D68 /* PlaneRectangleView.swift */, - 8D7593C427E18B9D00F9D8B4 /* ImageRectangleView.swift */, ); path = View; sourceTree = ""; @@ -225,11 +201,8 @@ 8DBC282927D33CC7007422D6 /* Protocol */ = { isa = PBXGroup; children = ( - 8D5DF32127D3107800F5E9C5 /* DetailViewDelgate.swift */, + 8D5DF32127D3107800F5E9C5 /* DetailViewDelegate.swift */, 8D7593A627DEDBAF00F9D8B4 /* Rectangleable.swift */, - 8D7593C227E189AA00F9D8B4 /* RectangleViewable.swift */, - 8D7593AC27DEDD1E00F9D8B4 /* RectangleCreator.swift */, - 8D7593BE27E0CAF100F9D8B4 /* RectangleViewCreator.swift */, ); path = Protocol; sourceTree = ""; @@ -335,19 +308,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 8D7593BB27E0BBF200F9D8B4 /* RectangleFactory.swift in Sources */, - 8D7593BA27E0BBEE00F9D8B4 /* Rectangleable.swift in Sources */, - 8D7593B927E0BBE300F9D8B4 /* RectangleCreator.swift in Sources */, - 8D7593B827E0AD0F00F9D8B4 /* PlaneRectangle.swift in Sources */, - 8D7593B627E0AC8A00F9D8B4 /* DrawingAppTests.swift in Sources */, - 8D1AB32327D9D0A20024AD38 /* IDFactory.swift in Sources */, - 8D1AB32227D9D09F0024AD38 /* ID.swift in Sources */, - 8D1AB32027D9D0680024AD38 /* Alpha.swift in Sources */, - 8D1AB31F27D9D0400024AD38 /* UIColor++.swift in Sources */, - 8D1AB31E27D9D0370024AD38 /* RGB.swift in Sources */, - 8D1AB31C27D9D01B0024AD38 /* Point.swift in Sources */, - 8D1AB31B27D9D0130024AD38 /* Size.swift in Sources */, - 8D1AB31A27D9CFB50024AD38 /* Plane.swift in Sources */, + 8D75A59F27E324280091EEC4 /* DrawingAppTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -358,27 +319,22 @@ 8D41426027DB2AB30070FDCB /* ImageButton.swift in Sources */, 8D6907FD27CCBEF100F78A51 /* MainViewController.swift in Sources */, 8D7593B127DEDE2000F9D8B4 /* Image.swift in Sources */, - 8D7593AD27DEDD1E00F9D8B4 /* RectangleCreator.swift in Sources */, 8D5DF31C27D1A88900F5E9C5 /* DetailView.swift in Sources */, 8D7593A927DEDC2E00F9D8B4 /* PlaneRectangle.swift in Sources */, 8D69083727CE08E900F78A51 /* Size.swift in Sources */, 8D69083B27CE249200F78A51 /* Alpha.swift in Sources */, - 8D7593C527E18B9D00F9D8B4 /* ImageRectangleView.swift in Sources */, 8D69083927CE08F000F78A51 /* Point.swift in Sources */, - 8D67432E27D7312200397D68 /* PlaneRectangleView.swift in Sources */, 8D5DF31A27D0F88300F5E9C5 /* Plane.swift in Sources */, 8D7593AF27DEDD5100F9D8B4 /* RectangleFactory.swift in Sources */, 8D6907F927CCBEF100F78A51 /* AppDelegate.swift in Sources */, 8D69084F27CF4D9F00F78A51 /* ID.swift in Sources */, - 8D7593BD27E0CAC800F9D8B4 /* RectanlgeViewFactory.swift in Sources */, + 8D7593BD27E0CAC800F9D8B4 /* RectangleViewFactory.swift in Sources */, 8D67432C27D72AD100397D68 /* RectangleButton.swift in Sources */, 8D6907FB27CCBEF100F78A51 /* SceneDelegate.swift in Sources */, 8D69084D27CF4D4200F78A51 /* IDFactory.swift in Sources */, 8D7593A727DEDBAF00F9D8B4 /* Rectangleable.swift in Sources */, - 8D7593C327E189AA00F9D8B4 /* RectangleViewable.swift in Sources */, 8DBC282B27D393A8007422D6 /* UIColor++.swift in Sources */, - 8D5DF32227D3107800F5E9C5 /* DetailViewDelgate.swift in Sources */, - 8D7593BF27E0CAF100F9D8B4 /* RectangleViewCreator.swift in Sources */, + 8D5DF32227D3107800F5E9C5 /* DetailViewDelegate.swift in Sources */, 8D69083D27CE249900F78A51 /* RGB.swift in Sources */, 8D7593AB27DEDC4100F9D8B4 /* ImageRectangle.swift in Sources */, ); diff --git a/DrawingApp/DrawingApp/Factory/RectangleFactory.swift b/DrawingApp/DrawingApp/Factory/RectangleFactory.swift index 57be9e73..2d7456e8 100644 --- a/DrawingApp/DrawingApp/Factory/RectangleFactory.swift +++ b/DrawingApp/DrawingApp/Factory/RectangleFactory.swift @@ -5,17 +5,30 @@ // Created by 박진섭 on 2022/03/14. // +import Foundation + //Image가 들어가있는 Rectangle, 기본 plane Rectangle들을 Rectangle로 묶어서 Factory메서드를 사용할수 있을것 같아 선언 -final class RectangleFactory:RectangleCreator { +final class RectangleFactory { - func makeRectangle(type:Rectangleable.Type) -> Rectangleable { + static func makePlaneRectangle() -> PlaneRectangle { let id = IDFactory.makeRandomID() let size = Size(width: 150, height: 120) let origin = Point.random() let rgb = RGB.random() let alpha = Alpha.random() - //type에 따라 init - return type.init(id: id, origin: origin, size: size, rgb: rgb, alpha: alpha) + return PlaneRectangle(id: id, origin: origin, size: size, rgb: rgb, alpha: alpha) } + + static func makeImageRectangle(imageData:Data) -> ImageRectangle { + let id = IDFactory.makeRandomID() + let size = Size(width: 150, height: 120) + let origin = Point.random() + let alpha = Alpha(1) + let imageData = imageData + + return ImageRectangle(id: id, origin: origin, size: size, alpha: alpha, imageData: imageData) + } + + } diff --git a/DrawingApp/DrawingApp/Factory/RectangleViewFactory.swift b/DrawingApp/DrawingApp/Factory/RectangleViewFactory.swift new file mode 100644 index 00000000..9d3f953f --- /dev/null +++ b/DrawingApp/DrawingApp/Factory/RectangleViewFactory.swift @@ -0,0 +1,56 @@ +// +// RectanlgeViewFactory.swift +// DrawingApp +// +// Created by 박진섭 on 2022/03/15. +// + +import UIKit + +final class RectangleViewFactory { + + //planeRectangleView + static func makePlaneRectangleView(sourceRectangle: Rectangleable) -> UIView { + let source = sourceRectangle as! PlaneRectangle + let rectangleView = UIView(frame: .zero) + rectangleView.frame = setUpFrameWithRectangle(source) + rectangleView.backgroundColor = setupBackgroundColor(rgb: source.rgb, alpha: source.alpha) + return rectangleView + } + + //IamgeView + static func makeImageRectangleView(sourceRectangle: Rectangleable) -> UIImageView { + let source = sourceRectangle as! ImageRectangle + let rectangleView = UIImageView(frame: .zero) + rectangleView.frame = setUpFrameWithRectangle(source) + rectangleView.image = setupImage(imageData: source.imageData) + return rectangleView + } + + + //Setup private Static functions, Pure Func? + private static func setUpFrameWithRectangle(_ source:Rectangleable) -> CGRect { + let x = source.origin.x + let y = source.origin.y + let width = source.size.width + let height = source.size.height + return CGRect(x: x, y: y, width: width, height: height) + } + + private static func setupBackgroundColor(rgb:RGB, alpha:Alpha) -> UIColor { + let red = rgb.red + let green = rgb.green + let blue = rgb.blue + return UIColor( + red: CGFloat(red) / 255, + green: CGFloat(green) / 255, + blue: CGFloat(blue) / 255, + alpha: CGFloat(alpha.value) + ) + } + + private static func setupImage(imageData:Data) -> UIImage{ + return UIImage(data: imageData) ?? UIImage() + } + +} diff --git a/DrawingApp/DrawingApp/Factory/RectanlgeViewFactory.swift b/DrawingApp/DrawingApp/Factory/RectanlgeViewFactory.swift deleted file mode 100644 index ecad1649..00000000 --- a/DrawingApp/DrawingApp/Factory/RectanlgeViewFactory.swift +++ /dev/null @@ -1,24 +0,0 @@ -// -// RectanlgeViewFactory.swift -// DrawingApp -// -// Created by 박진섭 on 2022/03/15. -// - -final class RectanlgeViewFactory:RectangleViewCreator { - static func makeRectangleView(sourceRectangle: Rectangleable) -> RectangleViewable { - switch type(of: sourceRectangle) { - case is ImageRectangle: - let rectangle = ImageRectanlgeView(frame:.zero) - rectangle.setupFrameWithRectangle(rect: sourceRectangle) - return rectangle - default: - let source = sourceRectangle as! PlaneRectangle - let rectangle = PlaneRectangleView() - rectangle.setupFrameWithRectangle(rect: sourceRectangle) - rectangle.setupBackGroundColor(rgb: source.rgb ?? RGB(red: 0, green: 0, blue: 0), alpha: source.alpha ?? Alpha(1.0)) - return rectangle - } - - } -} diff --git a/DrawingApp/DrawingApp/MainViewController.swift b/DrawingApp/DrawingApp/MainViewController.swift index d54ece1e..6ffcac4f 100644 --- a/DrawingApp/DrawingApp/MainViewController.swift +++ b/DrawingApp/DrawingApp/MainViewController.swift @@ -7,53 +7,43 @@ import UIKit import OSLog +import PhotosUI + +typealias RectangleView = UIView +typealias imageURLData = Data final class MainViewController: UIViewController{ //model private var plane = Plane() + private var image = Image() //view private var detailView = DetailView(frame: .zero) //생성시 오해를 막고 기존 생성자와 관련있게 하기 위해 frame에 .zero를 대입했습니다. private var rectangleButton:UIButton = RectangleButton(frame: .zero) private var imageButton:UIButton = ImageButton(frame: .zero) - //그려진 PlaneRetangleView를 모델(Rectangle)을 Key로 찾는 Dictionary로 만들어서 모델과 매칭을 시켜주었습니다. - private var retangleViews = [PlaneRectangle:PlaneRectangleView]() + private var planeRectangleViews = [PlaneRectangle:RectangleView]() + private var imageRectangleViews = [imageURLData:RectangleView]() + //선택된 PlaneRectangleView - private var seletedRectangleView:PlaneRectangleView? + private var seletedRectangleView:RectangleView? override func viewDidLoad() { super.viewDidLoad() - configureNotificationCenter() + configurePlaneNotificationCenter() + configureImageNotificationCenter() + + configureTapGesture() configureImageButton() configureRectangleButton() configureDeatailView() - configureTapGesture() addViews() } - //TapGesture - private func configureTapGesture() { - let tapGesture = UITapGestureRecognizer() - tapGesture.delegate = self - self.view.addGestureRecognizer(tapGesture) - } - - //DetailView - private func configureDeatailView() { - detailView.delegate = self - detailView.layer.zPosition = 1.0 //생성되는 사각형에 겹쳐서 클릭이 안되거나 안보이는 경우를 막기위해 zPosition을 주었다. - - let inset:CGFloat = 200 - detailView.frame = CGRect(x:self.view.frame.maxX - inset, - y: 0, - width: inset, - height: self.view.frame.height - ) - } - + + //MARK: -- ImageButton private func configureImageButton() { let width = 150.0 let height = 100.0 @@ -62,8 +52,19 @@ final class MainViewController: UIViewController{ imageButton.layer.zPosition = 1.0 imageButton.frame = CGRect(x: x, y: y, width: width, height: height) + imageButton.addAction(addImageAction(), for: .touchUpInside) } + //버튼 액션 - 앨범보여주기. + private func addImageAction() -> UIAction { + let action = UIAction {[weak self] _ in + guard let picker = self?.configurePHPicker() else { return } + self?.present(picker, animated: true, completion: nil) + } + return action + } + + //MARK: -- RectangleButton //사각형 추가 버튼 Frame정의 및 Action추가. private func configureRectangleButton() { let width = 150.0 @@ -74,43 +75,55 @@ final class MainViewController: UIViewController{ rectangleButton.layer.zPosition = 1.0 //생성되는 사각형에 겹쳐서 안보이는 경우를 막기위해 zPosition을 주었다. rectangleButton.frame = CGRect(x: x, y: y, width: width, height: height) - rectangleButton.addAction(addRectangleAction(rectangleCreator:RectangleFactory.init()), for: .touchUpInside) + rectangleButton.addAction(addRectangleAction(), for: .touchUpInside) } //버튼 액션 - 사각형 추가 - private func addRectangleAction(rectangleCreator:RectangleCreator) -> UIAction { + private func addRectangleAction() -> UIAction { let action = UIAction {[weak self] _ in - self?.plane.addRectangle(creator: rectangleCreator) + self?.plane.addRectangle() } return action } + //MARK: -- Else + //TapGesture + private func configureTapGesture() { + let tapGesture = UITapGestureRecognizer() + tapGesture.delegate = self + self.view.addGestureRecognizer(tapGesture) + } + + //DetailView + private func configureDeatailView() { + detailView.delegate = self + detailView.layer.zPosition = 1.0 //생성되는 사각형에 겹쳐서 클릭이 안되거나 안보이는 경우를 막기위해 zPosition을 주었다. + + let inset:CGFloat = 200 + detailView.frame = CGRect(x:self.view.frame.maxX - inset, + y: 0, + width: inset, + height: self.view.frame.height + ) + } + //Custom View추가 private func addViews() { view.addSubview(rectangleButton) view.addSubview(detailView) view.addSubview(imageButton) } -} - -//MARK: -- UIGesture 처리 -extension MainViewController:UIGestureRecognizerDelegate { - func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { - - //터치된 View의 origin x와 y값을 plane에게 넘겨줍니다. - guard let touchedView = touch.view else { return true } - - //Plane에게 touch된 View의 origin좌표를 넘겨준다. - let x:Double = touchedView.frame.origin.x - let y:Double = touchedView.frame.origin.y - let size:Size = Size(width: touchedView.frame.size.width, height: touchedView.frame.size.height) - plane.findSeletedRectangle(x: x, y: y, size: size) - - return true + + private func bringButtonViewToFront() { + self.view.bringSubviewToFront(self.rectangleButton) + self.view.bringSubviewToFront(self.detailView) + self.view.bringSubviewToFront(self.imageButton) } - //MARK: -- NotificationCenter - private func configureNotificationCenter() { + + + //MARK: -- PlaneNotificationCenter + private func configurePlaneNotificationCenter() { NotificationCenter.default.addObserver( self, selector:#selector(addRectangleView), @@ -137,7 +150,7 @@ extension MainViewController:UIGestureRecognizerDelegate { } //ChangeColor - @objc func changeColor(_ notification:Notification) { + @objc private func changeColor(_ notification:Notification) { guard let newRGB = notification.userInfo?[Plane.UserInfoKey.changedColor] as? RGB else { return } let alpha:Alpha = Alpha(Float(seletedRectangleView?.alpha ?? 0.0)) @@ -148,49 +161,130 @@ extension MainViewController:UIGestureRecognizerDelegate { //ChangeAlpha - @objc func changeAlpha(_ notification:Notification) { + @objc private func changeAlpha(_ notification:Notification) { guard let newAlpha = notification.userInfo?[Plane.UserInfoKey.changedAlpha] as? Alpha else { return } seletedRectangleView?.alpha = CGFloat(newAlpha.value) } //addRectagnleView - @objc func addRectangleView(_ notification:Notification) { + @objc private func addRectangleView(_ notification:Notification) { guard let newRectangle = notification.userInfo?[Plane.UserInfoKey.addedRectangle] as? PlaneRectangle else { return } - guard let rectangleView = RectanlgeViewFactory.makeRectangleView(sourceRectangle: newRectangle) as? PlaneRectangleView else { return } + let rectangleView = RectangleViewFactory.makePlaneRectangleView(sourceRectangle: newRectangle) - self.retangleViews[newRectangle] = rectangleView + self.planeRectangleViews[newRectangle] = rectangleView view.addSubview(rectangleView) - self.view.bringSubviewToFront(rectangleButton) - self.view.bringSubviewToFront(detailView) + bringButtonViewToFront() } //findSelected Rectangle & Set View - @objc func findSelectedRectangle(_ notification:Notification) { + @objc private func findSelectedRectangle(_ notification:Notification) { seletedRectangleView?.layer.borderWidth = .zero guard let seletedRectangle = notification.userInfo?[Plane.UserInfoKey.foundRectangle] as? PlaneRectangle else { return } - let rectangleView = self.retangleViews[seletedRectangle] + let rectangleView = self.planeRectangleViews[seletedRectangle] + self.seletedRectangleView = rectangleView + + seletedRectangleView?.layer.borderWidth = 2.0 + seletedRectangleView?.layer.borderColor = UIColor.red.cgColor + + self.detailView.alphaLabel.text = "\(seletedRectangle.alpha.value)" + self.detailView.alphaSlider.value = seletedRectangle.alpha.value + + self.detailView.backgroundColorButton.setTitle("\(seletedRectangle.rgb.hexValue)", for: .normal) + self.detailView.backgroundColorButton.isEnabled = true + } + //MARK: -- ImageNotificationCenter + private func configureImageNotificationCenter() { + NotificationCenter.default.addObserver( + self, + selector:#selector(addImageRectangleView), + name: Image.NotificationName.didAddRectangle, + object: image) + + NotificationCenter.default.addObserver( + self, + selector: #selector(findSelectedImageRectangle), + name: Image.NotificationName.didFindRectangle, + object: image ) + + NotificationCenter.default.addObserver( + self, + selector: #selector(changeIamgeAlpha), + name: Image.NotificationName.didchangeRectangleAlpha, + object: image ) + + + } + //ADD New ImageRectangleView + @objc private func addImageRectangleView(_ notification:Notification) { + guard let newImageRectangle = notification.userInfo?[Image.UserInfoKey.addedRectangle] as? ImageRectangle else { return } + + DispatchQueue.main.async { [weak self] in + guard let self = self else { return } + let rectangleView = RectangleViewFactory.makeImageRectangleView(sourceRectangle: newImageRectangle) + self.imageRectangleViews[newImageRectangle.imageData] = rectangleView + + self.view.addSubview(rectangleView) + self.bringButtonViewToFront() + } + + } + + //findSelected ImageRectangle & Set View + @objc private func findSelectedImageRectangle(_ notification:Notification) { + seletedRectangleView?.layer.borderWidth = .zero + + guard let seletedRectangle = notification.userInfo?[Image.UserInfoKey.foundRectangle] as? ImageRectangle else { return } + + let rectangleView = self.imageRectangleViews[seletedRectangle.imageData] self.seletedRectangleView = rectangleView seletedRectangleView?.layer.borderWidth = 2.0 - seletedRectangleView?.layer.borderColor = UIColor.blue.cgColor + seletedRectangleView?.layer.borderColor = UIColor.red.cgColor - self.detailView.alphaLabel.text = "\(seletedRectangle.alpha?.value ?? 0.0)" - self.detailView.alphaSlider.value = seletedRectangle.alpha?.value ?? 0.0 + self.detailView.alphaLabel.text = "\(seletedRectangle.alpha.value)" + self.detailView.alphaSlider.value = seletedRectangle.alpha.value - self.detailView.backgroundColorButton.setTitle("\(seletedRectangle.rgb?.hexValue ?? "")", for: .normal) + self.detailView.backgroundColorButton.setTitle("NONE", for: .normal) + self.detailView.backgroundColorButton.alpha = 0.3 + self.detailView.backgroundColorButton.isEnabled = false + } + + //ChangeImageAlpha + @objc private func changeIamgeAlpha(_ notification:Notification) { + guard let newAlpha = notification.userInfo?[Image.UserInfoKey.changedAlpha] as? Alpha else { return } + seletedRectangleView?.alpha = CGFloat(newAlpha.value) + } +} + + + + +//MARK: -- UIGesture 처리 +extension MainViewController:UIGestureRecognizerDelegate { + func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { + + //Plane에게 touch된 View의 좌표를 넘겨준다. + let x = Double(touch.location(in: self.view).x) + let y = Double(touch.location(in: self.view).y) + let point = Point(x: x, y: y) + plane.findSeletedRectangle(point: point) + image.findSeletedRectangle(point: point) + + return true } } //MARK: -- DetailViewDelegate //슬라이더를 움직일때 마다 현재 클릭한 모델 사각형의 alpha값을 바꾼다. -extension MainViewController:DetailViewDelgate { +extension MainViewController:DetailViewDelegate { func sliderViewEndEditing(sender: UISlider) { let currentSliderValue = sender.value plane.change(alpha: Alpha(currentSliderValue)) + image.change(alpha: Alpha(currentSliderValue)) } //랜덤한 RGB값을 설정하고 현재 클릭한 모델 사각형의 rgb값을 변경한다 @@ -200,4 +294,34 @@ extension MainViewController:DetailViewDelgate { } } - +//MARK: -- PHPikcer +extension MainViewController:PHPickerViewControllerDelegate { + func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { + + picker.dismiss(animated: true, completion: nil) + + //itemProviders + let _ = results.map { $0.itemProvider } + //Can load Object? + .filter { $0.canLoadObject(ofClass: UIImage.self) } + //Then load + .map { + $0.loadFileRepresentation(forTypeIdentifier: UTType.image.identifier) { [weak self] url, Error in + guard let url = url else { return } + guard let data = try? Data(contentsOf: url) else { return } + self?.image.addRectangle(imageData: data) + } + } + } + + //configurePHPicker + private func configurePHPicker() -> PHPickerViewController { + var configuration = PHPickerConfiguration() + configuration.selectionLimit = 0 + + let picker = PHPickerViewController(configuration: configuration) + picker.delegate = self + + return picker + } +} diff --git a/DrawingApp/DrawingApp/Model/Image.swift b/DrawingApp/DrawingApp/Model/Image.swift index ceec5f02..a356da78 100644 --- a/DrawingApp/DrawingApp/Model/Image.swift +++ b/DrawingApp/DrawingApp/Model/Image.swift @@ -10,10 +10,70 @@ import Foundation //TODO: Something deal with ImageRectangle class Image { - private var rectangles:[Point:ImageRectangle] = [:] - private var selectedRectangle:ImageRectangle? + private var imageRectangles:[ImageRectangle] = [] + private var selectedImageRectangle:ImageRectangle? var count:Int { - self.rectangles.count + self.imageRectangles.count } + + //사각형 생성,추가. + @discardableResult + func addRectangle(imageData:Data) -> ImageRectangle{ + let rect = RectangleFactory.makeImageRectangle(imageData: imageData) + imageRectangles.append(rect) + + NotificationCenter.default.post( + name: Image.NotificationName.didAddRectangle, + object: self, + userInfo:[Image.UserInfoKey.addedRectangle: rect] + ) + return rect + } + + //특정 좌표에 맞는 retangle을 찾고 seletedRectangle에 대입합니다. + func findSeletedRectangle(point:Point) { + let x = point.x + let y = point.y + + let foundRectangle = imageRectangles.last { + $0.origin.x <= x && $0.origin.y <= y && $0.size.width + $0.origin.x >= x && $0.size.height + $0.origin.y >= y + } + guard let foundRectangle = foundRectangle else { return } + + self.selectedImageRectangle = foundRectangle + + NotificationCenter.default.post( + name: Image.NotificationName.didFindRectangle, + object: self, + userInfo: [Image.UserInfoKey.foundRectangle:foundRectangle] + ) + } + + func change(alpha:Alpha){ + guard let selectedImageRectangle = selectedImageRectangle else { return } + selectedImageRectangle.changeAlpha(alpha) + + NotificationCenter.default.post( + name: Image.NotificationName.didchangeRectangleAlpha, + object: self, + userInfo: [Image.UserInfoKey.changedAlpha:alpha] + ) + } + + + //Image.UserInfoKey와 + enum UserInfoKey { + case addedRectangle + case foundRectangle + case changedAlpha + } + + //NotificationNames + struct NotificationName { + static let didchangeRectangleAlpha = Notification.Name("didChangeRectangleAlpha") + static let didAddRectangle = Notification.Name("didAddRectangle") + static let didFindRectangle = Notification.Name("didFindRectangle") + } + } diff --git a/DrawingApp/DrawingApp/Model/ImageRectangle.swift b/DrawingApp/DrawingApp/Model/ImageRectangle.swift index 74ccd438..a6850121 100644 --- a/DrawingApp/DrawingApp/Model/ImageRectangle.swift +++ b/DrawingApp/DrawingApp/Model/ImageRectangle.swift @@ -13,16 +13,15 @@ final class ImageRectangle:Rectangleable { private let id:ID private(set) var origin:Point private(set) var size:Size - private(set) var alpha:Alpha? - private(set) var rgb:RGB? - private(set) var image:Data? + private(set) var alpha:Alpha + private(set) var imageData:Data - init(id: ID, origin: Point, size: Size, rgb: RGB? = nil, alpha: Alpha? = nil) { + init(id: ID, origin: Point, size: Size, alpha: Alpha,imageData:Data) { self.id = id self.origin = origin self.size = size self.alpha = alpha - self.rgb = rgb + self.imageData = imageData } func changeOrigin(_ origin: Point) { @@ -46,6 +45,6 @@ extension ImageRectangle:Hashable { } //이미지가 같은면 같은 사각형. static func == (lhs: ImageRectangle, rhs: ImageRectangle) -> Bool { - lhs.image == rhs.image + lhs.imageData == rhs.imageData } } diff --git a/DrawingApp/DrawingApp/Model/Plane.swift b/DrawingApp/DrawingApp/Model/Plane.swift index edb194a4..c30b29be 100644 --- a/DrawingApp/DrawingApp/Model/Plane.swift +++ b/DrawingApp/DrawingApp/Model/Plane.swift @@ -9,8 +9,7 @@ import Foundation public class Plane { - //Point를 기반으로 Dictionary를 만듬으로써 View에서 TapGesture로 좌표값을 넘겼을때 그에 해당하는 Rectangle을 찾을 수있다. - private var rectangles:[Point:PlaneRectangle] = [:] + private var rectangles = [PlaneRectangle]() private var selectedRectangle:PlaneRectangle? var count:Int { @@ -43,14 +42,10 @@ public class Plane { //사각형 생성,추가. @discardableResult - func addRectangle(creator:RectangleCreator) -> PlaneRectangle{ - let rect = creator.makeRectangle(type: PlaneRectangle.self) as! PlaneRectangle - //Double로 만들어지는 Point값이 소수점에서 미세한 차이가 생길 수 있어서 round로 한번 변환 - let x = round(rect.origin.x) - let y = round(rect.origin.y) - let key = Point(x: x, y: y) + func addRectangle() -> PlaneRectangle{ + let rect = RectangleFactory.makePlaneRectangle() + rectangles.append(rect) - rectangles[key] = rect NotificationCenter.default.post( name: Plane.NotificationName.didAddRectangle, object: self, @@ -60,16 +55,15 @@ public class Plane { } //특정 좌표에 맞는 retangle을 찾고 seletedRectangle에 대입합니다. - func findSeletedRectangle(x:Double,y:Double,size:Size) { + func findSeletedRectangle(point:Point) { + let x = point.x + let y = point.y - checkEmptySpace(size: size) - - let convertX = round(x) - let convertY = round(y) - - let key = Point(x: convertX, y: convertY) - - guard let foundRectangle = rectangles[key] else { return } + let foundRectangle = rectangles.last { + $0.origin.x <= x && $0.origin.y <= y && $0.size.width + $0.origin.x >= x && $0.size.height + $0.origin.y >= y + } + guard let foundRectangle = foundRectangle else { return } + self.selectedRectangle = foundRectangle NotificationCenter.default.post( @@ -79,28 +73,6 @@ public class Plane { ) } - //빈화면을 클릭할 시 아무런 정보를 가지지 않은 채로 post합니다. - //nil값을 가진채로 Dictionary를 만들어 Userinfo를 보내도 같은 결과가 나오지만 굳이 Dictionary를 만들어 보낼 필요를 못느껴 새로운 post를 만들었습니다. - private func checkEmptySpace(size:Size) { - if isEmptySpace(size: size) { - self.selectedRectangle = nil - NotificationCenter.default.post( - name: Plane.NotificationName.didFindRectangle, - object: self - ) - } - } - - //Size값이 최대값을 가지고 있다면 빈 화면을 클릭한 것입니다. - private func isEmptySpace(size:Size) -> Bool{ - let height = size.height - let width = size.width - let emptySpaceSize = Size(width: Size.maxWidth, height: Size.maxHeight) - - return emptySpaceSize == Size(width: width, height: height) ? true : false - } - - //Plane.UserInfoKey와 같이 Plane과 관련있게 하고싶어서 Nested Enum을 선언 enum UserInfoKey { diff --git a/DrawingApp/DrawingApp/Model/PlaneRectangle.swift b/DrawingApp/DrawingApp/Model/PlaneRectangle.swift index 2b56b039..e812cf0c 100644 --- a/DrawingApp/DrawingApp/Model/PlaneRectangle.swift +++ b/DrawingApp/DrawingApp/Model/PlaneRectangle.swift @@ -11,10 +11,10 @@ final class PlaneRectangle:Rectangleable { private let id:ID private(set) var origin:Point private(set) var size:Size - private(set) var rgb:RGB? - private(set) var alpha:Alpha? + private(set) var rgb:RGB + private(set) var alpha:Alpha - init(id:ID,origin:Point,size:Size,rgb:RGB? = nil , alpha:Alpha? = nil) { + init(id:ID,origin:Point,size:Size,rgb:RGB, alpha:Alpha) { self.id = id self.origin = origin self.size = size diff --git a/DrawingApp/DrawingApp/Protocol/DetailViewDelgate.swift b/DrawingApp/DrawingApp/Protocol/DetailViewDelegate.swift similarity index 83% rename from DrawingApp/DrawingApp/Protocol/DetailViewDelgate.swift rename to DrawingApp/DrawingApp/Protocol/DetailViewDelegate.swift index 31b5ec4a..54465008 100644 --- a/DrawingApp/DrawingApp/Protocol/DetailViewDelgate.swift +++ b/DrawingApp/DrawingApp/Protocol/DetailViewDelegate.swift @@ -7,7 +7,7 @@ import UIKit -protocol DetailViewDelgate:AnyObject { +protocol DetailViewDelegate:AnyObject { func sliderViewEndEditing(sender:UISlider) func colorButtonTouched(sender:UIButton) } diff --git a/DrawingApp/DrawingApp/Protocol/RectangleCreator.swift b/DrawingApp/DrawingApp/Protocol/RectangleCreator.swift deleted file mode 100644 index 34615610..00000000 --- a/DrawingApp/DrawingApp/Protocol/RectangleCreator.swift +++ /dev/null @@ -1,10 +0,0 @@ -// -// RectangleCreator.swift -// DrawingApp -// -// Created by 박진섭 on 2022/03/14. -// - -protocol RectangleCreator { - func makeRectangle(type:Rectangleable.Type) -> Rectangleable -} diff --git a/DrawingApp/DrawingApp/Protocol/RectangleViewCreator.swift b/DrawingApp/DrawingApp/Protocol/RectangleViewCreator.swift deleted file mode 100644 index e222a4f8..00000000 --- a/DrawingApp/DrawingApp/Protocol/RectangleViewCreator.swift +++ /dev/null @@ -1,10 +0,0 @@ -// -// RectangleViewCreator.swift -// DrawingApp -// -// Created by 박진섭 on 2022/03/15. -// - -protocol RectangleViewCreator { - static func makeRectangleView(sourceRectangle:Rectangleable) -> RectangleViewable -} diff --git a/DrawingApp/DrawingApp/Protocol/RectangleViewable.swift b/DrawingApp/DrawingApp/Protocol/RectangleViewable.swift deleted file mode 100644 index 424f06d3..00000000 --- a/DrawingApp/DrawingApp/Protocol/RectangleViewable.swift +++ /dev/null @@ -1,12 +0,0 @@ -// -// RectangleViewable.swift -// DrawingApp -// -// Created by 박진섭 on 2022/03/16. -// - -import UIKit - -protocol RectangleViewable:UIView { - func setupFrameWithRectangle(rect:Rectangleable) -} diff --git a/DrawingApp/DrawingApp/Protocol/Rectangleable.swift b/DrawingApp/DrawingApp/Protocol/Rectangleable.swift index eddf01d1..b0cb63cf 100644 --- a/DrawingApp/DrawingApp/Protocol/Rectangleable.swift +++ b/DrawingApp/DrawingApp/Protocol/Rectangleable.swift @@ -7,7 +7,8 @@ //사각형의 기본 메서드 프로토콜. protocol Rectangleable { - init(id:ID,origin:Point,size:Size,rgb:RGB?, alpha:Alpha?) + var origin:Point { get } + var size:Size { get } func changeOrigin(_ origin:Point) func changeSize(_ size:Size) func changeAlpha(_ alpha:Alpha) diff --git a/DrawingApp/DrawingApp/View/DetailView.swift b/DrawingApp/DrawingApp/View/DetailView.swift index 41ad651a..927e7c85 100644 --- a/DrawingApp/DrawingApp/View/DetailView.swift +++ b/DrawingApp/DrawingApp/View/DetailView.swift @@ -9,7 +9,7 @@ import UIKit final class DetailView: UIView { - weak var delegate:DetailViewDelgate? + weak var delegate:DetailViewDelegate? //배경색 label var backgroundColorLabel:UILabel = { diff --git a/DrawingApp/DrawingApp/View/ImageRectangleView.swift b/DrawingApp/DrawingApp/View/ImageRectangleView.swift deleted file mode 100644 index 3031be73..00000000 --- a/DrawingApp/DrawingApp/View/ImageRectangleView.swift +++ /dev/null @@ -1,33 +0,0 @@ -// -// ImageRectangleView.swift -// DrawingApp -// -// Created by 박진섭 on 2022/03/16. -// - -import UIKit - -final class ImageRectanlgeView:UIImageView,RectangleViewable { - - override init(frame: CGRect) { - super.init(frame: frame) - } - - required init?(coder: NSCoder) { - super.init(coder: coder) - } - - func setupFrameWithRectangle(rect:Rectangleable) { - guard let planeRectangle = (rect as? PlaneRectangle) else { return } - let x = planeRectangle.origin.x - let y = planeRectangle.origin.y - let width = planeRectangle.size.width - let height = planeRectangle.size.height - self.frame = CGRect(x: x, y: y, width: width, height: height) - } - - func setupImage(imageData:Data) { - self.image = UIImage(data: imageData) - } - -} diff --git a/DrawingApp/DrawingApp/View/PlaneRectangleView.swift b/DrawingApp/DrawingApp/View/PlaneRectangleView.swift deleted file mode 100644 index 474efcfb..00000000 --- a/DrawingApp/DrawingApp/View/PlaneRectangleView.swift +++ /dev/null @@ -1,41 +0,0 @@ -// -// PlaneRectangleView.swift -// DrawingApp -// -// Created by 박진섭 on 2022/03/05. -// - -import UIKit - - -final class PlaneRectangleView:UIView,RectangleViewable { - - override init(frame: CGRect) { - super.init(frame: frame) - } - - required init?(coder: NSCoder) { - super.init(coder: coder) - } - - func setupFrameWithRectangle(rect:Rectangleable) { - guard let planeRectangle = (rect as? PlaneRectangle) else { return } - let x = planeRectangle.origin.x - let y = planeRectangle.origin.y - let width = planeRectangle.size.width - let height = planeRectangle.size.height - self.frame = CGRect(x: x, y: y, width: width, height: height) - } - - func setupBackGroundColor(rgb:RGB, alpha:Alpha) { - let red = rgb.red - let green = rgb.green - let blue = rgb.blue - self.backgroundColor = UIColor( - red: CGFloat(red) / 255, - green: CGFloat(green) / 255, - blue: CGFloat(blue) / 255, - alpha: CGFloat(alpha.value) - ) - } -} diff --git a/DrawingApp/DrawingAppTests/DrawingAppTests.swift b/DrawingApp/DrawingAppTests/DrawingAppTests.swift index 9821d642..f9416de8 100644 --- a/DrawingApp/DrawingAppTests/DrawingAppTests.swift +++ b/DrawingApp/DrawingAppTests/DrawingAppTests.swift @@ -18,8 +18,9 @@ class DrawingAppTests: XCTestCase { } func testChangeAlpha() { - let testRectangle = sut.addRectangle(creator: RectangleFactory()) - sut.findSeletedRectangle(x: round(testRectangle.origin.x), y: round(testRectangle.origin.y), size: testRectangle.size) + let testRectangle = sut.addRectangle() + let point = testRectangle.origin + sut.findSeletedRectangle(point: point) let testAlpha = Alpha(2.3) sut.change(alpha: testAlpha) @@ -30,9 +31,10 @@ class DrawingAppTests: XCTestCase { func testChangeColor() { - let testRectangle = sut.addRectangle(creator: RectangleFactory()) + let testRectangle = sut.addRectangle() + let point = testRectangle.origin - sut.findSeletedRectangle(x: round(testRectangle.origin.x), y: round(testRectangle.origin.y), size: testRectangle.size) + sut.findSeletedRectangle(point: point) let testRGB = RGB(red: 10, green: 11, blue: 12) sut.change(color: testRGB) @@ -41,27 +43,25 @@ class DrawingAppTests: XCTestCase { } func testAddRectangle() { - sut.addRectangle(creator: RectangleFactory()) + sut.addRectangle() XCTAssertTrue(sut.count == 1) } func testFindSelectedRectangle() { - let willSeletedRectangle = sut.addRectangle(creator: RectangleFactory()) - let TestRaectangle = sut.addRectangle(creator: RectangleFactory()) + let willSelectedRectangle = sut.addRectangle() + let willNotSelectedRectangle = sut.addRectangle() - let x = round(willSeletedRectangle.origin.x) - let y = round(willSeletedRectangle.origin.y) - let size = willSeletedRectangle.size - - let correctKey = Point(x: x, y: y) - - sut.findSeletedRectangle(x: correctKey.x, y: correctKey.y, size: size) + let x = willSelectedRectangle.origin.x + let y = willSelectedRectangle.origin.y + let imutableAlpha = willNotSelectedRectangle.alpha + sut.findSeletedRectangle(point: Point(x: x, y: y)) sut.change(alpha:Alpha(0.7)) - XCTAssertEqual(willSeletedRectangle.alpha, Alpha(0.7)) + XCTAssertEqual(willSelectedRectangle.alpha, Alpha(0.7)) + XCTAssertEqual(willNotSelectedRectangle.alpha, imutableAlpha) }