🐛 Decimal Numbers are now possible

This commit is contained in:
2024-07-02 18:31:49 +02:00
parent 0573b07e1c
commit af2af3d07a
4 changed files with 43 additions and 8 deletions

View File

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

View File

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

View File

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

View File

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