mirror of
https://github.com/neon443/NearFuture.git
synced 2026-03-11 22:56:15 +00:00
ok i locked in and the basic app is done 🎉 - up next, accent colors for events and then: widgets
This commit is contained in:
@@ -10,9 +10,87 @@ import SwiftData
|
||||
|
||||
@Model
|
||||
final class Item {
|
||||
var timestamp: Date
|
||||
|
||||
init(timestamp: Date) {
|
||||
self.timestamp = timestamp
|
||||
}
|
||||
var timestamp: Date
|
||||
|
||||
init(timestamp: Date) {
|
||||
self.timestamp = timestamp
|
||||
}
|
||||
}
|
||||
|
||||
struct Event: Identifiable, Codable {
|
||||
var id = UUID()
|
||||
var name: String
|
||||
var symbol: String
|
||||
var description: String
|
||||
var date: Date
|
||||
var recurrence: RecurrenceType
|
||||
|
||||
enum RecurrenceType: String, Codable, CaseIterable {
|
||||
case none, daily, weekly, monthly, yearly
|
||||
}
|
||||
}
|
||||
|
||||
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" }
|
||||
return "\(days) days"
|
||||
}
|
||||
|
||||
class EventViewModel: ObservableObject {
|
||||
@Published var events: [Event] = []
|
||||
|
||||
init() {
|
||||
loadEvents()
|
||||
}
|
||||
|
||||
func loadEvents() {
|
||||
if let savedData = UserDefaults.standard.data(forKey: "events") {
|
||||
let decoder = JSONDecoder()
|
||||
if let decodedEvents = try? decoder.decode([Event].self, from: savedData) {
|
||||
self.events = decodedEvents
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func saveEvents() {
|
||||
let encoder = JSONEncoder()
|
||||
if let encoded = try? encoder.encode(events) {
|
||||
UserDefaults.standard.set(encoded, forKey: "events")
|
||||
}
|
||||
}
|
||||
|
||||
func addEvent(name: String, symbol: String, description: String, date: Date, recurrence: Event.RecurrenceType) {
|
||||
let newEvent = Event(
|
||||
name: name,
|
||||
symbol: symbol,
|
||||
description: description,
|
||||
date: date,
|
||||
recurrence: recurrence
|
||||
)
|
||||
events.append(newEvent)
|
||||
saveEvents()
|
||||
}
|
||||
|
||||
func removeEvent(at index: IndexSet) {
|
||||
events.remove(atOffsets: index)
|
||||
saveEvents()
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: make it better lol
|
||||
func describeOccurrence(date: Date, recurrence: Event.RecurrenceType) -> String {
|
||||
switch recurrence {
|
||||
case .none:
|
||||
return "Occurs once on \(date.formatted(date: .long, time: .omitted))"
|
||||
case .daily:
|
||||
return "Repeats every day from \(date.formatted(date: .long, time: .omitted))"
|
||||
case .weekly:
|
||||
return "Repeats every week from \(date.formatted(date: .long, time: .omitted))"
|
||||
case .monthly:
|
||||
return "Repeats every month from \(date.formatted(date: .long, time: .omitted))"
|
||||
case .yearly:
|
||||
return "Repeats every month from \(date.formatted(date: .long, time: .omitted))"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user