- 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>
528 lines
20 KiB
Swift
528 lines
20 KiB
Swift
//
|
|
// FilterView.swift
|
|
// Feels
|
|
//
|
|
// Created by Trey Tartt on 1/12/22.
|
|
//
|
|
|
|
import SwiftUI
|
|
import SwiftData
|
|
|
|
struct YearView: View {
|
|
let months = [(0, "J"), (1, "F"), (2,"M"), (3,"A"), (4,"M"), (5, "J"), (6,"J"), (7,"A"), (8,"S"), (9,"O"), (10, "N"), (11,"D")]
|
|
|
|
@State private var toggle = true
|
|
|
|
@Query(sort: \MoodEntryModel.forDate, order: .reverse)
|
|
private var items: [MoodEntryModel]
|
|
|
|
@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
|
|
@AppStorage(UserDefaultsStore.Keys.moodImages.rawValue, store: GroupUserDefaults.groupDefaults) private var imagePack: MoodImages = .FontAwesome
|
|
@AppStorage(UserDefaultsStore.Keys.textColor.rawValue, store: GroupUserDefaults.groupDefaults) private var textColor: Color = DefaultTextColor.textColor
|
|
|
|
@EnvironmentObject var iapManager: IAPManager
|
|
@StateObject public var viewModel: YearViewModel
|
|
@StateObject private var filteredDays = DaysFilterClass.shared
|
|
@StateObject private var shareImage = ShareImageStateViewModel()
|
|
@State private var trialWarningHidden = false
|
|
@State private var showSubscriptionStore = false
|
|
|
|
// Heatmap-style grid: 12 columns for months
|
|
private let heatmapColumns = Array(repeating: GridItem(.flexible(), spacing: 2), count: 12)
|
|
|
|
var body: some View {
|
|
ZStack {
|
|
if self.viewModel.data.keys.isEmpty {
|
|
EmptyHomeView(showVote: false, viewModel: nil)
|
|
.padding()
|
|
} else {
|
|
ScrollView {
|
|
VStack(spacing: 16) {
|
|
ForEach(Array(self.viewModel.data.keys.sorted(by: >)), id: \.self) { yearKey in
|
|
YearCard(
|
|
year: yearKey,
|
|
yearData: self.viewModel.data[yearKey]!,
|
|
moodTint: moodTint,
|
|
imagePack: imagePack,
|
|
textColor: textColor,
|
|
theme: theme,
|
|
filteredDays: filteredDays.currentFilters,
|
|
onShare: { image in
|
|
shareImage.selectedShareImage = image
|
|
shareImage.showSheet = true
|
|
}
|
|
)
|
|
}
|
|
}
|
|
.padding(.horizontal)
|
|
.padding(.bottom, 100)
|
|
.background(
|
|
GeometryReader { proxy in
|
|
let offset = proxy.frame(in: .named("scroll")).minY
|
|
Color.clear.preference(key: ViewOffsetKey.self, value: offset)
|
|
}
|
|
)
|
|
}
|
|
.scrollDisabled(iapManager.shouldShowPaywall)
|
|
.mask(
|
|
// Fade effect when paywall should show: 100% at top, 0% at bottom
|
|
iapManager.shouldShowPaywall ?
|
|
AnyView(
|
|
LinearGradient(
|
|
gradient: Gradient(stops: [
|
|
.init(color: .black, location: 0),
|
|
.init(color: .black, location: 0.3),
|
|
.init(color: .clear, location: 1.0)
|
|
]),
|
|
startPoint: .top,
|
|
endPoint: .bottom
|
|
)
|
|
) : AnyView(Color.black)
|
|
)
|
|
}
|
|
|
|
if iapManager.shouldShowPaywall {
|
|
VStack {
|
|
Spacer()
|
|
VStack(spacing: 16) {
|
|
Text("Subscribe to see your full year")
|
|
.font(.headline)
|
|
.foregroundColor(textColor)
|
|
|
|
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()
|
|
}
|
|
} else if iapManager.shouldShowTrialWarning {
|
|
VStack {
|
|
Spacer()
|
|
if !trialWarningHidden {
|
|
IAPWarningView(iapManager: iapManager)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.sheet(isPresented: $showSubscriptionStore) {
|
|
FeelsSubscriptionStoreView()
|
|
}
|
|
.sheet(isPresented: $shareImage.showSheet) {
|
|
if let uiImage = shareImage.selectedShareImage {
|
|
ImageOnlyShareSheet(photo: uiImage)
|
|
}
|
|
}
|
|
.onAppear(perform: {
|
|
self.viewModel.filterEntries(startDate: Date(timeIntervalSince1970: 0), endDate: Date())
|
|
})
|
|
.background(
|
|
theme.currentTheme.bg
|
|
.edgesIgnoringSafeArea(.all)
|
|
)
|
|
.onPreferenceChange(ViewOffsetKey.self) { value in
|
|
withAnimation {
|
|
trialWarningHidden = value < 0
|
|
}
|
|
}
|
|
.padding([.top])
|
|
}
|
|
}
|
|
|
|
// MARK: - Year Card Component
|
|
struct YearCard: View {
|
|
let year: Int
|
|
let yearData: [Int: [DayChartView]]
|
|
let moodTint: MoodTints
|
|
let imagePack: MoodImages
|
|
let textColor: Color
|
|
let theme: Theme
|
|
let filteredDays: [Int]
|
|
let onShare: (UIImage) -> Void
|
|
|
|
@State private var showStats = true
|
|
|
|
private let months = ["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"]
|
|
private let heatmapColumns = Array(repeating: GridItem(.flexible(), spacing: 2), count: 12)
|
|
|
|
private var yearEntries: [MoodEntryModel] {
|
|
let firstOfYear = Calendar.current.date(from: DateComponents(year: year, month: 1, day: 1))!
|
|
let lastOfYear = Calendar.current.date(from: DateComponents(year: year + 1, month: 1, day: 1))!
|
|
return DataController.shared.getData(startDate: firstOfYear, endDate: lastOfYear, includedDays: filteredDays)
|
|
}
|
|
|
|
private var metrics: [MoodMetrics] {
|
|
return Random.createTotalPerc(fromEntries: yearEntries)
|
|
}
|
|
|
|
private var totalEntries: Int {
|
|
yearEntries.filter { ![Mood.missing, Mood.placeholder].contains($0.mood) }.count
|
|
}
|
|
|
|
private var topMood: Mood? {
|
|
metrics.filter { $0.total > 0 }.max(by: { $0.total < $1.total })?.mood
|
|
}
|
|
|
|
private var shareableView: some View {
|
|
VStack(spacing: 0) {
|
|
// Header with year
|
|
Text(String(year))
|
|
.font(.system(size: 48, weight: .heavy, design: .rounded))
|
|
.foregroundColor(textColor)
|
|
.padding(.top, 40)
|
|
.padding(.bottom, 8)
|
|
|
|
Text("Year in Review")
|
|
.font(.system(size: 18, weight: .medium, design: .rounded))
|
|
.foregroundColor(textColor.opacity(0.6))
|
|
.padding(.bottom, 30)
|
|
|
|
// Top mood highlight
|
|
if let topMood = topMood {
|
|
VStack(spacing: 12) {
|
|
Circle()
|
|
.fill(moodTint.color(forMood: topMood))
|
|
.frame(width: 120, height: 120)
|
|
.overlay(
|
|
imagePack.icon(forMood: topMood)
|
|
.resizable()
|
|
.aspectRatio(contentMode: .fit)
|
|
.foregroundColor(.white)
|
|
.padding(28)
|
|
)
|
|
.shadow(color: moodTint.color(forMood: topMood).opacity(0.5), radius: 25, x: 0, y: 12)
|
|
|
|
Text("Top Mood")
|
|
.font(.system(size: 14, weight: .medium, design: .rounded))
|
|
.foregroundColor(textColor.opacity(0.5))
|
|
|
|
Text(topMood.strValue.uppercased())
|
|
.font(.system(size: 24, weight: .bold, design: .rounded))
|
|
.foregroundColor(moodTint.color(forMood: topMood))
|
|
}
|
|
.padding(.bottom, 30)
|
|
}
|
|
|
|
// Stats row
|
|
HStack(spacing: 0) {
|
|
VStack(spacing: 4) {
|
|
Text("\(totalEntries)")
|
|
.font(.system(size: 42, weight: .bold, design: .rounded))
|
|
.foregroundColor(textColor)
|
|
Text("Days Tracked")
|
|
.font(.system(size: 13, weight: .medium, design: .rounded))
|
|
.foregroundColor(textColor.opacity(0.5))
|
|
}
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
.padding(.bottom, 30)
|
|
|
|
// Mood breakdown with bars
|
|
VStack(spacing: 14) {
|
|
ForEach(metrics.filter { $0.total > 0 }.sorted(by: { $0.mood.rawValue > $1.mood.rawValue })) { metric in
|
|
HStack(spacing: 14) {
|
|
Circle()
|
|
.fill(moodTint.color(forMood: metric.mood))
|
|
.frame(width: 36, height: 36)
|
|
.overlay(
|
|
imagePack.icon(forMood: metric.mood)
|
|
.resizable()
|
|
.aspectRatio(contentMode: .fit)
|
|
.foregroundColor(.white)
|
|
.padding(8)
|
|
)
|
|
|
|
GeometryReader { geo in
|
|
ZStack(alignment: .leading) {
|
|
RoundedRectangle(cornerRadius: 8)
|
|
.fill(Color.gray.opacity(0.2))
|
|
|
|
RoundedRectangle(cornerRadius: 8)
|
|
.fill(moodTint.color(forMood: metric.mood))
|
|
.frame(width: max(8, geo.size.width * CGFloat(metric.percent / 100)))
|
|
}
|
|
}
|
|
.frame(height: 16)
|
|
|
|
Text("\(Int(metric.percent))%")
|
|
.font(.system(size: 16, weight: .semibold, design: .rounded))
|
|
.foregroundColor(textColor)
|
|
.frame(width: 45, alignment: .trailing)
|
|
}
|
|
}
|
|
}
|
|
.padding(.horizontal, 32)
|
|
.padding(.bottom, 40)
|
|
|
|
// App branding
|
|
Text("ifeel")
|
|
.font(.system(size: 14, weight: .medium, design: .rounded))
|
|
.foregroundColor(textColor.opacity(0.3))
|
|
.padding(.bottom, 20)
|
|
}
|
|
.frame(width: 400)
|
|
.background(theme.currentTheme.secondaryBGColor)
|
|
}
|
|
|
|
var body: some View {
|
|
VStack(spacing: 0) {
|
|
// Year Header
|
|
HStack {
|
|
Button(action: { withAnimation(.easeInOut(duration: 0.2)) { showStats.toggle() } }) {
|
|
HStack {
|
|
Text(String(year))
|
|
.font(.title2.bold())
|
|
.foregroundColor(textColor)
|
|
|
|
Text("\(totalEntries) days")
|
|
.font(.subheadline)
|
|
.foregroundColor(textColor.opacity(0.6))
|
|
|
|
Image(systemName: showStats ? "chevron.up" : "chevron.down")
|
|
.font(.caption.weight(.semibold))
|
|
.foregroundColor(textColor.opacity(0.5))
|
|
.padding(.leading, 4)
|
|
}
|
|
}
|
|
.buttonStyle(.plain)
|
|
|
|
Spacer()
|
|
|
|
Button(action: {
|
|
let image = shareableView.asImage(size: CGSize(width: 400, height: 750))
|
|
onShare(image)
|
|
}) {
|
|
Image(systemName: "square.and.arrow.up")
|
|
.font(.subheadline.weight(.medium))
|
|
.foregroundColor(textColor.opacity(0.6))
|
|
}
|
|
.buttonStyle(.plain)
|
|
}
|
|
.padding(.horizontal, 16)
|
|
.padding(.vertical, 12)
|
|
|
|
// Stats Section (collapsible)
|
|
if showStats {
|
|
HStack(spacing: 16) {
|
|
// Donut Chart
|
|
MoodDonutChart(metrics: metrics, moodTint: moodTint)
|
|
.frame(width: 100, height: 100)
|
|
|
|
// Bar Chart
|
|
VStack(spacing: 6) {
|
|
ForEach(metrics.filter { $0.total > 0 }) { metric in
|
|
HStack(spacing: 8) {
|
|
imagePack.icon(forMood: metric.mood)
|
|
.resizable()
|
|
.aspectRatio(contentMode: .fit)
|
|
.frame(width: 16, height: 16)
|
|
.foregroundColor(moodTint.color(forMood: metric.mood))
|
|
|
|
GeometryReader { geo in
|
|
ZStack(alignment: .leading) {
|
|
RoundedRectangle(cornerRadius: 3)
|
|
.fill(Color.gray.opacity(0.15))
|
|
RoundedRectangle(cornerRadius: 3)
|
|
.fill(moodTint.color(forMood: metric.mood))
|
|
.frame(width: max(4, geo.size.width * CGFloat(metric.percent / 100)))
|
|
}
|
|
}
|
|
.frame(height: 8)
|
|
|
|
Text("\(Int(metric.percent))%")
|
|
.font(.caption2.weight(.medium))
|
|
.foregroundColor(textColor.opacity(0.7))
|
|
.frame(width: 32, alignment: .trailing)
|
|
}
|
|
}
|
|
}
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
.padding(.horizontal, 16)
|
|
.padding(.bottom, 12)
|
|
.transition(.opacity.combined(with: .move(edge: .top)))
|
|
}
|
|
|
|
Divider()
|
|
.padding(.horizontal, 16)
|
|
|
|
// Month Labels
|
|
HStack(spacing: 2) {
|
|
ForEach(months.indices, id: \.self) { index in
|
|
Text(months[index])
|
|
.font(.system(size: 9, weight: .medium))
|
|
.foregroundColor(textColor.opacity(0.5))
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
}
|
|
.padding(.horizontal, 16)
|
|
.padding(.top, 10)
|
|
.padding(.bottom, 4)
|
|
|
|
// Heatmap Grid
|
|
YearHeatmapGrid(
|
|
yearData: yearData,
|
|
moodTint: moodTint,
|
|
filteredDays: filteredDays
|
|
)
|
|
.padding(.horizontal, 16)
|
|
.padding(.bottom, 16)
|
|
}
|
|
.background(
|
|
RoundedRectangle(cornerRadius: 16)
|
|
.fill(theme.currentTheme.secondaryBGColor)
|
|
)
|
|
}
|
|
}
|
|
|
|
// MARK: - Year Heatmap Grid
|
|
struct YearHeatmapGrid: View {
|
|
let yearData: [Int: [DayChartView]]
|
|
let moodTint: MoodTints
|
|
let filteredDays: [Int]
|
|
|
|
private let heatmapColumns = Array(repeating: GridItem(.flexible(), spacing: 2), count: 12)
|
|
|
|
var body: some View {
|
|
LazyVGrid(columns: heatmapColumns, spacing: 2) {
|
|
ForEach(Array(yearData.keys.sorted(by: <)), id: \.self) { monthKey in
|
|
if let monthData = yearData[monthKey] {
|
|
MonthColumn(
|
|
monthData: monthData,
|
|
moodTint: moodTint,
|
|
filteredDays: filteredDays
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Month Column (Vertical stack of days)
|
|
struct MonthColumn: View {
|
|
let monthData: [DayChartView]
|
|
let moodTint: MoodTints
|
|
let filteredDays: [Int]
|
|
|
|
var body: some View {
|
|
VStack(spacing: 2) {
|
|
ForEach(monthData, id: \.self) { dayView in
|
|
YearHeatmapCell(
|
|
color: dayView.color,
|
|
weekDay: dayView.weekDay,
|
|
isFiltered: filteredDays.contains(dayView.weekDay)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Year Heatmap Cell
|
|
struct YearHeatmapCell: View {
|
|
let color: Color
|
|
let weekDay: Int
|
|
let isFiltered: Bool
|
|
|
|
var body: some View {
|
|
RoundedRectangle(cornerRadius: 2)
|
|
.fill(cellColor)
|
|
.aspectRatio(1, contentMode: .fit)
|
|
.accessibilityLabel(accessibilityDescription)
|
|
}
|
|
|
|
private var accessibilityDescription: String {
|
|
if !isFiltered {
|
|
return "Filtered out"
|
|
} else if color == Mood.placeholder.color {
|
|
return "Empty"
|
|
} else if color == Mood.missing.color {
|
|
return "No mood logged"
|
|
} else {
|
|
return "Mood entry"
|
|
}
|
|
}
|
|
|
|
private var cellColor: Color {
|
|
if !isFiltered {
|
|
return Color.gray.opacity(0.1)
|
|
} else if color == Mood.placeholder.color || color == Mood.missing.color {
|
|
return Color.gray.opacity(0.2)
|
|
} else {
|
|
return color
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Donut Chart
|
|
struct MoodDonutChart: View {
|
|
let metrics: [MoodMetrics]
|
|
let moodTint: MoodTints
|
|
|
|
private var filteredMetrics: [MoodMetrics] {
|
|
metrics.filter { $0.total > 0 }
|
|
}
|
|
|
|
private var total: Int {
|
|
metrics.reduce(0) { $0 + $1.total }
|
|
}
|
|
|
|
var body: some View {
|
|
GeometryReader { geo in
|
|
let size = min(geo.size.width, geo.size.height)
|
|
let lineWidth = size * 0.2
|
|
|
|
ZStack {
|
|
// Background ring
|
|
Circle()
|
|
.stroke(Color.gray.opacity(0.15), lineWidth: lineWidth)
|
|
|
|
// Mood segments
|
|
ForEach(Array(filteredMetrics.enumerated()), id: \.element.id) { index, metric in
|
|
Circle()
|
|
.trim(from: startAngle(for: index), to: endAngle(for: index))
|
|
.stroke(moodTint.color(forMood: metric.mood), style: StrokeStyle(lineWidth: lineWidth, lineCap: .butt))
|
|
.rotationEffect(.degrees(-90))
|
|
}
|
|
|
|
// Center text
|
|
VStack(spacing: 0) {
|
|
Text("\(total)")
|
|
.font(.system(size: size * 0.22, weight: .bold, design: .rounded))
|
|
Text("days")
|
|
.font(.system(size: size * 0.12, weight: .medium))
|
|
.foregroundColor(.secondary)
|
|
}
|
|
}
|
|
.frame(width: size, height: size)
|
|
}
|
|
}
|
|
|
|
private func startAngle(for index: Int) -> CGFloat {
|
|
let precedingTotal = filteredMetrics.prefix(index).reduce(0) { $0 + $1.total }
|
|
return CGFloat(precedingTotal) / CGFloat(max(1, total))
|
|
}
|
|
|
|
private func endAngle(for index: Int) -> CGFloat {
|
|
let includingCurrent = filteredMetrics.prefix(index + 1).reduce(0) { $0 + $1.total }
|
|
return CGFloat(includingCurrent) / CGFloat(max(1, total))
|
|
}
|
|
}
|
|
|
|
struct YearView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
Group {
|
|
YearView(viewModel: YearViewModel())
|
|
|
|
YearView(viewModel: YearViewModel())
|
|
.preferredColorScheme(.dark)
|
|
}
|
|
}
|
|
}
|