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:
@@ -47,6 +47,7 @@ class FoundationModelsInsightService: ObservableObject {
|
||||
|
||||
private var cachedInsights: [String: (insights: [Insight], timestamp: Date)] = [:]
|
||||
private let cacheValidityDuration: TimeInterval = 3600 // 1 hour
|
||||
private var inProgressPeriods: Set<String> = []
|
||||
|
||||
// MARK: - Initialization
|
||||
|
||||
@@ -199,6 +200,14 @@ class FoundationModelsInsightService: ObservableObject {
|
||||
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 {
|
||||
throw InsightGenerationError.modelUnavailable(reason: lastError?.localizedDescription ?? "Model not available")
|
||||
}
|
||||
|
||||
@@ -15,13 +15,15 @@ final class ImageCache {
|
||||
private let cache = NSCache<NSString, UIImage>()
|
||||
private let queue = DispatchQueue(label: "com.tt.feels.imagecache", qos: .userInitiated)
|
||||
|
||||
private var memoryWarningToken: NSObjectProtocol?
|
||||
|
||||
private init() {
|
||||
// Configure cache limits
|
||||
cache.countLimit = 100 // Max 100 images
|
||||
cache.totalCostLimit = 50 * 1024 * 1024 // 50 MB max
|
||||
|
||||
// Clear cache on memory warning
|
||||
NotificationCenter.default.addObserver(
|
||||
memoryWarningToken = NotificationCenter.default.addObserver(
|
||||
forName: UIApplication.didReceiveMemoryWarningNotification,
|
||||
object: nil,
|
||||
queue: .main
|
||||
@@ -30,6 +32,12 @@ final class ImageCache {
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
if let token = memoryWarningToken {
|
||||
NotificationCenter.default.removeObserver(token)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Public API
|
||||
|
||||
/// Get image from cache
|
||||
|
||||
@@ -153,35 +153,32 @@ extension WatchConnectivityManager: WCSessionDelegate {
|
||||
}
|
||||
|
||||
private func handleReceivedMessage(_ message: [String: Any]) {
|
||||
guard let action = message["action"] as? String else {
|
||||
Self.logger.error("No action in message")
|
||||
return
|
||||
}
|
||||
|
||||
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")
|
||||
Task { @MainActor in
|
||||
guard let action = message["action"] as? String else {
|
||||
Self.logger.error("No action in message")
|
||||
return
|
||||
}
|
||||
|
||||
let date = Date(timeIntervalSince1970: timestamp)
|
||||
Self.logger.info("Processing mood \(moodRaw) from watch for \(date)")
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
case "reloadWidgets":
|
||||
Self.logger.info("Received reloadWidgets from watch")
|
||||
Task { @MainActor in
|
||||
case "reloadWidgets":
|
||||
Self.logger.info("Received reloadWidgets from watch")
|
||||
WidgetCenter.shared.reloadAllTimelines()
|
||||
}
|
||||
|
||||
default:
|
||||
Self.logger.warning("Unknown action: \(action)")
|
||||
default:
|
||||
Self.logger.warning("Unknown action: \(action)")
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user