Fix 25 audit issues: memory leaks, concurrency, performance, accessibility

Address findings from comprehensive audit across 5 workstreams:

- Memory: Token-based DataController listeners (prevent closure leaks),
  static DateFormatters, ImageCache observer cleanup, MotionManager
  reference counting, FoundationModels dedup guard
- Concurrency: Replace Task.detached with Task in FeelsApp (preserve
  MainActor isolation), wrap WatchConnectivity handler in MainActor
- Performance: Cache sortedGroupedData in DayViewViewModel, cache demo
  data in MonthView/YearView, remove broken ReduceMotionModifier
- Accessibility: VoiceOver support for LockScreen, DemoHeatmapCell
  labels, MonthCard button labels, InsightsView header traits,
  Smart Invert protection on neon headers

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Trey t
2026-02-19 09:11:48 -06:00
parent b58dfd5093
commit c22d246865
18 changed files with 175 additions and 73 deletions

View File

@@ -106,7 +106,7 @@ struct FeelsApp: App {
} }
// Defer all non-critical foreground work to avoid blocking UI // Defer all non-critical foreground work to avoid blocking UI
Task.detached(priority: .utility) { @MainActor in Task(priority: .utility) {
// Refresh from disk to pick up widget/watch changes // Refresh from disk to pick up widget/watch changes
DataController.shared.refreshFromDisk() DataController.shared.refreshFromDisk()
@@ -124,17 +124,17 @@ struct FeelsApp: App {
} }
// Defer Live Activity scheduling (heavy DB operations) // Defer Live Activity scheduling (heavy DB operations)
Task.detached(priority: .utility) { Task(priority: .utility) {
await LiveActivityScheduler.shared.scheduleBasedOnCurrentTime() await LiveActivityScheduler.shared.scheduleBasedOnCurrentTime()
} }
// Catch up on side effects from widget/watch votes // Catch up on side effects from widget/watch votes
Task.detached(priority: .utility) { Task(priority: .utility) {
await MoodLogger.shared.processPendingSideEffects() await MoodLogger.shared.processPendingSideEffects()
} }
// Check subscription status (network call) - throttled // Check subscription status (network call) - throttled
Task.detached(priority: .background) { Task(priority: .background) {
await iapManager.checkSubscriptionStatus() await iapManager.checkSubscriptionStatus()
await iapManager.trackSubscriptionAnalytics(source: "app_foreground") await iapManager.trackSubscriptionAnalytics(source: "app_foreground")
} }

View File

@@ -19,6 +19,7 @@ final class MoodLogger {
/// Key for tracking the last date side effects were applied /// Key for tracking the last date side effects were applied
private static let lastSideEffectsDateKey = "lastSideEffectsAppliedDate" private static let lastSideEffectsDateKey = "lastSideEffectsAppliedDate"
private static let sideEffectsDateFormatter = ISO8601DateFormatter()
private init() {} private init() {}
@@ -248,14 +249,14 @@ final class MoodLogger {
/// Mark that side effects have been applied for a given date /// Mark that side effects have been applied for a given date
private func markSideEffectsApplied(for date: Date) { private func markSideEffectsApplied(for date: Date) {
let dateString = ISO8601DateFormatter().string(from: Calendar.current.startOfDay(for: date)) let dateString = Self.sideEffectsDateFormatter.string(from: Calendar.current.startOfDay(for: date))
GroupUserDefaults.groupDefaults.set(dateString, forKey: Self.lastSideEffectsDateKey) GroupUserDefaults.groupDefaults.set(dateString, forKey: Self.lastSideEffectsDateKey)
} }
/// Check if side effects have been applied for a given date /// Check if side effects have been applied for a given date
private func sideEffectsApplied(for date: Date) -> Bool { private func sideEffectsApplied(for date: Date) -> Bool {
guard let lastDateString = GroupUserDefaults.groupDefaults.string(forKey: Self.lastSideEffectsDateKey), guard let lastDateString = GroupUserDefaults.groupDefaults.string(forKey: Self.lastSideEffectsDateKey),
let lastDate = ISO8601DateFormatter().date(from: lastDateString) else { let lastDate = Self.sideEffectsDateFormatter.date(from: lastDateString) else {
return false return false
} }

View File

@@ -22,7 +22,8 @@ final class DataController: ObservableObject {
// Listeners for data changes (keeping existing pattern) // Listeners for data changes (keeping existing pattern)
private var editedDataClosure = [() -> Void]() typealias DataListenerToken = UUID
private var dataListeners: [DataListenerToken: () -> Void] = [:]
// Computed properties for earliest/latest entries // Computed properties for earliest/latest entries
var earliestEntry: MoodEntryModel? { var earliestEntry: MoodEntryModel? {
@@ -48,15 +49,22 @@ final class DataController: ObservableObject {
// MARK: - Listener Management // MARK: - Listener Management
func addNewDataListener(closure: @escaping (() -> Void)) { @discardableResult
editedDataClosure.append(closure) func addNewDataListener(closure: @escaping (() -> Void)) -> DataListenerToken {
let token = DataListenerToken()
dataListeners[token] = closure
return token
}
func removeDataListener(token: DataListenerToken) {
dataListeners.removeValue(forKey: token)
} }
@discardableResult @discardableResult
func saveAndRunDataListeners() -> Bool { func saveAndRunDataListeners() -> Bool {
let success = save() let success = save()
if success { if success {
for closure in editedDataClosure { for closure in dataListeners.values {
closure() closure()
} }
} }
@@ -91,7 +99,7 @@ final class DataController: ObservableObject {
modelContext.rollback() modelContext.rollback()
// Notify listeners to re-fetch their data // Notify listeners to re-fetch their data
for closure in editedDataClosure { for closure in dataListeners.values {
closure() closure()
} }

View File

@@ -77,8 +77,12 @@ protocol MoodDataPersisting {
@discardableResult @discardableResult
func saveAndRunDataListeners() -> Bool func saveAndRunDataListeners() -> Bool
/// Add a listener for data changes /// Add a listener for data changes, returns a token for removal
func addNewDataListener(closure: @escaping (() -> Void)) @discardableResult
func addNewDataListener(closure: @escaping (() -> Void)) -> DataController.DataListenerToken
/// Remove a previously registered data listener
func removeDataListener(token: DataController.DataListenerToken)
} }
/// Combined protocol for full data controller functionality /// Combined protocol for full data controller functionality

View File

@@ -47,6 +47,7 @@ class FoundationModelsInsightService: ObservableObject {
private var cachedInsights: [String: (insights: [Insight], timestamp: Date)] = [:] private var cachedInsights: [String: (insights: [Insight], timestamp: Date)] = [:]
private let cacheValidityDuration: TimeInterval = 3600 // 1 hour private let cacheValidityDuration: TimeInterval = 3600 // 1 hour
private var inProgressPeriods: Set<String> = []
// MARK: - Initialization // MARK: - Initialization
@@ -199,6 +200,14 @@ class FoundationModelsInsightService: ObservableObject {
return cached.insights return cached.insights
} }
// Prevent duplicate concurrent generation for the same period
guard !inProgressPeriods.contains(periodName) else {
// Already generating for this period, wait for cache
return cachedInsights[periodName]?.insights ?? []
}
inProgressPeriods.insert(periodName)
defer { inProgressPeriods.remove(periodName) }
guard isAvailable else { guard isAvailable else {
throw InsightGenerationError.modelUnavailable(reason: lastError?.localizedDescription ?? "Model not available") throw InsightGenerationError.modelUnavailable(reason: lastError?.localizedDescription ?? "Model not available")
} }

View File

@@ -15,13 +15,15 @@ final class ImageCache {
private let cache = NSCache<NSString, UIImage>() private let cache = NSCache<NSString, UIImage>()
private let queue = DispatchQueue(label: "com.tt.feels.imagecache", qos: .userInitiated) private let queue = DispatchQueue(label: "com.tt.feels.imagecache", qos: .userInitiated)
private var memoryWarningToken: NSObjectProtocol?
private init() { private init() {
// Configure cache limits // Configure cache limits
cache.countLimit = 100 // Max 100 images cache.countLimit = 100 // Max 100 images
cache.totalCostLimit = 50 * 1024 * 1024 // 50 MB max cache.totalCostLimit = 50 * 1024 * 1024 // 50 MB max
// Clear cache on memory warning // Clear cache on memory warning
NotificationCenter.default.addObserver( memoryWarningToken = NotificationCenter.default.addObserver(
forName: UIApplication.didReceiveMemoryWarningNotification, forName: UIApplication.didReceiveMemoryWarningNotification,
object: nil, object: nil,
queue: .main queue: .main
@@ -30,6 +32,12 @@ final class ImageCache {
} }
} }
deinit {
if let token = memoryWarningToken {
NotificationCenter.default.removeObserver(token)
}
}
// MARK: - Public API // MARK: - Public API
/// Get image from cache /// Get image from cache

View File

@@ -153,35 +153,32 @@ extension WatchConnectivityManager: WCSessionDelegate {
} }
private func handleReceivedMessage(_ message: [String: Any]) { private func handleReceivedMessage(_ message: [String: Any]) {
guard let action = message["action"] as? String else { Task { @MainActor in
Self.logger.error("No action in message") guard let action = message["action"] as? String else {
return Self.logger.error("No action in message")
}
switch action {
case "logMood":
guard let moodRaw = message["mood"] as? Int,
let mood = Mood(rawValue: moodRaw),
let timestamp = message["date"] as? TimeInterval else {
Self.logger.error("Invalid mood message format")
return return
} }
let date = Date(timeIntervalSince1970: timestamp) switch action {
Self.logger.info("Processing mood \(moodRaw) from watch for \(date)") case "logMood":
guard let moodRaw = message["mood"] as? Int,
let mood = Mood(rawValue: moodRaw),
let timestamp = message["date"] as? TimeInterval else {
Self.logger.error("Invalid mood message format")
return
}
Task { @MainActor in let date = Date(timeIntervalSince1970: timestamp)
Self.logger.info("Processing mood \(moodRaw) from watch for \(date)")
MoodLogger.shared.logMood(mood, for: date, entryType: .watch) MoodLogger.shared.logMood(mood, for: date, entryType: .watch)
}
case "reloadWidgets": case "reloadWidgets":
Self.logger.info("Received reloadWidgets from watch") Self.logger.info("Received reloadWidgets from watch")
Task { @MainActor in
WidgetCenter.shared.reloadAllTimelines() WidgetCenter.shared.reloadAllTimelines()
}
default: default:
Self.logger.warning("Unknown action: \(action)") Self.logger.warning("Unknown action: \(action)")
}
} }
} }
#endif #endif

View File

@@ -21,25 +21,7 @@ extension EnvironmentValues {
} }
} }
/// View modifier that respects reduce motion preference
struct ReduceMotionModifier: ViewModifier {
@Environment(\.accessibilityReduceMotion) var reduceMotion
let animation: Animation?
let reducedAnimation: Animation?
func body(content: Content) -> some View {
content
.animation(reduceMotion ? reducedAnimation : animation, value: UUID())
}
}
extension View { extension View {
/// Applies animation only when reduce motion is disabled
func accessibleAnimation(_ animation: Animation? = .default, reduced: Animation? = nil) -> some View {
modifier(ReduceMotionModifier(animation: animation, reducedAnimation: reduced))
}
/// Wraps content in withAnimation respecting reduce motion /// Wraps content in withAnimation respecting reduce motion
func withAccessibleAnimation<V: Equatable>(_ animation: Animation? = .default, value: V, action: @escaping () -> Void) -> some View { func withAccessibleAnimation<V: Equatable>(_ animation: Animation? = .default, value: V, action: @escaping () -> Void) -> some View {
self.onChange(of: value) { _, _ in self.onChange(of: value) { _, _ in
@@ -59,11 +41,10 @@ extension View {
extension View { extension View {
/// Adds accessibility label with optional hint /// Adds accessibility label with optional hint
func accessibleMoodCell(mood: Mood, date: Date) -> some View { func accessibleMoodCell(mood: Mood, date: Date) -> some View {
let formatter = DateFormatter() let dateString = DateFormattingCache.shared.string(for: date, format: .dateMedium)
formatter.dateStyle = .medium
return self return self
.accessibilityLabel("\(mood.strValue) on \(formatter.string(from: date))") .accessibilityLabel("\(mood.strValue) on \(dateString)")
.accessibilityHint("Double tap to edit mood") .accessibilityHint("Double tap to edit mood")
} }

View File

@@ -95,11 +95,9 @@ struct DayView: View {
} }
} }
/// Cached sorted year/month data to avoid sorting dictionaries in ForEach /// Sorted year/month data cached in ViewModel avoids re-sorting on every render
private var sortedGroupedData: [(year: Int, months: [(month: Int, entries: [MoodEntryModel])])] { private var sortedGroupedData: [(year: Int, months: [(month: Int, entries: [MoodEntryModel])])] {
viewModel.grouped viewModel.sortedGroupedData
.sorted { $0.key > $1.key }
.map { (year: $0.key, months: $0.value.sorted { $0.key > $1.key }.map { (month: $0.key, entries: $0.value) }) }
} }
private var listView: some View { private var listView: some View {
@@ -313,6 +311,7 @@ extension DayView {
} }
} }
) )
.accessibilityIgnoresInvertColors(true)
} }
private func inkSectionHeader(month: Int, year: Int) -> some View { private func inkSectionHeader(month: Int, year: Int) -> some View {

View File

@@ -12,6 +12,7 @@ import SwiftData
class DayViewViewModel: ObservableObject { class DayViewViewModel: ObservableObject {
@Published var grouped = [Int: [Int: [MoodEntryModel]]]() @Published var grouped = [Int: [Int: [MoodEntryModel]]]()
@Published var numberOfItems = 0 @Published var numberOfItems = 0
@Published var sortedGroupedData: [(year: Int, months: [(month: Int, entries: [MoodEntryModel])])] = []
let addMonthStartWeekdayPadding: Bool let addMonthStartWeekdayPadding: Bool
@@ -28,10 +29,12 @@ class DayViewViewModel: ObservableObject {
grouped.values.flatMap(\.values).reduce(0) { $0 + $1.count } grouped.values.flatMap(\.values).reduce(0) { $0 + $1.count }
} }
private var dataListenerToken: DataController.DataListenerToken?
init(addMonthStartWeekdayPadding: Bool) { init(addMonthStartWeekdayPadding: Bool) {
self.addMonthStartWeekdayPadding = addMonthStartWeekdayPadding self.addMonthStartWeekdayPadding = addMonthStartWeekdayPadding
DataController.shared.addNewDataListener { [weak self] in dataListenerToken = DataController.shared.addNewDataListener { [weak self] in
guard let self = self else { return } guard let self = self else { return }
// Avoid withAnimation for bulk data updates - it causes expensive view diffing // Avoid withAnimation for bulk data updates - it causes expensive view diffing
self.updateData() self.updateData()
@@ -39,6 +42,14 @@ class DayViewViewModel: ObservableObject {
updateData() updateData()
} }
deinit {
if let token = dataListenerToken {
Task { @MainActor in
DataController.shared.removeDataListener(token: token)
}
}
}
private func getGroupedData(addMonthStartWeekdayPadding: Bool) { private func getGroupedData(addMonthStartWeekdayPadding: Bool) {
var newStuff = DataController.shared.splitIntoYearMonth(includedDays: [1,2,3,4,5,6,7]) var newStuff = DataController.shared.splitIntoYearMonth(includedDays: [1,2,3,4,5,6,7])
if addMonthStartWeekdayPadding { if addMonthStartWeekdayPadding {
@@ -50,6 +61,9 @@ class DayViewViewModel: ObservableObject {
public func updateData() { public func updateData() {
getGroupedData(addMonthStartWeekdayPadding: self.addMonthStartWeekdayPadding) getGroupedData(addMonthStartWeekdayPadding: self.addMonthStartWeekdayPadding)
sortedGroupedData = grouped
.sorted { $0.key > $1.key }
.map { (year: $0.key, months: $0.value.sorted { $0.key > $1.key }.map { (month: $0.key, entries: $0.value) }) }
} }
public func add(mood: Mood, forDate date: Date, entryType: EntryType) { public func add(mood: Mood, forDate date: Date, entryType: EntryType) {

View File

@@ -2224,6 +2224,9 @@ struct MotionCardView: View {
.onAppear { .onAppear {
motionManager.startIfNeeded() motionManager.startIfNeeded()
} }
.onDisappear {
motionManager.stopIfNoConsumers()
}
} }
} }
@@ -2237,10 +2240,13 @@ class MotionManager: ObservableObject {
@Published var yOffset: CGFloat = 0 @Published var yOffset: CGFloat = 0
private var isRunning = false private var isRunning = false
private var activeConsumers = 0
private init() {} private init() {}
func startIfNeeded() { func startIfNeeded() {
activeConsumers += 1
guard !isRunning, guard !isRunning,
motionManager.isDeviceMotionAvailable, motionManager.isDeviceMotionAvailable,
!UIAccessibility.isReduceMotionEnabled else { return } !UIAccessibility.isReduceMotionEnabled else { return }
@@ -2257,8 +2263,18 @@ class MotionManager: ObservableObject {
} }
} }
func stopIfNoConsumers() {
activeConsumers = max(0, activeConsumers - 1)
guard activeConsumers == 0, isRunning else { return }
isRunning = false
motionManager.stopDeviceMotionUpdates()
xOffset = 0
yOffset = 0
}
func stop() { func stop() {
guard isRunning else { return } guard isRunning else { return }
activeConsumers = 0
isRunning = false isRunning = false
motionManager.stopDeviceMotionUpdates() motionManager.stopDeviceMotionUpdates()
} }

View File

@@ -237,6 +237,7 @@ struct InsightsSectionView: View {
.padding(.vertical, 14) .padding(.vertical, 14)
} }
.buttonStyle(.plain) .buttonStyle(.plain)
.accessibilityAddTraits(.isHeader)
// Insights List (collapsible) // Insights List (collapsible)
if isExpanded { if isExpanded {
@@ -415,6 +416,7 @@ struct InsightCardView: View {
RoundedRectangle(cornerRadius: 12) RoundedRectangle(cornerRadius: 12)
.fill(colorScheme == .dark ? Color(.systemGray5) : Color(.systemGray6)) .fill(colorScheme == .dark ? Color(.systemGray5) : Color(.systemGray6))
) )
.accessibilityElement(children: .combine)
} }
} }

View File

@@ -49,14 +49,24 @@ class InsightsViewModel: ObservableObject {
// MARK: - Initialization // MARK: - Initialization
private var dataListenerToken: DataController.DataListenerToken?
init() { init() {
isAIAvailable = insightService.isAvailable isAIAvailable = insightService.isAvailable
DataController.shared.addNewDataListener { [weak self] in dataListenerToken = DataController.shared.addNewDataListener { [weak self] in
self?.onDataChanged() self?.onDataChanged()
} }
} }
deinit {
if let token = dataListenerToken {
Task { @MainActor in
DataController.shared.removeDataListener(token: token)
}
}
}
/// Called when mood data changes in another tab. Invalidates cached insights /// Called when mood data changes in another tab. Invalidates cached insights
/// so they are regenerated with fresh data on next view appearance. /// so they are regenerated with fresh data on next view appearance.
private func onDataChanged() { private func onDataChanged() {

View File

@@ -1616,10 +1616,12 @@ struct LockScreenView: View {
ZStack { ZStack {
// Themed background // Themed background
backgroundView backgroundView
.accessibilityHidden(true)
// Floating particles (Aurora only) // Floating particles (Aurora only)
if lockScreenStyle == .aurora { if lockScreenStyle == .aurora {
FloatingParticlesView() FloatingParticlesView()
.accessibilityHidden(true)
} }
// Main content // Main content
@@ -1630,6 +1632,7 @@ struct LockScreenView: View {
centralElement centralElement
.opacity(showContent ? 1 : 0) .opacity(showContent ? 1 : 0)
.scaleEffect(showContent ? 1 : 0.8) .scaleEffect(showContent ? 1 : 0.8)
.accessibilityHidden(true)
Spacer() Spacer()
.frame(height: 50) .frame(height: 50)
@@ -1649,6 +1652,7 @@ struct LockScreenView: View {
.multilineTextAlignment(.center) .multilineTextAlignment(.center)
.opacity(showContent ? 1 : 0) .opacity(showContent ? 1 : 0)
.offset(y: showContent ? 0 : 20) .offset(y: showContent ? 0 : 20)
.accessibilityElement(children: .combine)
Spacer() Spacer()
.frame(height: 16) .frame(height: 16)
@@ -1666,6 +1670,8 @@ struct LockScreenView: View {
.opacity(showContent ? 1 : 0) .opacity(showContent ? 1 : 0)
.offset(y: showContent ? 0 : 30) .offset(y: showContent ? 0 : 30)
.padding(.horizontal, 32) .padding(.horizontal, 32)
.accessibilityLabel("Unlock")
.accessibilityHint("Double tap to authenticate with \(authManager.biometricName)")
// Passcode button // Passcode button
if authManager.canUseDevicePasscode { if authManager.canUseDevicePasscode {
@@ -1684,6 +1690,8 @@ struct LockScreenView: View {
.disabled(authManager.isAuthenticating) .disabled(authManager.isAuthenticating)
.padding(.top, 16) .padding(.top, 16)
.opacity(showContent ? 1 : 0) .opacity(showContent ? 1 : 0)
.accessibilityLabel("Use device passcode")
.accessibilityHint("Double tap to authenticate with your device passcode")
} }
Spacer() Spacer()

View File

@@ -44,12 +44,13 @@ struct MonthView: View {
/// Cached sorted year/month data to avoid recalculating in ForEach /// Cached sorted year/month data to avoid recalculating in ForEach
@State private var cachedSortedData: [(year: Int, months: [(month: Int, entries: [MoodEntryModel])])] = [] @State private var cachedSortedData: [(year: Int, months: [(month: Int, entries: [MoodEntryModel])])] = []
@State private var cachedDemoSortedData: [(year: Int, months: [(month: Int, entries: [MoodEntryModel])])] = []
// MARK: - Demo Animation // MARK: - Demo Animation
@StateObject private var demoManager = DemoAnimationManager.shared @StateObject private var demoManager = DemoAnimationManager.shared
/// Generate fake demo data for the past 12 months /// Generate fake demo data for the past 12 months
private var demoSortedData: [(year: Int, months: [(month: Int, entries: [MoodEntryModel])])] { private func computeDemoSortedData() -> [(year: Int, months: [(month: Int, entries: [MoodEntryModel])])] {
var result: [(year: Int, months: [(month: Int, entries: [MoodEntryModel])])] = [] var result: [(year: Int, months: [(month: Int, entries: [MoodEntryModel])])] = []
let calendar = Calendar.current let calendar = Calendar.current
let now = Date() let now = Date()
@@ -158,7 +159,7 @@ struct MonthView: View {
/// Data to display - uses demo data when in demo mode, otherwise cached real data /// Data to display - uses demo data when in demo mode, otherwise cached real data
private var displayData: [(year: Int, months: [(month: Int, entries: [MoodEntryModel])])] { private var displayData: [(year: Int, months: [(month: Int, entries: [MoodEntryModel])])] {
demoManager.isDemoMode ? demoSortedData : cachedSortedData demoManager.isDemoMode ? cachedDemoSortedData : cachedSortedData
} }
var body: some View { var body: some View {
@@ -362,7 +363,7 @@ struct MonthView: View {
} }
.onAppear { .onAppear {
cachedSortedData = computeSortedYearMonthData() cachedSortedData = computeSortedYearMonthData()
// Demo mode is toggled manually via triple-tap cachedDemoSortedData = computeDemoSortedData()
} }
.onChange(of: viewModel.numberOfItems) { _, _ in .onChange(of: viewModel.numberOfItems) { _, _ in
// Use numberOfItems as a lightweight proxy for data changes // Use numberOfItems as a lightweight proxy for data changes
@@ -588,6 +589,8 @@ struct MonthCard: View, Equatable {
} }
} }
.buttonStyle(.plain) .buttonStyle(.plain)
.accessibilityLabel("\(Random.monthName(fromMonthInt: month)) \(String(year)), \(showStats ? "expanded" : "collapsed")")
.accessibilityHint("Double tap to toggle statistics")
Spacer() Spacer()
@@ -599,6 +602,7 @@ struct MonthCard: View, Equatable {
.foregroundColor(labelColor.opacity(0.6)) .foregroundColor(labelColor.opacity(0.6))
} }
.buttonStyle(.plain) .buttonStyle(.plain)
.accessibilityLabel("Share \(Random.monthName(fromMonthInt: month)) \(String(year)) mood data")
} }
.padding(.horizontal, 16) .padding(.horizontal, 16)
.padding(.vertical, 12) .padding(.vertical, 12)
@@ -753,6 +757,7 @@ struct DemoHeatmapCell: View {
// Generate random mood once when cell appears // Generate random mood once when cell appears
randomMood = DemoAnimationManager.randomPositiveMood() randomMood = DemoAnimationManager.randomPositiveMood()
} }
.accessibilityLabel(accessibilityDescription)
} }
/// Whether this cell has been animated (filled with color) /// Whether this cell has been animated (filled with color)
@@ -784,6 +789,18 @@ struct DemoHeatmapCell: View {
return moodTint.color(forMood: entry.mood) return moodTint.color(forMood: entry.mood)
} }
} }
private var accessibilityDescription: String {
if entry.mood == .placeholder {
return "Empty day"
} else if entry.mood == .missing {
return "No mood logged for \(DateFormattingCache.shared.string(for: entry.forDate, format: .dateMedium))"
} else if !isFiltered {
return "\(DateFormattingCache.shared.string(for: entry.forDate, format: .dateMedium)): \(entry.mood.strValue) (filtered out)"
} else {
return "\(DateFormattingCache.shared.string(for: entry.forDate, format: .dateMedium)): \(entry.mood.strValue)"
}
}
} }
// MARK: - Mini Bar Chart // MARK: - Mini Bar Chart

View File

@@ -155,11 +155,15 @@ struct SettingsContentView: View {
.cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight]) .cornerRadius(Constants.viewsCornerRaidus, corners: [.topLeft, .topRight, .bottomLeft, .bottomRight])
} }
private var formattedReminderTime: String { private static let timeFormatter: DateFormatter = {
let onboardingData = UserDefaultsStore.getOnboarding()
let formatter = DateFormatter() let formatter = DateFormatter()
formatter.timeStyle = .short formatter.timeStyle = .short
return formatter.string(from: onboardingData.date) return formatter
}()
private var formattedReminderTime: String {
let onboardingData = UserDefaultsStore.getOnboarding()
return Self.timeFormatter.string(from: onboardingData.date)
} }
// MARK: - Section Headers // MARK: - Section Headers

View File

@@ -26,6 +26,7 @@ struct YearView: View {
/// Cached sorted year keys to avoid re-sorting in ForEach on every render /// Cached sorted year keys to avoid re-sorting in ForEach on every render
@State private var cachedSortedYearKeys: [Int] = [] @State private var cachedSortedYearKeys: [Int] = []
@State private var cachedDemoYearData: [Int: [Int: [DayChartView]]] = [:]
// MARK: - Demo Animation // MARK: - Demo Animation
@StateObject private var demoManager = DemoAnimationManager.shared @StateObject private var demoManager = DemoAnimationManager.shared
@@ -34,7 +35,7 @@ struct YearView: View {
private let heatmapColumns = Array(repeating: GridItem(.flexible(), spacing: 2), count: 12) private let heatmapColumns = Array(repeating: GridItem(.flexible(), spacing: 2), count: 12)
/// Generate demo year data for the past 3 years (full 12 months each) /// Generate demo year data for the past 3 years (full 12 months each)
private var demoYearData: [Int: [Int: [DayChartView]]] { private func computeDemoYearData() -> [Int: [Int: [DayChartView]]] {
var result: [Int: [Int: [DayChartView]]] = [:] var result: [Int: [Int: [DayChartView]]] = [:]
let calendar = Calendar.current let calendar = Calendar.current
let currentYear = calendar.component(.year, from: Date()) let currentYear = calendar.component(.year, from: Date())
@@ -107,7 +108,7 @@ struct YearView: View {
/// Year keys to display - demo data or real data /// Year keys to display - demo data or real data
private var displayYearKeys: [Int] { private var displayYearKeys: [Int] {
if demoManager.isDemoMode { if demoManager.isDemoMode {
return Array(demoYearData.keys.sorted(by: >)) return Array(cachedDemoYearData.keys.sorted(by: >))
} }
return cachedSortedYearKeys return cachedSortedYearKeys
} }
@@ -115,7 +116,7 @@ struct YearView: View {
/// Year data for a specific year - demo or real /// Year data for a specific year - demo or real
private func yearDataFor(_ year: Int) -> [Int: [DayChartView]] { private func yearDataFor(_ year: Int) -> [Int: [DayChartView]] {
if demoManager.isDemoMode { if demoManager.isDemoMode {
return demoYearData[year] ?? [:] return cachedDemoYearData[year] ?? [:]
} }
return viewModel.data[year] ?? [:] return viewModel.data[year] ?? [:]
} }
@@ -282,7 +283,7 @@ struct YearView: View {
.onAppear(perform: { .onAppear(perform: {
self.viewModel.filterEntries(startDate: Date(timeIntervalSince1970: 0), endDate: Date()) self.viewModel.filterEntries(startDate: Date(timeIntervalSince1970: 0), endDate: Date())
cachedSortedYearKeys = Array(viewModel.data.keys.sorted(by: >)) cachedSortedYearKeys = Array(viewModel.data.keys.sorted(by: >))
// Demo mode is toggled manually via triple-tap cachedDemoYearData = computeDemoYearData()
}) })
.onChange(of: viewModel.data.keys.count) { _, _ in .onChange(of: viewModel.data.keys.count) { _, _ in
cachedSortedYearKeys = Array(viewModel.data.keys.sorted(by: >)) cachedSortedYearKeys = Array(viewModel.data.keys.sorted(by: >))
@@ -787,6 +788,19 @@ struct DemoYearHeatmapCell: View {
// Generate random mood once when cell appears // Generate random mood once when cell appears
randomMood = DemoAnimationManager.randomPositiveMood() randomMood = DemoAnimationManager.randomPositiveMood()
} }
.accessibilityLabel(accessibilityDescription)
}
private var accessibilityDescription: String {
if !isFiltered {
return "Filtered out"
} else if color == Mood.placeholder.color {
return "Empty"
} else if color == Mood.missing.color {
return "No mood logged"
} else {
return "Mood entry"
}
} }
/// Whether this cell has been animated (filled with color) /// Whether this cell has been animated (filled with color)

View File

@@ -24,13 +24,23 @@ class YearViewModel: ObservableObject {
} }
} }
private var dataListenerToken: DataController.DataListenerToken?
init() { init() {
DataController.shared.addNewDataListener { [weak self] in dataListenerToken = DataController.shared.addNewDataListener { [weak self] in
self?.refreshData() self?.refreshData()
} }
updateData() updateData()
} }
deinit {
if let token = dataListenerToken {
Task { @MainActor in
DataController.shared.removeDataListener(token: token)
}
}
}
/// Re-fetch data using the current date range. Called by the data listener /// Re-fetch data using the current date range. Called by the data listener
/// when mood entries change in other tabs. /// when mood entries change in other tabs.
public func refreshData() { public func refreshData() {