Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Explore curatoin view model 적용 #469

Merged
merged 14 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions HappyAnding/HappyAnding.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
8792478D2918CE450040D5C3 /* UINavigationContoller+Gesture.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8792478C2918CE450040D5C3 /* UINavigationContoller+Gesture.swift */; };
8792479B291BDF820040D5C3 /* SearchView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8792479A291BDF820040D5C3 /* SearchView.swift */; };
8795A170292AB945004B765F /* UIScreen+Size.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8795A16F292AB945004B765F /* UIScreen+Size.swift */; };
8795A172292ABFDE004B765F /* ReadShortcutVersionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8795A171292ABFDE004B765F /* ReadShortcutVersionView.swift */; };
8795A174292ACA50004B765F /* ReadShortcutCommentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8795A173292ACA50004B765F /* ReadShortcutCommentView.swift */; };
87B47F3B2A3DC2740009E75F /* ReadCurationViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87B47F3A2A3DC2740009E75F /* ReadCurationViewModel.swift */; };
87CFD8492939187200F97B86 /* NicknameTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87CFD8482939187200F97B86 /* NicknameTextField.swift */; };
87DBFB062A2127C0000CC442 /* CheckVersionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87DBFB052A2127C0000CC442 /* CheckVersionView.swift */; };
87E606B0291062F900C3DA13 /* AppleAuthCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87E606AF291062F900C3DA13 /* AppleAuthCoordinator.swift */; };
Expand Down Expand Up @@ -206,6 +209,9 @@
8792478C2918CE450040D5C3 /* UINavigationContoller+Gesture.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UINavigationContoller+Gesture.swift"; sourceTree = "<group>"; };
8792479A291BDF820040D5C3 /* SearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchView.swift; sourceTree = "<group>"; };
8795A16F292AB945004B765F /* UIScreen+Size.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIScreen+Size.swift"; sourceTree = "<group>"; };
8795A171292ABFDE004B765F /* ReadShortcutVersionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadShortcutVersionView.swift; sourceTree = "<group>"; };
8795A173292ACA50004B765F /* ReadShortcutCommentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadShortcutCommentView.swift; sourceTree = "<group>"; };
87B47F3A2A3DC2740009E75F /* ReadCurationViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadCurationViewModel.swift; sourceTree = "<group>"; };
87CFD8482939187200F97B86 /* NicknameTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NicknameTextField.swift; sourceTree = "<group>"; };
87DBFB052A2127C0000CC442 /* CheckVersionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckVersionView.swift; sourceTree = "<group>"; };
87E606AD2910623C00C3DA13 /* HappyAnding.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = HappyAnding.entitlements; sourceTree = "<group>"; };
Expand Down Expand Up @@ -596,6 +602,7 @@
A0F822AB2910B8F100AF4448 /* ShortcutsZipViewModel.swift */,
4D61A766291E1EE8000EF531 /* NavigationViewModel.swift */,
F9AC2BB52935201C00165820 /* CheckUpdateVersion.swift */,
87B47F3A2A3DC2740009E75F /* ReadCurationViewModel.swift */,
);
path = ViewModel;
sourceTree = "<group>";
Expand Down Expand Up @@ -927,6 +934,7 @@
F9724BBF292755E400860F8A /* Comment.swift in Sources */,
87E99CA328FFF22E009B691F /* ExploreCurationView.swift in Sources */,
A0F822B729164D2300AF4448 /* ListCategoryShortcutView.swift in Sources */,
87B47F3B2A3DC2740009E75F /* ReadCurationViewModel.swift in Sources */,
872B5D3D2A2E0FF9008DCC57 /* CurationType.swift in Sources */,
4D3DBB88292E67E600DE8160 /* EditNicknameView.swift in Sources */,
87E99CE82907C6E6009B691F /* Shortcuts.swift in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions HappyAnding/HappyAnding/Extensions/View/View+Navigation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ extension View {
case is NavigationReadShortcutType:
ReadShortcutView(data: data as! NavigationReadShortcutType)
case is NavigationReadCurationType:
ReadCurationView(data: data as! NavigationReadCurationType)
ReadCurationView(viewModel: ExploreCurationViewModel(data: data as! NavigationReadCurationType))
case is CurationType:
ListCurationView(curationType: data as! CurationType)
case is NavigationProfile:
Expand Down Expand Up @@ -119,7 +119,7 @@ struct NavigationViewModifier: ViewModifier {
ShowProfileView(data: data)
}
.navigationDestination(for: NavigationReadCurationType.self) { data in
ReadCurationView(data: data)
ReadCurationView(viewModel: ExploreCurationViewModel(data: data))
}
.navigationDestination(for: CurationType.self) { data in
ListCurationView(curationType: data)
Expand Down
22 changes: 22 additions & 0 deletions HappyAnding/HappyAnding/Model/Curation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ struct Curation: Identifiable, Equatable, Codable, Hashable {
let data = (try? JSONEncoder().encode(self)) ?? Data()
return (try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any]) ?? [:]
}

init(title: String, subtitle: String, isAdmin: Bool, background: String, author: String, shortcuts: [ShortcutCellModel]) {
self.id = UUID().uuidString
self.title = title
self.subtitle = subtitle
self.dateTime = Date().getDate()
self.isAdmin = isAdmin
self.background = background
self.author = author
self.shortcuts = shortcuts
}

init() {
self.id = UUID().uuidString
self.title = ""
self.subtitle = ""
self.dateTime = Date().getDate()
self.isAdmin = false
self.background = ""
self.author = ""
self.shortcuts = []
}
}

struct ShortcutCellModel: Identifiable, Codable, Equatable, Hashable {
Expand Down
14 changes: 14 additions & 0 deletions HappyAnding/HappyAnding/Model/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ struct User: Identifiable, Codable, Hashable {
let data = (try? JSONEncoder().encode(self)) ?? Data()
return (try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any]) ?? [:]
}

init() {
self.id = ""
self.nickname = ""
self.likedShortcuts = []
self.downloadedShortcuts = []
}

init(id: String, nickname: String, likedShortcuts: [String], downloadedShortcuts: [DownloadedShortcut]) {
self.id = id
self.nickname = nickname
self.likedShortcuts = likedShortcuts
self.downloadedShortcuts = downloadedShortcuts
}
}

struct DownloadedShortcut: Identifiable, Codable, Hashable {
Expand Down
56 changes: 56 additions & 0 deletions HappyAnding/HappyAnding/ViewModel/ReadCurationViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// ReadCurationViewModel.swift
// HappyAnding
//
// Created by 이지원 on 2023/06/17.
//

import SwiftUI


final class ExploreCurationViewModel: ObservableObject {
JIWON1923 marked this conversation as resolved.
Show resolved Hide resolved
var shortcutsZipViewModel = ShortcutsZipViewModel.share

@Published var isWriting = false
@Published var isTappedDeleteButton = false
@Published var curation: Curation
@Published var shortcuts = [ShortcutCellModel]()
@Published var authInformation: User
@Published var gradeImage = Image(systemName: "person.crop.circle.fill")
@Published private(set) var isAdmin = false
JIWON1923 marked this conversation as resolved.
Show resolved Hide resolved

init(data: NavigationReadCurationType) {
self.curation = data.curation
self.authInformation = User()
fetchUserGrade()
}

private func fetchUserGrade() {
shortcutsZipViewModel.fetchUser(userID: curation.author,
isCurrentUser: false) { user in
self.authInformation = user
let grade = self.shortcutsZipViewModel.checkShortcutGrade(userID: self.authInformation.id)
let image = self.shortcutsZipViewModel.fetchShortcutGradeImage(isBig: false, shortcutGrade: grade)
self.gradeImage = image
}
}

func checkAuthor() -> Bool {
return curation.author == shortcutsZipViewModel.currentUser()
}

func deleteCuration() {
shortcutsZipViewModel.deleteData(model: curation)
shortcutsZipViewModel.curationsMadeByUser = shortcutsZipViewModel.curationsMadeByUser.filter { $0.id != curation.id }
}

func shareCuration() {
guard let deepLink = URL(string: "ShortcutsZip://myPage/CurationDetailView?curationID=\(curation.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)
}
}

16 changes: 5 additions & 11 deletions HappyAnding/HappyAnding/Views/Components/UserCurationCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ struct UserCurationCell: View {
@EnvironmentObject var shortcutsZipViewModel: ShortcutsZipViewModel

@State var curation: Curation
@State var index = 0

var lineLimit: Int?
let navigationParentView: NavigationParentView
Expand All @@ -25,7 +24,7 @@ struct UserCurationCell: View {

//MARK: - 단축어 아이콘 배열
HStack {
ForEach(shortcutsZipViewModel.userCurations[index].shortcuts.prefix(4), id: \.self) { shortcut in
ForEach(curation.shortcuts.prefix(4), id: \.self) { shortcut in
ZStack {
Rectangle()
.fill(Color.fetchGradient(color: shortcut.color))
Expand All @@ -38,7 +37,7 @@ struct UserCurationCell: View {
}

//단축어가 4개 이상인 경우에만 그리는 아이콘
if shortcutsZipViewModel.userCurations[index].shortcuts.count > 4 {
if curation.shortcuts.count > 4 {
ZStack(alignment: .center) {
Rectangle()
.fill(Color.gray2)
Expand All @@ -47,7 +46,7 @@ struct UserCurationCell: View {
HStack(spacing: 0) {
Image(systemName: "plus")
.smallIcon()
Text("\(shortcutsZipViewModel.userCurations[index].shortcuts.count-4)")
Text("\(curation.shortcuts.count-4)")
.shortcutsZipFootnote()
}
.foregroundColor(.gray5)
Expand All @@ -59,23 +58,18 @@ struct UserCurationCell: View {

//MARK: - curation title, subtitle

Text(shortcutsZipViewModel.userCurations[index].title)
Text(curation.title)
.shortcutsZipHeadline()
.foregroundColor(Color.gray5)
.frame(maxWidth: .infinity, alignment: .leading)
Text(shortcutsZipViewModel.userCurations[index].subtitle.lineBreaking)
Text(curation.subtitle.lineBreaking)
.shortcutsZipBody2()
.multilineTextAlignment(.leading)
.lineLimit(lineLimit)
.foregroundColor(Color.gray5)
.padding(.bottom, 20)
.fixedSize(horizontal: false, vertical: true)
}
.onAppear() {
if let index = shortcutsZipViewModel.userCurations.firstIndex(of: curation) {
self.index = index
}
}
.padding(.horizontal, 24)
.background(Color.backgroudList)
.overlay(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct UserCurationListView: View {

@State var isWriting = false
@State var data: CurationType
@State var curation = Curation()

var body: some View {
VStack(spacing: 0) {
Expand Down Expand Up @@ -78,9 +79,7 @@ struct UserCurationListView: View {

@ViewBuilder
private func writeCurationView() -> some View {
WriteCurationSetView(isWriting: $isWriting
, isEdit: false
)
WriteCurationSetView(isWriting: $isWriting, curation: $curation, isEdit: false)
.navigationDestination(for: WriteCurationInfoType.self) { data in
WriteCurationInfoView(data: data, isWriting: $isWriting)
}
Expand Down
Loading