Files
Reflect/Shared/MoodLogger.swift
Trey t 086f8b8807 Add comprehensive WCAG 2.1 AA accessibility support
- Add VoiceOver labels and hints to all voting layouts, settings, widgets,
  onboarding screens, and entry cells
- Add Reduce Motion support to button animations throughout the app
- Ensure 44x44pt minimum touch targets on widget mood buttons
- Enhance AccessibilityHelpers with Dynamic Type support, ScaledValue wrapper,
  and VoiceOver detection utilities
- Gate premium features (Insights, Month/Year views) behind subscription
- Update widgets to show subscription prompts for non-subscribers

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-23 23:26:21 -06:00

172 lines
6.7 KiB
Swift

//
// MoodLogger.swift
// Feels
//
// Centralized mood logging service that handles all side effects
//
import Foundation
import WidgetKit
import os.log
/// Centralized service for logging moods with all associated side effects.
/// All mood entry points should use this service to ensure consistent behavior.
@MainActor
final class MoodLogger {
static let shared = MoodLogger()
private static let logger = Logger(subsystem: "com.tt.ifeel", category: "MoodLogger")
/// Key for tracking the last date side effects were applied
private static let lastSideEffectsDateKey = "lastSideEffectsAppliedDate"
private init() {}
/// Log a mood entry with all associated side effects.
/// This is the single source of truth for mood logging in the app.
///
/// - Parameters:
/// - mood: The mood to log
/// - date: The date for the mood entry
/// - entryType: The source of the mood entry (header, widget, siri, etc.)
/// - syncHealthKit: Whether to sync to HealthKit (default true, but widget can't access HealthKit)
/// - updateTips: Whether to update TipKit parameters (default true, but widget can't access TipKit)
func logMood(
_ mood: Mood,
for date: Date,
entryType: EntryType,
syncHealthKit: Bool = true,
updateTips: Bool = true
) {
// 1. Add mood entry to data store
DataController.shared.add(mood: mood, forDate: date, entryType: entryType)
// Apply side effects and mark as complete
applySideEffects(mood: mood, for: date, syncHealthKit: syncHealthKit, updateTips: updateTips)
}
/// Apply side effects for a mood entry without saving the entry itself.
/// Used for catch-up when widget saved data but couldn't run side effects.
///
/// - Parameters:
/// - mood: The mood that was logged
/// - date: The date of the mood entry
/// - syncHealthKit: Whether to sync to HealthKit
/// - updateTips: Whether to update TipKit parameters
func applySideEffects(
mood: Mood,
for date: Date,
syncHealthKit: Bool = true,
updateTips: Bool = true
) {
// Skip side effects for placeholder/missing moods
guard mood != .missing && mood != .placeholder else { return }
Self.logger.info("Applying side effects for mood \(mood.rawValue) on \(date)")
// 1. Sync to HealthKit if enabled, requested, and user has full access
if syncHealthKit {
let healthKitEnabled = GroupUserDefaults.groupDefaults.bool(forKey: UserDefaultsStore.Keys.healthKitEnabled.rawValue)
let hasAccess = !IAPManager.shared.shouldShowPaywall
if healthKitEnabled && hasAccess {
Task {
try? await HealthKitManager.shared.saveMood(mood, for: date)
}
}
}
// 2. Calculate current streak for Live Activity and TipKit
let streak = calculateCurrentStreak()
// 3. Update Live Activity
LiveActivityManager.shared.updateActivity(streak: streak, mood: mood)
LiveActivityScheduler.shared.scheduleForNextDay()
// 4. Update TipKit parameters if requested
if updateTips {
TipsManager.shared.onMoodLogged()
TipsManager.shared.updateStreak(streak)
}
// 5. Request app review at moments of delight
ReviewRequestManager.shared.onMoodLogged(streak: streak)
// 6. Reload widgets
WidgetCenter.shared.reloadAllTimelines()
// 7. Notify watch to refresh complications
WatchConnectivityManager.shared.notifyWatchToReload()
// 8. Mark side effects as applied for this date
markSideEffectsApplied(for: date)
}
/// Check for and process any pending side effects from widget/extension votes.
/// Call this when the app becomes active to ensure all side effects are applied.
func processPendingSideEffects() {
let votingDate = ShowBasedOnVoteLogics.getCurrentVotingDate(onboardingData: UserDefaultsStore.getOnboarding())
let dayStart = Calendar.current.startOfDay(for: votingDate)
let dayEnd = Calendar.current.date(byAdding: .day, value: 1, to: dayStart)!
// Check if there's an entry for the current voting date
guard let entry = DataController.shared.getData(startDate: dayStart, endDate: dayEnd, includedDays: []).first,
entry.mood != .missing && entry.mood != .placeholder else {
Self.logger.debug("No valid mood entry for today, skipping side effects catch-up")
return
}
// Check if side effects were already applied for this date
if sideEffectsApplied(for: votingDate) {
Self.logger.debug("Side effects already applied for \(votingDate)")
return
}
// Apply the missing side effects
Self.logger.info("Catching up side effects for widget/watch vote: \(entry.mood.rawValue)")
applySideEffects(mood: entry.mood, for: entry.forDate)
}
// MARK: - Side Effects Tracking
/// Mark that side effects have been applied for a given date
private func markSideEffectsApplied(for date: Date) {
let dateString = ISO8601DateFormatter().string(from: Calendar.current.startOfDay(for: date))
GroupUserDefaults.groupDefaults.set(dateString, forKey: Self.lastSideEffectsDateKey)
}
/// Check if side effects have been applied for a given date
private func sideEffectsApplied(for date: Date) -> Bool {
guard let lastDateString = GroupUserDefaults.groupDefaults.string(forKey: Self.lastSideEffectsDateKey),
let lastDate = ISO8601DateFormatter().date(from: lastDateString) else {
return false
}
let targetDay = Calendar.current.startOfDay(for: date)
let lastDay = Calendar.current.startOfDay(for: lastDate)
return targetDay == lastDay
}
/// Calculate the current mood streak
private func calculateCurrentStreak() -> Int {
var streak = 0
var checkDate = ShowBasedOnVoteLogics.getCurrentVotingDate(onboardingData: UserDefaultsStore.getOnboarding())
while true {
let dayStart = Calendar.current.startOfDay(for: checkDate)
let dayEnd = Calendar.current.date(bySettingHour: 23, minute: 59, second: 59, of: dayStart)!
let entry = DataController.shared.getData(startDate: dayStart, endDate: dayEnd, includedDays: []).first
if let entry = entry, entry.mood != .missing && entry.mood != .placeholder {
streak += 1
checkDate = Calendar.current.date(byAdding: .day, value: -1, to: checkDate)!
} else {
break
}
}
return streak
}
}