From 189edf63cfe408f087c61f4e8ae73dcf7f138cec Mon Sep 17 00:00:00 2001 From: Ehren Kret Date: Mon, 10 Feb 2025 09:00:02 -0600 Subject: [PATCH] remove `Date.isBefore(_)` and `Date.isAfter(_)` extensions --- ...ConversationViewController+Delegates.swift | 3 +- .../Chat List/CLVTableDataSource.swift | 2 +- Signal/test/util/DateUtilTest.swift | 37 ++++++++++--------- SignalServiceKit/DebugLogs/DebugLogger.swift | 2 +- .../BackupAttachmentDownloadManager.swift | 8 ++-- .../Messages/Interactions/MentionFinder.swift | 2 +- .../Messages/OWSMessageDecrypter.swift | 2 +- .../Messages/Reactions/ReactionManager.swift | 2 +- SignalServiceKit/Util/Date+SSK.swift | 8 ---- SignalServiceKit/Util/OWSFileSystem.swift | 2 +- .../RecipientPickers/ConversationItem.swift | 4 +- 11 files changed, 31 insertions(+), 41 deletions(-) diff --git a/Signal/ConversationView/ConversationViewController+Delegates.swift b/Signal/ConversationView/ConversationViewController+Delegates.swift index 51984407a1..f720400e7c 100644 --- a/Signal/ConversationView/ConversationViewController+Delegates.swift +++ b/Signal/ConversationView/ConversationViewController+Delegates.swift @@ -348,8 +348,7 @@ extension ConversationViewController: InputAccessoryViewPlaceholderDelegate { // how long they will be blocked for. let animationCompletionDate = Date().addingTimeInterval(duration) let lastKeyboardAnimationDate = Date().addingTimeInterval(-1.0) - if viewState.lastKeyboardAnimationDate == nil || - viewState.lastKeyboardAnimationDate?.isBefore(lastKeyboardAnimationDate) == true { + if viewState.lastKeyboardAnimationDate == nil || viewState.lastKeyboardAnimationDate! < lastKeyboardAnimationDate { viewState.lastKeyboardAnimationDate = animationCompletionDate } } diff --git a/Signal/src/ViewControllers/HomeView/Chat List/CLVTableDataSource.swift b/Signal/src/ViewControllers/HomeView/Chat List/CLVTableDataSource.swift index fc3b057f6b..cf19480df7 100644 --- a/Signal/src/ViewControllers/HomeView/Chat List/CLVTableDataSource.swift +++ b/Signal/src/ViewControllers/HomeView/Chat List/CLVTableDataSource.swift @@ -683,7 +683,7 @@ extension CLVTableDataSource: UITableViewDataSource { extension CLVTableDataSource { func updateAndSetRefreshTimer(for cell: ChatListCell?) { if let cell = cell, let timestamp = cell.nextUpdateTimestamp { - if nextUpdateAt == nil || timestamp.isBefore(nextUpdateAt!) { + if nextUpdateAt == nil || timestamp < nextUpdateAt! { nextUpdateAt = timestamp } } diff --git a/Signal/test/util/DateUtilTest.swift b/Signal/test/util/DateUtilTest.swift index a7994da78c..c76f0bda86 100644 --- a/Signal/test/util/DateUtilTest.swift +++ b/Signal/test/util/DateUtilTest.swift @@ -4,9 +4,28 @@ // import XCTest +import Testing import SignalServiceKit +@Test func testDateComparison() { + let firstDate = Date() + let sameDate = Date(timeIntervalSinceReferenceDate: firstDate.timeIntervalSinceReferenceDate) + let laterDate = Date(timeIntervalSinceReferenceDate: firstDate.timeIntervalSinceReferenceDate + 1) + + #expect(firstDate.timeIntervalSinceReferenceDate == sameDate.timeIntervalSinceReferenceDate) + #expect(firstDate.timeIntervalSinceReferenceDate != laterDate.timeIntervalSinceReferenceDate) + #expect(firstDate == sameDate) + #expect(firstDate != laterDate) + #expect(firstDate.timeIntervalSinceReferenceDate < laterDate.timeIntervalSinceReferenceDate) + #expect(!(firstDate < sameDate)) + #expect(firstDate < laterDate) + #expect(!(laterDate < firstDate)) + #expect(!(firstDate > sameDate)) + #expect(!(firstDate > laterDate)) + #expect(laterDate > firstDate) +} + class DateUtilTest: XCTestCase { func buildDate(year: Int = 0, month: Int = 0, @@ -27,24 +46,6 @@ class DateUtilTest: XCTestCase { return calendar.date(from: dateComponents)! } - func testDateComparison() { - let firstDate = Date() - let sameDate = Date(timeIntervalSinceReferenceDate: firstDate.timeIntervalSinceReferenceDate) - let laterDate = Date(timeIntervalSinceReferenceDate: firstDate.timeIntervalSinceReferenceDate + 1) - - XCTAssertEqual(firstDate.timeIntervalSinceReferenceDate, sameDate.timeIntervalSinceReferenceDate) - XCTAssertNotEqual(firstDate.timeIntervalSinceReferenceDate, laterDate.timeIntervalSinceReferenceDate) - XCTAssertEqual(firstDate, sameDate) - XCTAssertNotEqual(firstDate, laterDate) - XCTAssertTrue(firstDate.timeIntervalSinceReferenceDate < laterDate.timeIntervalSinceReferenceDate) - XCTAssertFalse(firstDate.isBefore(sameDate)) - XCTAssertTrue(firstDate.isBefore(laterDate)) - XCTAssertFalse(laterDate.isBefore(firstDate)) - XCTAssertFalse(firstDate.isAfter(sameDate)) - XCTAssertFalse(firstDate.isAfter(laterDate)) - XCTAssertTrue(laterDate.isAfter(firstDate)) - } - func testDateComparators() { // Use a specific reference date to make this test deterministic, // and to avoid failing around midnight, new year's, etc. diff --git a/SignalServiceKit/DebugLogs/DebugLogger.swift b/SignalServiceKit/DebugLogs/DebugLogger.swift index bbd8e103d2..6c96af8e0d 100644 --- a/SignalServiceKit/DebugLogs/DebugLogger.swift +++ b/SignalServiceKit/DebugLogs/DebugLogger.swift @@ -35,7 +35,7 @@ private final class DebugLogFileManager: DDLogFileManagerDefault { // retrieving last modification date didn't throw but didn't return NSDate type continue } - if lastModified.isAfter(cutoffDate) { + if lastModified > cutoffDate { // Still within the window. continue } diff --git a/SignalServiceKit/MessageBackup/Attachments/BackupAttachmentDownloadManager.swift b/SignalServiceKit/MessageBackup/Attachments/BackupAttachmentDownloadManager.swift index e530ee6d26..0b3b908e69 100644 --- a/SignalServiceKit/MessageBackup/Attachments/BackupAttachmentDownloadManager.swift +++ b/SignalServiceKit/MessageBackup/Attachments/BackupAttachmentDownloadManager.swift @@ -821,11 +821,11 @@ public class BackupAttachmentDownloadManagerImpl: BackupAttachmentDownloadManage let shouldStoreAllMediaLocally = backupAttachmentDownloadStore .getShouldStoreAllMediaLocally(tx: tx) + let now = dateProvider() let isRecent: Bool if let attachmentTimestamp { // We're "recent" if our newest owning message wouldn't have expired off the queue. - isRecent = dateProvider().ows_millisecondsSince1970 - attachmentTimestamp - <= remoteConfigProvider.currentConfig().messageQueueTimeMs + isRecent = now.ows_millisecondsSince1970 - attachmentTimestamp <= remoteConfigProvider.currentConfig().messageQueueTimeMs } else { // If we don't have a timestamp, its a wallpaper and we should always pass // the recency check. @@ -843,9 +843,7 @@ public class BackupAttachmentDownloadManagerImpl: BackupAttachmentDownloadManage // Download if the upload was < 45 days old, // otherwise don't bother trying automatically. // (The user could still try a manual download later). - canDownloadTransitTierFullsize = Date(millisecondsSince1970: timestampForComparison) - .addingTimeInterval(45 * .day) - .isAfter(dateProvider()) + canDownloadTransitTierFullsize = Date(millisecondsSince1970: timestampForComparison).addingTimeInterval(45 * .day) > now } else { canDownloadTransitTierFullsize = false } diff --git a/SignalServiceKit/Messages/Interactions/MentionFinder.swift b/SignalServiceKit/Messages/Interactions/MentionFinder.swift index f7d220245f..0aec10ae55 100644 --- a/SignalServiceKit/Messages/Interactions/MentionFinder.swift +++ b/SignalServiceKit/Messages/Interactions/MentionFinder.swift @@ -122,7 +122,7 @@ public class MentionFinder { return false } - guard !mention.creationDate.isAfter(thresholdDate) else { + guard mention.creationDate <= thresholdDate else { Logger.info("Skipping orphan mention due to age: \(mention.creationDate.timeIntervalSinceNow)") return false } diff --git a/SignalServiceKit/Messages/OWSMessageDecrypter.swift b/SignalServiceKit/Messages/OWSMessageDecrypter.swift index 89ff437a0b..83364a7e0e 100644 --- a/SignalServiceKit/Messages/OWSMessageDecrypter.swift +++ b/SignalServiceKit/Messages/OWSMessageDecrypter.swift @@ -701,7 +701,7 @@ public class OWSMessageDecrypter { // about 5 seconds of leeway sufficient. let latestAcceptableFireDate = expirationDate.addingTimeInterval(5) - if latestAcceptableFireDate.isBefore(fireDate) { + if latestAcceptableFireDate < fireDate { placeholderCleanupTimer = Timer.scheduledTimer( withTimeInterval: expirationDate.timeIntervalSinceNow, repeats: false, diff --git a/SignalServiceKit/Messages/Reactions/ReactionManager.swift b/SignalServiceKit/Messages/Reactions/ReactionManager.swift index 6cf79c22c5..3bc097d42e 100644 --- a/SignalServiceKit/Messages/Reactions/ReactionManager.swift +++ b/SignalServiceKit/Messages/Reactions/ReactionManager.swift @@ -271,7 +271,7 @@ public class ReactionManager: NSObject { } let creationDate = Date(millisecondsSince1970: reaction.sentAtTimestamp) - guard !creationDate.isAfter(thresholdDate) else { + guard creationDate <= thresholdDate else { Logger.info("Skipping orphan reaction due to age: \(creationDate.timeIntervalSinceNow)") return false } diff --git a/SignalServiceKit/Util/Date+SSK.swift b/SignalServiceKit/Util/Date+SSK.swift index 9b9a49e247..bfaa37c670 100644 --- a/SignalServiceKit/Util/Date+SSK.swift +++ b/SignalServiceKit/Util/Date+SSK.swift @@ -84,18 +84,10 @@ public extension Date { return result } - func isBefore(_ date: Date) -> Bool { - self < date - } - var isBeforeNow: Bool { self < Date() } - func isAfter(_ date: Date) -> Bool { - self > date - } - var isAfterNow: Bool { self > Date() } diff --git a/SignalServiceKit/Util/OWSFileSystem.swift b/SignalServiceKit/Util/OWSFileSystem.swift index 81553bb25a..98f5280c1d 100644 --- a/SignalServiceKit/Util/OWSFileSystem.swift +++ b/SignalServiceKit/Util/OWSFileSystem.swift @@ -66,7 +66,7 @@ private func ClearOldTemporaryDirectoriesSync() { Logger.error("failed to get a modification date for file or directory at: \(filePath)") continue } - if mtime.isAfter(thresholdDate) { + if mtime > thresholdDate { continue } } catch { diff --git a/SignalUI/RecipientPickers/ConversationItem.swift b/SignalUI/RecipientPickers/ConversationItem.swift index d034c215cd..c8c9b6310e 100644 --- a/SignalUI/RecipientPickers/ConversationItem.swift +++ b/SignalUI/RecipientPickers/ConversationItem.swift @@ -317,8 +317,8 @@ public struct StoryConversationItem { if (lhs.0 as? TSPrivateStoryThread)?.isMyStory == true { return true } if (rhs.0 as? TSPrivateStoryThread)?.isMyStory == true { return false } if let priorityDateThreshold = prioritizeThreadsCreatedAfter { - let lhsCreatedAfterThreshold = lhs.0.creationDate?.isAfter(priorityDateThreshold) ?? false - let rhsCreatedAfterThreshold = rhs.0.creationDate?.isAfter(priorityDateThreshold) ?? false + let lhsCreatedAfterThreshold = lhs.0.creationDate != nil && lhs.0.creationDate! > priorityDateThreshold + let rhsCreatedAfterThreshold = rhs.0.creationDate != nil && rhs.0.creationDate! > priorityDateThreshold if lhsCreatedAfterThreshold != rhsCreatedAfterThreshold { return lhsCreatedAfterThreshold }