Skip to content

Commit 48f478a

Browse files
committed
NotificationStore: Optimistic Mark as Read
1 parent a2b2a47 commit 48f478a

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

Yosemite/Yosemite/Stores/NotificationStore.swift

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,18 @@ private extension NotificationStore {
123123
func updateReadStatus(for noteIds: [Int64], read: Bool, onCompletion: @escaping (Error?) -> Void) {
124124
let remote = NotificationsRemote(network: network)
125125

126-
remote.updateReadStatus(noteIds: noteIds, read: read) { [weak self] (error) in
127-
guard let `self` = self, error == nil else {
128-
onCompletion(error)
126+
remote.updateReadStatus(noteIds: noteIds, read: read) { error in
127+
guard let error = error else {
128+
onCompletion(nil)
129129
return
130130
}
131131

132-
self.updateLocalNoteReadStatus(for: noteIds, read: read) {
133-
onCompletion(nil)
132+
self.invalidateCache(for: noteIds) {
133+
onCompletion(error)
134134
}
135135
}
136+
137+
updateLocalNoteReadStatus(for: noteIds, read: read)
136138
}
137139
}
138140

@@ -172,7 +174,7 @@ extension NotificationStore {
172174
/// - remoteNotes: Collection of Notes
173175
/// - completion: Callback to be executed on completion
174176
///
175-
func updateLocalNotes(with remoteNotes: [Note], completion: (() -> Void)? = nil) {
177+
func updateLocalNotes(with remoteNotes: [Note], onCompletion: (() -> Void)? = nil) {
176178
let derivedStorage = type(of: self).sharedDerivedStorage(with: storageManager)
177179

178180
derivedStorage.perform {
@@ -183,15 +185,17 @@ extension NotificationStore {
183185
}
184186

185187
storageManager.saveDerivedType(derivedStorage: derivedStorage) {
186-
DispatchQueue.main.async {
187-
completion?()
188+
guard let onCompletion = onCompletion else {
189+
return
188190
}
191+
192+
DispatchQueue.main.async(execute: onCompletion)
189193
}
190194
}
191195

192196
/// Updates the read status for the specified Notifications. The callback happens on the Main Thread.
193197
///
194-
func updateLocalNoteReadStatus(for noteIDs: [Int64], read: Bool, completion: (() -> Void)? = nil) {
198+
func updateLocalNoteReadStatus(for noteIDs: [Int64], read: Bool, onCompletion: (() -> Void)? = nil) {
195199
let derivedStorage = type(of: self).sharedDerivedStorage(with: storageManager)
196200

197201
derivedStorage.perform {
@@ -202,9 +206,11 @@ extension NotificationStore {
202206
}
203207

204208
storageManager.saveDerivedType(derivedStorage: derivedStorage) {
205-
DispatchQueue.main.async {
206-
completion?()
209+
guard let onCompletion = onCompletion else {
210+
return
207211
}
212+
213+
DispatchQueue.main.async(execute: onCompletion)
208214
}
209215
}
210216

@@ -235,6 +241,27 @@ extension NotificationStore {
235241
}
236242
}
237243
}
244+
245+
///
246+
///
247+
func invalidateCache(for noteIds: [Int64], onCompletion: (() -> Void)? = nil) {
248+
let derivedStorage = type(of: self).sharedDerivedStorage(with: storageManager)
249+
250+
derivedStorage.perform {
251+
let notifications = noteIds.compactMap { derivedStorage.loadNotification(noteID: $0) }
252+
for note in notifications {
253+
note.noteHash = Int64.min
254+
}
255+
}
256+
257+
storageManager.saveDerivedType(derivedStorage: derivedStorage) {
258+
guard let onCompletion = onCompletion else {
259+
return
260+
}
261+
262+
DispatchQueue.main.async(execute: onCompletion)
263+
}
264+
}
238265
}
239266

240267

0 commit comments

Comments
 (0)