diff --git a/HappyAnding/HappyAnding/ViewModel/ExploreCurationViewModels/ExploreCurationViewModel.swift b/HappyAnding/HappyAnding/ViewModel/ExploreCurationViewModels/ExploreCurationViewModel.swift new file mode 100644 index 00000000..36c2188f --- /dev/null +++ b/HappyAnding/HappyAnding/ViewModel/ExploreCurationViewModels/ExploreCurationViewModel.swift @@ -0,0 +1,36 @@ +// +// ExploreCurationViewModel.swift +// HappyAnding +// +// Created by 이지원 on 2023/06/25. +// + +import Foundation + +final class ExploreCurationViewModel: ObservableObject { + var shortcutsZipViewModel = ShortcutsZipViewModel.share + @Published var adminCurationList = [Curation]() + @Published var personalCurationList = [Curation]() + @Published var userCurationList = [Curation]() + + init() { + fetchAdminCurationList() + } + + private func fetchAdminCurationList() { + self.adminCurationList = shortcutsZipViewModel.adminCurations + } + + func getCurationList(with curationType: CurationType) -> [Curation] { + curationType.filterCuration(from: shortcutsZipViewModel) + } + + func getSectionTitle(with curationType: CurationType) -> String { + switch curationType { + case .personalCuration: + return (shortcutsZipViewModel.userInfo?.nickname ?? "") + curationType.title + default: + return curationType.title + } + } +} diff --git a/HappyAnding/HappyAnding/Views/ExploreCurationViews/ExploreCurationView.swift b/HappyAnding/HappyAnding/Views/ExploreCurationViews/ExploreCurationView.swift index 78014dd2..eeb34765 100644 --- a/HappyAnding/HappyAnding/Views/ExploreCurationViews/ExploreCurationView.swift +++ b/HappyAnding/HappyAnding/Views/ExploreCurationViews/ExploreCurationView.swift @@ -9,8 +9,7 @@ import SwiftUI struct ExploreCurationView: View { - @EnvironmentObject var shortcutsZipViewModel: ShortcutsZipViewModel - + @StateObject var viewModel: ExploreCurationViewModel @AppStorage("useWithoutSignIn") var useWithoutSignIn = false var body: some View { @@ -27,9 +26,6 @@ struct ExploreCurationView: View { } .padding(.top, 20) .padding(.bottom, 44) - .onChange(of: shortcutsZipViewModel.userCurations) { _ in - shortcutsZipViewModel.refreshPersonalCurations() - } } .navigationBarTitle(TextLiteral.exploreCurationViewTitle) .navigationBarTitleDisplayMode(.large) @@ -48,7 +44,7 @@ struct ExploreCurationView: View { ScrollView(.horizontal, showsIndicators: false) { HStack(spacing: 0) { - ForEach(shortcutsZipViewModel.adminCurations, id: \.id) { curation in + ForEach(viewModel.adminCurationList, id: \.id) { curation in AdminCurationCell(adminCuration: curation) .navigationLinkRouter(data: NavigationReadCurationType(isAdmin: true, curation: curation, @@ -64,19 +60,10 @@ struct ExploreCurationView: View { @ViewBuilder private func sectionView(with sectionType: CurationType) -> some View { - let curations = sectionType.filterCuration(from: shortcutsZipViewModel) - var sectionTitle: String { - if sectionType == .personalCuration { - return (shortcutsZipViewModel.userInfo?.nickname ?? "") + sectionType.title - } else { - return sectionType.title - } - } - VStack(spacing: 0) { HStack(alignment: .bottom) { - SubtitleTextView(text: sectionTitle) + SubtitleTextView(text: viewModel.getSectionTitle(with: sectionType)) Spacer() @@ -86,7 +73,7 @@ struct ExploreCurationView: View { .padding(.bottom, 12) .padding(.horizontal, 16) - ForEach(curations.prefix(2), id: \.self) { curation in + ForEach(viewModel.getCurationList(with:sectionType).prefix(2), id: \.self) { curation in let data = NavigationReadCurationType(curation: curation, navigationParentView: .curations) diff --git a/HappyAnding/HappyAnding/Views/TabView/ShortcutTabView.swift b/HappyAnding/HappyAnding/Views/TabView/ShortcutTabView.swift index f6750129..e0e4c94f 100644 --- a/HappyAnding/HappyAnding/Views/TabView/ShortcutTabView.swift +++ b/HappyAnding/HappyAnding/Views/TabView/ShortcutTabView.swift @@ -121,7 +121,7 @@ struct ShortcutTabView: View { @ViewBuilder private func secondTab() -> some View { - ExploreCurationView() + ExploreCurationView(viewModel: ExploreCurationViewModel()) .modifierNavigation() .navigationBarBackground ({ Color.shortcutsZipBackground }) .id(secondTabID)