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

Use updatedAt time to key notification send times #2201

Merged
merged 2 commits into from
Sep 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Classes/Systems/LocalNotificationsCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
40 changes: 33 additions & 7 deletions FreetimeTests/LocalNotificationCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,25 @@ 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,
reason: .assign,
repository: makeRepo(),
subject: V3NotificationSubject(title: title, type: .issue, url: nil),
unread: true,
updatedAt: Date()
updatedAt: updatedAt
)
}

func test_whenUpdating_thatReadFirstTime_thenSecondCallEmpty() {
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

Expand All @@ -85,17 +86,17 @@ 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
XCTAssertEqual(results.count, 1)
}

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
Expand All @@ -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)
}
}

}