Skip to content

Commit

Permalink
[Feat] #472 - ListCurationViewModel 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
JIWON1923 committed Jun 25, 2023
1 parent 94e3b87 commit 400f320
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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("단축어") ? "" : "") 없어요"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -41,7 +30,7 @@ struct ListCurationView: View {
LazyVStack(spacing: 0) {
Spacer()
.frame(height: 20)
makeCurationCellList(curations)
makeCurationCellList(viewModel.curationList)

Spacer()
.frame(height: 32)
Expand All @@ -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)

Expand Down

0 comments on commit 400f320

Please sign in to comment.