Added Event Colors

This commit is contained in:
neon443
2024-12-27 14:00:39 +00:00
parent 35bb7c43ad
commit 8d33a47477
6 changed files with 219 additions and 210 deletions

View File

@@ -7,6 +7,7 @@
import Foundation
import SwiftData
import SwiftUI
@Model
final class Item {
@@ -21,6 +22,7 @@ struct Event: Identifiable, Codable {
var id = UUID()
var name: String
var symbol: String
var color: ColorCodable
var description: String
var date: Date
var recurrence: RecurrenceType
@@ -30,11 +32,49 @@ struct Event: Identifiable, Codable {
}
}
struct ColorCodable: Codable {
var red: Double
var green: Double
var blue: Double
var alpha: Double
//for the brainrotted: alpha is the opacity/transparency of the color,
//alpha == 0 completely transparent
//alpha == 1 completely opaque
var color: Color {
Color(red: red, green: green, blue: blue, opacity: alpha)
}
init(_ color: Color) {
let uiColor = UIColor(color)
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
uiColor.getRed(&r, green: &g, blue: &b, alpha: &a)
self.red = Double(r)
self.green = Double(g)
self.blue = Double(b)
self.alpha = Double(a)
}
init(red: Double, green: Double, blue: Double, alpha: Double = 1.0) {
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
}
}
func daysUntilEvent(_ eventDate: Date) -> String {
let calendar = Calendar.current
let currentDate = Date()
let components = calendar.dateComponents([.day], from: currentDate, to: eventDate)
guard let days = components.day else { return "N/A" }
guard days >= 0 else {
return "\(days) days ago"
}
guard days != 0 else {
return "Today"
}
return "\(days) days"
}
@@ -61,10 +101,18 @@ class EventViewModel: ObservableObject {
}
}
func addEvent(name: String, symbol: String, description: String, date: Date, recurrence: Event.RecurrenceType) {
func addEvent(
name: String,
symbol: String,
color: ColorCodable,
description: String,
date: Date,
recurrence: Event.RecurrenceType
) {
let newEvent = Event(
name: name,
symbol: symbol,
color: color,
description: description,
date: date,
recurrence: recurrence