Skip to content

Commit

Permalink
[Feat] #473 - Add ReadShortcutViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
jim4020key committed Jul 24, 2023
1 parent 7f322fa commit 69a9c14
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 193 deletions.
4 changes: 4 additions & 0 deletions HappyAnding/HappyAnding.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -194,6 +195,7 @@
4D778A33290A53BA00C15AC4 /* UIApplication+Keyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIApplication+Keyboard.swift"; sourceTree = "<group>"; };
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 = "<group>"; };
4D93D0742A61D0D10042CBA8 /* ReadShortcutViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadShortcutViewModel.swift; sourceTree = "<group>"; };
4DAD635D292AB61700ABF8C1 /* UpdateShortcutView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateShortcutView.swift; sourceTree = "<group>"; };
87276C372933F6AB00C92F4C /* CustomTextEditor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomTextEditor.swift; sourceTree = "<group>"; };
872A7D8E2918393B004A05B8 /* PrivacyPolicyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivacyPolicyView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -656,6 +658,7 @@
4D61A766291E1EE8000EF531 /* NavigationViewModel.swift */,
F9AC2BB52935201C00165820 /* CheckUpdateVersion.swift */,
4D93D06E2A5956E60042CBA8 /* ShowProfileViewModel.swift */,
4D93D0742A61D0D10042CBA8 /* ReadShortcutViewModel.swift */,
);
path = ViewModel;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
8 changes: 4 additions & 4 deletions HappyAnding/HappyAnding/Extensions/View/View+Navigation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 0 additions & 7 deletions HappyAnding/HappyAnding/Model/NavigationStackModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
111 changes: 111 additions & 0 deletions HappyAnding/HappyAnding/ViewModel/ReadShortcutViewModel.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,23 @@ 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)

}
}

@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)
}
Expand Down
Loading

0 comments on commit 69a9c14

Please sign in to comment.