|
@@ -4,30 +4,49 @@ import SwiftUI
|
|
|
struct MealStatsView: View {
|
|
struct MealStatsView: View {
|
|
|
@Binding var selectedDuration: Stat.StateModel.StatsTimeInterval
|
|
@Binding var selectedDuration: Stat.StateModel.StatsTimeInterval
|
|
|
let mealStats: [MealStats]
|
|
let mealStats: [MealStats]
|
|
|
- let calculateAverages: @Sendable(Date, Date) async -> (carbs: Double, fat: Double, protein: Double)
|
|
|
|
|
|
|
+ let state: Stat.StateModel
|
|
|
|
|
|
|
|
- @State private var scrollPosition = Date()
|
|
|
|
|
|
|
+ @State private var scrollPosition = Date() // gets updated in onAppear block
|
|
|
@State private var selectedDate: Date?
|
|
@State private var selectedDate: Date?
|
|
|
@State private var currentAverages: (carbs: Double, fat: Double, protein: Double) = (0, 0, 0)
|
|
@State private var currentAverages: (carbs: Double, fat: Double, protein: Double) = (0, 0, 0)
|
|
|
@State private var updateTimer = Stat.UpdateTimer()
|
|
@State private var updateTimer = Stat.UpdateTimer()
|
|
|
- @State private var isScrolling = false
|
|
|
|
|
|
|
|
|
|
|
|
+ /// Returns the time interval length for the visible domain based on selected duration
|
|
|
|
|
+ /// - Returns: TimeInterval representing the visible time range in seconds
|
|
|
|
|
+ ///
|
|
|
|
|
+ /// Time intervals:
|
|
|
|
|
+ /// - Day: 24 hours (86400 seconds)
|
|
|
|
|
+ /// - Week: 7 days (604800 seconds)
|
|
|
|
|
+ /// - Month: 30 days (2592000 seconds)
|
|
|
|
|
+ /// - Total: 90 days (7776000 seconds)
|
|
|
private var visibleDomainLength: TimeInterval {
|
|
private var visibleDomainLength: TimeInterval {
|
|
|
switch selectedDuration {
|
|
switch selectedDuration {
|
|
|
- case .Day: return 24 * 3600 // 1 day
|
|
|
|
|
- case .Week: return 7 * 24 * 3600 // 1 week
|
|
|
|
|
- case .Month: return 30 * 24 * 3600 // 1 month
|
|
|
|
|
- case .Total: return 90 * 24 * 3600 // 3 months
|
|
|
|
|
|
|
+ case .Day: return 24 * 3600 // One day in seconds
|
|
|
|
|
+ case .Week: return 7 * 24 * 3600 // One week in seconds
|
|
|
|
|
+ case .Month: return 30 * 24 * 3600 // One month in seconds (approximated)
|
|
|
|
|
+ case .Total: return 90 * 24 * 3600 // Three months in seconds
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// Calculates the visible date range based on scroll position and domain length
|
|
|
|
|
+ /// - Returns: Tuple containing start and end dates of the visible range
|
|
|
|
|
+ ///
|
|
|
|
|
+ /// The start date is determined by the current scroll position, while the end date
|
|
|
|
|
+ /// is calculated by adding the visible domain length to the start date
|
|
|
private var visibleDateRange: (start: Date, end: Date) {
|
|
private var visibleDateRange: (start: Date, end: Date) {
|
|
|
- let halfDomain = visibleDomainLength / 2
|
|
|
|
|
- let start = scrollPosition.addingTimeInterval(-halfDomain)
|
|
|
|
|
- let end = scrollPosition.addingTimeInterval(halfDomain)
|
|
|
|
|
|
|
+ let start = scrollPosition // Current scroll position marks the start
|
|
|
|
|
+ let end = start.addingTimeInterval(visibleDomainLength)
|
|
|
return (start, end)
|
|
return (start, end)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// Returns the appropriate date format style based on the selected time interval
|
|
|
|
|
+ /// - Returns: A Date.FormatStyle configured for the current time interval
|
|
|
|
|
+ ///
|
|
|
|
|
+ /// Format styles:
|
|
|
|
|
+ /// - Day: Shows hour only (e.g. "13")
|
|
|
|
|
+ /// - Week: Shows abbreviated weekday (e.g. "Mon")
|
|
|
|
|
+ /// - Month: Shows day of month (e.g. "15")
|
|
|
|
|
+ /// - Total: Shows abbreviated month (e.g. "Jan")
|
|
|
private var dateFormat: Date.FormatStyle {
|
|
private var dateFormat: Date.FormatStyle {
|
|
|
switch selectedDuration {
|
|
switch selectedDuration {
|
|
|
case .Day:
|
|
case .Day:
|
|
@@ -41,86 +60,150 @@ struct MealStatsView: View {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// Returns DateComponents for aligning dates based on the selected duration
|
|
|
|
|
+ /// - Returns: DateComponents configured for the appropriate alignment
|
|
|
|
|
+ ///
|
|
|
|
|
+ /// This property provides date components for aligning dates in the chart:
|
|
|
|
|
+ /// - For Day view: Aligns to start of day (midnight)
|
|
|
|
|
+ /// - For Week view: Aligns to Monday (weekday 2)
|
|
|
|
|
+ /// - For Month/Total view: Aligns to first day of month
|
|
|
private var alignmentComponents: DateComponents {
|
|
private var alignmentComponents: DateComponents {
|
|
|
switch selectedDuration {
|
|
switch selectedDuration {
|
|
|
case .Day:
|
|
case .Day:
|
|
|
- return DateComponents(hour: 0) // Align to start of day
|
|
|
|
|
|
|
+ return DateComponents(hour: 0) // Align to midnight
|
|
|
case .Week:
|
|
case .Week:
|
|
|
- return DateComponents(weekday: 2) // 2 = Monday in Calendar
|
|
|
|
|
|
|
+ return DateComponents(weekday: 2) // Monday is weekday 2 in Calendar
|
|
|
case .Month,
|
|
case .Month,
|
|
|
.Total:
|
|
.Total:
|
|
|
- return DateComponents(day: 1) // Align to first day of month
|
|
|
|
|
|
|
+ return DateComponents(day: 1) // First day of month
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// Returns meal statistics for a specific date
|
|
|
|
|
+ /// - Parameter date: The date to find meal statistics for
|
|
|
|
|
+ /// - Returns: MealStats object if found for the given date, nil otherwise
|
|
|
|
|
+ ///
|
|
|
|
|
+ /// This function searches through the meal statistics array to find the first entry
|
|
|
|
|
+ /// that matches the provided date (comparing only the day component, not time).
|
|
|
private func getMealForDate(_ date: Date) -> MealStats? {
|
|
private func getMealForDate(_ date: Date) -> MealStats? {
|
|
|
mealStats.first { stat in
|
|
mealStats.first { stat in
|
|
|
Calendar.current.isDate(stat.date, inSameDayAs: date)
|
|
Calendar.current.isDate(stat.date, inSameDayAs: date)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// Updates the current averages for macronutrients based on the visible date range
|
|
|
|
|
+ ///
|
|
|
|
|
+ /// This function:
|
|
|
|
|
+ /// - Gets the cached meal averages for the currently visible date range from the state
|
|
|
|
|
+ /// - Updates the currentAverages property with the retrieved values (carbs, fat, protein)
|
|
|
private func updateAverages() {
|
|
private func updateAverages() {
|
|
|
- Task.detached(priority: .userInitiated) {
|
|
|
|
|
- let dateRange = await MainActor.run { visibleDateRange }
|
|
|
|
|
- let averages = await calculateAverages(dateRange.start, dateRange.end)
|
|
|
|
|
-
|
|
|
|
|
- await MainActor.run {
|
|
|
|
|
- currentAverages = averages
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // Get cached averages for visible time window
|
|
|
|
|
+ currentAverages = state.getCachedMealAverages(for: visibleDateRange)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private func formatVisibleDateRange(showTimeRange: Bool = false) -> String {
|
|
|
|
|
|
|
+ /// Formats the visible date range into a human-readable string
|
|
|
|
|
+ /// - Returns: A formatted string representing the visible date range
|
|
|
|
|
+ ///
|
|
|
|
|
+ /// For Day view:
|
|
|
|
|
+ /// - Uses relative terms like "Today", "Yesterday", "Tomorrow" when applicable
|
|
|
|
|
+ /// - Shows time range in hours and minutes
|
|
|
|
|
+ /// - Combines dates if start and end are on the same day
|
|
|
|
|
+ ///
|
|
|
|
|
+ /// For other views:
|
|
|
|
|
+ /// - Uses standard date formatting
|
|
|
|
|
+ private func formatVisibleDateRange() -> String {
|
|
|
let start = visibleDateRange.start
|
|
let start = visibleDateRange.start
|
|
|
let end = visibleDateRange.end
|
|
let end = visibleDateRange.end
|
|
|
let calendar = Calendar.current
|
|
let calendar = Calendar.current
|
|
|
|
|
+ let today = Date()
|
|
|
|
|
|
|
|
- switch selectedDuration {
|
|
|
|
|
- case .Day:
|
|
|
|
|
- let today = Date()
|
|
|
|
|
- let isToday = calendar.isDate(start, inSameDayAs: today)
|
|
|
|
|
- let isYesterday = calendar.isDate(start, inSameDayAs: calendar.date(byAdding: .day, value: -1, to: today)!)
|
|
|
|
|
|
|
+ let timeFormat = start.formatted(.dateTime.hour().minute())
|
|
|
|
|
|
|
|
- if isToday || isYesterday, !showTimeRange {
|
|
|
|
|
- return isToday ? "Today" : "Yesterday"
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // Special handling for Day view with relative dates
|
|
|
|
|
+ if selectedDuration == .Day {
|
|
|
|
|
+ let startDateText: String
|
|
|
|
|
+ let endDateText: String
|
|
|
|
|
|
|
|
- let timeRange =
|
|
|
|
|
- "\(start.formatted(.dateTime.hour(.twoDigits(amPM: .wide)))) - \(end.formatted(.dateTime.hour(.twoDigits(amPM: .wide))))"
|
|
|
|
|
|
|
+ // Format start date
|
|
|
|
|
+ if calendar.isDate(start, inSameDayAs: today) {
|
|
|
|
|
+ startDateText = "Today"
|
|
|
|
|
+ } else if calendar.isDate(start, inSameDayAs: calendar.date(byAdding: .day, value: -1, to: today)!) {
|
|
|
|
|
+ startDateText = "Yesterday"
|
|
|
|
|
+ } else if calendar.isDate(start, inSameDayAs: calendar.date(byAdding: .day, value: 1, to: today)!) {
|
|
|
|
|
+ startDateText = "Tomorrow"
|
|
|
|
|
+ } else {
|
|
|
|
|
+ startDateText = start.formatted(.dateTime.day().month())
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if isToday {
|
|
|
|
|
- return "Today, \(timeRange)"
|
|
|
|
|
- } else if isYesterday {
|
|
|
|
|
- return "Yesterday, \(timeRange)"
|
|
|
|
|
|
|
+ // Format end date
|
|
|
|
|
+ if calendar.isDate(end, inSameDayAs: today) {
|
|
|
|
|
+ endDateText = "Today"
|
|
|
|
|
+ } else if calendar.isDate(end, inSameDayAs: calendar.date(byAdding: .day, value: -1, to: today)!) {
|
|
|
|
|
+ endDateText = "Yesterday"
|
|
|
|
|
+ } else if calendar.isDate(end, inSameDayAs: calendar.date(byAdding: .day, value: 1, to: today)!) {
|
|
|
|
|
+ endDateText = "Tomorrow"
|
|
|
} else {
|
|
} else {
|
|
|
- return "\(start.formatted(.dateTime.month().day())), \(timeRange)"
|
|
|
|
|
|
|
+ endDateText = end.formatted(.dateTime.day().month())
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // If start and end are on the same day, show date only once
|
|
|
|
|
+ if calendar.isDate(start, inSameDayAs: end) {
|
|
|
|
|
+ return "\(startDateText), \(timeFormat) - \(end.formatted(.dateTime.hour().minute()))"
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- default:
|
|
|
|
|
- return "\(start.formatted(.dateTime.month().day())) - \(end.formatted(.dateTime.month().day()))"
|
|
|
|
|
|
|
+ return "\(startDateText), \(timeFormat) - \(endDateText), \(end.formatted(.dateTime.hour().minute()))"
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Standard format for other views
|
|
|
|
|
+ return "\(start.formatted()) - \(end.formatted())"
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// Returns the initial scroll position date based on the selected duration
|
|
|
|
|
+ /// - Returns: A Date representing where the chart should initially scroll to
|
|
|
|
|
+ ///
|
|
|
|
|
+ /// This function calculates an appropriate starting scroll position by subtracting
|
|
|
|
|
+ /// a time interval from the current date based on the selected duration:
|
|
|
|
|
+ /// - For Day view: 1 day before now
|
|
|
|
|
+ /// - For Week view: 7 days before now
|
|
|
|
|
+ /// - For Month view: 1 month before now
|
|
|
|
|
+ /// - For Total view: 3 months before now
|
|
|
|
|
+ private func getInitialScrollPosition() -> Date {
|
|
|
|
|
+ let calendar = Calendar.current
|
|
|
|
|
+ let now = Date()
|
|
|
|
|
+
|
|
|
|
|
+ // Calculate scroll position based on selected time interval
|
|
|
|
|
+ switch selectedDuration {
|
|
|
|
|
+ case .Day:
|
|
|
|
|
+ return calendar.date(byAdding: .day, value: -1, to: now)!
|
|
|
|
|
+ case .Week:
|
|
|
|
|
+ return calendar.date(byAdding: .day, value: -7, to: now)!
|
|
|
|
|
+ case .Month:
|
|
|
|
|
+ return calendar.date(byAdding: .month, value: -1, to: now)!
|
|
|
|
|
+ case .Total:
|
|
|
|
|
+ return calendar.date(byAdding: .month, value: -3, to: now)!
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
var body: some View {
|
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
|
statsView
|
|
statsView
|
|
|
-
|
|
|
|
|
chartsView
|
|
chartsView
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
.onAppear {
|
|
.onAppear {
|
|
|
|
|
+ scrollPosition = getInitialScrollPosition()
|
|
|
updateAverages()
|
|
updateAverages()
|
|
|
}
|
|
}
|
|
|
.onChange(of: scrollPosition) {
|
|
.onChange(of: scrollPosition) {
|
|
|
- isScrolling = true
|
|
|
|
|
updateTimer.scheduleUpdate {
|
|
updateTimer.scheduleUpdate {
|
|
|
updateAverages()
|
|
updateAverages()
|
|
|
- isScrolling = false
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
.onChange(of: selectedDuration) {
|
|
.onChange(of: selectedDuration) {
|
|
|
- updateAverages()
|
|
|
|
|
- scrollPosition = Date()
|
|
|
|
|
|
|
+ Task {
|
|
|
|
|
+ scrollPosition = getInitialScrollPosition()
|
|
|
|
|
+ updateAverages()
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -167,7 +250,7 @@ struct MealStatsView: View {
|
|
|
|
|
|
|
|
Spacer()
|
|
Spacer()
|
|
|
|
|
|
|
|
- Text(formatVisibleDateRange(showTimeRange: isScrolling))
|
|
|
|
|
|
|
+ Text(formatVisibleDateRange())
|
|
|
.font(.subheadline)
|
|
.font(.subheadline)
|
|
|
.foregroundStyle(.secondary)
|
|
.foregroundStyle(.secondary)
|
|
|
}
|
|
}
|
|
@@ -281,29 +364,43 @@ struct MealStatsView: View {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/// A view that displays detailed meal information in a popover
|
|
|
|
|
+///
|
|
|
|
|
+/// This view shows a formatted display of meal macronutrients including:
|
|
|
|
|
+/// - Date of the meal
|
|
|
|
|
+/// - Carbohydrates in grams
|
|
|
|
|
+/// - Fat in grams
|
|
|
|
|
+/// - Protein in grams
|
|
|
private struct MealSelectionPopover: View {
|
|
private struct MealSelectionPopover: View {
|
|
|
|
|
+ // The date when the meal was logged
|
|
|
let date: Date
|
|
let date: Date
|
|
|
|
|
+ // The meal statistics to display
|
|
|
let meal: MealStats
|
|
let meal: MealStats
|
|
|
|
|
|
|
|
var body: some View {
|
|
var body: some View {
|
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
|
|
|
+ // Display formatted date header
|
|
|
Text(date.formatted(.dateTime.month().day()))
|
|
Text(date.formatted(.dateTime.month().day()))
|
|
|
.font(.caption)
|
|
.font(.caption)
|
|
|
.foregroundStyle(.secondary)
|
|
.foregroundStyle(.secondary)
|
|
|
|
|
|
|
|
|
|
+ // Grid layout for macronutrient values
|
|
|
Grid(alignment: .leading) {
|
|
Grid(alignment: .leading) {
|
|
|
|
|
+ // Carbohydrates row
|
|
|
GridRow {
|
|
GridRow {
|
|
|
Text("Carbs:")
|
|
Text("Carbs:")
|
|
|
Text(meal.carbs.formatted(.number.precision(.fractionLength(1))))
|
|
Text(meal.carbs.formatted(.number.precision(.fractionLength(1))))
|
|
|
.gridColumnAlignment(.trailing)
|
|
.gridColumnAlignment(.trailing)
|
|
|
Text("g")
|
|
Text("g")
|
|
|
}
|
|
}
|
|
|
|
|
+ // Fat row
|
|
|
GridRow {
|
|
GridRow {
|
|
|
Text("Fat:")
|
|
Text("Fat:")
|
|
|
Text(meal.fat.formatted(.number.precision(.fractionLength(1))))
|
|
Text(meal.fat.formatted(.number.precision(.fractionLength(1))))
|
|
|
.gridColumnAlignment(.trailing)
|
|
.gridColumnAlignment(.trailing)
|
|
|
Text("g")
|
|
Text("g")
|
|
|
}
|
|
}
|
|
|
|
|
+ // Protein row
|
|
|
GridRow {
|
|
GridRow {
|
|
|
Text("Protein:")
|
|
Text("Protein:")
|
|
|
Text(meal.protein.formatted(.number.precision(.fractionLength(1))))
|
|
Text(meal.protein.formatted(.number.precision(.fractionLength(1))))
|
|
@@ -315,6 +412,7 @@ private struct MealSelectionPopover: View {
|
|
|
}
|
|
}
|
|
|
.padding(8)
|
|
.padding(8)
|
|
|
.background(
|
|
.background(
|
|
|
|
|
+ // Add background styling with shadow
|
|
|
RoundedRectangle(cornerRadius: 8)
|
|
RoundedRectangle(cornerRadius: 8)
|
|
|
.fill(Color(.systemBackground))
|
|
.fill(Color(.systemBackground))
|
|
|
.shadow(radius: 2)
|
|
.shadow(radius: 2)
|