diff --git a/AboTracker.xcodeproj/project.pbxproj b/AboTracker.xcodeproj/project.pbxproj index 08cf052..e33c4b7 100644 --- a/AboTracker.xcodeproj/project.pbxproj +++ b/AboTracker.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ D40CCAEF2C2DC5D8007C4A9F /* Subscription.swift in Sources */ = {isa = PBXBuildFile; fileRef = D40CCAEE2C2DC5D8007C4A9F /* Subscription.swift */; }; D40CCAF32C2EE305007C4A9F /* AddSubscriptionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D40CCAF22C2EE304007C4A9F /* AddSubscriptionView.swift */; }; D426C55A2C2F0F150057455D /* PaymentCalendarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D426C5592C2F0F150057455D /* PaymentCalendarView.swift */; }; + D43587412C3450F300DD321B /* Helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D43587402C3450F300DD321B /* Helper.swift */; }; D4544FEF2C320AF30090E311 /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4544FEE2C320AF30090E311 /* HomeView.swift */; }; /* End PBXBuildFile section */ @@ -27,6 +28,7 @@ D40CCAEE2C2DC5D8007C4A9F /* Subscription.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Subscription.swift; sourceTree = ""; }; D40CCAF22C2EE304007C4A9F /* AddSubscriptionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddSubscriptionView.swift; sourceTree = ""; }; D426C5592C2F0F150057455D /* PaymentCalendarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaymentCalendarView.swift; sourceTree = ""; }; + D43587402C3450F300DD321B /* Helper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Helper.swift; sourceTree = ""; }; D4544FEE2C320AF30090E311 /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -66,6 +68,7 @@ D40CCAE32C2DC5AA007C4A9F /* Assets.xcassets */, D40CCAE52C2DC5AA007C4A9F /* AboTracker.entitlements */, D40CCAE62C2DC5AA007C4A9F /* Preview Content */, + D43587402C3450F300DD321B /* Helper.swift */, ); path = AboTracker; sourceTree = ""; @@ -165,6 +168,7 @@ D4544FEF2C320AF30090E311 /* HomeView.swift in Sources */, D426C55A2C2F0F150057455D /* PaymentCalendarView.swift in Sources */, D40CCAE02C2DC5A9007C4A9F /* AboTrackerApp.swift in Sources */, + D43587412C3450F300DD321B /* Helper.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/AboTracker/AboTrackerApp.swift b/AboTracker/AboTrackerApp.swift index 03d4a14..d549c82 100644 --- a/AboTracker/AboTrackerApp.swift +++ b/AboTracker/AboTrackerApp.swift @@ -13,5 +13,6 @@ struct AboTrackerApp: App { WindowGroup { ContentView() } + .modelContainer(for: [Subscription.self, Payment.self]) } } diff --git a/AboTracker/Helper.swift b/AboTracker/Helper.swift new file mode 100644 index 0000000..dee51b6 --- /dev/null +++ b/AboTracker/Helper.swift @@ -0,0 +1,17 @@ +import Foundation + +/*func getMockedSubs() -> [Subscription] { + return [ + Subscription(name: "Test", payments: [Payment(amount: 9.99, intervall: .monthly, startDate: getDate(from: "2023-01-01"))], color: .blue), + Subscription(name: "Fitness First", payments: [ + Payment(amount: 7.9, intervall: .weekly, startDate: getDate(from: "2023-01-23")), + Payment(amount: 29, intervall: .quarter, startDate: getDate(from: "2023-04-03")) + ], color: .red) + ] +} + +private func getDate(from dateString: String) -> Date { + let formatter = DateFormatter() + formatter.dateFormat = "yyyy-MM-dd" + return formatter.date(from: dateString) ?? Date() +}*/ diff --git a/AboTracker/Subscription.swift b/AboTracker/Subscription.swift index 266c442..d90ade1 100644 --- a/AboTracker/Subscription.swift +++ b/AboTracker/Subscription.swift @@ -1,16 +1,18 @@ import Foundation import SwiftUI +import SwiftData +import UIKit -final class Subscription: Identifiable { - public let id = UUID() +@Model final class Subscription: Identifiable { + @Attribute(.unique) public let id = UUID() var name: String - var payments: [Payment] - var color: Color + @Relationship(deleteRule: .cascade, minimumModelCount: 1) var payments: [Payment] + //var color: Color - init(name: String, payments: [Payment], color: Color) { + init(name: String, payments: [Payment]/*, color: Color*/) { self.name = name self.payments = payments - self.color = color + //self.color = color } func getMonthlyAmount() -> Float { @@ -94,8 +96,8 @@ final class Subscription: Identifiable { } } -final class Payment: Identifiable { - public let id = UUID() +@Model final class Payment: Identifiable { + @Attribute(.unique) public let id = UUID() var amount: Float var intervall: PaymentIntervall var startDate: Date @@ -107,7 +109,7 @@ final class Payment: Identifiable { } } -enum Currency: CustomStringConvertible { +enum Currency: Codable, CustomStringConvertible { case euro case dollar @@ -121,7 +123,7 @@ enum Currency: CustomStringConvertible { } } -enum PaymentIntervall: CustomStringConvertible { +enum PaymentIntervall: Codable, CustomStringConvertible { case weekly case monthly case quarter diff --git a/AboTracker/views/AddSubscriptionView.swift b/AboTracker/views/AddSubscriptionView.swift index f64a1b1..b130dab 100644 --- a/AboTracker/views/AddSubscriptionView.swift +++ b/AboTracker/views/AddSubscriptionView.swift @@ -1,8 +1,10 @@ import SwiftUI +import SwiftData struct AddSubscriptionView: View { @Environment(\.presentationMode) var presentationMode - @Binding var subs: [Subscription] + @Environment(\.modelContext) private var modelContext + @Query var subs: [Subscription] @State private var name: String = "" @State private var payments: [Payment] = [Payment(amount: 0, intervall: .monthly, startDate: Date())] @@ -48,8 +50,8 @@ struct AddSubscriptionView: View { Section { Button("Add Subscription") { - let newSubscription = Subscription(name: name, payments: payments, color: color) - subs.append(newSubscription) + let newSubscription = Subscription(name: name, payments: payments/*, color: color*/) + modelContext.insert(newSubscription) presentationMode.wrappedValue.dismiss() } } diff --git a/AboTracker/views/ContentView.swift b/AboTracker/views/ContentView.swift index 3f77bb7..9bba2f3 100644 --- a/AboTracker/views/ContentView.swift +++ b/AboTracker/views/ContentView.swift @@ -1,27 +1,18 @@ import SwiftUI +import SwiftData struct ContentView: View { - @State private var subs: [Subscription] = [] - - init() { - self._subs = State(initialValue: [ - Subscription(name: "Test", payments: [Payment(amount: 9.99, intervall: .monthly, startDate: getDate(from: "2023-01-01"))], color: .blue), - Subscription(name: "Fitness First", payments: [ - Payment(amount: 7.9, intervall: .weekly, startDate: getDate(from: "2023-01-23")), - Payment(amount: 29, intervall: .quarter, startDate: getDate(from: "2023-04-03")) - ], color: .red) - ]) - } + @Query var subs: [Subscription] var body: some View { TabView { - HomeView(subs: $subs) + HomeView() .tabItem { Image(systemName: "house.fill") Text("Home") } - PaymentCalendarView(subs: $subs) + PaymentCalendarView() .tabItem { Image(systemName: "calendar") Text("Calendar") @@ -30,15 +21,11 @@ struct ContentView: View { .background(Color.gray.opacity(0.2)) } - private func getDate(from dateString: String) -> Date { - let formatter = DateFormatter() - formatter.dateFormat = "yyyy-MM-dd" - return formatter.date(from: dateString) ?? Date() - } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() + .modelContainer(for: [Subscription.self, Payment.self]) } } diff --git a/AboTracker/views/HomeView.swift b/AboTracker/views/HomeView.swift index 63a9a22..c96d0d8 100644 --- a/AboTracker/views/HomeView.swift +++ b/AboTracker/views/HomeView.swift @@ -1,7 +1,9 @@ import SwiftUI +import SwiftData struct HomeView: View { - @Binding var subs: [Subscription] + @Environment(\.modelContext) private var modelContext + @Query var subs: [Subscription] @State private var showAddSubscriptionSheet = false var body: some View { @@ -25,7 +27,7 @@ struct HomeView: View { } .padding(.vertical, 8) } - .listRowBackground(sub.color.opacity(0.2)) + .listRowBackground(/*sub.color.*/Color.red.opacity(0.2)) .cornerRadius(8) } .onDelete(perform: deleteSubscription) @@ -70,14 +72,15 @@ struct HomeView: View { #endif } .sheet(isPresented: $showAddSubscriptionSheet) { - AddSubscriptionView(subs: $subs) + AddSubscriptionView() } } } } func deleteSubscription(at offsets: IndexSet) { - subs.remove(atOffsets: offsets) + // ToDo: Add delete + // subs.remove(atOffsets: offsets) } func getMonthlyTotal(subs: [Subscription]) -> Float { @@ -99,12 +102,7 @@ struct HomeView: View { struct HomeView_Previews: PreviewProvider { static var previews: some View { - HomeView(subs: .constant([ - Subscription(name: "Test", payments: [Payment(amount: 9.99, intervall: .monthly, startDate: Date())], color: .blue), - Subscription(name: "Fitness First", payments: [ - Payment(amount: 7.9, intervall: .weekly, startDate: Date()), - Payment(amount: 29, intervall: .quarter, startDate: Date()) - ], color: .red) - ])) + HomeView() + .modelContainer(for: [Subscription.self, Payment.self]) } } diff --git a/AboTracker/views/PaymentCalendarView.swift b/AboTracker/views/PaymentCalendarView.swift index fb760cb..a0d86d3 100644 --- a/AboTracker/views/PaymentCalendarView.swift +++ b/AboTracker/views/PaymentCalendarView.swift @@ -1,7 +1,8 @@ import SwiftUI +import SwiftData struct PaymentCalendarView: View { - @Binding var subs: [Subscription] + @Query var subs: [Subscription] let calendar = Calendar.current let dateFormatter = DateFormatter() @@ -92,13 +93,13 @@ struct PaymentCalendarView: View { } func getPaymentColor(for date: Date) -> Color { - for sub in subs { + /*for sub in subs { for payment in sub.payments { if isPaymentDue(payment: payment, for: date) { return sub.color.opacity(0.3) } } - } + }*/ return Color.clear } @@ -152,12 +153,7 @@ struct PaymentCalendarView: View { struct PaymentCalendarView_Previews: PreviewProvider { static var previews: some View { - PaymentCalendarView(subs: .constant([ - Subscription(name: "Test", payments: [Payment(amount: 9.99, intervall: .monthly, startDate: Date())], color: .blue), - Subscription(name: "Fitness First", payments: [ - Payment(amount: 7.9, intervall: .weekly, startDate: Date()), - Payment(amount: 29, intervall: .quarter, startDate: Date()) - ], color: .red) - ])) + PaymentCalendarView() + .modelContainer(for: [Subscription.self, Payment.self]) } }