-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat] #473 - Add ReadShortcutViewModel
- Loading branch information
1 parent
7f322fa
commit 69a9c14
Showing
14 changed files
with
214 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
HappyAnding/HappyAnding/ViewModel/ReadShortcutViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.