From 69a9c149268765920e20df7d49ab096c8a9e55de Mon Sep 17 00:00:00 2001 From: jim4020key Date: Sun, 16 Jul 2023 18:42:09 +0900 Subject: [PATCH] [Feat] #473 - Add ReadShortcutViewModel --- .../HappyAnding.xcodeproj/project.pbxproj | 4 + .../Extensions/View/View+Navigation.swift | 8 +- .../Model/NavigationStackModel.swift | 7 - .../ViewModel/ReadShortcutViewModel.swift | 111 +++++++++ .../Components/MyShortcutCardListView.swift | 5 +- .../ReadCurationView.swift | 7 +- .../ExploreShortcutView.swift | 10 +- .../ListCategoryShortcutView.swift | 6 +- .../ListShortcutView.swift | 12 +- .../ReadShortcutViews/ReadShortcutView.swift | 214 ++++++------------ .../ReadShortcutViews/ShowProfileView.swift | 7 +- .../UpdateShortcutView.swift | 8 +- .../HappyAnding/Views/SearchView.swift | 5 +- .../Views/TabView/ShortcutTabView.swift | 3 +- 14 files changed, 214 insertions(+), 193 deletions(-) create mode 100644 HappyAnding/HappyAnding/ViewModel/ReadShortcutViewModel.swift diff --git a/HappyAnding/HappyAnding.xcodeproj/project.pbxproj b/HappyAnding/HappyAnding.xcodeproj/project.pbxproj index 0a35bf03..f69c149a 100644 --- a/HappyAnding/HappyAnding.xcodeproj/project.pbxproj +++ b/HappyAnding/HappyAnding.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 4D7D16072986BBD7008B3332 /* TextLiteral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D7D16062986BBD7008B3332 /* TextLiteral.swift */; }; 4D7D16082986BBDE008B3332 /* TextLiteral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D7D16062986BBD7008B3332 /* TextLiteral.swift */; }; 4D93D06F2A5956E60042CBA8 /* ShowProfileViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D93D06E2A5956E60042CBA8 /* ShowProfileViewModel.swift */; }; + 4D93D0752A61D0D10042CBA8 /* ReadShortcutViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D93D0742A61D0D10042CBA8 /* ReadShortcutViewModel.swift */; }; 4DAD635E292AB61700ABF8C1 /* UpdateShortcutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DAD635D292AB61700ABF8C1 /* UpdateShortcutView.swift */; }; 4DF62DD52A0550ED00A8B377 /* UIScreen+Size.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8795A16F292AB945004B765F /* UIScreen+Size.swift */; }; 87276C382933F6AB00C92F4C /* CustomTextEditor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87276C372933F6AB00C92F4C /* CustomTextEditor.swift */; }; @@ -194,6 +195,7 @@ 4D778A33290A53BA00C15AC4 /* UIApplication+Keyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIApplication+Keyboard.swift"; sourceTree = ""; }; 4D7D16062986BBD7008B3332 /* TextLiteral.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = TextLiteral.swift; path = HappyAnding/TextLiteral.swift; sourceTree = SOURCE_ROOT; }; 4D93D06E2A5956E60042CBA8 /* ShowProfileViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShowProfileViewModel.swift; sourceTree = ""; }; + 4D93D0742A61D0D10042CBA8 /* ReadShortcutViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadShortcutViewModel.swift; sourceTree = ""; }; 4DAD635D292AB61700ABF8C1 /* UpdateShortcutView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateShortcutView.swift; sourceTree = ""; }; 87276C372933F6AB00C92F4C /* CustomTextEditor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomTextEditor.swift; sourceTree = ""; }; 872A7D8E2918393B004A05B8 /* PrivacyPolicyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivacyPolicyView.swift; sourceTree = ""; }; @@ -656,6 +658,7 @@ 4D61A766291E1EE8000EF531 /* NavigationViewModel.swift */, F9AC2BB52935201C00165820 /* CheckUpdateVersion.swift */, 4D93D06E2A5956E60042CBA8 /* ShowProfileViewModel.swift */, + 4D93D0742A61D0D10042CBA8 /* ReadShortcutViewModel.swift */, ); path = ViewModel; sourceTree = ""; @@ -969,6 +972,7 @@ F9136EB6293612310034AAB2 /* ShortcutsZipView.swift in Sources */, 87E99CB128FFF273009B691F /* WriteCurationSetView.swift in Sources */, 4D61A767291E1EE8000EF531 /* NavigationViewModel.swift in Sources */, + 4D93D0752A61D0D10042CBA8 /* ReadShortcutViewModel.swift in Sources */, F96D45BD29816578000C2441 /* StickyHeader.swift in Sources */, 87E99CEE29080D33009B691F /* User.swift in Sources */, F976E82C29368E0D0088BBA1 /* Version.swift in Sources */, diff --git a/HappyAnding/HappyAnding/Extensions/View/View+Navigation.swift b/HappyAnding/HappyAnding/Extensions/View/View+Navigation.swift index 8f659162..f36e8a5e 100644 --- a/HappyAnding/HappyAnding/Extensions/View/View+Navigation.swift +++ b/HappyAnding/HappyAnding/Extensions/View/View+Navigation.swift @@ -79,8 +79,8 @@ extension View { switch data { case is NavigationListShortcutType: ListShortcutView(data: data as! NavigationListShortcutType) - case is NavigationReadShortcutType: - ReadShortcutView(data: data as! NavigationReadShortcutType) + case is Shortcuts: + ReadShortcutView(viewModel: ReadShortcutViewModel(data: data as! Shortcuts)) case is Curation: ReadCurationView(viewModel: ReadCurationViewModel(data: data as! Curation)) case is CurationType: @@ -124,8 +124,8 @@ struct NavigationViewModifier: ViewModifier { .navigationDestination(for: CurationType.self) { data in ListCurationView(viewModel: ListCurationViewModel(data: data)) } - .navigationDestination(for: NavigationReadShortcutType.self) { data in - ReadShortcutView(data: data) + .navigationDestination(for: Shortcuts.self) { data in + ReadShortcutView(viewModel: ReadShortcutViewModel(data: data)) } .navigationDestination(for: NavigationListShortcutType.self) { data in ListShortcutView(data: data) diff --git a/HappyAnding/HappyAnding/Model/NavigationStackModel.swift b/HappyAnding/HappyAnding/Model/NavigationStackModel.swift index 5760ec24..21d0f376 100644 --- a/HappyAnding/HappyAnding/Model/NavigationStackModel.swift +++ b/HappyAnding/HappyAnding/Model/NavigationStackModel.swift @@ -15,13 +15,6 @@ struct NavigationListShortcutType: Identifiable, Hashable { let navigationParentView: NavigationParentView } -struct NavigationReadShortcutType: Identifiable, Hashable { - var id = UUID().uuidString - - var shortcut: Shortcuts? - let shortcutID: String - let navigationParentView: NavigationParentView -} struct NavigationProfile: Identifiable, Hashable { var id = UUID().uuidString diff --git a/HappyAnding/HappyAnding/ViewModel/ReadShortcutViewModel.swift b/HappyAnding/HappyAnding/ViewModel/ReadShortcutViewModel.swift new file mode 100644 index 00000000..dea9e37d --- /dev/null +++ b/HappyAnding/HappyAnding/ViewModel/ReadShortcutViewModel.swift @@ -0,0 +1,111 @@ +// +// ReadShortcutViewModel.swift +// HappyAnding +// +// Created by kimjimin on 2023/07/15. +// + +import SwiftUI + +final class ReadShortcutViewModel: ObservableObject { + + var shortcutsZipViewModel = ShortcutsZipViewModel.share + + @Published var shortcut: Shortcuts + + @Published var isDeletingShortcut = false + @Published var isEditingShortcut = false + @Published var isUpdatingShortcut = false + + @Published var isMyLike = false + @Published var isMyFirstLike = false + @Published var isDownloadingShortcut = false + @Published var isDowngradingUserLevel = false + + @Published var currentTab: Int = 0 + @Published var comments: Comments = Comments(id: "", comments: []) + @Published var comment: Comment = Comment(user_nickname: "", user_id: "", date: "", depth: 0, contents: "") + @Published var nestedCommentTarget: String = "" + @Published var commentText = "" + + @Published var isEditingComment = false + @Published var isUndoingCommentEdit = false + + init(data: Shortcuts) { + self.shortcut = shortcutsZipViewModel.fetchShortcutDetail(id: data.id) ?? data + self.isMyLike = shortcutsZipViewModel.checkLikedShortrcut(shortcutID: data.id) + self.isMyFirstLike = isMyLike + self.comments = shortcutsZipViewModel.fetchComment(shortcutID: data.id) + } + + func checkIfDownloaded() { + if (shortcutsZipViewModel.userInfo?.downloadedShortcuts.firstIndex(where: { $0.id == shortcut.id })) == nil { + shortcut.numberOfDownload += 1 + } + self.isDownloadingShortcut = true + } + + func updateNumberOfDownload() { + shortcutsZipViewModel.updateNumberOfDownload(shortcut: shortcut, downloadlinkIndex: 0) + } + + func onViewDissapear() { + if isMyLike != isMyFirstLike { + shortcutsZipViewModel.updateNumberOfLike(isMyLike: isMyLike, shortcut: shortcut) + } + } + + func deleteShortcut() { + shortcutsZipViewModel.deleteShortcutIDInUser(shortcutID: shortcut.id) + shortcutsZipViewModel.deleteShortcutInCuration(curationsIDs: shortcut.curationIDs, shortcutID: shortcut.id) + shortcutsZipViewModel.deleteData(model: shortcut) + shortcutsZipViewModel.shortcutsMadeByUser = shortcutsZipViewModel.shortcutsMadeByUser.filter { $0.id != shortcut.id } + shortcutsZipViewModel.updateShortcutGrade() + } + + func cancelEditingComment() { + self.isEditingComment.toggle() + self.comment = self.comment.resetComment() + self.commentText = "" + } + + func postComment() { + if !isEditingComment { + comment.contents = commentText + comment.date = Date().getDate() + comment.user_id = shortcutsZipViewModel.userInfo!.id + comment.user_nickname = shortcutsZipViewModel.userInfo!.nickname + comments.comments.append(comment) + } else { + if let index = comments.comments.firstIndex(where: { $0.id == comment.id }) { + comments.comments[index].contents = commentText + } + isEditingComment = false + } + shortcutsZipViewModel.setData(model: comments) + commentText = "" + comment = comment.resetComment() + } + + func cancelNestedComment() { + comment.bundle_id = "\(Date().getDate())_\(UUID().uuidString)" + comment.depth = 0 + } + + func checkAuthor() -> Bool { + return self.shortcut.author == shortcutsZipViewModel.currentUser() + } + + func shareShortcut() { + guard let deepLink = URL(string: "ShortcutsZip://myPage/detailView?shortcutID=\(shortcut.id)") else { return } + let activityVC = UIActivityViewController(activityItems: [deepLink], applicationActivities: nil) + let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene + guard let window = windowScene?.windows.first else { return } + window.rootViewController?.present(activityVC, animated: true, completion: nil) + } + + func checkDowngrade() { + isDeletingShortcut.toggle() + isDowngradingUserLevel = shortcutsZipViewModel.isShortcutDowngrade() + } +} diff --git a/HappyAnding/HappyAnding/Views/Components/MyShortcutCardListView.swift b/HappyAnding/HappyAnding/Views/Components/MyShortcutCardListView.swift index 70d5634b..416c36d9 100644 --- a/HappyAnding/HappyAnding/Views/Components/MyShortcutCardListView.swift +++ b/HappyAnding/HappyAnding/Views/Components/MyShortcutCardListView.swift @@ -56,13 +56,10 @@ struct MyShortcutCardListView: View { if let shortcuts { ForEach(Array((shortcuts.enumerated())), id: \.offset) { index, shortcut in if index < 7 { - let data = NavigationReadShortcutType(shortcutID: shortcut.id, - navigationParentView: self.navigationParentView) - MyShortcutCardView(myShortcutIcon: shortcut.sfSymbol, myShortcutName: shortcut.title, myShortcutColor: shortcut.color) - .navigationLinkRouter(data: data) + .navigationLinkRouter(data: shortcut) } } } diff --git a/HappyAnding/HappyAnding/Views/ExploreCurationViews/ReadCurationView.swift b/HappyAnding/HappyAnding/Views/ExploreCurationViews/ReadCurationView.swift index 291a764c..ecd4ae97 100644 --- a/HappyAnding/HappyAnding/Views/ExploreCurationViews/ReadCurationView.swift +++ b/HappyAnding/HappyAnding/Views/ExploreCurationViews/ReadCurationView.swift @@ -23,11 +23,10 @@ struct ReadCurationView: View { VStack(spacing: 0) { ForEach($viewModel.curation.shortcuts, id: \.self) { shortcut in - let data = NavigationReadShortcutType(shortcutID: shortcut.id, - navigationParentView: .curations) - ShortcutCell(shortcutCell: shortcut.wrappedValue, + + ShortcutCell(shortcutCell: shortcut, navigationParentView: .curations) - .navigationLinkRouter(data: data) + .navigationLinkRouter(data: shortcut) } } diff --git a/HappyAnding/HappyAnding/Views/ExploreShortcutViews/ExploreShortcutView/ExploreShortcutView.swift b/HappyAnding/HappyAnding/Views/ExploreShortcutViews/ExploreShortcutView/ExploreShortcutView.swift index a5ff6dde..3e8f1fbf 100644 --- a/HappyAnding/HappyAnding/Views/ExploreShortcutViews/ExploreShortcutView/ExploreShortcutView.swift +++ b/HappyAnding/HappyAnding/Views/ExploreShortcutViews/ExploreShortcutView/ExploreShortcutView.swift @@ -133,13 +133,11 @@ extension ExploreShortcutView { ForEach(Array(shortcuts.enumerated()), id:\.offset) { index, shortcut in if index < 3 { - let data = NavigationReadShortcutType(shortcutID:shortcut.id, - navigationParentView: .shortcuts) ShortcutCell(shortcut: shortcut, rankNumber: index + 1, navigationParentView: .shortcuts) - .navigationLinkRouter(data: data) + .navigationLinkRouter(data: shortcut) } } .background(Color.shortcutsZipBackground) @@ -168,15 +166,11 @@ extension ExploreShortcutView { HStack { ForEach(Array((shortcutsZipViewModel.shortcutsInCategory[randomCategories[0].index].enumerated())), id: \.offset) { index, shortcut in if index < 7 { - let data = NavigationReadShortcutType( - shortcutID: shortcut.id, - navigationParentView: .shortcuts) - ShortcutCardCell( categoryShortcutIcon: shortcut.sfSymbol, categoryShortcutName: shortcut.title, categoryShortcutColor: shortcut.color) - .navigationLinkRouter(data: data) + .navigationLinkRouter(data: shortcut) } } } diff --git a/HappyAnding/HappyAnding/Views/ExploreShortcutViews/ListCategoryShortcutView.swift b/HappyAnding/HappyAnding/Views/ExploreShortcutViews/ListCategoryShortcutView.swift index 2cb26e76..dc535672 100644 --- a/HappyAnding/HappyAnding/Views/ExploreShortcutViews/ListCategoryShortcutView.swift +++ b/HappyAnding/HappyAnding/Views/ExploreShortcutViews/ListCategoryShortcutView.swift @@ -22,12 +22,10 @@ struct ListCategoryShortcutView: View { LazyVStack(spacing: 0) { ForEach(data.shortcuts, id: \.self) { shortcut in - let data = NavigationReadShortcutType(shortcut: shortcut, - shortcutID: shortcut.id, - navigationParentView: self.data.navigationParentView) + ShortcutCell(shortcut: shortcut, navigationParentView: self.data.navigationParentView) - .navigationLinkRouter(data: data) + .navigationLinkRouter(data: shortcut) .listRowInsets(EdgeInsets()) .listRowSeparator(.hidden) } diff --git a/HappyAnding/HappyAnding/Views/ExploreShortcutViews/ListShortcutView.swift b/HappyAnding/HappyAnding/Views/ExploreShortcutViews/ListShortcutView.swift index 5d21a150..7d140e5a 100644 --- a/HappyAnding/HappyAnding/Views/ExploreShortcutViews/ListShortcutView.swift +++ b/HappyAnding/HappyAnding/Views/ExploreShortcutViews/ListShortcutView.swift @@ -67,13 +67,11 @@ struct ListShortcutView: View { @ViewBuilder private func makeShortcutCellList(_ shortcuts: [Shortcuts]) -> some View { ForEach(shortcuts, id: \.self) { shortcut in - let navigationData = NavigationReadShortcutType(shortcut: shortcut, - shortcutID: shortcut.id, - navigationParentView: self.data.navigationParentView) + ShortcutCell(shortcut: shortcut, sectionType: data.sectionType, navigationParentView: data.navigationParentView) - .navigationLinkRouter(data: navigationData) + .navigationLinkRouter(data: shortcut) } } @@ -81,13 +79,11 @@ struct ListShortcutView: View { @ViewBuilder private func makeIndexShortcutCellList(_ shortcuts: [Shortcuts]) -> some View { ForEach(Array(shortcuts.enumerated()), id: \.offset) { index, shortcut in - let navigationData = NavigationReadShortcutType(shortcut: shortcut, - shortcutID: shortcut.id, - navigationParentView: self.data.navigationParentView) + ShortcutCell(shortcut: shortcut, rankNumber: index + 1, navigationParentView: data.navigationParentView) - .navigationLinkRouter(data: navigationData) + .navigationLinkRouter(data: shortcut) .listRowInsets(EdgeInsets()) .listRowSeparator(.hidden) } diff --git a/HappyAnding/HappyAnding/Views/ReadShortcutViews/ReadShortcutView.swift b/HappyAnding/HappyAnding/Views/ReadShortcutViews/ReadShortcutView.swift index 367bbe9a..10f11943 100644 --- a/HappyAnding/HappyAnding/Views/ReadShortcutViews/ReadShortcutView.swift +++ b/HappyAnding/HappyAnding/Views/ReadShortcutViews/ReadShortcutView.swift @@ -14,29 +14,9 @@ struct ReadShortcutView: View { @Environment(\.openURL) private var openURL @Environment(\.loginAlertKey) var loginAlerter - @EnvironmentObject var shortcutsZipViewModel: ShortcutsZipViewModel - + @StateObject var viewModel: ReadShortcutViewModel @StateObject var writeNavigation = WriteShortcutNavigation() - @State var isDeletingShortcut = false - @State var isEditingShortcut = false - @State var isUpdatingShortcut = false - - @State var isMyLike = false - @State var isMyFirstLike = false - @State var isDownloadingShortcut = false - @State var isDowngradingUserLevel = false - - @State var currentTab: Int = 0 - @State var data: NavigationReadShortcutType - @State var comments: Comments = Comments(id: "", comments: []) - @State var comment: Comment = Comment(user_nickname: "", user_id: "", date: "", depth: 0, contents: "") - @State var nestedCommentTarget: String = "" - @State var commentText = "" - - @State var isEditingComment = false - @State var isUndoingCommentEdit = false - @AppStorage("useWithoutSignIn") var useWithoutSignIn: Bool = false @FocusState private var isFocused: Bool @Namespace var namespace @@ -49,18 +29,18 @@ struct ReadShortcutView: View { ScrollViewReader { proxy in ScrollView { VStack(spacing: 0) { - if data.shortcut != nil { + if viewModel.shortcut != nil { StickyHeader(height: 40) /// 단축어 타이틀 - ReadShortcutViewHeader(shortcut: $data.shortcut.unwrap()!, isMyLike: $isMyLike) + ReadShortcutViewHeader(shortcut: $viewModel.shortcut, isMyLike: $viewModel.isMyLike) /// 탭뷰 (기본 정보, 버전 정보, 댓글) LazyVStack(pinnedViews: [.sectionHeaders]) { Section(header: tabBarView) { ZStack { - TabView(selection: self.$currentTab) { + TabView(selection: $viewModel.currentTab) { Color.clear .tag(0) Color.clear @@ -71,24 +51,24 @@ struct ReadShortcutView: View { .tabViewStyle(.page(indexDisplayMode: .never)) .frame(minHeight: UIScreen.screenHeight / 2) - switch currentTab { + switch viewModel.currentTab { case 0: - ReadShortcutContentView(shortcut: $data.shortcut.unwrap()!) + ReadShortcutContentView(shortcut: $viewModel.shortcut) case 1: - ReadShortcutVersionView(shortcut: $data.shortcut.unwrap()!, isUpdating: $isUpdatingShortcut) + ReadShortcutVersionView(shortcut: $viewModel.shortcut, isUpdating: $viewModel.isUpdatingShortcut) case 2: ReadShortcutCommentView(isFocused: _isFocused, - newComment: $comment, - comments: $comments, - nestedCommentTarget: $nestedCommentTarget, - isEditingComment: $isEditingComment, - shortcutID: data.shortcutID) + newComment: $viewModel.comment, + comments: $viewModel.comments, + nestedCommentTarget: $viewModel.nestedCommentTarget, + isEditingComment: $viewModel.isEditingComment, + shortcutID: viewModel.shortcut.id) .id(bottomID) default: EmptyView() } } - .animation(.easeInOut, value: currentTab) + .animation(.easeInOut, value: viewModel.currentTab) .padding(.top, 4) .padding(.horizontal, 16) } @@ -100,14 +80,14 @@ struct ReadShortcutView: View { NotificationCenter.default.addObserver(forName: UIResponder.keyboardDidShowNotification, object: nil, queue: .main) { notification in withAnimation { - if currentTab == 2 && !isEditingComment && comment.depth == 0 { + if viewModel.currentTab == 2 && !viewModel.isEditingComment && viewModel.comment.depth == 0 { proxy.scrollTo(bottomID, anchor: .bottom) } } } } } - .scrollDisabled(isEditingComment) + .scrollDisabled(viewModel.isEditingComment) .background(Color.shortcutsZipBackground) .navigationBarBackground ({ Color.shortcutsZipWhite }) .navigationBarTitleDisplayMode(NavigationBarItem.TitleDisplayMode.inline) @@ -117,33 +97,28 @@ struct ReadShortcutView: View { /// Safe Area에 고정된 댓글창, 다운로드 버튼 VStack { - if !isEditingComment { - if currentTab == 2 { + if !viewModel.isEditingComment { + if viewModel.currentTab == 2 { commentTextField } if !isFocused { - if let shortcut = data.shortcut { - Button { - if !useWithoutSignIn { - if let url = URL(string: shortcut.downloadLink[0]) { - if (shortcutsZipViewModel.userInfo?.downloadedShortcuts.firstIndex(where: { $0.id == data.shortcutID })) == nil { - data.shortcut?.numberOfDownload += 1 - } - isDownloadingShortcut = true - openURL(url) - } - shortcutsZipViewModel.updateNumberOfDownload(shortcut: shortcut, downloadlinkIndex: 0) - } else { - loginAlerter.isPresented = true + Button { + if !useWithoutSignIn { + if let url = URL(string: viewModel.shortcut.downloadLink[0]) { + viewModel.checkIfDownloaded() + openURL(url) } - } label: { - Text("다운로드 | \(Image(systemName: "arrow.down.app.fill")) \(shortcut.numberOfDownload)") - .shortcutsZipBody1() - .foregroundColor(Color.textIcon) - .padding() - .frame(maxWidth: .infinity) - .background(Color.shortcutsZipPrimary) + viewModel.updateNumberOfDownload() + } else { + loginAlerter.isPresented = true } + } label: { + Text("다운로드 | \(Image(systemName: "arrow.down.app.fill")) \(viewModel.shortcut.numberOfDownload)") + .shortcutsZipBody1() + .foregroundColor(Color.textIcon) + .padding() + .frame(maxWidth: .infinity) + .background(Color.shortcutsZipPrimary) } } } @@ -152,58 +127,45 @@ struct ReadShortcutView: View { } .onAppear { UINavigationBar.appearance().standardAppearance.configureWithTransparentBackground() - data.shortcut = shortcutsZipViewModel.fetchShortcutDetail(id: data.shortcutID) - isMyLike = shortcutsZipViewModel.checkLikedShortrcut(shortcutID: data.shortcutID) - isMyFirstLike = isMyLike - self.comments = shortcutsZipViewModel.fetchComment(shortcutID: data.shortcutID) - } - .onChange(of: isEditingShortcut || isUpdatingShortcut) { _ in - if !isEditingShortcut || !isUpdatingShortcut { - data.shortcut = shortcutsZipViewModel.fetchShortcutDetail(id: data.shortcutID) - } - } - .onChange(of: shortcutsZipViewModel.allComments) { _ in - self.comments = shortcutsZipViewModel.fetchComment(shortcutID: data.shortcutID) } +// .onChange(of: viewModel.isEditingShortcut || viewModel.isUpdatingShortcut) { _ in +// if !isEditingShortcut || !isUpdatingShortcut { +// data.shortcut = shortcutsZipViewModel.fetchShortcutDetail(id: data.shortcutID) +// } +// } +// .onChange(of: viewModel.shortcutsZipViewModel.allComments) { _ in +// self.comments = shortcutsZipViewModel.fetchComment(shortcutID: data.shortcutID) +// } .onDisappear { - if let shortcut = data.shortcut { - if isMyLike != isMyFirstLike { - shortcutsZipViewModel.updateNumberOfLike(isMyLike: isMyLike, shortcut: shortcut) - } - } + viewModel.onViewDissapear() } - .alert(TextLiteral.readShortcutViewDeletionTitle, isPresented: $isDeletingShortcut) { + .alert(TextLiteral.readShortcutViewDeletionTitle, isPresented: $viewModel.isDeletingShortcut) { Button(role: .cancel) { } label: { Text(TextLiteral.cancel) } Button(role: .destructive) { - if let shortcut = data.shortcut { - shortcutsZipViewModel.deleteShortcutIDInUser(shortcutID: shortcut.id) - shortcutsZipViewModel.deleteShortcutInCuration(curationsIDs: shortcut.curationIDs, shortcutID: shortcut.id) - shortcutsZipViewModel.deleteData(model: shortcut) - shortcutsZipViewModel.shortcutsMadeByUser = shortcutsZipViewModel.shortcutsMadeByUser.filter { $0.id != shortcut.id } - shortcutsZipViewModel.updateShortcutGrade() - self.presentation.wrappedValue.dismiss() - } + viewModel.deleteShortcut() + self.presentation.wrappedValue.dismiss() } label: { Text(TextLiteral.delete) } } message: { - Text(isDowngradingUserLevel ? TextLiteral.readShortcutViewDeletionMessageDowngrade : TextLiteral.readShortcutViewDeletionMessage) + Text(viewModel.isDowngradingUserLevel ? TextLiteral.readShortcutViewDeletionMessageDowngrade : TextLiteral.readShortcutViewDeletionMessage) } - .fullScreenCover(isPresented: $isEditingShortcut) { + .fullScreenCover(isPresented: $viewModel.isEditingShortcut) { NavigationRouter(content: writeShortcutView, path: $writeNavigation.navigationPath) .environmentObject(writeNavigation) } - .fullScreenCover(isPresented: $isUpdatingShortcut) { - UpdateShortcutView(isUpdating: $isUpdatingShortcut, shortcut: $data.shortcut) + //TODO: update shortcut + .fullScreenCover(isPresented: $viewModel.isUpdatingShortcut) { + UpdateShortcutView(isUpdating: $viewModel.isUpdatingShortcut, shortcut: $viewModel.shortcut) } /// 댓글 수정할 때 뒷 배경을 어둡게 만들기 위한 뷰 - if isEditingComment { + if viewModel.isEditingComment { Color.black .ignoresSafeArea() .opacity(0.4) @@ -218,13 +180,13 @@ struct ReadShortcutView: View { } } .onAppear { - commentText = comment.contents + viewModel.commentText = viewModel.comment.contents } .onTapGesture(count: 1) { isFocused.toggle() - isUndoingCommentEdit.toggle() + viewModel.isUndoingCommentEdit.toggle() } - .alert(TextLiteral.readShortcutViewDeleteFixesTitle, isPresented: $isUndoingCommentEdit) { + .alert(TextLiteral.readShortcutViewDeleteFixesTitle, isPresented: $viewModel.isUndoingCommentEdit) { Button(role: .cancel) { isFocused.toggle() } label: { @@ -233,9 +195,7 @@ struct ReadShortcutView: View { Button(role: .destructive) { withAnimation(.easeInOut) { - isEditingComment.toggle() - comment = comment.resetComment() - commentText = "" + viewModel.cancelEditingComment() } } label: { Text(TextLiteral.delete) @@ -250,11 +210,9 @@ struct ReadShortcutView: View { @ViewBuilder private func writeShortcutView() -> some View { - if let shortcut = data.shortcut { - WriteShortcutView(isWriting: $isEditingShortcut, - shortcut: shortcut, - isEdit: true) - } + WriteShortcutView(isWriting: $viewModel.isEditingShortcut, + shortcut: viewModel.shortcut, + isEdit: true) } } @@ -265,57 +223,43 @@ extension ReadShortcutView { private var commentTextField: some View { VStack(spacing: 0) { - if comment.depth == 1 && !isEditingComment { + if viewModel.comment.depth == 1 && !viewModel.isEditingComment { nestedCommentTargetView } HStack { - if comment.depth == 1 && !isEditingComment { + if viewModel.comment.depth == 1 && !viewModel.isEditingComment { Image(systemName: "arrow.turn.down.right") .smallIcon() .foregroundColor(.gray4) } - TextField(useWithoutSignIn ? TextLiteral.readShortcutViewCommentDescriptionBeforeLogin : TextLiteral.readShortcutViewCommentDescription, text: $commentText, axis: .vertical) + TextField(useWithoutSignIn ? TextLiteral.readShortcutViewCommentDescriptionBeforeLogin : TextLiteral.readShortcutViewCommentDescription, text: $viewModel.commentText, axis: .vertical) .keyboardType(.twitter) .disabled(useWithoutSignIn) .disableAutocorrection(true) .textInputAutocapitalization(.never) .shortcutsZipBody2() - .lineLimit(comment.depth == 1 ? 2 : 4) + .lineLimit(viewModel.comment.depth == 1 ? 2 : 4) .focused($isFocused) .onAppear { UIApplication.shared.hideKeyboard() } Button { - if !isEditingComment { - comment.contents = commentText - comment.date = Date().getDate() - comment.user_id = shortcutsZipViewModel.userInfo!.id - comment.user_nickname = shortcutsZipViewModel.userInfo!.nickname - comments.comments.append(comment) - } else { - if let index = comments.comments.firstIndex(where: { $0.id == comment.id }) { - comments.comments[index].contents = commentText - } - isEditingComment = false - } - shortcutsZipViewModel.setData(model: comments) - commentText = "" - comment = comment.resetComment() + viewModel.postComment() isFocused.toggle() } label: { Image(systemName: "paperplane.fill") .mediumIcon() - .foregroundColor(commentText == "" ? Color.gray2 : Color.gray5) + .foregroundColor(viewModel.commentText == "" ? Color.gray2 : Color.gray5) } - .disabled(commentText == "" ? true : false) + .disabled(viewModel.commentText == "" ? true : false) } .padding(.vertical, 12) .padding(.horizontal, 16) .background( Rectangle() .fill(Color.gray1) - .cornerRadius(12 ,corners: (comment.depth == 1) && (!isEditingComment) ? [.bottomLeft, .bottomRight] : .allCorners) + .cornerRadius(12 ,corners: (viewModel.comment.depth == 1) && (!viewModel.isEditingComment) ? [.bottomLeft, .bottomRight] : .allCorners) ) .padding(.horizontal, 16) .padding(.bottom, 20) @@ -325,15 +269,14 @@ extension ReadShortcutView { private var nestedCommentTargetView: some View { HStack { - Text("@ \(nestedCommentTarget)") + Text("@ \(viewModel.nestedCommentTarget)") .shortcutsZipFootnote() .foregroundColor(.gray5) Spacer() Button { - comment.bundle_id = "\(Date().getDate())_\(UUID().uuidString)" - comment.depth = 0 + viewModel.cancelNestedComment() } label: { Image(systemName: "xmark") .smallIcon() @@ -354,7 +297,7 @@ extension ReadShortcutView { @ViewBuilder private func readShortcutViewNavigationBarItems() -> some View { - if self.data.shortcut?.author == shortcutsZipViewModel.currentUser() { + if viewModel.checkAuthor() { Menu { Section { editButton @@ -374,7 +317,7 @@ extension ReadShortcutView { private var editButton: some View { Button { - isEditingShortcut.toggle() + viewModel.isEditingShortcut.toggle() } label: { Label(TextLiteral.edit, systemImage: "square.and.pencil") } @@ -382,7 +325,7 @@ extension ReadShortcutView { private var updateButton: some View { Button { - isUpdatingShortcut.toggle() + viewModel.isUpdatingShortcut.toggle() } label: { Label(TextLiteral.update, systemImage: "clock.arrow.circlepath") } @@ -390,13 +333,7 @@ extension ReadShortcutView { private var shareButton: some View { Button { - if let shortcut = data.shortcut { - guard let deepLink = URL(string: "ShortcutsZip://myPage/detailView?shortcutID=\(shortcut.id)") else { return } - let activityVC = UIActivityViewController(activityItems: [deepLink], applicationActivities: nil) - let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene - guard let window = windowScene?.windows.first else { return } - window.rootViewController?.present(activityVC, animated: true, completion: nil) - } + viewModel.shareShortcut() } label: { Label(TextLiteral.share, systemImage: "square.and.arrow.up") .foregroundColor(.gray4) @@ -406,8 +343,7 @@ extension ReadShortcutView { private var deleteButton: some View { Button(role: .destructive) { - isDeletingShortcut.toggle() - isDowngradingUserLevel = shortcutsZipViewModel.isShortcutDowngrade() + viewModel.checkDowngrade() } label: { Label(TextLiteral.delete, systemImage: "trash.fill") } @@ -428,10 +364,10 @@ extension ReadShortcutView { private func tabBarItem(string: String, tab: Int) -> some View { Button { - self.currentTab = tab + viewModel.currentTab = tab } label: { VStack { - if self.currentTab == tab { + if viewModel.currentTab == tab { Text(string) .shortcutsZipHeadline() .foregroundColor(.gray5) @@ -446,7 +382,7 @@ extension ReadShortcutView { Color.clear.frame(height: 2) } } - .animation(.spring(), value: currentTab) + .animation(.spring(), value: viewModel.currentTab) } .buttonStyle(.plain) } diff --git a/HappyAnding/HappyAnding/Views/ReadShortcutViews/ShowProfileView.swift b/HappyAnding/HappyAnding/Views/ReadShortcutViews/ShowProfileView.swift index 3613ae4b..143f3fc0 100644 --- a/HappyAnding/HappyAnding/Views/ReadShortcutViews/ShowProfileView.swift +++ b/HappyAnding/HappyAnding/Views/ReadShortcutViews/ShowProfileView.swift @@ -135,12 +135,9 @@ struct ShowProfileView: View { VStack(spacing: 0) { ForEach(viewModel.shortcuts, id:\.self) { shortcut in - let data = NavigationReadShortcutType(shortcutID:shortcut.id, - navigationParentView: .shortcuts) - ShortcutCell(shortcut: shortcut, - navigationParentView: data.navigationParentView) - .navigationLinkRouter(data: data) + navigationParentView: .shortcuts) + .navigationLinkRouter(data: shortcut) } Spacer() diff --git a/HappyAnding/HappyAnding/Views/ReadShortcutViews/UpdateShortcutView.swift b/HappyAnding/HappyAnding/Views/ReadShortcutViews/UpdateShortcutView.swift index 8b6a72ba..e1ea8660 100644 --- a/HappyAnding/HappyAnding/Views/ReadShortcutViews/UpdateShortcutView.swift +++ b/HappyAnding/HappyAnding/Views/ReadShortcutViews/UpdateShortcutView.swift @@ -14,7 +14,7 @@ struct UpdateShortcutView: View { @FocusState var isDescriptionFieldFocused: Bool @Binding var isUpdating: Bool - @Binding var shortcut: Shortcuts? + @Binding var shortcut: Shortcuts @State var updatedLink = "" @State var updateDescription = "" @@ -72,9 +72,9 @@ struct UpdateShortcutView: View { Spacer() Button(action: { - if let shortcut { - shortcutsZipViewModel.updateShortcutVersion(shortcut: shortcut, updateDescription: updateDescription, updateLink: updatedLink) - } + shortcutsZipViewModel.updateShortcutVersion(shortcut: shortcut, + updateDescription: updateDescription, + updateLink: updatedLink) isUpdating.toggle() }, label: { ZStack { diff --git a/HappyAnding/HappyAnding/Views/SearchView.swift b/HappyAnding/HappyAnding/Views/SearchView.swift index 614746ea..2657da96 100644 --- a/HappyAnding/HappyAnding/Views/SearchView.swift +++ b/HappyAnding/HappyAnding/Views/SearchView.swift @@ -37,12 +37,9 @@ struct SearchView: View { VStack(spacing: 0) { ForEach(shortcutResults.sorted(by: { $0.title < $1.title }), id: \.self) { shortcut in - let data = NavigationReadShortcutType(shortcutID: shortcut.id, - navigationParentView: .shortcuts) - ShortcutCell(shortcut: shortcut, navigationParentView: NavigationParentView.shortcuts) - .navigationLinkRouter(data: data) + .navigationLinkRouter(data: shortcut) .listRowInsets(EdgeInsets()) .listRowSeparator(.hidden) } diff --git a/HappyAnding/HappyAnding/Views/TabView/ShortcutTabView.swift b/HappyAnding/HappyAnding/Views/TabView/ShortcutTabView.swift index db91ece5..eb164a81 100644 --- a/HappyAnding/HappyAnding/Views/TabView/ShortcutTabView.swift +++ b/HappyAnding/HappyAnding/Views/TabView/ShortcutTabView.swift @@ -152,8 +152,7 @@ struct ShortcutTabView: View { tempShortcutId = shortcutIDfromURL isShortcutDeeplink = true - let data = NavigationReadShortcutType(shortcutID: self.tempShortcutId, - navigationParentView: .myPage) + let data = shortcutsZipViewModel.fetchShortcutDetail(id: tempShortcutId) navigateLink(data: data) }