Migrate from Core Data to SwiftData
- Replace Core Data with SwiftData for iOS 18+ - Create MoodEntryModel as @Model class replacing MoodEntry entity - Create SharedModelContainer for App Group container sharing - Create DataController with CRUD extensions replacing PersistenceController - Update all views and view models to use MoodEntryModel - Update widget extension to use SwiftData - Remove old Core Data files (Persistence*.swift, .xcdatamodeld) - Add EntryType enum with all entry type cases - Fix widget label truncation with proper spacing and text scaling 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,13 +7,14 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
@MainActor
|
||||
class MoodEntryFunctions {
|
||||
static func padMoodEntriesForCalendar(entries grouped: [Int: [Int: [MoodEntry]]]) -> [Int: [Int: [MoodEntry]]] {
|
||||
var newGrouped = [Int: [Int: [MoodEntry]]]()
|
||||
|
||||
static func padMoodEntriesForCalendar(entries grouped: [Int: [Int: [MoodEntryModel]]]) -> [Int: [Int: [MoodEntryModel]]] {
|
||||
var newGrouped = [Int: [Int: [MoodEntryModel]]]()
|
||||
|
||||
let allYears = grouped.keys.sorted(by: > )
|
||||
for year in allYears {
|
||||
var newMonth = [Int: [MoodEntry]]()
|
||||
var newMonth = [Int: [MoodEntryModel]]()
|
||||
|
||||
let oldMonths = grouped[year]!
|
||||
let monthKeys = oldMonths.keys.sorted(by: > )
|
||||
@@ -26,38 +27,38 @@ class MoodEntryFunctions {
|
||||
}
|
||||
return newGrouped
|
||||
}
|
||||
|
||||
static func padMoodEntriesMonth(monthEntries entries: [MoodEntry]) -> [MoodEntry] {
|
||||
let sortedEntries = entries.sorted(by: { $0.forDate! < $1.forDate! })
|
||||
|
||||
static func padMoodEntriesMonth(monthEntries entries: [MoodEntryModel]) -> [MoodEntryModel] {
|
||||
let sortedEntries = entries.sorted(by: { $0.forDate < $1.forDate })
|
||||
var mutableEntries = sortedEntries
|
||||
|
||||
|
||||
if let firstDate = sortedEntries.first {
|
||||
let date = firstDate.forDate!
|
||||
|
||||
let date = firstDate.forDate
|
||||
|
||||
// if the first entry for a month is in the middle of the month we
|
||||
// need to add in the missing entries, as placeholders, to the beignning to get
|
||||
// the entries on the right day. think user downloads in the middle of the month
|
||||
// and entry is on the 13th ... this needs to show on the 13th entry spot
|
||||
var startOfMonth = date.startOfMonth
|
||||
startOfMonth = Calendar.current.date(byAdding: .hour, value: 9, to: startOfMonth)!
|
||||
let lastMissingDate = mutableEntries.first?.forDate ?? date.endOfMonth
|
||||
let lastMissingDate = mutableEntries.first?.forDate ?? date.endOfMonth
|
||||
var missingDates = Date.dates(from: startOfMonth, toDate: lastMissingDate, includingToDate: true)
|
||||
missingDates = missingDates.dropLast()
|
||||
|
||||
|
||||
for date in missingDates {
|
||||
mutableEntries.insert(PersistenceController.shared.generateObjectNotInArray(forDate: date, withMood: .placeholder), at: 0)
|
||||
mutableEntries.insert(DataController.shared.generateObjectNotInArray(forDate: date, withMood: .placeholder), at: 0)
|
||||
}
|
||||
|
||||
|
||||
mutableEntries = mutableEntries.sorted(by: {
|
||||
$0.forDate! < $1.forDate!
|
||||
$0.forDate < $1.forDate
|
||||
})
|
||||
|
||||
|
||||
// fill in calendar day offset .. if month starts on wed we need to
|
||||
// pad the beginning sun, mon, tues
|
||||
if let firstDate = mutableEntries.first?.forDate {
|
||||
let weekday = Int16(Calendar.current.component(.weekday, from: firstDate))
|
||||
for _ in 1..<weekday {
|
||||
mutableEntries.insert(PersistenceController.shared.generateObjectNotInArray(withMood: .placeholder), at: 0)
|
||||
mutableEntries.insert(DataController.shared.generateObjectNotInArray(withMood: .placeholder), at: 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user