Add AI mood report feature with PDF export for therapist sharing

Adds a Reports tab to the Insights view with date range selection, two report
types (Quick Summary / Detailed), Foundation Models AI generation with batched
concurrent processing, and clinical PDF export via WKWebView HTML rendering.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Trey t
2026-03-11 10:13:54 -05:00
parent 31fb2a7fe2
commit 19b4c8b05b
9 changed files with 2149 additions and 148 deletions

View File

@@ -7,6 +7,11 @@
import SwiftUI
enum InsightsTab: String, CaseIterable {
case insights = "Insights"
case reports = "Reports"
}
struct InsightsView: View {
@AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
@AppStorage(UserDefaultsStore.Keys.moodTint.rawValue, store: GroupUserDefaults.groupDefaults) private var moodTint: MoodTints = .Default
@@ -18,161 +23,65 @@ struct InsightsView: View {
@StateObject private var viewModel = InsightsViewModel()
@EnvironmentObject var iapManager: IAPManager
@State private var showSubscriptionStore = false
@State private var selectedTab: InsightsTab = .insights
var body: some View {
ZStack {
ScrollView {
VStack(spacing: 20) {
// Header
HStack {
Text("Insights")
.font(.title.weight(.bold))
.foregroundColor(textColor)
.accessibilityIdentifier(AccessibilityID.Insights.header)
Spacer()
// AI badge
if viewModel.isAIAvailable {
HStack(spacing: 4) {
Image(systemName: "sparkles")
.font(.caption.weight(.medium))
Text("AI")
.font(.caption.weight(.semibold))
}
.foregroundColor(.white)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(
LinearGradient(
colors: [.purple, .blue],
startPoint: .leading,
endPoint: .trailing
)
)
.clipShape(Capsule())
.aiInsightsTip()
}
}
.padding(.horizontal)
// This Month Section
InsightsSectionView(
title: "This Month",
icon: "calendar",
insights: viewModel.monthInsights,
loadingState: viewModel.monthLoadingState,
textColor: textColor,
moodTint: moodTint,
imagePack: imagePack,
colorScheme: colorScheme
)
.accessibilityIdentifier(AccessibilityID.Insights.monthSection)
// This Year Section
InsightsSectionView(
title: "This Year",
icon: "calendar.badge.clock",
insights: viewModel.yearInsights,
loadingState: viewModel.yearLoadingState,
textColor: textColor,
moodTint: moodTint,
imagePack: imagePack,
colorScheme: colorScheme
)
.accessibilityIdentifier(AccessibilityID.Insights.yearSection)
// All Time Section
InsightsSectionView(
title: "All Time",
icon: "infinity",
insights: viewModel.allTimeInsights,
loadingState: viewModel.allTimeLoadingState,
textColor: textColor,
moodTint: moodTint,
imagePack: imagePack,
colorScheme: colorScheme
)
.accessibilityIdentifier(AccessibilityID.Insights.allTimeSection)
}
.padding(.vertical)
.padding(.bottom, 100)
}
.refreshable {
viewModel.refreshInsights()
// Small delay to show refresh animation
try? await Task.sleep(nanoseconds: 500_000_000)
}
.disabled(iapManager.shouldShowPaywall)
if iapManager.shouldShowPaywall {
// Premium insights prompt
VStack(spacing: 24) {
Spacer()
// Icon
ZStack {
Circle()
.fill(
LinearGradient(
colors: [.purple.opacity(0.2), .blue.opacity(0.2)],
startPoint: .topLeading,
endPoint: .bottomTrailing
)
)
.frame(width: 100, height: 100)
VStack(spacing: 0) {
// Header
HStack {
Text("Insights")
.font(.title.weight(.bold))
.foregroundColor(textColor)
.accessibilityIdentifier(AccessibilityID.Insights.header)
Spacer()
// AI badge
if viewModel.isAIAvailable {
HStack(spacing: 4) {
Image(systemName: "sparkles")
.font(.largeTitle)
.foregroundStyle(
LinearGradient(
colors: [.purple, .blue],
startPoint: .topLeading,
endPoint: .bottomTrailing
)
)
.font(.caption.weight(.medium))
Text("AI")
.font(.caption.weight(.semibold))
}
// Text
VStack(spacing: 12) {
Text("Unlock AI-Powered Insights")
.font(.title2.weight(.bold))
.foregroundColor(textColor)
.multilineTextAlignment(.center)
Text("Discover patterns in your mood, get personalized recommendations, and understand what affects how you feel.")
.font(.body)
.foregroundColor(textColor.opacity(0.7))
.multilineTextAlignment(.center)
.padding(.horizontal, 32)
}
// Subscribe button
Button {
showSubscriptionStore = true
} label: {
HStack {
Image(systemName: "sparkles")
Text("Get Personal Insights")
}
.font(.headline.weight(.bold))
.foregroundColor(.white)
.frame(maxWidth: .infinity)
.padding(.vertical, 16)
.background(
LinearGradient(
colors: [.purple, .blue],
startPoint: .leading,
endPoint: .trailing
)
.foregroundColor(.white)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(
LinearGradient(
colors: [.purple, .blue],
startPoint: .leading,
endPoint: .trailing
)
.clipShape(RoundedRectangle(cornerRadius: 14))
}
.padding(.horizontal, 24)
Spacer()
)
.clipShape(Capsule())
.aiInsightsTip()
}
}
.padding(.horizontal)
// Segmented picker
Picker("", selection: $selectedTab) {
ForEach(InsightsTab.allCases, id: \.self) { tab in
Text(tab.rawValue).tag(tab)
}
}
.pickerStyle(.segmented)
.padding(.horizontal, 16)
.padding(.top, 12)
.padding(.bottom, 16)
.accessibilityIdentifier(AccessibilityID.Reports.segmentedPicker)
// Content
ZStack {
if selectedTab == .insights {
insightsContent
} else {
ReportsView()
}
if iapManager.shouldShowPaywall {
paywallOverlay
}
.background(theme.currentTheme.bg)
.accessibilityIdentifier(AccessibilityID.Paywall.insightsOverlay)
}
}
.sheet(isPresented: $showSubscriptionStore) {
@@ -188,6 +97,133 @@ struct InsightsView: View {
}
.padding(.top)
}
// MARK: - Insights Content
private var insightsContent: some View {
ScrollView {
VStack(spacing: 20) {
// This Month Section
InsightsSectionView(
title: "This Month",
icon: "calendar",
insights: viewModel.monthInsights,
loadingState: viewModel.monthLoadingState,
textColor: textColor,
moodTint: moodTint,
imagePack: imagePack,
colorScheme: colorScheme
)
.accessibilityIdentifier(AccessibilityID.Insights.monthSection)
// This Year Section
InsightsSectionView(
title: "This Year",
icon: "calendar.badge.clock",
insights: viewModel.yearInsights,
loadingState: viewModel.yearLoadingState,
textColor: textColor,
moodTint: moodTint,
imagePack: imagePack,
colorScheme: colorScheme
)
.accessibilityIdentifier(AccessibilityID.Insights.yearSection)
// All Time Section
InsightsSectionView(
title: "All Time",
icon: "infinity",
insights: viewModel.allTimeInsights,
loadingState: viewModel.allTimeLoadingState,
textColor: textColor,
moodTint: moodTint,
imagePack: imagePack,
colorScheme: colorScheme
)
.accessibilityIdentifier(AccessibilityID.Insights.allTimeSection)
}
.padding(.vertical)
.padding(.bottom, 100)
}
.refreshable {
viewModel.refreshInsights()
// Small delay to show refresh animation
try? await Task.sleep(nanoseconds: 500_000_000)
}
.disabled(iapManager.shouldShowPaywall)
}
// MARK: - Paywall Overlay
private var paywallOverlay: some View {
VStack(spacing: 24) {
Spacer()
// Icon
ZStack {
Circle()
.fill(
LinearGradient(
colors: [.purple.opacity(0.2), .blue.opacity(0.2)],
startPoint: .topLeading,
endPoint: .bottomTrailing
)
)
.frame(width: 100, height: 100)
Image(systemName: "sparkles")
.font(.largeTitle)
.foregroundStyle(
LinearGradient(
colors: [.purple, .blue],
startPoint: .topLeading,
endPoint: .bottomTrailing
)
)
}
// Text
VStack(spacing: 12) {
Text("Unlock AI-Powered Insights")
.font(.title2.weight(.bold))
.foregroundColor(textColor)
.multilineTextAlignment(.center)
Text("Discover patterns in your mood, get personalized recommendations, and understand what affects how you feel.")
.font(.body)
.foregroundColor(textColor.opacity(0.7))
.multilineTextAlignment(.center)
.padding(.horizontal, 32)
}
// Subscribe button
Button {
showSubscriptionStore = true
} label: {
HStack {
Image(systemName: "sparkles")
Text("Get Personal Insights")
}
.font(.headline.weight(.bold))
.foregroundColor(.white)
.frame(maxWidth: .infinity)
.padding(.vertical, 16)
.background(
LinearGradient(
colors: [.purple, .blue],
startPoint: .leading,
endPoint: .trailing
)
)
.clipShape(RoundedRectangle(cornerRadius: 14))
}
.padding(.horizontal, 24)
Spacer()
}
.background(theme.currentTheme.bg)
.accessibilityIdentifier(AccessibilityID.Paywall.insightsOverlay)
}
}
// MARK: - Insights Section View