Fetch and display weather data (temp, condition, hi/lo, humidity) when users log a mood. Weather is stored as JSON on MoodEntryModel and shown as a card in EntryDetailView. Premium-gated with location permission prompt. Includes BGTask retry for failed fetches and full analytics. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
73 lines
1.7 KiB
Swift
73 lines
1.7 KiB
Swift
//
|
|
// DataControllerUPDATE.swift
|
|
// Reflect
|
|
//
|
|
// SwiftData UPDATE operations.
|
|
//
|
|
|
|
import SwiftData
|
|
import Foundation
|
|
|
|
extension DataController {
|
|
@discardableResult
|
|
func update(entryDate: Date, withMood mood: Mood) -> Bool {
|
|
guard let entry = getEntry(byDate: entryDate) else {
|
|
return false
|
|
}
|
|
|
|
entry.moodValue = mood.rawValue
|
|
entry.timestamp = Date()
|
|
saveAndRunDataListeners()
|
|
|
|
AnalyticsManager.shared.track(.moodUpdated(mood: mood.rawValue))
|
|
return true
|
|
}
|
|
|
|
// MARK: - Notes
|
|
|
|
@discardableResult
|
|
func updateNotes(forDate date: Date, notes: String?) -> Bool {
|
|
guard let entry = getEntry(byDate: date) else {
|
|
return false
|
|
}
|
|
|
|
entry.notes = notes
|
|
saveAndRunDataListeners()
|
|
|
|
AnalyticsManager.shared.track(.noteUpdated(characterCount: (notes ?? "").count))
|
|
return true
|
|
}
|
|
|
|
// MARK: - Weather
|
|
|
|
@discardableResult
|
|
func updateWeather(forDate date: Date, weatherJSON: String?) -> Bool {
|
|
guard let entry = getEntry(byDate: date) else {
|
|
return false
|
|
}
|
|
|
|
entry.weatherJSON = weatherJSON
|
|
saveAndRunDataListeners()
|
|
return true
|
|
}
|
|
|
|
// MARK: - Photo
|
|
|
|
@discardableResult
|
|
func updatePhoto(forDate date: Date, photoID: UUID?) -> Bool {
|
|
guard let entry = getEntry(byDate: date) else {
|
|
return false
|
|
}
|
|
|
|
entry.photoID = photoID
|
|
saveAndRunDataListeners()
|
|
|
|
if photoID != nil {
|
|
AnalyticsManager.shared.track(.photoAdded)
|
|
} else {
|
|
AnalyticsManager.shared.track(.photoDeleted)
|
|
}
|
|
return true
|
|
}
|
|
}
|