Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 7b07463

Browse files
authored
Use updatedAt time to key notification send times (#2201)
* Use updatedAt time to key notification send times * better formatting and fix tests
1 parent 6c6c947 commit 7b07463

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

Classes/Systems/LocalNotificationsCache.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ final class LocalNotificationsCache {
4343
let idCol = "id"
4444

4545
var map = [String: V3Notification]()
46-
notifications.forEach { map[$0.id] = $0 }
46+
notifications.forEach {
47+
let key = "\($0.id)-\($0.updatedAt.timeIntervalSince1970)"
48+
map[key] = $0
49+
}
4750
let apiIDs = map.keys.map { $0 }
4851

4952
do {

FreetimeTests/LocalNotificationCacheTests.swift

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,25 @@ class LocalNotificationCacheTests: XCTestCase {
5858
)
5959
}
6060

61-
func makeNotificaction(id: String, title: String) -> V3Notification {
61+
func makeNotificaction(id: String, title: String, updatedAt: Date) -> V3Notification {
6262
return V3Notification(
6363
id: id,
6464
lastReadAt: nil,
6565
reason: .assign,
6666
repository: makeRepo(),
6767
subject: V3NotificationSubject(title: title, type: .issue, url: nil),
6868
unread: true,
69-
updatedAt: Date()
69+
updatedAt: updatedAt
7070
)
7171
}
7272

7373
func test_whenUpdating_thatReadFirstTime_thenSecondCallEmpty() {
7474
clear(for: #function)
7575

7676
let cache = LocalNotificationsCache(path: path(for: #function))
77+
let date = Date()
7778
let n1 = [
78-
makeNotificaction(id: "123", title: "foo")
79+
makeNotificaction(id: "123", title: "foo", updatedAt: date)
7980
]
8081
var executions = 0
8182

@@ -85,17 +86,17 @@ class LocalNotificationCacheTests: XCTestCase {
8586
}
8687

8788
let n2 = [
88-
makeNotificaction(id: "123", title: "foo"),
89-
makeNotificaction(id: "456", title: "bar")
89+
makeNotificaction(id: "123", title: "foo", updatedAt: date),
90+
makeNotificaction(id: "456", title: "bar", updatedAt: date)
9091
]
9192
cache.update(notifications: n2) { results in
9293
executions += 1
9394
XCTAssertEqual(results.count, 1)
9495
}
9596

9697
let n3 = [
97-
makeNotificaction(id: "123", title: "foo"),
98-
makeNotificaction(id: "456", title: "bar")
98+
makeNotificaction(id: "123", title: "foo", updatedAt: date),
99+
makeNotificaction(id: "456", title: "bar", updatedAt: date)
99100
]
100101

101102
cache.update(notifications: n3) { results in
@@ -105,5 +106,30 @@ class LocalNotificationCacheTests: XCTestCase {
105106

106107
XCTAssertEqual(executions, 3)
107108
}
109+
110+
func test_whenUpdatingWithSameNotificationWithUpdatedTime_thatNotificationReceived() {
111+
clear(for: #function)
112+
113+
let cache = LocalNotificationsCache(path: path(for: #function))
114+
let n1 = [
115+
makeNotificaction(id: "123", title: "foo", updatedAt: Date())
116+
]
117+
118+
cache.update(notifications: n1) { results in
119+
XCTAssertEqual(results.count, 1)
120+
}
121+
122+
cache.update(notifications: n1) { results in
123+
XCTAssertEqual(results.count, 0)
124+
}
125+
126+
let n2 = [
127+
makeNotificaction(id: "123", title: "foo", updatedAt: Date(timeIntervalSinceNow: 10))
128+
]
129+
130+
cache.update(notifications: n2) { results in
131+
XCTAssertEqual(results.count, 1)
132+
}
133+
}
108134

109135
}

0 commit comments

Comments
 (0)