From 0afc6d379b0cfd7ac0a14ea99ef13dce2eaa3bea Mon Sep 17 00:00:00 2001 From: E-know Date: Fri, 9 Sep 2022 03:54:38 +0900 Subject: [PATCH 1/4] [HotFix] Change to IVO pattern --- AsyncSwift.xcodeproj/project.pbxproj | 4 ++ AsyncSwift/Observed/AppData.swift | 2 +- AsyncSwift/Observed/StampView+Observed.swift | 43 +++++++++++++++ AsyncSwift/Views/StampView.swift | 57 ++++++-------------- 4 files changed, 64 insertions(+), 42 deletions(-) create mode 100644 AsyncSwift/Observed/StampView+Observed.swift diff --git a/AsyncSwift.xcodeproj/project.pbxproj b/AsyncSwift.xcodeproj/project.pbxproj index a568a6e..9db27c4 100644 --- a/AsyncSwift.xcodeproj/project.pbxproj +++ b/AsyncSwift.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + B289943328CA69FF002B9F67 /* StampView+Observed.swift in Sources */ = {isa = PBXBuildFile; fileRef = B289943228CA69FF002B9F67 /* StampView+Observed.swift */; }; B2E1083128C9CD6900C3DD59 /* AppData.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2E1083028C9CD6900C3DD59 /* AppData.swift */; }; C68DE93628C7685800CA4CC8 /* AsyncSwiftApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = C68DE93528C7685800CA4CC8 /* AsyncSwiftApp.swift */; }; C68DE93828C7685800CA4CC8 /* MainTabView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C68DE93728C7685800CA4CC8 /* MainTabView.swift */; }; @@ -25,6 +26,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + B289943228CA69FF002B9F67 /* StampView+Observed.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "StampView+Observed.swift"; sourceTree = ""; }; B2E1083028C9CD6900C3DD59 /* AppData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppData.swift; sourceTree = ""; }; C68DE93228C7685800CA4CC8 /* AsyncSwift.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AsyncSwift.app; sourceTree = BUILT_PRODUCTS_DIR; }; C68DE93528C7685800CA4CC8 /* AsyncSwiftApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AsyncSwiftApp.swift; sourceTree = ""; }; @@ -122,6 +124,7 @@ children = ( C6F7798A28C9CBC60036773B /* EventView+Observed.swift */, B2E1083028C9CD6900C3DD59 /* AppData.swift */, + B289943228CA69FF002B9F67 /* StampView+Observed.swift */, ); path = Observed; sourceTree = ""; @@ -217,6 +220,7 @@ C68DE95128C77DDA00CA4CC8 /* TicketingView.swift in Sources */, C6F7798B28C9CBC60036773B /* EventView+Observed.swift in Sources */, C6F7798728C9CB3A0036773B /* StampView.swift in Sources */, + B289943328CA69FF002B9F67 /* StampView+Observed.swift in Sources */, C68DE94F28C77DC900CA4CC8 /* EventView.swift in Sources */, C68DE93828C7685800CA4CC8 /* MainTabView.swift in Sources */, C68DE93628C7685800CA4CC8 /* AsyncSwiftApp.swift in Sources */, diff --git a/AsyncSwift/Observed/AppData.swift b/AsyncSwift/Observed/AppData.swift index 819288b..dd6b3cc 100644 --- a/AsyncSwift/Observed/AppData.swift +++ b/AsyncSwift/Observed/AppData.swift @@ -10,7 +10,7 @@ import UIKit final class AppData: ObservableObject { @Published var currentTab: Tab = .event - + var tempisScanned = true } diff --git a/AsyncSwift/Observed/StampView+Observed.swift b/AsyncSwift/Observed/StampView+Observed.swift new file mode 100644 index 0000000..07607de --- /dev/null +++ b/AsyncSwift/Observed/StampView+Observed.swift @@ -0,0 +1,43 @@ +// +// StampView+Observed.swift +// AsyncSwift +// +// Created by Inho Choi on 2022/09/09. +// + +import SwiftUI + +extension StampView { + final class Observed: ObservableObject { + @Published var cardAnimatonModel = CardAnimationModel() + + let durationAndDelay: CGFloat = 0.3 + + func didTabCard () { + cardAnimatonModel.isTapped.toggle() + + if cardAnimatonModel.isTapped { // 카드 회전 연속을 위해서 if문 분리 + withAnimation(.linear(duration: durationAndDelay)) { + cardAnimatonModel.backDegree = 90 + } + withAnimation(.linear(duration: durationAndDelay).delay(durationAndDelay)) { + cardAnimatonModel.frontDegree = 0 + } + } else { + withAnimation(.linear(duration: durationAndDelay)) { + cardAnimatonModel.frontDegree = -90 + } + withAnimation(.linear(duration: durationAndDelay).delay(durationAndDelay)) { + cardAnimatonModel.backDegree = 0 + } + } + } + } + + struct CardAnimationModel: Identifiable { + let id = UUID() + var backDegree: Double = 0.0 + var frontDegree: Double = -90.0 + var isTapped = false + } +} diff --git a/AsyncSwift/Views/StampView.swift b/AsyncSwift/Views/StampView.swift index 7b6f9a1..99b4284 100644 --- a/AsyncSwift/Views/StampView.swift +++ b/AsyncSwift/Views/StampView.swift @@ -8,43 +8,39 @@ import SwiftUI struct StampView: View { - var tempisScanned = true - - @State private var backDegree: Double = 0.0 - @State private var frontDegree: Double = -90.0 - @State private var isFlipped = false - - let durationAndDelay: CGFloat = 0.3 + @EnvironmentObject var appData: AppData + @ObservedObject var observed = Observed() var body: some View { NavigationView { Group { - if tempisScanned { - Button(action: { flipCard() }) { - ZStack { - stampBack - stampFront - } + if appData.tempisScanned { + ZStack { + stampBack + stampFront + } + .onTapGesture { + observed.didTabCard() } } else { notScannedView } } - .padding(36) - .navigationTitle("Stamp") + .padding(36) + .navigationTitle("Stamp") } } // body } // View private extension StampView { - + @ViewBuilder var stampBack: some View { Image("Seminar002StampBack") .resizable() .aspectRatio(contentMode: .fit) - .shadow(color: Color(.sRGB, red: 0, green: 0, blue: 0, opacity: 0.2), radius: 20 , x: 40 * frontDegree / 90, y: 4) - .rotation3DEffect(Angle(degrees: frontDegree), axis: (x: 0, y: 1, z: 0)) + .shadow(color: Color(.sRGB, red: 0, green: 0, blue: 0, opacity: 0.2), radius: 20, x: 40 * observed.cardAnimatonModel.frontDegree / 90, y: 4) + .rotation3DEffect(Angle(degrees: observed.cardAnimatonModel.frontDegree), axis: (x: 0, y: 1, z: 0)) } @ViewBuilder @@ -53,8 +49,8 @@ private extension StampView { .resizable() .aspectRatio(contentMode: .fit) - .shadow(color: Color(.sRGB, red: 0, green: 0, blue: 0, opacity: 0.2), radius: 20, x: 40 * backDegree / 90, y: 4) - .rotation3DEffect(Angle(degrees: backDegree), axis: (x: 0, y: 1, z: 0)) + .shadow(color: Color(.sRGB, red: 0, green: 0, blue: 0, opacity: 0.2), radius: 20, x: 40 * observed.cardAnimatonModel.backDegree / 90, y: 4) + .rotation3DEffect(Angle(degrees: observed.cardAnimatonModel.backDegree), axis: (x: 0, y: 1, z: 0)) } @ViewBuilder @@ -67,27 +63,6 @@ private extension StampView { .foregroundColor(.gray) } } - - //MARK: Flip Card Function - func flipCard () { - isFlipped.toggle() - - if isFlipped { // 카드 회전 연속을 위해서 if문 분리 - withAnimation(.linear(duration: durationAndDelay)) { - backDegree = 90 - } - withAnimation(.linear(duration: durationAndDelay).delay(durationAndDelay)) { - frontDegree = 0 - } - } else { - withAnimation(.linear(duration: durationAndDelay)) { - frontDegree = -90 - } - withAnimation(.linear(duration: durationAndDelay).delay(durationAndDelay)) { - backDegree = 0 - } - } - } } struct StampView_Previews: PreviewProvider { From 38c921f721ff5821272c35fb45efc33363c917d6 Mon Sep 17 00:00:00 2001 From: E-know Date: Fri, 9 Sep 2022 15:10:13 +0900 Subject: [PATCH 2/4] [Modify] Modify Variable name and add description --- AsyncSwift/Observed/AppData.swift | 4 ++-- AsyncSwift/Views/StampView.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/AsyncSwift/Observed/AppData.swift b/AsyncSwift/Observed/AppData.swift index dd6b3cc..5f29232 100644 --- a/AsyncSwift/Observed/AppData.swift +++ b/AsyncSwift/Observed/AppData.swift @@ -9,8 +9,8 @@ import SwiftUI import UIKit final class AppData: ObservableObject { - @Published var currentTab: Tab = .event - var tempisScanned = true + @Published var currentTab: Tab = .event // Universal Link로 앱진입시 StampView 전환을 위한 변수 + var scannedSeminarQR = true // Universal Link로 진입시 QR코드 스캔 여부 } diff --git a/AsyncSwift/Views/StampView.swift b/AsyncSwift/Views/StampView.swift index 99b4284..52621ba 100644 --- a/AsyncSwift/Views/StampView.swift +++ b/AsyncSwift/Views/StampView.swift @@ -14,7 +14,7 @@ struct StampView: View { var body: some View { NavigationView { Group { - if appData.tempisScanned { + if appData.scannedSeminarQR { ZStack { stampBack stampFront From 6b583b9fa2208433b594833edf8366a4fa912392 Mon Sep 17 00:00:00 2001 From: Inho Choi <55151796+E-know@users.noreply.github.com> Date: Sat, 10 Sep 2022 00:18:34 +0900 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Eunyeong Kim --- AsyncSwift/Observed/StampView+Observed.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AsyncSwift/Observed/StampView+Observed.swift b/AsyncSwift/Observed/StampView+Observed.swift index 07607de..23c3152 100644 --- a/AsyncSwift/Observed/StampView+Observed.swift +++ b/AsyncSwift/Observed/StampView+Observed.swift @@ -11,7 +11,7 @@ extension StampView { final class Observed: ObservableObject { @Published var cardAnimatonModel = CardAnimationModel() - let durationAndDelay: CGFloat = 0.3 + private let durationAndDelay: CGFloat = 0.3 func didTabCard () { cardAnimatonModel.isTapped.toggle() From 6ac3418dc241c26c5bbcd4e8c93d63dfd6e24dbc Mon Sep 17 00:00:00 2001 From: E-know Date: Sat, 10 Sep 2022 21:37:59 +0900 Subject: [PATCH 4/4] [Modify] Restrict definition of observed property in StampView --- AsyncSwift/Observed/StampView+Observed.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/AsyncSwift/Observed/StampView+Observed.swift b/AsyncSwift/Observed/StampView+Observed.swift index 23c3152..f6da6b3 100644 --- a/AsyncSwift/Observed/StampView+Observed.swift +++ b/AsyncSwift/Observed/StampView+Observed.swift @@ -35,9 +35,13 @@ extension StampView { } struct CardAnimationModel: Identifiable { + fileprivate init() {} + let id = UUID() var backDegree: Double = 0.0 var frontDegree: Double = -90.0 var isTapped = false } } + +