Skip to content

Commit

Permalink
[Feat] #470 - checkboxCell 합치기.
Browse files Browse the repository at this point in the history
- viewBuilder 이용
- idx -> index로 수정
  • Loading branch information
JMM00 committed Jul 8, 2023
1 parent d636f8d commit aee2cbc
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 90 deletions.
14 changes: 7 additions & 7 deletions HappyAnding/HappyAnding/ViewModel/WriteCurationViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
// )
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}

0 comments on commit aee2cbc

Please sign in to comment.