From df0a282c38479eefa4df0e4eeb2c4cc0c52fef8d Mon Sep 17 00:00:00 2001 From: Abhishek Date: Wed, 8 Jun 2022 17:07:30 -0400 Subject: [PATCH] Granularity.. --- Clocker/ClockerUnitTests/EventInfoTests.swift | 6 +++--- .../Events and Reminders/CalendarHandler.swift | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Clocker/ClockerUnitTests/EventInfoTests.swift b/Clocker/ClockerUnitTests/EventInfoTests.swift index 8bcb76e..22ea503 100644 --- a/Clocker/ClockerUnitTests/EventInfoTests.swift +++ b/Clocker/ClockerUnitTests/EventInfoTests.swift @@ -87,12 +87,12 @@ class EventInfoTests: XCTestCase { isAllDay: false, meetingURL: nil, attendeStatus: .accepted) - XCTAssert(mockEventInfo.metadataForMeeting() == "in 1h", + XCTAssert(mockEventInfo.metadataForMeeting() == "in 1h 10m", "Metadata for meeting: \(mockEventInfo.metadataForMeeting()) doesn't match expectation") } func testMetadataForEventHappeningAfterThreeHours() throws { - let pastChunk = TimeChunk(seconds: 10, minutes: 10, hours: 3, days: 0, weeks: 0, months: 0, years: 0) + let pastChunk = TimeChunk(seconds: 10, minutes: 4, hours: 3, days: 0, weeks: 0, months: 0, years: 0) let mockEvent = EKEvent(eventStore: eventStore) mockEvent.title = "Mock Title" mockEvent.startDate = Date().add(pastChunk) @@ -101,7 +101,7 @@ class EventInfoTests: XCTestCase { isAllDay: false, meetingURL: nil, attendeStatus: .accepted) - XCTAssert(mockEventInfo.metadataForMeeting() == "in 3h", + XCTAssert(mockEventInfo.metadataForMeeting() == "in 3h 4m", "Metadata for meeting: \(mockEventInfo.metadataForMeeting()) doesn't match expectation") } } diff --git a/Clocker/Events and Reminders/CalendarHandler.swift b/Clocker/Events and Reminders/CalendarHandler.swift index bc1e8a2..6d11243 100644 --- a/Clocker/Events and Reminders/CalendarHandler.swift +++ b/Clocker/Events and Reminders/CalendarHandler.swift @@ -444,17 +444,30 @@ struct EventInfo { let isAllDay: Bool let meetingURL: URL? let attendeStatus: EKParticipantStatus + private let nsCalendar = Calendar.autoupdatingCurrent func metadataForMeeting() -> String { let timeIntervalSinceNowForMeeting = event.startDate.timeIntervalSinceNow - if timeIntervalSinceNowForMeeting < 0, timeIntervalSinceNowForMeeting > -300 { + if timeIntervalSinceNowForMeeting == 0 { + return "started." + } else if timeIntervalSinceNowForMeeting < 0, timeIntervalSinceNowForMeeting > -300 { return "started +\(event.startDate.shortTimeAgoSinceNow)." } else if event.startDate.isToday, timeIntervalSinceNowForMeeting > 0 { let timeSince = Date().timeAgo(since: event.startDate).lowercased() let withoutAn = timeSince.replacingOccurrences(of: "an", with: CLEmptyString) - let withoutAgo = withoutAn.replacingOccurrences(of: "ago", with: CLEmptyString) + var withoutAgo = withoutAn.replacingOccurrences(of: "ago", with: CLEmptyString) // If the user has not turned on seconds granularity for one of the timezones, // we return "in 12 seconds" which looks weird. + + let upToHours: Set = [.second, .minute, .hour] + let difference = nsCalendar.dateComponents(upToHours, from: Date(), to: event.startDate as Date) + let minuteDifference = difference.minute ?? 0 + let hourDifference = difference.hour ?? 0 + + if hourDifference > 0, minuteDifference > 0 { + withoutAgo.append(contentsOf: "\(minuteDifference)m") + } + return withoutAgo.contains("seconds") ? "in <1m" : "in \(withoutAgo.lowercased())".trimmingCharacters(in: .whitespaces) } else if event.startDate.isTomorrow { let hoursUntil = event.startDate.hoursUntil