🐛 Decimal Numbers are now possible
This commit is contained in:
@@ -7,12 +7,28 @@ import UIKit
|
|||||||
@Attribute(.unique) public let id = UUID()
|
@Attribute(.unique) public let id = UUID()
|
||||||
var name: String
|
var name: String
|
||||||
@Relationship(deleteRule: .cascade, minimumModelCount: 1) var payments: [Payment]
|
@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.name = name
|
||||||
self.payments = payments
|
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 {
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ struct AddSubscriptionView: View {
|
|||||||
|
|
||||||
Section {
|
Section {
|
||||||
Button("Add Subscription") {
|
Button("Add Subscription") {
|
||||||
let newSubscription = Subscription(name: name, payments: payments/*, color: color*/)
|
let newSubscription = Subscription(name: name, payments: payments, color: color)
|
||||||
modelContext.insert(newSubscription)
|
modelContext.insert(newSubscription)
|
||||||
presentationMode.wrappedValue.dismiss()
|
presentationMode.wrappedValue.dismiss()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ struct HomeView: View {
|
|||||||
}
|
}
|
||||||
.padding(.vertical, 8)
|
.padding(.vertical, 8)
|
||||||
}
|
}
|
||||||
.listRowBackground(/*sub.color.*/Color.red.opacity(0.2))
|
.listRowBackground(sub.getColorOrAccent().opacity(0.2))
|
||||||
.cornerRadius(8)
|
.cornerRadius(8)
|
||||||
}
|
}
|
||||||
.onDelete(perform: deleteSubscription)
|
.onDelete(perform: deleteSubscription)
|
||||||
|
|||||||
@@ -93,13 +93,13 @@ struct PaymentCalendarView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getPaymentColor(for date: Date) -> Color {
|
func getPaymentColor(for date: Date) -> Color {
|
||||||
/*for sub in subs {
|
for sub in subs {
|
||||||
for payment in sub.payments {
|
for payment in sub.payments {
|
||||||
if isPaymentDue(payment: payment, for: date) {
|
if isPaymentDue(payment: payment, for: date) {
|
||||||
return sub.color.opacity(0.3)
|
return sub.getColorOrAccent().opacity(0.3)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}*/
|
|
||||||
return Color.clear
|
return Color.clear
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user