From 4dfc6a39038ab70805d5f3bda1f7f9cf8ecc36ed Mon Sep 17 00:00:00 2001 From: Ryan Nystrom Date: Sat, 29 Sep 2018 13:59:01 -0400 Subject: [PATCH 1/2] Use updatedAt time to key notification send times --- Classes/Systems/LocalNotificationsCache.swift | 5 +++- .../LocalNotificationCacheTests.swift | 29 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Classes/Systems/LocalNotificationsCache.swift b/Classes/Systems/LocalNotificationsCache.swift index 83124ed31..e684d7669 100644 --- a/Classes/Systems/LocalNotificationsCache.swift +++ b/Classes/Systems/LocalNotificationsCache.swift @@ -43,7 +43,10 @@ final class LocalNotificationsCache { let idCol = "id" var map = [String: V3Notification]() - notifications.forEach { map[$0.id] = $0 } + notifications.forEach { + let key = "\($0.id)-\($0.updatedAt.timeIntervalSinceNow)" + map[key] = $0 + } let apiIDs = map.keys.map { $0 } do { diff --git a/FreetimeTests/LocalNotificationCacheTests.swift b/FreetimeTests/LocalNotificationCacheTests.swift index a20e51d84..fa06ebdcb 100644 --- a/FreetimeTests/LocalNotificationCacheTests.swift +++ b/FreetimeTests/LocalNotificationCacheTests.swift @@ -58,7 +58,7 @@ class LocalNotificationCacheTests: XCTestCase { ) } - func makeNotificaction(id: String, title: String) -> V3Notification { + func makeNotificaction(id: String, title: String, updatedAt: Date = Date()) -> V3Notification { return V3Notification( id: id, lastReadAt: nil, @@ -66,7 +66,7 @@ class LocalNotificationCacheTests: XCTestCase { repository: makeRepo(), subject: V3NotificationSubject(title: title, type: .issue, url: nil), unread: true, - updatedAt: Date() + updatedAt: updatedAt ) } @@ -105,5 +105,30 @@ class LocalNotificationCacheTests: XCTestCase { XCTAssertEqual(executions, 3) } + + func test_whenUpdatingWithSameNotificationWithUpdatedTime_thatNotificationReceived() { + clear(for: #function) + + let cache = LocalNotificationsCache(path: path(for: #function)) + let n1 = [ + makeNotificaction(id: "123", title: "foo", updatedAt: Date()) + ] + + cache.update(notifications: n1) { results in + XCTAssertEqual(results.count, 1) + } + + cache.update(notifications: n1) { results in + XCTAssertEqual(results.count, 0) + } + + let n2 = [ + makeNotificaction(id: "123", title: "foo", updatedAt: Date(timeIntervalSinceNow: 10)) + ] + + cache.update(notifications: n2) { results in + XCTAssertEqual(results.count, 1) + } + } } From 84788b42a9e62eee1ace46fa12893f1a3b5b1625 Mon Sep 17 00:00:00 2001 From: Ryan Nystrom Date: Sat, 29 Sep 2018 14:06:15 -0400 Subject: [PATCH 2/2] better formatting and fix tests --- Classes/Systems/LocalNotificationsCache.swift | 2 +- FreetimeTests/LocalNotificationCacheTests.swift | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Classes/Systems/LocalNotificationsCache.swift b/Classes/Systems/LocalNotificationsCache.swift index e684d7669..3e47d5a37 100644 --- a/Classes/Systems/LocalNotificationsCache.swift +++ b/Classes/Systems/LocalNotificationsCache.swift @@ -44,7 +44,7 @@ final class LocalNotificationsCache { var map = [String: V3Notification]() notifications.forEach { - let key = "\($0.id)-\($0.updatedAt.timeIntervalSinceNow)" + let key = "\($0.id)-\($0.updatedAt.timeIntervalSince1970)" map[key] = $0 } let apiIDs = map.keys.map { $0 } diff --git a/FreetimeTests/LocalNotificationCacheTests.swift b/FreetimeTests/LocalNotificationCacheTests.swift index fa06ebdcb..8ee3f1799 100644 --- a/FreetimeTests/LocalNotificationCacheTests.swift +++ b/FreetimeTests/LocalNotificationCacheTests.swift @@ -58,7 +58,7 @@ class LocalNotificationCacheTests: XCTestCase { ) } - func makeNotificaction(id: String, title: String, updatedAt: Date = Date()) -> V3Notification { + func makeNotificaction(id: String, title: String, updatedAt: Date) -> V3Notification { return V3Notification( id: id, lastReadAt: nil, @@ -74,8 +74,9 @@ class LocalNotificationCacheTests: XCTestCase { clear(for: #function) let cache = LocalNotificationsCache(path: path(for: #function)) + let date = Date() let n1 = [ - makeNotificaction(id: "123", title: "foo") + makeNotificaction(id: "123", title: "foo", updatedAt: date) ] var executions = 0 @@ -85,8 +86,8 @@ class LocalNotificationCacheTests: XCTestCase { } let n2 = [ - makeNotificaction(id: "123", title: "foo"), - makeNotificaction(id: "456", title: "bar") + makeNotificaction(id: "123", title: "foo", updatedAt: date), + makeNotificaction(id: "456", title: "bar", updatedAt: date) ] cache.update(notifications: n2) { results in executions += 1 @@ -94,8 +95,8 @@ class LocalNotificationCacheTests: XCTestCase { } let n3 = [ - makeNotificaction(id: "123", title: "foo"), - makeNotificaction(id: "456", title: "bar") + makeNotificaction(id: "123", title: "foo", updatedAt: date), + makeNotificaction(id: "456", title: "bar", updatedAt: date) ] cache.update(notifications: n3) { results in