Files
Reflect/Shared/Views/InsightsView/InsightsView.swift
2025-12-10 09:53:51 -06:00

236 lines
8.0 KiB
Swift

//
// InsightsView.swift
// Feels
//
// Created by Claude Code on 12/9/24.
//
import SwiftUI
struct InsightsView: View {
@AppStorage(UserDefaultsStore.Keys.theme.rawValue, store: GroupUserDefaults.groupDefaults) private var theme: Theme = .system
@AppStorage(UserDefaultsStore.Keys.textColor.rawValue, store: GroupUserDefaults.groupDefaults) private var textColor: Color = DefaultTextColor.textColor
@AppStorage(UserDefaultsStore.Keys.moodTint.rawValue, store: GroupUserDefaults.groupDefaults) private var moodTint: MoodTints = .Default
@AppStorage(UserDefaultsStore.Keys.moodImages.rawValue, store: GroupUserDefaults.groupDefaults) private var imagePack: MoodImages = .FontAwesome
@Environment(\.colorScheme) private var colorScheme
@StateObject private var viewModel = InsightsViewModel()
@EnvironmentObject var iapManager: IAPManager
@State private var showSubscriptionStore = false
var body: some View {
ZStack {
ScrollView {
VStack(spacing: 20) {
// Header
HStack {
Text("Insights")
.font(.system(size: 28, weight: .bold, design: .rounded))
.foregroundColor(textColor)
Spacer()
}
.padding(.horizontal)
// This Month Section
InsightsSectionView(
title: "This Month",
icon: "calendar",
insights: viewModel.monthInsights,
textColor: textColor,
moodTint: moodTint,
imagePack: imagePack,
colorScheme: colorScheme
)
// This Year Section
InsightsSectionView(
title: "This Year",
icon: "calendar.badge.clock",
insights: viewModel.yearInsights,
textColor: textColor,
moodTint: moodTint,
imagePack: imagePack,
colorScheme: colorScheme
)
// All Time Section
InsightsSectionView(
title: "All Time",
icon: "infinity",
insights: viewModel.allTimeInsights,
textColor: textColor,
moodTint: moodTint,
imagePack: imagePack,
colorScheme: colorScheme
)
}
.padding(.vertical)
.padding(.bottom, 100)
}
.disabled(iapManager.shouldShowPaywall)
if iapManager.shouldShowPaywall {
Color.black.opacity(0.3)
.ignoresSafeArea()
.onTapGesture {
showSubscriptionStore = true
}
VStack {
Spacer()
Button {
showSubscriptionStore = true
} label: {
Text(String(localized: "subscription_required_button"))
.font(.headline)
.foregroundColor(.white)
.frame(maxWidth: .infinity)
.padding()
.background(RoundedRectangle(cornerRadius: 10).fill(Color.pink))
}
.padding()
}
}
}
.sheet(isPresented: $showSubscriptionStore) {
FeelsSubscriptionStoreView()
}
.background(
theme.currentTheme.bg
.edgesIgnoringSafeArea(.all)
)
.onAppear {
EventLogger.log(event: "show_insights_view")
viewModel.generateInsights()
}
.padding(.top)
}
}
// MARK: - Insights Section View
struct InsightsSectionView: View {
let title: String
let icon: String
let insights: [Insight]
let textColor: Color
let moodTint: MoodTints
let imagePack: MoodImages
let colorScheme: ColorScheme
@State private var isExpanded = true
var body: some View {
VStack(spacing: 0) {
// Section Header
Button(action: { withAnimation(.easeInOut(duration: 0.2)) { isExpanded.toggle() } }) {
HStack {
Image(systemName: icon)
.font(.system(size: 18, weight: .medium))
.foregroundColor(textColor.opacity(0.6))
Text(title)
.font(.system(size: 20, weight: .bold))
.foregroundColor(textColor)
Spacer()
Image(systemName: isExpanded ? "chevron.up" : "chevron.down")
.font(.system(size: 12, weight: .semibold))
.foregroundColor(textColor.opacity(0.4))
}
.padding(.horizontal, 16)
.padding(.vertical, 14)
}
.buttonStyle(.plain)
// Insights List (collapsible)
if isExpanded {
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)))
}
}
.background(
RoundedRectangle(cornerRadius: 16)
.fill(colorScheme == .dark ? Color(.systemGray6) : .white)
)
.padding(.horizontal)
}
}
// MARK: - Insight Card View
struct InsightCardView: View {
let insight: Insight
let textColor: Color
let moodTint: MoodTints
let imagePack: MoodImages
let colorScheme: ColorScheme
private var accentColor: Color {
if let mood = insight.mood {
return moodTint.color(forMood: mood)
}
return .accentColor
}
var body: some View {
HStack(alignment: .top, spacing: 14) {
// Icon
ZStack {
Circle()
.fill(accentColor.opacity(0.15))
.frame(width: 44, height: 44)
if let mood = insight.mood {
imagePack.icon(forMood: mood)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 22, height: 22)
.foregroundColor(accentColor)
} else {
Image(systemName: insight.icon)
.font(.system(size: 18, weight: .semibold))
.foregroundColor(accentColor)
}
}
// Text Content
VStack(alignment: .leading, spacing: 4) {
Text(insight.title)
.font(.system(size: 15, weight: .semibold))
.foregroundColor(textColor)
Text(insight.description)
.font(.system(size: 14))
.foregroundColor(textColor.opacity(0.7))
.fixedSize(horizontal: false, vertical: true)
}
Spacer()
}
.padding(14)
.background(
RoundedRectangle(cornerRadius: 12)
.fill(colorScheme == .dark ? Color(.systemGray5) : Color(.systemGray6))
)
}
}
struct InsightsView_Previews: PreviewProvider {
static var previews: some View {
InsightsView()
.environmentObject(IAPManager())
}
}