From af2af3d07a5ea889d584a062b20f60d36061fee8 Mon Sep 17 00:00:00 2001 From: k3y0708 Date: Tue, 2 Jul 2024 18:31:49 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Decimal=20Numbers=20are=20now=20?= =?UTF-8?q?possible?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AboTracker/Subscription.swift | 41 ++++++++++++++++++++-- AboTracker/views/AddSubscriptionView.swift | 2 +- AboTracker/views/HomeView.swift | 2 +- AboTracker/views/PaymentCalendarView.swift | 6 ++-- 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/AboTracker/Subscription.swift b/AboTracker/Subscription.swift index d90ade1..eb40f52 100644 --- a/AboTracker/Subscription.swift +++ b/AboTracker/Subscription.swift @@ -7,12 +7,28 @@ import UIKit @Attribute(.unique) public let id = UUID() var name: String @Relationship(deleteRule: .cascade, minimumModelCount: 1) var payments: [Payment] - //var color: Color + var colorData: Data? - init(name: String, payments: [Payment]/*, color: Color*/) { + init(name: String, payments: [Payment], color: Color) { self.name = name self.payments = payments - //self.color = color + self.colorData = try? NSKeyedArchiver.archivedData(withRootObject: color, requiringSecureCoding: false) + } + + func getColor() -> Color? { + guard let colorData = self.colorData else { return nil } + do { + if let color = try NSKeyedUnarchiver.unarchivedObject(ofClass: UIColor.self, from: colorData) { + return Color(color) + } + } catch { + print("Error unarchiving color data: \(error)") + } + return nil + } + + func getColorOrAccent() -> Color { + return getColor() ?? Color.accentColor } func getMonthlyAmount() -> Float { @@ -142,3 +158,22 @@ enum PaymentIntervall: Codable, CustomStringConvertible { } } } + +extension Color { + var hexString: String? { + guard let components = UIColor(self).cgColor.components, components.count >= 3 else { + return nil + } + + let r = Float(components[0]) + let g = Float(components[1]) + let b = Float(components[2]) + let a = Float(components.count == 4 ? components[3] : 1.0) + + return String(format: "%02lX%02lX%02lX%02lX", + lroundf(r * 255), + lroundf(g * 255), + lroundf(b * 255), + lroundf(a * 255)) + } +} diff --git a/AboTracker/views/AddSubscriptionView.swift b/AboTracker/views/AddSubscriptionView.swift index a2a7bf0..ccf9499 100644 --- a/AboTracker/views/AddSubscriptionView.swift +++ b/AboTracker/views/AddSubscriptionView.swift @@ -50,7 +50,7 @@ struct AddSubscriptionView: View { Section { Button("Add Subscription") { - let newSubscription = Subscription(name: name, payments: payments/*, color: color*/) + let newSubscription = Subscription(name: name, payments: payments, color: color) modelContext.insert(newSubscription) presentationMode.wrappedValue.dismiss() } diff --git a/AboTracker/views/HomeView.swift b/AboTracker/views/HomeView.swift index c96d0d8..9326cb9 100644 --- a/AboTracker/views/HomeView.swift +++ b/AboTracker/views/HomeView.swift @@ -27,7 +27,7 @@ struct HomeView: View { } .padding(.vertical, 8) } - .listRowBackground(/*sub.color.*/Color.red.opacity(0.2)) + .listRowBackground(sub.getColorOrAccent().opacity(0.2)) .cornerRadius(8) } .onDelete(perform: deleteSubscription) diff --git a/AboTracker/views/PaymentCalendarView.swift b/AboTracker/views/PaymentCalendarView.swift index a0d86d3..f580eb0 100644 --- a/AboTracker/views/PaymentCalendarView.swift +++ b/AboTracker/views/PaymentCalendarView.swift @@ -93,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 sub.getColorOrAccent().opacity(0.3) } } - }*/ + } return Color.clear }