diff --git a/Classes/Systems/LocalNotificationsCache.swift b/Classes/Systems/LocalNotificationsCache.swift index 83124ed31..3e47d5a37 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.timeIntervalSince1970)" + map[key] = $0 + } let apiIDs = map.keys.map { $0 } do { diff --git a/FreetimeTests/LocalNotificationCacheTests.swift b/FreetimeTests/LocalNotificationCacheTests.swift index a20e51d84..8ee3f1799 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) -> 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 ) } @@ -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 @@ -105,5 +106,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) + } + } }