|
| 1 | +// |
| 2 | +// WidgetHeatmapPlaceholderShape.swift |
| 3 | +// DevLog |
| 4 | +// |
| 5 | +// Created by opfic on 4/30/26. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +struct WidgetHeatmapPlaceholderShape { |
| 11 | + let currentMonths: [WidgetHeatmapPlaceholderMonthShape] |
| 12 | + let quarterMonths: [WidgetHeatmapPlaceholderMonthShape] |
| 13 | + |
| 14 | + var currentMonthWeekCounts: [Int] { |
| 15 | + currentMonths.map(\.weeks.count) |
| 16 | + } |
| 17 | + |
| 18 | + var quarterWeekCounts: [Int] { |
| 19 | + quarterMonths.map(\.weeks.count) |
| 20 | + } |
| 21 | + |
| 22 | + init( |
| 23 | + date: Date = Date(), |
| 24 | + calendar: Calendar = .current |
| 25 | + ) { |
| 26 | + let quarterStart = calendar.startOfQuarter(for: date) |
| 27 | + let monthStarts = (0..<3).compactMap { |
| 28 | + calendar.date(byAdding: .month, value: $0, to: quarterStart) |
| 29 | + } |
| 30 | + let widgetHeatmapPlaceholderMonthShapes = monthStarts.map { |
| 31 | + Self.makeMonth(monthStart: $0, calendar: calendar) |
| 32 | + } |
| 33 | + |
| 34 | + if let currentMonth = widgetHeatmapPlaceholderMonthShapes.first(where: { |
| 35 | + calendar.isDate($0.monthStart, equalTo: date, toGranularity: .month) |
| 36 | + }) { |
| 37 | + currentMonths = [currentMonth] |
| 38 | + } else { |
| 39 | + currentMonths = Array(widgetHeatmapPlaceholderMonthShapes.prefix(1)) |
| 40 | + } |
| 41 | + |
| 42 | + quarterMonths = widgetHeatmapPlaceholderMonthShapes |
| 43 | + } |
| 44 | + |
| 45 | + private static func makeMonth( |
| 46 | + monthStart: Date, |
| 47 | + calendar: Calendar |
| 48 | + ) -> WidgetHeatmapPlaceholderMonthShape { |
| 49 | + guard let monthInterval = calendar.dateInterval(of: .month, for: monthStart), |
| 50 | + let monthLastDay = calendar.date(byAdding: .day, value: -1, to: monthInterval.end), |
| 51 | + let firstWeekInterval = calendar.dateInterval(of: .weekOfYear, for: monthInterval.start), |
| 52 | + let lastWeekInterval = calendar.dateInterval(of: .weekOfYear, for: monthLastDay) else { |
| 53 | + return WidgetHeatmapPlaceholderMonthShape(monthStart: monthStart, weeks: []) |
| 54 | + } |
| 55 | + |
| 56 | + var weeks = [WidgetHeatmapPlaceholderWeekShape]() |
| 57 | + var cursor = firstWeekInterval.start |
| 58 | + |
| 59 | + while cursor < lastWeekInterval.end { |
| 60 | + weeks.append( |
| 61 | + WidgetHeatmapPlaceholderWeekShape( |
| 62 | + id: weeks.count, |
| 63 | + days: makeDays( |
| 64 | + weekStart: cursor, |
| 65 | + monthStart: monthStart, |
| 66 | + calendar: calendar |
| 67 | + ) |
| 68 | + ) |
| 69 | + ) |
| 70 | + |
| 71 | + guard let nextWeek = calendar.date(byAdding: .weekOfYear, value: 1, to: cursor) else { |
| 72 | + break |
| 73 | + } |
| 74 | + cursor = nextWeek |
| 75 | + } |
| 76 | + |
| 77 | + return WidgetHeatmapPlaceholderMonthShape(monthStart: monthStart, weeks: weeks) |
| 78 | + } |
| 79 | + |
| 80 | + private static func makeDays( |
| 81 | + weekStart: Date, |
| 82 | + monthStart: Date, |
| 83 | + calendar: Calendar |
| 84 | + ) -> [WidgetHeatmapPlaceholderDayShape] { |
| 85 | + var days = [WidgetHeatmapPlaceholderDayShape]() |
| 86 | + var cursor = weekStart |
| 87 | + |
| 88 | + for _ in 0..<7 { |
| 89 | + let normalizedDate = calendar.startOfDay(for: cursor) |
| 90 | + days.append( |
| 91 | + WidgetHeatmapPlaceholderDayShape( |
| 92 | + date: normalizedDate, |
| 93 | + isVisible: calendar.isDate( |
| 94 | + normalizedDate, |
| 95 | + equalTo: monthStart, |
| 96 | + toGranularity: .month |
| 97 | + ) |
| 98 | + ) |
| 99 | + ) |
| 100 | + |
| 101 | + guard let nextDay = calendar.date(byAdding: .day, value: 1, to: cursor) else { |
| 102 | + break |
| 103 | + } |
| 104 | + cursor = nextDay |
| 105 | + } |
| 106 | + |
| 107 | + return days |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +struct WidgetHeatmapPlaceholderMonthShape: Identifiable, Hashable { |
| 112 | + var id: Date { monthStart } |
| 113 | + let monthStart: Date |
| 114 | + let weeks: [WidgetHeatmapPlaceholderWeekShape] |
| 115 | +} |
| 116 | + |
| 117 | +struct WidgetHeatmapPlaceholderWeekShape: Identifiable, Hashable { |
| 118 | + let id: Int |
| 119 | + let days: [WidgetHeatmapPlaceholderDayShape] |
| 120 | +} |
| 121 | + |
| 122 | +struct WidgetHeatmapPlaceholderDayShape: Identifiable, Hashable { |
| 123 | + var id: Date { date } |
| 124 | + let date: Date |
| 125 | + let isVisible: Bool |
| 126 | +} |
0 commit comments