Skip to content

Commit

Permalink
fix #5
Browse files Browse the repository at this point in the history
  • Loading branch information
DragonCat4012 committed Oct 8, 2024
1 parent 2348fd9 commit 8eb9e4d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
47 changes: 47 additions & 0 deletions VGApp/Tabs/ItemsList/ItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -66,6 +69,8 @@ struct Itemview: View {
newItemView()
case .newRecycle:
newRecycleView()
case .editItem:
editItemView()
}
}
.alert(isPresented: $vm.showDeleteAlert) {
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions VGApp/Tabs/ItemsList/ItemViewmodel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SwiftUI
enum SHEETTYPE {
case newItem
case newRecycle
case editItem
}

class ItemViewmodel: ObservableObject {
Expand All @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions VGApp/Util/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -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";

0 comments on commit 8eb9e4d

Please sign in to comment.