9927d21b86
Ambos vivían en UserDefaults (device-local, no sincroniza). Migrados a atributos CoreData en entidades ya syncable=YES, así que ahora sincronizan por CloudKit: - InvestmentSource.monthlyContribution (Decimal, opcional) - Category.allocationTarget (Double, opcional) Los stores mantienen su API por UUID (0 cambios en call sites) pero ahora leen/escriben del atributo CoreData vía fetch por id. migrateIfNeeded(context:) mueve una sola vez los valores del diccionario UserDefaults legacy al modelo y borra la clave; enganchado en CoreDataStack.loadPersistentStores junto a MonthlyCheckInStore. DashboardLayoutStore se mantiene en UserDefaults a propósito (preferencia de UI por dispositivo: el columnSpan solo aplica en iPad). NOTA: requiere desplegar el schema CloudKit a producción (CD_monthlyContribution, CD_allocationTarget) antes de que sincronice en TestFlight/App Store. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015qUZrBusG82T37R7PeokqJ
60 lines
2.4 KiB
Swift
60 lines
2.4 KiB
Swift
import Foundation
|
|
import CoreData
|
|
|
|
/// Per-source recurring monthly contribution amount.
|
|
///
|
|
/// Backed by the `InvestmentSource.monthlyContribution` Core Data attribute so the value
|
|
/// syncs across devices via iCloud/CloudKit. Previously stored in `UserDefaults`, which is
|
|
/// device-local and did NOT sync — `migrateIfNeeded(context:)` moves any legacy value over.
|
|
enum MonthlyContributionStore {
|
|
/// Legacy UserDefaults key (pre-iCloud). Only read once during migration.
|
|
private static let legacyKey = "monthlyContributions"
|
|
|
|
private static var viewContext: NSManagedObjectContext { CoreDataStack.shared.viewContext }
|
|
|
|
static func contribution(for sourceId: UUID) -> Decimal? {
|
|
guard let source = fetchSource(sourceId),
|
|
let amount = source.monthlyContribution?.decimalValue,
|
|
amount > 0 else { return nil }
|
|
return amount
|
|
}
|
|
|
|
static func setContribution(_ amount: Decimal?, for sourceId: UUID) {
|
|
guard let source = fetchSource(sourceId) else { return }
|
|
if let amount, amount > 0 {
|
|
source.monthlyContribution = NSDecimalNumber(decimal: amount)
|
|
} else {
|
|
source.monthlyContribution = nil
|
|
}
|
|
try? viewContext.save()
|
|
}
|
|
|
|
private static func fetchSource(_ id: UUID) -> InvestmentSource? {
|
|
let request = InvestmentSource.fetchRequest()
|
|
request.predicate = NSPredicate(format: "id == %@", id as CVarArg)
|
|
request.fetchLimit = 1
|
|
return try? viewContext.fetch(request).first
|
|
}
|
|
|
|
// MARK: - Migration
|
|
|
|
/// One-time migration of legacy UserDefaults contributions into Core Data so they sync
|
|
/// via iCloud. Runs on every launch but is a no-op once the legacy key is cleared.
|
|
static func migrateIfNeeded(context: NSManagedObjectContext) {
|
|
guard let data = UserDefaults.standard.data(forKey: legacyKey),
|
|
let dict = try? JSONDecoder().decode([String: Double].self, from: data),
|
|
!dict.isEmpty else { return }
|
|
|
|
let sources = (try? context.fetch(InvestmentSource.fetchRequest())) ?? []
|
|
var changed = false
|
|
for source in sources where source.monthlyContribution == nil {
|
|
if let raw = dict[source.id.uuidString], raw > 0 {
|
|
source.monthlyContribution = NSDecimalNumber(value: raw)
|
|
changed = true
|
|
}
|
|
}
|
|
if changed { try? context.save() }
|
|
UserDefaults.standard.removeObject(forKey: legacyKey)
|
|
}
|
|
}
|