From aee2cbcf0a626b4a4303aacbe6a71ac41bcd3426 Mon Sep 17 00:00:00 2001 From: Jeon Jimin Date: Sat, 8 Jul 2023 12:52:23 +0900 Subject: [PATCH] =?UTF-8?q?[Feat]=20#470=20-=20checkboxCell=20=ED=95=A9?= =?UTF-8?q?=EC=B9=98=EA=B8=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - viewBuilder 이용 - idx -> index로 수정 --- .../ViewModel/WriteCurationViewModel.swift | 14 +- .../CheckBoxShortcutCell.swift | 164 +++++++++--------- .../WriteCurationSetView.swift | 59 ++++++- 3 files changed, 147 insertions(+), 90 deletions(-) diff --git a/HappyAnding/HappyAnding/ViewModel/WriteCurationViewModel.swift b/HappyAnding/HappyAnding/ViewModel/WriteCurationViewModel.swift index ad1e25f1..01895694 100644 --- a/HappyAnding/HappyAnding/ViewModel/WriteCurationViewModel.swift +++ b/HappyAnding/HappyAnding/ViewModel/WriteCurationViewModel.swift @@ -74,19 +74,19 @@ final class WriteCurationViewModel: ObservableObject, Hashable { } } - func checkboxCellTapGesture(idx: Int) { - if isShortcutsTapped[idx] { - isShortcutsTapped[idx] = false + func checkboxCellTapGesture(index: Int) { + if isShortcutsTapped[index] { + isShortcutsTapped[index] = false // TODO: 현재는 name을 기준으로 검색중, id로 검색해서 삭제해야함 / Shortcuts 자체를 배열에 저장해야함 - if let index = curation.shortcuts.firstIndex(of: shortcutCells[idx]) { - curation.shortcuts.remove(at: index) + if let firstIndex = curation.shortcuts.firstIndex(of: shortcutCells[index]) { + curation.shortcuts.remove(at: firstIndex) } } else { if curation.shortcuts.count < 10 { - curation.shortcuts.append(shortcutCells[idx]) - isShortcutsTapped[idx] = true + curation.shortcuts.append(shortcutCells[index]) + isShortcutsTapped[index] = true } } } diff --git a/HappyAnding/HappyAnding/Views/WriteCurationViews/CheckBoxShortcutCell.swift b/HappyAnding/HappyAnding/Views/WriteCurationViews/CheckBoxShortcutCell.swift index b763d5d2..2f576e0a 100644 --- a/HappyAnding/HappyAnding/Views/WriteCurationViews/CheckBoxShortcutCell.swift +++ b/HappyAnding/HappyAnding/Views/WriteCurationViews/CheckBoxShortcutCell.swift @@ -1,85 +1,85 @@ +//// +//// CheckBoxShortcutCell.swift +//// HappyAnding +//// +//// Created by HanGyeongjun on 2022/10/26. +//// // -// CheckBoxShortcutCell.swift -// HappyAnding +//import SwiftUI // -// Created by HanGyeongjun on 2022/10/26. +//struct CheckBoxShortcutCell: View { // - -import SwiftUI - -struct CheckBoxShortcutCell: View { - - @StateObject var viewModel: WriteCurationViewModel - - let idx: Int - - var body: some View { - - ZStack { - Color.shortcutsZipBackground - - HStack { - toggle - icon - shortcutInfo - Spacer() - } - .padding(.vertical, 20) - .background( background ) - .padding(.horizontal, 16) - } - .onTapGesture { - viewModel.checkboxCellTapGesture(idx: idx) - } - .padding(.top, 0) - .background(Color.shortcutsZipBackground) - } - - var toggle: some View { - Image(systemName: viewModel.isShortcutsTapped[idx] ? "checkmark.square.fill" : "square") - .smallIcon() - .foregroundColor(viewModel.isShortcutsTapped[idx] ? .shortcutsZipPrimary : .gray3) - .padding(.leading, 20) - } - - var icon: some View { - - ZStack(alignment: .center) { - Rectangle() - .fill(Color.fetchGradient(color: viewModel.shortcutCells[idx].color)) - .cornerRadius(8) - .frame(width: 52, height: 52) - - Image(systemName: viewModel.shortcutCells[idx].sfSymbol) - .mediumShortcutIcon() - .foregroundColor(.white) - } - .padding(.leading, 12) - } - - var shortcutInfo: some View { - - VStack(alignment: .leading, spacing: 4) { - Text(viewModel.shortcutCells[idx].title) - .shortcutsZipHeadline() - .foregroundColor(.gray5) - .lineLimit(1) - Text(viewModel.shortcutCells[idx].subtitle) - .shortcutsZipFootnote() - .foregroundColor(.gray3) - .lineLimit(2) - } - .padding(.leading, 12) - .padding(.trailing, 20) - } - - var background: some View { - - RoundedRectangle(cornerRadius: 12) - .fill(viewModel.isShortcutsTapped[idx] ? Color.shortcutsZipWhite : Color.backgroudList) - .overlay( - RoundedRectangle(cornerRadius: 12) - .strokeBorder(viewModel.isShortcutsTapped[idx] ? Color.shortcutsZipPrimary : Color.backgroudListBorder) - ) - } -} +// @StateObject var viewModel: WriteCurationViewModel +// +// let idx: Int +// +// var body: some View { +// +// ZStack { +// Color.shortcutsZipBackground +// +// HStack { +// toggle +// icon +// shortcutInfo +// Spacer() +// } +// .padding(.vertical, 20) +// .background( background ) +// .padding(.horizontal, 16) +// } +// .onTapGesture { +// viewModel.checkboxCellTapGesture(idx: idx) +// } +// .padding(.top, 0) +// .background(Color.shortcutsZipBackground) +// } +// +// var toggle: some View { +// Image(systemName: viewModel.isShortcutsTapped[idx] ? "checkmark.square.fill" : "square") +// .smallIcon() +// .foregroundColor(viewModel.isShortcutsTapped[idx] ? .shortcutsZipPrimary : .gray3) +// .padding(.leading, 20) +// } +// +// var icon: some View { +// +// ZStack(alignment: .center) { +// Rectangle() +// .fill(Color.fetchGradient(color: viewModel.shortcutCells[idx].color)) +// .cornerRadius(8) +// .frame(width: 52, height: 52) +// +// Image(systemName: viewModel.shortcutCells[idx].sfSymbol) +// .mediumShortcutIcon() +// .foregroundColor(.white) +// } +// .padding(.leading, 12) +// } +// +// var shortcutInfo: some View { +// +// VStack(alignment: .leading, spacing: 4) { +// Text(viewModel.shortcutCells[idx].title) +// .shortcutsZipHeadline() +// .foregroundColor(.gray5) +// .lineLimit(1) +// Text(viewModel.shortcutCells[idx].subtitle) +// .shortcutsZipFootnote() +// .foregroundColor(.gray3) +// .lineLimit(2) +// } +// .padding(.leading, 12) +// .padding(.trailing, 20) +// } +// +// var background: some View { +// +// RoundedRectangle(cornerRadius: 12) +// .fill(viewModel.isShortcutsTapped[idx] ? Color.shortcutsZipWhite : Color.backgroudList) +// .overlay( +// RoundedRectangle(cornerRadius: 12) +// .strokeBorder(viewModel.isShortcutsTapped[idx] ? Color.shortcutsZipPrimary : Color.backgroudListBorder) +// ) +// } +//} diff --git a/HappyAnding/HappyAnding/Views/WriteCurationViews/WriteCurationSetView.swift b/HappyAnding/HappyAnding/Views/WriteCurationViews/WriteCurationSetView.swift index 738b694c..33e1c600 100644 --- a/HappyAnding/HappyAnding/Views/WriteCurationViews/WriteCurationSetView.swift +++ b/HappyAnding/HappyAnding/Views/WriteCurationViews/WriteCurationSetView.swift @@ -79,7 +79,7 @@ struct WriteCurationSetView: View { ScrollView { ForEach(Array(viewModel.shortcutCells.enumerated()), id: \.offset) { index, shortcut in - CheckBoxShortcutCell(viewModel: viewModel, idx: index) + checkBoxShortcutCell(viewModel: viewModel, index: index) } } .frame(maxWidth: .infinity) @@ -100,4 +100,61 @@ struct WriteCurationSetView: View { .padding(.horizontal, 16) .padding(.bottom, 20) } + + @ViewBuilder + private func checkBoxShortcutCell(viewModel: WriteCurationViewModel, index: Int) -> some View { + + ZStack { + Color.shortcutsZipBackground + + HStack { + Image(systemName: viewModel.isShortcutsTapped[index] ? "checkmark.square.fill" : "square") + .smallIcon() + .foregroundColor(viewModel.isShortcutsTapped[index] ? .shortcutsZipPrimary : .gray3) + .padding(.leading, 20) + + ZStack(alignment: .center) { + Rectangle() + .fill(Color.fetchGradient(color: viewModel.shortcutCells[index].color)) + .cornerRadius(8) + .frame(width: 52, height: 52) + + Image(systemName: viewModel.shortcutCells[index].sfSymbol) + .mediumShortcutIcon() + .foregroundColor(.white) + } + .padding(.leading, 12) + + VStack(alignment: .leading, spacing: 4) { + Text(viewModel.shortcutCells[index].title) + .shortcutsZipHeadline() + .foregroundColor(.gray5) + .lineLimit(1) + Text(viewModel.shortcutCells[index].subtitle) + .shortcutsZipFootnote() + .foregroundColor(.gray3) + .lineLimit(2) + } + .padding(.leading, 12) + .padding(.trailing, 20) + + Spacer() + } + .padding(.vertical, 20) + .background( + RoundedRectangle(cornerRadius: 12) + .fill(viewModel.isShortcutsTapped[index] ? Color.shortcutsZipWhite : Color.backgroudList) + .overlay( + RoundedRectangle(cornerRadius: 12) + .strokeBorder(viewModel.isShortcutsTapped[index] ? Color.shortcutsZipPrimary : Color.backgroudListBorder) + ) + ) + .padding(.horizontal, 16) + } + .onTapGesture { + viewModel.checkboxCellTapGesture(index: index) + } + .padding(.top, 0) + .background(Color.shortcutsZipBackground) + } }