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