Skip to content

Commit

Permalink
[Chore] 옵션에 따른 가격 설정 과정에서 Picker 에러 해결중 #4
Browse files Browse the repository at this point in the history
  • Loading branch information
jekyun-park committed Dec 28, 2022
1 parent 33035d6 commit b8bf32d
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import SwiftUI

struct ContentView: View {
@State private var options: [String: [String]] = [
"사이즈": ["S_0", "M_0", "L_0"],
"컬러": ["레드_2000", "블루_3000", "블랙_1000"]
]
var body: some View {
TabView {
ProductDetailModalView().tabItem {
ProductDetailModalView(options: $options).tabItem {
Image(systemName: "house")
Text("")
}.tag(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,104 @@
import SwiftUI

struct ProductDetailModalView: View {
@ObservedObject var tempVM: TempViewModel = TempViewModel()

// @Binding var options: [String: [String]]
@State var selectedColor = ""
// @ObservedObject var tempVM: TempViewModel = TempViewModel()

@Binding var options: [String: [String]]
@State var count: Int = 1
@State private var selection = ""
// FIXME: - 유저가 중복선택할 경우를 생각해서 Set을 적용할지 생각해보기.
@State private var selectedOptions: [String] = []

var colors = ["red", "green", "blue"]
var price: Int = 50000


@State private var selectedOptions: [(optionName: String, option: String, optionPrice: Int)] = []

// 기본 가격(옵션 제외)
var basePrice: Int = 50000
// 옵션 추가 금액
var optionPrice: Int = 0

var optionsArray: [String] {
Array(tempVM.options.keys).sorted()
Array(options.keys).sorted()
}

var body: some View {

VStack {
// 옵션 마다 picker를 제공
ForEach(optionsArray, id:\.self) { key in
// FIXME: - Picker Error
Picker("", selection: $selection) {
ForEach(tempVM.options[key]!, id: \.0) {
Text("\($0) \($1)")
// 옵션 마다 picker를 만들어준다.
ForEach(Array(optionsArray.enumerated()), id: \.offset) { (index, key) in
// key: 옵션명(컬러, 사이즈 등)
HStack {
Text(key)

Spacer()

// FIXME: - Picker Error
Picker(key, selection: $selection) {
Text("선택없음").tag(Optional<String>(nil))
ForEach(options[key]!, id: \.self) { item in
let value = item.split(separator: "_").map { String($0) }
Text("\(value[0]) +\(value[1])").tag(Optional(item))
}
}
// .onChange(of: selection, perform: { value in
// if !value.isEmpty {
// let newValue = value.split(separator: "_").map { String($0) }
// let newTuple = (String(key), String(newValue[0]), Int(newValue[1])!)
// selectedOptions.append(newTuple)
// } else {
// print(value)
// }
// })
.onReceive([self.selection].publisher.first()) { (value) in
// 선택 옵션을 selectedOptions 딕셔너리에 업데이트
if !value.isEmpty {
let newValue = value.split(separator: "_").map { String($0) }
let newTuple = (String(key), String(newValue[0]), Int(newValue[1])!)
}

}
.pickerStyle(.menu)
.background(.white)
.cornerRadius(15)
}
.pickerStyle(.menu)
.background(.white)
.cornerRadius(15)
.padding()
}
.onReceive([self.selection].publisher.first()) { (value) in
selectedOptions.append(value)
.padding()

}

HStack {
Text("수량")

Spacer()

CustomStepper(value: $count, textColor: .black)
}
.padding()

.padding()

HStack {
Text("옵션")
Spacer()
}
.padding()

VStack {

ForEach(selectedOptions, id: \.0) { tuple in
HStack {
Spacer()
Text("\(tuple.1)(+\(tuple.2)원)")
}
}
}
.padding()

HStack {
Text("가격")

Spacer()
Text("\(count * price)")

Text("\(count * (basePrice + optionPrice))")
}
.padding()
.padding()


}
}
}
Expand All @@ -71,7 +114,7 @@ struct CustomStepper: View {
@Binding var value: Int
var textColor: Color
var step = 1

var body: some View {
HStack {
Button(action: {
Expand All @@ -80,24 +123,24 @@ struct CustomStepper: View {
self.feedback()
}
}, label: {
Image(systemName: "minus.square")
.foregroundColor(value == 1 ? .gray : .black)
})
Image(systemName: "minus.square")
.foregroundColor(value == 1 ? .gray : .black)
})

Text("\(value)").font(.system(.caption, design: .rounded))
.foregroundColor(textColor)

Button(action: {
self.value += self.step
self.feedback()

}, label: {
Image(systemName: "plus.square")
.foregroundColor(.black)
})
Image(systemName: "plus.square")
.foregroundColor(.black)
})
}
}

func feedback() {
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()
Expand All @@ -106,6 +149,9 @@ struct CustomStepper: View {

struct ProductDetailModalView_Previews: PreviewProvider {
static var previews: some View {
ProductDetailModalView()
ProductDetailModalView(options: .constant([
"사이즈": ["S", "M", "L"],
"컬러": ["레드", "블루", "블랙"]
]))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import SwiftUI

struct ProductDetailView: View {

@State private var imageHeight: CGFloat = 0
// @State private var imageHeight: CGFloat = 0
@State private var options: [String: [String]] = [
"사이즈": ["S", "M", "L"],
"컬러": ["레드", "블루", "블랙"]
]


var body: some View {
// FIXME: - 외부에서 NavigationStack으로 감싸주기
NavigationView {
Expand Down Expand Up @@ -55,23 +54,28 @@ struct ProductDetailView: View {
}
.padding(.leading, 15)

Image("productInformation")
.resizable()
.scaledToFit()

Button {
// 버튼을 눌렀을때 image의 height를 최대로 늘려준다.
imageHeight = CGFloat.infinity
} label: {
HStack {
Spacer()
Text("상품정보 더보기")
Spacer()
}
}
// Image("productInformation")
// .resizable()
// .scaledToFit()

// Button {
// // 버튼을 눌렀을때 image의 height를 최대로 늘려준다.
// imageHeight = CGFloat.infinity
// } label: {
// HStack {
// Spacer()
// Text("상품정보 더보기")
// Spacer()
// }
// }

Image("productDetail")
.productDetailImageModifier(height: imageHeight)
.resizable()
.aspectRatio(contentMode: .fit)
// .frame(height: height)
// .productDetailImageModifier(height: imageHeight)
// TODO: - 문의 Section
// TODO: - 리뷰 Section
}
}//scroll vstack

Expand Down Expand Up @@ -100,15 +104,15 @@ struct ProductDetailView: View {
.tint(.white)
}
}


// TODO: - navibar (safe area)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: { }) {
Image(systemName: "magnifyingglass")
}
.foregroundColor(.black)
}

// ToolbarItem(placement: .navigationBarTrailing) {
// Button(action: { }) {
// Image(systemName: "magnifyingglass")
// }
// .foregroundColor(.black)
// }
}
}
// navigationView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TempViewModel: ObservableObject {
@Published var options: [String: [(String, Int)]] = [:]

init() {
self.fetchPostDetail()
// self.fetchPostDetail()
}

func fetchPostDetail() {
Expand All @@ -34,5 +34,6 @@ class TempViewModel: ObservableObject {
options[key]!.append((newValue[0], Int(newValue[1])!))
}
}
print(options)
}
}

0 comments on commit b8bf32d

Please sign in to comment.