Add Swift Data

This commit is contained in:
2024-07-02 17:51:49 +02:00
parent 01f471c53c
commit de58ebd79c
8 changed files with 59 additions and 52 deletions

View File

@@ -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 = "<group>"; };
D40CCAF22C2EE304007C4A9F /* AddSubscriptionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddSubscriptionView.swift; sourceTree = "<group>"; };
D426C5592C2F0F150057455D /* PaymentCalendarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaymentCalendarView.swift; sourceTree = "<group>"; };
D43587402C3450F300DD321B /* Helper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Helper.swift; sourceTree = "<group>"; };
D4544FEE2C320AF30090E311 /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -66,6 +68,7 @@
D40CCAE32C2DC5AA007C4A9F /* Assets.xcassets */,
D40CCAE52C2DC5AA007C4A9F /* AboTracker.entitlements */,
D40CCAE62C2DC5AA007C4A9F /* Preview Content */,
D43587402C3450F300DD321B /* Helper.swift */,
);
path = AboTracker;
sourceTree = "<group>";
@@ -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;
};

View File

@@ -13,5 +13,6 @@ struct AboTrackerApp: App {
WindowGroup {
ContentView()
}
.modelContainer(for: [Subscription.self, Payment.self])
}
}

17
AboTracker/Helper.swift Normal file
View File

@@ -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()
}*/

View File

@@ -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

View File

@@ -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()
}
}

View File

@@ -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])
}
}

View File

@@ -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])
}
}

View File

@@ -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])
}
}