From ace4e9035984810011f936879b5768d7e76d367b Mon Sep 17 00:00:00 2001 From: Kiara <68660034+DragonCat4012@users.noreply.github.com> Date: Tue, 8 Oct 2024 10:17:38 +0200 Subject: [PATCH] Add multiple recycles --- VGApp.xcodeproj/project.pbxproj | 4 ++++ VGApp/Tabs/ItemsList/ItemView.swift | 20 +++++++++++++--- VGApp/Tabs/ItemsList/ItemViewmodel.swift | 9 +++++++- VGApp/Util/ButtonStyle.swift | 29 ++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 VGApp/Util/ButtonStyle.swift diff --git a/VGApp.xcodeproj/project.pbxproj b/VGApp.xcodeproj/project.pbxproj index 160296a..e73610f 100644 --- a/VGApp.xcodeproj/project.pbxproj +++ b/VGApp.xcodeproj/project.pbxproj @@ -36,6 +36,7 @@ F1B2733429B613DC00E841F4 /* ItemViewmodel.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1B2733329B613DC00E841F4 /* ItemViewmodel.swift */; }; F1B2733629B61D4400E841F4 /* IconManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1B2733529B61D4400E841F4 /* IconManager.swift */; }; F1B2733829B61EE700E841F4 /* ItemCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1B2733729B61EE700E841F4 /* ItemCell.swift */; }; + F1D675BF2CB51D76001B9FD6 /* ButtonStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1D675BE2CB51D76001B9FD6 /* ButtonStyle.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -82,6 +83,7 @@ F1B2733329B613DC00E841F4 /* ItemViewmodel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemViewmodel.swift; sourceTree = ""; }; F1B2733529B61D4400E841F4 /* IconManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IconManager.swift; sourceTree = ""; }; F1B2733729B61EE700E841F4 /* ItemCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemCell.swift; sourceTree = ""; }; + F1D675BE2CB51D76001B9FD6 /* ButtonStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonStyle.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -162,6 +164,7 @@ F1B2733529B61D4400E841F4 /* IconManager.swift */, F1AAD8B52A6183C7006F51FB /* StringExtension.swift */, F1AAD8B12A617E46006F51FB /* DateExtension.swift */, + F1D675BE2CB51D76001B9FD6 /* ButtonStyle.swift */, ); path = Util; sourceTree = ""; @@ -359,6 +362,7 @@ F1B2733829B61EE700E841F4 /* ItemCell.swift in Sources */, F12680752A477F6E007931B6 /* AppData+CoreDataProperties.swift in Sources */, DDEBB40E27B5BB9F004D80BB /* Util.swift in Sources */, + F1D675BF2CB51D76001B9FD6 /* ButtonStyle.swift in Sources */, F12680712A477F6E007931B6 /* Item+CoreDataProperties.swift in Sources */, F12680732A477F6E007931B6 /* RecycleItem+CoreDataProperties.swift in Sources */, F1678A192979BAE600F676A4 /* ListDetailView.swift in Sources */, diff --git a/VGApp/Tabs/ItemsList/ItemView.swift b/VGApp/Tabs/ItemsList/ItemView.swift index f41cb24..ab7a56a 100644 --- a/VGApp/Tabs/ItemsList/ItemView.swift +++ b/VGApp/Tabs/ItemsList/ItemView.swift @@ -161,15 +161,29 @@ struct Itemview: View { .padding(.bottom, 20) .padding(.top, 20) + HStack { + Button("-") { + if vm.recycleCount > 1 { + vm.recycleCount -= 1 + } + }.buttonStyle(RecycleButtonStyle()).disabled(vm.recycleCount <= 1) + Text(String(vm.recycleCount)).frame(minWidth: 60) + Button("+") { + vm.recycleCount += 1 + }.buttonStyle(RecycleButtonStyle()) + } + + Spacer() + ForEach(Array(vm.typeArr.enumerated()), id: \.offset) { _, type in Button { vm.addRecyle(type) - vm.setUsedRecyleTypesArr() } label: { - Text(Util.recTypeName(type)).frame(minWidth: 200, maxWidth: .infinity) - }.buttonStyle(.bordered) + Text(Util.recTypeName(type)).frame(maxWidth: .infinity) + }.buttonStyle(RecycleOptionButtonStyle()) } .padding(.bottom, 30).frame(maxWidth: .infinity) + Spacer() } .padding() } diff --git a/VGApp/Tabs/ItemsList/ItemViewmodel.swift b/VGApp/Tabs/ItemsList/ItemViewmodel.swift index 582b966..30e32e1 100644 --- a/VGApp/Tabs/ItemsList/ItemViewmodel.swift +++ b/VGApp/Tabs/ItemsList/ItemViewmodel.swift @@ -28,6 +28,7 @@ class ItemViewmodel: ObservableObject { @Published var typeArr: [RecycleTypes] = [] @Published var usedRecycleTypes: [RecycleTypes] = [] @Published var recycleDict: [RecycleTypes: Int] = [:] + @Published var recycleCount: Int = 1 @Published var showDeleteAlert = false @@ -94,9 +95,15 @@ class ItemViewmodel: ObservableObject { } func addRecyle(_ type: RecycleTypes) { - CoreData.addRecycle(list!, type: type) + guard let list = list else { return } + for _ in 1...recycleCount { + CoreData.addRecycle(list, type: type) + } + recycleCount = 1 Util.save() withAnimation { showsSheet = false } + // updateViews() + setUsedRecyleTypesArr() } func createItem() { diff --git a/VGApp/Util/ButtonStyle.swift b/VGApp/Util/ButtonStyle.swift new file mode 100644 index 0000000..928d7a5 --- /dev/null +++ b/VGApp/Util/ButtonStyle.swift @@ -0,0 +1,29 @@ +// +// ButtonStyle.swift +// VGApp +// +// Created by Kiara on 08.10.24. +// + +import SwiftUI + +struct RecycleButtonStyle: ButtonStyle { + func makeBody(configuration: Configuration) -> some View { + configuration.label + .padding() + .background(Color.accentColor) + .foregroundStyle(.black) + .clipShape(RoundedRectangle(cornerRadius: 8)) + .frame(width: 40, height: 40) + } +} + +struct RecycleOptionButtonStyle: ButtonStyle { + func makeBody(configuration: Configuration) -> some View { + configuration.label + .padding() + .background(Color.accentColor) + .foregroundStyle(.black) + .clipShape(RoundedRectangle(cornerRadius: 8)) + } +}