diff --git a/HappyAnding/HappyAnding/ViewModel/WriteCurationViewModel.swift b/HappyAnding/HappyAnding/ViewModel/WriteCurationViewModel.swift index 7c08911b..ad1e25f1 100644 --- a/HappyAnding/HappyAnding/ViewModel/WriteCurationViewModel.swift +++ b/HappyAnding/HappyAnding/ViewModel/WriteCurationViewModel.swift @@ -24,6 +24,9 @@ final class WriteCurationViewModel: ObservableObject, Hashable { //좋아요 + 내가 작성한 단축어 목록 @Published var shortcutCells = [ShortcutCellModel]() + //CheckboxShortcutCell 선택 여부 저장 + @Published var isShortcutsTapped: [Bool] = [] + //모음집 편집 시 전달받는 기존 모음집 정보 @Published var curation = Curation(title: "", subtitle: "", isAdmin: false, background: "", author: "", shortcuts: [ShortcutCellModel]()) @@ -41,9 +44,7 @@ final class WriteCurationViewModel: ObservableObject, Hashable { !(isValidTitle && isValidDescription) } - init() { - - } + init() { } init(data: Curation) { self.curation = data @@ -54,6 +55,14 @@ final class WriteCurationViewModel: ObservableObject, Hashable { if isEdit { deletedShortcutCells = curation.shortcuts } + + //isShortcutsTapped 초기화 + isShortcutsTapped = [Bool](repeating: false, count: shortcutCells.count) + for shortcut in curation.shortcuts { + if let index = shortcutCells.firstIndex(of: shortcut) { + isShortcutsTapped[index] = true + } + } } func addCuration() { @@ -64,4 +73,21 @@ final class WriteCurationViewModel: ObservableObject, Hashable { writeCurationNavigation.navigationPath = .init() } } + + func checkboxCellTapGesture(idx: Int) { + if isShortcutsTapped[idx] { + isShortcutsTapped[idx] = false + // TODO: 현재는 name을 기준으로 검색중, id로 검색해서 삭제해야함 / Shortcuts 자체를 배열에 저장해야함 + + if let index = curation.shortcuts.firstIndex(of: shortcutCells[idx]) { + curation.shortcuts.remove(at: index) + } + } + else { + if curation.shortcuts.count < 10 { + curation.shortcuts.append(shortcutCells[idx]) + isShortcutsTapped[idx] = true + } + } + } } diff --git a/HappyAnding/HappyAnding/Views/WriteCurationViews/CheckBoxShortcutCell.swift b/HappyAnding/HappyAnding/Views/WriteCurationViews/CheckBoxShortcutCell.swift index 5a2dedf8..b763d5d2 100644 --- a/HappyAnding/HappyAnding/Views/WriteCurationViews/CheckBoxShortcutCell.swift +++ b/HappyAnding/HappyAnding/Views/WriteCurationViews/CheckBoxShortcutCell.swift @@ -9,11 +9,9 @@ import SwiftUI struct CheckBoxShortcutCell: View { - @Binding var selectedShortcutCells: [ShortcutCellModel] - - @State var isShortcutTapped: Bool = false - - let shortcutCell: ShortcutCellModel + @StateObject var viewModel: WriteCurationViewModel + + let idx: Int var body: some View { @@ -31,30 +29,16 @@ struct CheckBoxShortcutCell: View { .padding(.horizontal, 16) } .onTapGesture { - if isShortcutTapped { - isShortcutTapped = false - - // TODO: 현재는 name을 기준으로 검색중, id로 검색해서 삭제해야함 / Shortcuts 자체를 배열에 저장해야함 - - if let index = selectedShortcutCells.firstIndex(of: shortcutCell) { - selectedShortcutCells.remove(at: index) - } - } - else { - if selectedShortcutCells.count < 10 { - isShortcutTapped = true - selectedShortcutCells.append(shortcutCell) - } - } + viewModel.checkboxCellTapGesture(idx: idx) } .padding(.top, 0) .background(Color.shortcutsZipBackground) } var toggle: some View { - Image(systemName: isShortcutTapped ? "checkmark.square.fill" : "square") + Image(systemName: viewModel.isShortcutsTapped[idx] ? "checkmark.square.fill" : "square") .smallIcon() - .foregroundColor(isShortcutTapped ? .shortcutsZipPrimary : .gray3) + .foregroundColor(viewModel.isShortcutsTapped[idx] ? .shortcutsZipPrimary : .gray3) .padding(.leading, 20) } @@ -62,11 +46,11 @@ struct CheckBoxShortcutCell: View { ZStack(alignment: .center) { Rectangle() - .fill(Color.fetchGradient(color: shortcutCell.color)) + .fill(Color.fetchGradient(color: viewModel.shortcutCells[idx].color)) .cornerRadius(8) .frame(width: 52, height: 52) - Image(systemName: shortcutCell.sfSymbol) + Image(systemName: viewModel.shortcutCells[idx].sfSymbol) .mediumShortcutIcon() .foregroundColor(.white) } @@ -76,11 +60,11 @@ struct CheckBoxShortcutCell: View { var shortcutInfo: some View { VStack(alignment: .leading, spacing: 4) { - Text(shortcutCell.title) + Text(viewModel.shortcutCells[idx].title) .shortcutsZipHeadline() .foregroundColor(.gray5) .lineLimit(1) - Text(shortcutCell.subtitle) + Text(viewModel.shortcutCells[idx].subtitle) .shortcutsZipFootnote() .foregroundColor(.gray3) .lineLimit(2) @@ -92,15 +76,10 @@ struct CheckBoxShortcutCell: View { var background: some View { RoundedRectangle(cornerRadius: 12) - .fill(isShortcutTapped ? Color.shortcutsZipWhite : Color.backgroudList) + .fill(viewModel.isShortcutsTapped[idx] ? Color.shortcutsZipWhite : Color.backgroudList) .overlay( RoundedRectangle(cornerRadius: 12) - .strokeBorder(isShortcutTapped ? Color.shortcutsZipPrimary : Color.backgroudListBorder) + .strokeBorder(viewModel.isShortcutsTapped[idx] ? Color.shortcutsZipPrimary : Color.backgroudListBorder) ) - }} - -//struct CheckBoxShortcutCell_Previews: PreviewProvider { -// static var previews: some View { -// CheckBoxShortcutCell(color: "Blue", sfSymbol: "books.vertical.fill", name: "ShortcutsTitle", description: "DescriptionDescription") -// } -//} + } +} diff --git a/HappyAnding/HappyAnding/Views/WriteCurationViews/WriteCurationSetView.swift b/HappyAnding/HappyAnding/Views/WriteCurationViews/WriteCurationSetView.swift index 4c91c2a7..c508271b 100644 --- a/HappyAnding/HappyAnding/Views/WriteCurationViews/WriteCurationSetView.swift +++ b/HappyAnding/HappyAnding/Views/WriteCurationViews/WriteCurationSetView.swift @@ -77,11 +77,8 @@ struct WriteCurationSetView: View { var shortcutList: some View { ScrollView { - ForEach(Array(viewModel.shortcutCells)) { shortcut in - CheckBoxShortcutCell( - selectedShortcutCells: $viewModel.curation.shortcuts, - isShortcutTapped: $viewModel.curation.shortcuts.contains{$0.id == shortcut.id}, - shortcutCell: shortcut) + ForEach(Array(viewModel.shortcutCells.enumerated()), id: \.offset) { index, shortcut in + CheckBoxShortcutCell(viewModel: viewModel, idx: index) } } .frame(maxWidth: .infinity)