🚧 Add files
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Bucket
|
||||||
|
uuid = "21F11B35-51F5-45D2-B767-D9510D0FF6A9"
|
||||||
|
type = "1"
|
||||||
|
version = "2.0">
|
||||||
|
</Bucket>
|
||||||
@@ -15,6 +15,10 @@ import UIKit
|
|||||||
self.colorData = try? NSKeyedArchiver.archivedData(withRootObject: color, requiringSecureCoding: false)
|
self.colorData = try? NSKeyedArchiver.archivedData(withRootObject: color, requiringSecureCoding: false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setColor(c: Color) {
|
||||||
|
colorData = try? NSKeyedArchiver.archivedData(withRootObject: c, requiringSecureCoding: false)
|
||||||
|
}
|
||||||
|
|
||||||
func getColor() -> Color? {
|
func getColor() -> Color? {
|
||||||
guard let colorData = self.colorData else { return nil }
|
guard let colorData = self.colorData else { return nil }
|
||||||
do {
|
do {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import SwiftData
|
|||||||
struct AddSubscriptionView: View {
|
struct AddSubscriptionView: View {
|
||||||
@Environment(\.presentationMode) var presentationMode
|
@Environment(\.presentationMode) var presentationMode
|
||||||
@Environment(\.modelContext) private var modelContext
|
@Environment(\.modelContext) private var modelContext
|
||||||
@Query var subs: [Subscription]
|
@Binding var subscription: Subscription?
|
||||||
|
|
||||||
@State private var name: String = ""
|
@State private var name: String = ""
|
||||||
@State private var payments: [Payment] = [Payment(amount: 0, intervall: .monthly, startDate: Date())]
|
@State private var payments: [Payment] = [Payment(amount: 0, intervall: .monthly, startDate: Date())]
|
||||||
@@ -49,14 +49,27 @@ struct AddSubscriptionView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Section {
|
Section {
|
||||||
Button("Add Subscription") {
|
Button(subscription == nil ? "Add Subscription" : "Save Subscription") {
|
||||||
let newSubscription = Subscription(name: name, payments: payments, color: color)
|
if let existingSubscription = subscription {
|
||||||
modelContext.insert(newSubscription)
|
existingSubscription.name = name
|
||||||
|
existingSubscription.payments = payments
|
||||||
|
existingSubscription.setColor(c: color)
|
||||||
|
} else {
|
||||||
|
let newSubscription = Subscription(name: name, payments: payments, color: color)
|
||||||
|
modelContext.insert(newSubscription)
|
||||||
|
}
|
||||||
presentationMode.wrappedValue.dismiss()
|
presentationMode.wrappedValue.dismiss()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.navigationTitle("Add Subscription")
|
.navigationTitle(subscription == nil ? "Add Subscription" : "Edit Subscription")
|
||||||
|
.onAppear {
|
||||||
|
if let existingSubscription = subscription {
|
||||||
|
name = existingSubscription.name
|
||||||
|
payments = existingSubscription.payments
|
||||||
|
color = existingSubscription.getColor() ?? .blue
|
||||||
|
}
|
||||||
|
}
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
.navigationBarItems(trailing: Button("Cancel") {
|
.navigationBarItems(trailing: Button("Cancel") {
|
||||||
presentationMode.wrappedValue.dismiss()
|
presentationMode.wrappedValue.dismiss()
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ struct HomeView: View {
|
|||||||
@Environment(\.modelContext) private var modelContext
|
@Environment(\.modelContext) private var modelContext
|
||||||
@Query var subs: [Subscription]
|
@Query var subs: [Subscription]
|
||||||
@State private var showAddSubscriptionSheet = false
|
@State private var showAddSubscriptionSheet = false
|
||||||
|
@State private var selectedSubscription: Subscription?
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationView {
|
NavigationView {
|
||||||
VStack {
|
VStack {
|
||||||
@@ -29,6 +30,10 @@ struct HomeView: View {
|
|||||||
}
|
}
|
||||||
.listRowBackground(sub.getColorOrAccent().opacity(0.2))
|
.listRowBackground(sub.getColorOrAccent().opacity(0.2))
|
||||||
.cornerRadius(8)
|
.cornerRadius(8)
|
||||||
|
.onTapGesture {
|
||||||
|
selectedSubscription = sub
|
||||||
|
showAddSubscriptionSheet = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.onDelete(perform: deleteSubscription)
|
.onDelete(perform: deleteSubscription)
|
||||||
|
|
||||||
@@ -56,6 +61,7 @@ struct HomeView: View {
|
|||||||
}
|
}
|
||||||
ToolbarItem(placement: .navigationBarTrailing) {
|
ToolbarItem(placement: .navigationBarTrailing) {
|
||||||
Button(action: {
|
Button(action: {
|
||||||
|
selectedSubscription = nil
|
||||||
showAddSubscriptionSheet.toggle()
|
showAddSubscriptionSheet.toggle()
|
||||||
}) {
|
}) {
|
||||||
Image(systemName: "plus")
|
Image(systemName: "plus")
|
||||||
@@ -64,6 +70,7 @@ struct HomeView: View {
|
|||||||
#elseif os(macOS)
|
#elseif os(macOS)
|
||||||
ToolbarItem(placement: .primaryAction) {
|
ToolbarItem(placement: .primaryAction) {
|
||||||
Button(action: {
|
Button(action: {
|
||||||
|
selectedSubscription = nil
|
||||||
showAddSubscriptionSheet.toggle()
|
showAddSubscriptionSheet.toggle()
|
||||||
}) {
|
}) {
|
||||||
Image(systemName: "plus")
|
Image(systemName: "plus")
|
||||||
@@ -72,7 +79,7 @@ struct HomeView: View {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
.sheet(isPresented: $showAddSubscriptionSheet) {
|
.sheet(isPresented: $showAddSubscriptionSheet) {
|
||||||
AddSubscriptionView()
|
AddSubscriptionView(subscription: $selectedSubscription)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user