Skip to content

Commit b8bf32d

Browse files
committed
[Chore] 옵션에 따른 가격 설정 과정에서 Picker 에러 해결중 #4
1 parent 33035d6 commit b8bf32d

File tree

4 files changed

+130
-75
lines changed

4 files changed

+130
-75
lines changed

big-project-a-customer-ios/big-project-a-customer-ios/ContentView.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
import SwiftUI
99

1010
struct ContentView: View {
11+
@State private var options: [String: [String]] = [
12+
"사이즈": ["S_0", "M_0", "L_0"],
13+
"컬러": ["레드_2000", "블루_3000", "블랙_1000"]
14+
]
1115
var body: some View {
1216
TabView {
13-
ProductDetailModalView().tabItem {
17+
ProductDetailModalView(options: $options).tabItem {
1418
Image(systemName: "house")
1519
Text("")
1620
}.tag(1)

big-project-a-customer-ios/big-project-a-customer-ios/FolderProductDetail/ProductDetailModalView.swift

Lines changed: 94 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,104 @@
88
import SwiftUI
99

1010
struct ProductDetailModalView: View {
11-
@ObservedObject var tempVM: TempViewModel = TempViewModel()
12-
13-
// @Binding var options: [String: [String]]
14-
@State var selectedColor = ""
11+
// @ObservedObject var tempVM: TempViewModel = TempViewModel()
12+
13+
@Binding var options: [String: [String]]
1514
@State var count: Int = 1
1615
@State private var selection = ""
1716
// FIXME: - 유저가 중복선택할 경우를 생각해서 Set을 적용할지 생각해보기.
18-
@State private var selectedOptions: [String] = []
19-
20-
var colors = ["red", "green", "blue"]
21-
var price: Int = 50000
22-
17+
18+
@State private var selectedOptions: [(optionName: String, option: String, optionPrice: Int)] = []
19+
20+
// 기본 가격(옵션 제외)
21+
var basePrice: Int = 50000
22+
// 옵션 추가 금액
23+
var optionPrice: Int = 0
24+
2325
var optionsArray: [String] {
24-
Array(tempVM.options.keys).sorted()
26+
Array(options.keys).sorted()
2527
}
26-
28+
2729
var body: some View {
28-
30+
2931
VStack {
30-
// 옵션 마다 picker를 제공
31-
ForEach(optionsArray, id:\.self) { key in
32-
// FIXME: - Picker Error
33-
Picker("", selection: $selection) {
34-
ForEach(tempVM.options[key]!, id: \.0) {
35-
Text("\($0) \($1)")
32+
// 옵션 마다 picker를 만들어준다.
33+
ForEach(Array(optionsArray.enumerated()), id: \.offset) { (index, key) in
34+
// key: 옵션명(컬러, 사이즈 등)
35+
HStack {
36+
Text(key)
37+
38+
Spacer()
39+
40+
// FIXME: - Picker Error
41+
Picker(key, selection: $selection) {
42+
Text("선택없음").tag(Optional<String>(nil))
43+
ForEach(options[key]!, id: \.self) { item in
44+
let value = item.split(separator: "_").map { String($0) }
45+
Text("\(value[0]) +\(value[1])").tag(Optional(item))
46+
}
3647
}
48+
// .onChange(of: selection, perform: { value in
49+
// if !value.isEmpty {
50+
// let newValue = value.split(separator: "_").map { String($0) }
51+
// let newTuple = (String(key), String(newValue[0]), Int(newValue[1])!)
52+
// selectedOptions.append(newTuple)
53+
// } else {
54+
// print(value)
55+
// }
56+
// })
57+
.onReceive([self.selection].publisher.first()) { (value) in
58+
// 선택 옵션을 selectedOptions 딕셔너리에 업데이트
59+
if !value.isEmpty {
60+
let newValue = value.split(separator: "_").map { String($0) }
61+
let newTuple = (String(key), String(newValue[0]), Int(newValue[1])!)
62+
}
63+
64+
}
65+
.pickerStyle(.menu)
66+
.background(.white)
67+
.cornerRadius(15)
3768
}
38-
.pickerStyle(.menu)
39-
.background(.white)
40-
.cornerRadius(15)
41-
.padding()
42-
}
43-
.onReceive([self.selection].publisher.first()) { (value) in
44-
selectedOptions.append(value)
69+
.padding()
70+
4571
}
46-
72+
4773
HStack {
4874
Text("수량")
49-
75+
5076
Spacer()
51-
77+
5278
CustomStepper(value: $count, textColor: .black)
5379
}
54-
.padding()
55-
80+
.padding()
81+
82+
HStack {
83+
Text("옵션")
84+
Spacer()
85+
}
86+
.padding()
87+
88+
VStack {
89+
90+
ForEach(selectedOptions, id: \.0) { tuple in
91+
HStack {
92+
Spacer()
93+
Text("\(tuple.1)(+\(tuple.2)원)")
94+
}
95+
}
96+
}
97+
.padding()
98+
5699
HStack {
57100
Text("가격")
58-
101+
59102
Spacer()
60-
61-
Text("\(count * price)")
103+
104+
Text("\(count * (basePrice + optionPrice))")
62105
}
63-
.padding()
64-
65-
106+
.padding()
107+
108+
66109
}
67110
}
68111
}
@@ -71,7 +114,7 @@ struct CustomStepper: View {
71114
@Binding var value: Int
72115
var textColor: Color
73116
var step = 1
74-
117+
75118
var body: some View {
76119
HStack {
77120
Button(action: {
@@ -80,24 +123,24 @@ struct CustomStepper: View {
80123
self.feedback()
81124
}
82125
}, label: {
83-
Image(systemName: "minus.square")
84-
.foregroundColor(value == 1 ? .gray : .black)
85-
})
86-
126+
Image(systemName: "minus.square")
127+
.foregroundColor(value == 1 ? .gray : .black)
128+
})
129+
87130
Text("\(value)").font(.system(.caption, design: .rounded))
88131
.foregroundColor(textColor)
89-
132+
90133
Button(action: {
91134
self.value += self.step
92135
self.feedback()
93-
136+
94137
}, label: {
95-
Image(systemName: "plus.square")
96-
.foregroundColor(.black)
97-
})
138+
Image(systemName: "plus.square")
139+
.foregroundColor(.black)
140+
})
98141
}
99142
}
100-
143+
101144
func feedback() {
102145
let generator = UIImpactFeedbackGenerator(style: .light)
103146
generator.impactOccurred()
@@ -106,6 +149,9 @@ struct CustomStepper: View {
106149

107150
struct ProductDetailModalView_Previews: PreviewProvider {
108151
static var previews: some View {
109-
ProductDetailModalView()
152+
ProductDetailModalView(options: .constant([
153+
"사이즈": ["S", "M", "L"],
154+
"컬러": ["레드", "블루", "블랙"]
155+
]))
110156
}
111157
}

big-project-a-customer-ios/big-project-a-customer-ios/FolderProductDetail/ProductDetailView.swift

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ import SwiftUI
99

1010
struct ProductDetailView: View {
1111

12-
@State private var imageHeight: CGFloat = 0
12+
// @State private var imageHeight: CGFloat = 0
1313
@State private var options: [String: [String]] = [
1414
"사이즈": ["S", "M", "L"],
1515
"컬러": ["레드", "블루", "블랙"]
1616
]
1717

18-
1918
var body: some View {
2019
// FIXME: - 외부에서 NavigationStack으로 감싸주기
2120
NavigationView {
@@ -55,23 +54,28 @@ struct ProductDetailView: View {
5554
}
5655
.padding(.leading, 15)
5756

58-
Image("productInformation")
59-
.resizable()
60-
.scaledToFit()
61-
62-
Button {
63-
// 버튼을 눌렀을때 image의 height를 최대로 늘려준다.
64-
imageHeight = CGFloat.infinity
65-
} label: {
66-
HStack {
67-
Spacer()
68-
Text("상품정보 더보기")
69-
Spacer()
70-
}
71-
}
57+
// Image("productInformation")
58+
// .resizable()
59+
// .scaledToFit()
60+
61+
// Button {
62+
// // 버튼을 눌렀을때 image의 height를 최대로 늘려준다.
63+
// imageHeight = CGFloat.infinity
64+
// } label: {
65+
// HStack {
66+
// Spacer()
67+
// Text("상품정보 더보기")
68+
// Spacer()
69+
// }
70+
// }
7271

7372
Image("productDetail")
74-
.productDetailImageModifier(height: imageHeight)
73+
.resizable()
74+
.aspectRatio(contentMode: .fit)
75+
// .frame(height: height)
76+
// .productDetailImageModifier(height: imageHeight)
77+
// TODO: - 문의 Section
78+
// TODO: - 리뷰 Section
7579
}
7680
}//scroll vstack
7781

@@ -100,15 +104,15 @@ struct ProductDetailView: View {
100104
.tint(.white)
101105
}
102106
}
103-
107+
108+
// TODO: - navibar (safe area)
104109
.toolbar {
105-
ToolbarItem(placement: .navigationBarTrailing) {
106-
Button(action: { }) {
107-
Image(systemName: "magnifyingglass")
108-
}
109-
.foregroundColor(.black)
110-
}
111-
110+
// ToolbarItem(placement: .navigationBarTrailing) {
111+
// Button(action: { }) {
112+
// Image(systemName: "magnifyingglass")
113+
// }
114+
// .foregroundColor(.black)
115+
// }
112116
}
113117
}
114118
// navigationView

big-project-a-customer-ios/big-project-a-customer-ios/FolderProductDetail/TempViewModel.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TempViewModel: ObservableObject {
1111
@Published var options: [String: [(String, Int)]] = [:]
1212

1313
init() {
14-
self.fetchPostDetail()
14+
// self.fetchPostDetail()
1515
}
1616

1717
func fetchPostDetail() {
@@ -34,5 +34,6 @@ class TempViewModel: ObservableObject {
3434
options[key]!.append((newValue[0], Int(newValue[1])!))
3535
}
3636
}
37+
print(options)
3738
}
3839
}

0 commit comments

Comments
 (0)