diff --git a/HappyAnding/HappyAnding/ViewModel/ExploreCurationViewModels/ListCurationViewModel.swift b/HappyAnding/HappyAnding/ViewModel/ExploreCurationViewModels/ListCurationViewModel.swift new file mode 100644 index 00000000..99668538 --- /dev/null +++ b/HappyAnding/HappyAnding/ViewModel/ExploreCurationViewModels/ListCurationViewModel.swift @@ -0,0 +1,36 @@ +// +// ListCurationViewModel.swift +// HappyAnding +// +// Created by 이지원 on 2023/06/25. +// + +import Foundation + +final class ListCurationViewModel: ObservableObject { + var shortcutsZipViewModel = ShortcutsZipViewModel.share + + @Published var curationType: CurationType + @Published private(set) var curationList = [Curation]() + @Published private(set) var sectionTitle: String = "" + + init(data: CurationType) { + self.curationType = data + self.curationList = curationType.filterCuration(from: shortcutsZipViewModel) + self.sectionTitle = fetchSectionTitle() + print("new viewModel: ", curationType, curationList) + } + + private func fetchSectionTitle() -> String { + switch curationType { + case .personalCuration: + return (shortcutsZipViewModel.userInfo?.nickname ?? "") + curationType.title + default: + return curationType.title + } + } + + func getEmptyContentsWording() -> String { + "아직 \(sectionTitle)\(sectionTitle.contains("단축어") ? "가" : "이") 없어요" + } +} diff --git a/HappyAnding/HappyAnding/ViewModel/ShortcutsZipViewModel.swift b/HappyAnding/HappyAnding/ViewModel/ShortcutsZipViewModel.swift index 61208df4..6e3c3d64 100644 --- a/HappyAnding/HappyAnding/ViewModel/ShortcutsZipViewModel.swift +++ b/HappyAnding/HappyAnding/ViewModel/ShortcutsZipViewModel.swift @@ -32,7 +32,11 @@ class ShortcutsZipViewModel: ObservableObject { @Published var shortcutsInCategory: [[Shortcuts]] = [[Shortcuts]].init(repeating: [], count: Category.allCases.count) // Category에서 사용할 숏컷 배열 @Published var curationsMadeByUser: [Curation] = [] // 유저가 만든 큐레이션배열 - @Published var userCurations: [Curation] = [] + @Published var userCurations: [Curation] = [] { + didSet { + self.refreshPersonalCurations() + } + } @Published var personalCurations: [Curation] = [] // "땡땡님을 위한 모음집" 큐레이션배열 @Published var adminCurations: [Curation] = [] diff --git a/HappyAnding/HappyAnding/Views/ExploreCurationViews/ListCurationView.swift b/HappyAnding/HappyAnding/Views/ExploreCurationViews/ListCurationView.swift index 08cd740e..1caa61c7 100644 --- a/HappyAnding/HappyAnding/Views/ExploreCurationViews/ListCurationView.swift +++ b/HappyAnding/HappyAnding/Views/ExploreCurationViews/ListCurationView.swift @@ -15,23 +15,12 @@ import SwiftUI struct ListCurationView: View { - @EnvironmentObject var shortcutsZipViewModel: ShortcutsZipViewModel - - @State var curationType: CurationType - @State var curations = [Curation]() - - private var sectionTitle: String { - if curationType == .personalCuration { - return (shortcutsZipViewModel.userInfo?.nickname ?? "") + curationType.title - } else { - return curationType.title - } - } + @StateObject var viewModel: ListCurationViewModel var body: some View { VStack { - if curations.isEmpty { - Text("아직 \(sectionTitle)\(sectionTitle.contains("단축어") ? "가" : "이") 없어요") + if viewModel.curationList.isEmpty { + Text(viewModel.getEmptyContentsWording()) .shortcutsZipBody2() .foregroundColor(Color.gray4) .frame(maxWidth: .infinity, maxHeight: .infinity) @@ -41,7 +30,7 @@ struct ListCurationView: View { LazyVStack(spacing: 0) { Spacer() .frame(height: 20) - makeCurationCellList(curations) + makeCurationCellList(viewModel.curationList) Spacer() .frame(height: 32) @@ -55,16 +44,14 @@ struct ListCurationView: View { .background(Color.shortcutsZipBackground.ignoresSafeArea(.all, edges: .all)) .navigationBarBackground ({ Color.shortcutsZipBackground }) .navigationBarTitleDisplayMode(.inline) - .navigationTitle(sectionTitle) - .onAppear { - curations = curationType.filterCuration(from: shortcutsZipViewModel) - } + .navigationTitle(viewModel.sectionTitle) } @ViewBuilder private func makeCurationCellList(_ curations: [Curation]) -> some View { ForEach(Array(curations.enumerated()), id: \.offset) { index, curation in + // TODO: ReadcurationViewModels과 통합 let data = NavigationReadCurationType(curation: curation, navigationParentView: .curations)