Add AI-powered insights using Apple Foundation Models

- Replace static insights with on-device AI generation via FoundationModels framework
- Add @Generable AIInsight model for structured LLM output
- Create FoundationModelsInsightService with session-per-request for concurrent generation
- Add MoodDataSummarizer to prepare mood data for AI analysis
- Implement loading states with skeleton UI and pull-to-refresh
- Add AI availability badge and error handling
- Support default (supportive) and rude (sarcastic) personality modes
- Optimize prompts to fit within 4096 token context limit
- Bump iOS deployment target to 26.0 for Foundation Models support

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Trey t
2025-12-13 10:20:11 -06:00
parent 6adef2d6fc
commit 5974002a82
6 changed files with 934 additions and 1046 deletions

View File

@@ -28,6 +28,27 @@ struct InsightsView: View {
.font(.system(size: 28, weight: .bold, design: .rounded))
.foregroundColor(textColor)
Spacer()
// AI badge
if viewModel.isAIAvailable {
HStack(spacing: 4) {
Image(systemName: "sparkles")
.font(.system(size: 12, weight: .medium))
Text("AI")
.font(.system(size: 12, weight: .semibold))
}
.foregroundColor(.white)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(
LinearGradient(
colors: [.purple, .blue],
startPoint: .leading,
endPoint: .trailing
)
)
.clipShape(Capsule())
}
}
.padding(.horizontal)
@@ -36,6 +57,7 @@ struct InsightsView: View {
title: "This Month",
icon: "calendar",
insights: viewModel.monthInsights,
loadingState: viewModel.monthLoadingState,
textColor: textColor,
moodTint: moodTint,
imagePack: imagePack,
@@ -47,6 +69,7 @@ struct InsightsView: View {
title: "This Year",
icon: "calendar.badge.clock",
insights: viewModel.yearInsights,
loadingState: viewModel.yearLoadingState,
textColor: textColor,
moodTint: moodTint,
imagePack: imagePack,
@@ -58,6 +81,7 @@ struct InsightsView: View {
title: "All Time",
icon: "infinity",
insights: viewModel.allTimeInsights,
loadingState: viewModel.allTimeLoadingState,
textColor: textColor,
moodTint: moodTint,
imagePack: imagePack,
@@ -67,6 +91,11 @@ struct InsightsView: View {
.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 {
@@ -108,10 +137,12 @@ struct InsightsView: View {
}
// MARK: - Insights Section View
struct InsightsSectionView: View {
let title: String
let icon: String
let insights: [Insight]
let loadingState: InsightLoadingState
let textColor: Color
let moodTint: MoodTints
let imagePack: MoodImages
@@ -132,6 +163,13 @@ struct InsightsSectionView: View {
.font(.system(size: 20, weight: .bold))
.foregroundColor(textColor)
// Loading indicator in header
if loadingState == .loading {
ProgressView()
.scaleEffect(0.7)
.padding(.leading, 4)
}
Spacer()
Image(systemName: isExpanded ? "chevron.up" : "chevron.down")
@@ -145,20 +183,61 @@ struct InsightsSectionView: View {
// Insights List (collapsible)
if isExpanded {
VStack(spacing: 10) {
ForEach(insights) { insight in
InsightCardView(
insight: insight,
textColor: textColor,
moodTint: moodTint,
imagePack: imagePack,
colorScheme: colorScheme
)
switch loadingState {
case .loading:
// Skeleton loading view
VStack(spacing: 10) {
ForEach(0..<3, id: \.self) { _ in
InsightSkeletonView(colorScheme: colorScheme)
}
}
.padding(.horizontal, 16)
.padding(.bottom, 16)
.transition(.opacity)
case .error:
// Show insights (which contain error message) with error styling
VStack(spacing: 10) {
ForEach(insights) { insight in
InsightCardView(
insight: insight,
textColor: textColor,
moodTint: moodTint,
imagePack: imagePack,
colorScheme: colorScheme
)
}
}
.padding(.horizontal, 16)
.padding(.bottom, 16)
.transition(.opacity.combined(with: .move(edge: .top)))
case .loaded, .idle:
// Normal insights display with staggered animation
VStack(spacing: 10) {
ForEach(Array(insights.enumerated()), id: \.element.id) { index, insight in
InsightCardView(
insight: insight,
textColor: textColor,
moodTint: moodTint,
imagePack: imagePack,
colorScheme: colorScheme
)
.transition(.asymmetric(
insertion: .opacity.combined(with: .scale(scale: 0.95)).combined(with: .offset(y: 10)),
removal: .opacity
))
.animation(
.spring(response: 0.4, dampingFraction: 0.8)
.delay(Double(index) * 0.05),
value: insights.count
)
}
}
.padding(.horizontal, 16)
.padding(.bottom, 16)
.transition(.opacity.combined(with: .move(edge: .top)))
}
.padding(.horizontal, 16)
.padding(.bottom, 16)
.transition(.opacity.combined(with: .move(edge: .top)))
}
}
.background(
@@ -166,10 +245,60 @@ struct InsightsSectionView: View {
.fill(colorScheme == .dark ? Color(.systemGray6) : .white)
)
.padding(.horizontal)
.animation(.easeInOut(duration: 0.2), value: isExpanded)
}
}
// MARK: - Skeleton Loading View
struct InsightSkeletonView: View {
let colorScheme: ColorScheme
@State private var isAnimating = false
var body: some View {
HStack(alignment: .top, spacing: 14) {
// Icon placeholder
Circle()
.fill(Color.gray.opacity(0.3))
.frame(width: 44, height: 44)
// Text placeholders
VStack(alignment: .leading, spacing: 8) {
RoundedRectangle(cornerRadius: 4)
.fill(Color.gray.opacity(0.3))
.frame(width: 120, height: 16)
RoundedRectangle(cornerRadius: 4)
.fill(Color.gray.opacity(0.2))
.frame(maxWidth: .infinity)
.frame(height: 14)
RoundedRectangle(cornerRadius: 4)
.fill(Color.gray.opacity(0.2))
.frame(width: 180, height: 14)
}
Spacer()
}
.padding(14)
.background(
RoundedRectangle(cornerRadius: 12)
.fill(colorScheme == .dark ? Color(.systemGray5) : Color(.systemGray6))
)
.opacity(isAnimating ? 0.6 : 1.0)
.animation(
.easeInOut(duration: 0.8)
.repeatForever(autoreverses: true),
value: isAnimating
)
.onAppear {
isAnimating = true
}
}
}
// MARK: - Insight Card View
struct InsightCardView: View {
let insight: Insight
let textColor: Color

File diff suppressed because it is too large Load Diff