diff --git a/InputMetrics/InputMetrics/Services/EventMonitor.swift b/InputMetrics/InputMetrics/Services/EventMonitor.swift index 24bc832..07d856a 100644 --- a/InputMetrics/InputMetrics/Services/EventMonitor.swift +++ b/InputMetrics/InputMetrics/Services/EventMonitor.swift @@ -35,7 +35,7 @@ class EventMonitor { private static let timeFormatter: DateFormatter = { let f = DateFormatter() f.locale = Locale(identifier: "en_US_POSIX") - f.dateFormat = "HH:mm" + f.dateFormat = "yyyy-MM-dd'T'HH:mm" return f }() diff --git a/InputMetrics/InputMetrics/ViewModels/MenuBarViewModel.swift b/InputMetrics/InputMetrics/ViewModels/MenuBarViewModel.swift index a57981e..98f2e08 100644 --- a/InputMetrics/InputMetrics/ViewModels/MenuBarViewModel.swift +++ b/InputMetrics/InputMetrics/ViewModels/MenuBarViewModel.swift @@ -42,6 +42,47 @@ final class MenuBarViewModel { leftClicks + rightClicks + middleClicks } + var formattedActiveTimeRange: String? { + guard let first = firstActiveAt, let last = lastActiveAt else { return nil } + + let isoFormatter = DateFormatter() + isoFormatter.locale = Locale(identifier: "en_US_POSIX") + isoFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm" + + let legacyFormatter = DateFormatter() + legacyFormatter.locale = Locale(identifier: "en_US_POSIX") + legacyFormatter.dateFormat = "HH:mm" + + let timeFormatter = DateFormatter() + timeFormatter.locale = Locale(identifier: "en_US_POSIX") + timeFormatter.dateFormat = "HH:mm" + + let dateTimeFormatter = DateFormatter() + dateTimeFormatter.locale = Locale(identifier: "en_US_POSIX") + dateTimeFormatter.dateFormat = "MMM d, HH:mm" + + func parse(_ value: String) -> Date? { + isoFormatter.date(from: value) ?? legacyFormatter.date(from: value) + } + + guard let firstDate = parse(first), let lastDate = parse(last) else { + return "\(first) - \(last)" + } + + let firstIsISO = isoFormatter.date(from: first) != nil + let lastIsISO = isoFormatter.date(from: last) != nil + + // Only show dates if both values carry date info and they span different days + if firstIsISO && lastIsISO { + let calendar = Calendar.current + if calendar.startOfDay(for: firstDate) != calendar.startOfDay(for: lastDate) { + return "\(dateTimeFormatter.string(from: firstDate)) - \(dateTimeFormatter.string(from: lastDate))" + } + } + + return "\(timeFormatter.string(from: firstDate)) - \(timeFormatter.string(from: lastDate))" + } + var topKeys: [KeyboardEntry] { Array(keyboardEntries.sorted { $0.count > $1.count }.prefix(5)) } diff --git a/InputMetrics/InputMetrics/Views/MenuBarView.swift b/InputMetrics/InputMetrics/Views/MenuBarView.swift index ea2feab..1bc37a5 100644 --- a/InputMetrics/InputMetrics/Views/MenuBarView.swift +++ b/InputMetrics/InputMetrics/Views/MenuBarView.swift @@ -97,9 +97,8 @@ struct MenuBarView: View { .font(.title3) .foregroundStyle(.teal) - if let first = viewModel.firstActiveAt, - let last = viewModel.lastActiveAt { - Text("\(first) - \(last)") + if let range = viewModel.formattedActiveTimeRange { + Text(range) .font(.headline.monospacedDigit()) } else { Text("Activity tracking will begin as you use your Mac")