diff --git a/VGApp/Tabs/ItemsList/ItemView.swift b/VGApp/Tabs/ItemsList/ItemView.swift index 0a14b07..2a72ef3 100644 --- a/VGApp/Tabs/ItemsList/ItemView.swift +++ b/VGApp/Tabs/ItemsList/ItemView.swift @@ -24,6 +24,9 @@ struct Itemview: View { List { ForEach(vm.items) { item in ItemCell(item: item) + .onTapGesture { + vm.showEditItemSheet(item) + } }.onDelete { indexSet in vm.removeItems(at: indexSet) } @@ -66,6 +69,8 @@ struct Itemview: View { newItemView() case .newRecycle: newRecycleView() + case .editItem: + editItemView() } } .alert(isPresented: $vm.showDeleteAlert) { @@ -131,6 +136,48 @@ struct Itemview: View { } } + func editItemView() -> some View { + NavigationView(content: { + VStack { + ZStack { + TextField("newItem_item_name", text: $vm.toBeEditedItem_Name) + .padding() + RoundedRectangle(cornerRadius: 8).fill(.clear) + .background(RoundedRectangle(cornerRadius: 8).stroke(.green, lineWidth: 1)) + .frame(height: 30) + } + + ZStack { + TextField("newItem_item_code", text: $vm.toBeEditedItem_Number) + .padding() + .keyboardType(.numberPad) + .onChange(of: vm.toBeEditedItem_Number) { _ in + if vm.toBeEditedItem_Number.count > 4 { + vm.toBeEditedItem_Number = String(vm.toBeEditedItem_Number.prefix(4)) + } + } + RoundedRectangle(cornerRadius: 8).fill(.clear) + .background(RoundedRectangle(cornerRadius: 8).stroke(.green, lineWidth: 1)) + .frame(height: 30) + } + Spacer().frame(height: 50) + Button { + vm.saveEditedItem() + } label: { + ZStack { + RoundedRectangle(cornerRadius: 8).fill(.green).frame(height: 40) + Text("editItem_add_button_title").foregroundColor(.white) + } + } + navigationStyling("editItem_title".localized) + + }.padding() + .onAppear { + vm.updateViews() + } + }) + } + func recycleSection() -> some View { return Section(header: Text("itemView_section_recycle")) { ForEach(vm.usedRecycleTypes, id: \.self) { type in diff --git a/VGApp/Tabs/ItemsList/ItemViewmodel.swift b/VGApp/Tabs/ItemsList/ItemViewmodel.swift index 31d9914..9dc5eb2 100644 --- a/VGApp/Tabs/ItemsList/ItemViewmodel.swift +++ b/VGApp/Tabs/ItemsList/ItemViewmodel.swift @@ -10,6 +10,7 @@ import SwiftUI enum SHEETTYPE { case newItem case newRecycle + case editItem } class ItemViewmodel: ObservableObject { @@ -30,6 +31,11 @@ class ItemViewmodel: ObservableObject { @Published var recycleDict: [RecycleTypes: Int] = [:] @Published var recycleCount: Int = 1 + // edit Item + @Published var toBeEditedItem: Item? + @Published var toBeEditedItem_Name: String = "" + @Published var toBeEditedItem_Number: String = "0000" + @Published var showDeleteAlert = false func updateViews() { @@ -94,6 +100,32 @@ class ItemViewmodel: ObservableObject { withAnimation { showsSheet = true } } + func showEditItemSheet(_ item: Item) { + toBeEditedItem = item + toBeEditedItem_Name = item.name + toBeEditedItem_Number = item.number + + selectedSheetType = .editItem + withAnimation { showsSheet = true } + } + + func saveEditedItem() { + guard let list = list else { return } + guard let toBeEditedItem = toBeEditedItem else { return } + + // remove old one + CoreData.removeItem(toBeEditedItem, list) + + // create new one + Util.createItem(name: toBeEditedItem_Name, code: toBeEditedItem_Number) + withAnimation { showsSheet = false } + toBeEditedItem_Name = "" + toBeEditedItem_Number = "" + + Util.save() + updateViews() + } + func addRecyle(_ type: RecycleTypes) { guard let list = list else { return } for _ in 1...recycleCount { diff --git a/VGApp/Util/Localizable.strings b/VGApp/Util/Localizable.strings index 110318e..f06e494 100644 --- a/VGApp/Util/Localizable.strings +++ b/VGApp/Util/Localizable.strings @@ -62,6 +62,9 @@ "newItem_item_code"="Nummer"; "newItem_add_button_title"="Item hinzufügen"; +"editItem_title"="Item bearbeiten"; +"editItem_add_button_title"="Änderung speichern"; + "newRecycle_title"="Leergut hinzufügen"; "newRecycle_recycle_type"="Leergut Typ"; "newRecycle_add_button_title"="Leergut hinzufügen";