Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081public import Foundation
public struct ActivityLedger: Codable, Equatable, Sendable { public private(set) var days: [ActivityDay] = [] public private(set) var usedFeatures: Set<ActivityFeature> = []
public init() {}
public mutating func record( placements: Int, source: ActivitySource, at date: Date, timeZone: TimeZone = .current ) { precondition(placements > 0) let day = Self.dayKey(date, timeZone: timeZone) if let index = days.firstIndex(where: { $0.date == day }) { days[index].add(source, count: placements) } else { var entry = ActivityDay(date: day) entry.add(source, count: placements) days.append(entry) days.sort { $0.date < $1.date } } }
public mutating func record(feature: ActivityFeature) { usedFeatures.insert(feature) }
public var allTime: ActivityTotals { days.reduce(into: ActivityTotals()) { $0.add($1.totals) } }
public func today(at date: Date, timeZone: TimeZone = .current) -> ActivityTotals { let key = Self.dayKey(date, timeZone: timeZone) return days.first { $0.date == key }?.totals ?? ActivityTotals() }
public func lastSevenDays(at date: Date, timeZone: TimeZone = .current) -> ActivityTotals { var calendar = Calendar(identifier: .gregorian) calendar.timeZone = timeZone guard let start = calendar.date(byAdding: .day, value: -6, to: date) else { preconditionFailure("Gregorian calendar could not find the start of the week") } let first = Self.dayKey(start, timeZone: timeZone) let last = Self.dayKey(date, timeZone: timeZone) return days.reduce(into: ActivityTotals()) { totals, day in if day.date >= first && day.date <= last { totals.add(day.totals) } } }
func validate() throws { guard Set(days.map(\.date)).count == days.count, days.allSatisfy({ Self.validDay($0.date) && $0.totals.isValid }) else { throw ActivityStore.Failure.invalidData } var total = 0 for day in days { let result = total.addingReportingOverflow(day.totals.placements) guard !result.overflow else { throw ActivityStore.Failure.invalidData } total = result.partialValue } }
private static func dayKey(_ date: Date, timeZone: TimeZone) -> String { var calendar = Calendar(identifier: .gregorian) calendar.timeZone = timeZone let parts = calendar.dateComponents([.year, .month, .day], from: date) guard let year = parts.year, let month = parts.month, let day = parts.day else { preconditionFailure("Gregorian date components are missing") } return String(format: "%04d-%02d-%02d", locale: Locale(identifier: "en_US_POSIX"), year, month, day) }
private static func validDay(_ key: String) -> Bool { let parts = key.split(separator: "-", omittingEmptySubsequences: false) guard key.utf8.count == 10, parts.count == 3, let year = Int(parts[0]), let month = Int(parts[1]), let day = Int(parts[2]), year >= 1, year <= 9999, month >= 1, month <= 12, day >= 1, day <= 31 else { return false } let calendar = Calendar(identifier: .gregorian) guard let date = calendar.date(from: DateComponents(year: year, month: month, day: day)) else { return false } return dayKey(date, timeZone: calendar.timeZone) == key }}