Skip to content

Commit

Permalink
[Feat] #470 - checkbox shortcut 뷰모델 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
JMM00 committed Aug 13, 2023
1 parent bcb0803 commit b1ebefa
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 43 deletions.
32 changes: 29 additions & 3 deletions HappyAnding/HappyAnding/ViewModel/WriteCurationViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]())

Expand All @@ -41,9 +44,7 @@ final class WriteCurationViewModel: ObservableObject, Hashable {
!(isValidTitle && isValidDescription)
}

init() {

}
init() { }

init(data: Curation) {
self.curation = data
Expand All @@ -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() {
Expand All @@ -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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -31,42 +29,28 @@ 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)
}

var icon: some 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)
}
Expand All @@ -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)
Expand All @@ -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")
// }
//}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b1ebefa

Please sign in to comment.