Skip to content
Closed
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
4 changes: 4 additions & 0 deletions Networking/Networking/Mapper/NoteListMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ struct NoteListMapper: Mapper {
/// (Attempts) to convert an instance of Data into an array of Note Entities.
///
func map(response: Data) throws -> [Note] {
let jsonString = String(data: response, encoding: .utf8)
print("==== raw json =====")
print(jsonString)
print("//// raw json /////")
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(DateFormatter.Defaults.dateTimeFormatter)

Expand Down
2 changes: 1 addition & 1 deletion Networking/Networking/Model/NoteBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extension NoteBlock: Decodable {

/// Decodable Initializer.
///
public init(from decoder: Decoder) throws {
public init(from decoder: Decoder) throws {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

let container = try decoder.container(keyedBy: CodingKeys.self)

let media = container.failsafeDecodeIfPresent([NoteMedia].self, forKey: .media) ?? []
Expand Down
3 changes: 3 additions & 0 deletions Networking/Networking/Remote/NotificationsRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class NotificationsRemote: Remote {
/// - completion: callback to be executed on completion.
///
public func loadNotes(noteIds: [Int64]? = nil, pageSize: Int? = nil, completion: @escaping ([Note]?, Error?) -> Void) {
print("==== loading note with id====" )
print(noteIds)
print("//// loading note with id====" )
let request = requestForNotifications(fields: .all, noteIds: noteIds, pageSize: pageSize)
let mapper = NoteListMapper()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,9 @@ private extension NotificationDetailsViewController {
/// Dispatches the moderation command (Approve/Unapprove, Spam, Trash) to the backend
///
func moderateComment(siteID: Int, commentID: Int, doneStatus: CommentStatus, undoStatus: CommentStatus) {
guard let undo = moderateCommentAction(siteID: siteID, commentID: commentID, status: undoStatus, onCompletion: { (error) in
guard let undo = moderateCommentAction(siteID: siteID, commentID: commentID, status: undoStatus, onCompletion: { [weak self] error in
guard let error = error else {
self?.invalidateNotificationCache()
WooAnalytics.shared.track(.notificationReviewActionSuccess)
return
}
Expand All @@ -390,8 +391,9 @@ private extension NotificationDetailsViewController {
return
}

guard let done = moderateCommentAction(siteID: siteID, commentID: commentID, status: doneStatus, onCompletion: { (error) in
guard let done = moderateCommentAction(siteID: siteID, commentID: commentID, status: doneStatus, onCompletion: { [weak self] error in
guard let error = error else {
self?.invalidateNotificationCache()
WooAnalytics.shared.track(.notificationReviewActionSuccess)
NotificationDetailsViewController.displayModerationCompleteNotice(newStatus: doneStatus, onUndoAction: {
WooAnalytics.shared.track(.notificationReviewActionUndo)
Expand Down Expand Up @@ -467,6 +469,32 @@ private extension NotificationDetailsViewController {
}
return action
}

func invalidateNotificationCache() {
let noteID = note.noteId
print("==== starting invalidation sequence for note ", noteID)
print("==== body ", note.body)

let group = DispatchGroup()

group.enter()
let action = NotificationAction.invalidateCache(noteId: noteID) {
group.leave()
}
StoresManager.shared.dispatch(action)

group.enter()
let syncNoteAction = NotificationAction.synchronizeNotification(noteId: noteID) { error in
group.leave()
}

StoresManager.shared.dispatch(syncNoteAction)

group.notify(queue: .main) {
print("===== clear and update completed ")
//onCompletion()
}
}
}


Expand Down
4 changes: 4 additions & 0 deletions Yosemite/Yosemite/Actions/NotificationAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public enum NotificationAction: Action {
///
case updateMultipleReadStatus(noteIds: [Int64], read: Bool, onCompletion: (Error?) -> Void)
case updateLocalDeletedStatus(noteId: Int64, deleteInProgress: Bool, onCompletion: (Error?) -> Void)

/// Invalidates the cache for a given Notification.
///
case invalidateCache(noteId: Int64, onCompletion: () -> Void)
}
20 changes: 19 additions & 1 deletion Yosemite/Yosemite/Stores/NotificationStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class NotificationStore: Store {
updateReadStatus(for: noteIds, read: read, onCompletion: onCompletion)
case .updateLocalDeletedStatus(let noteId, let deleteInProgress, let onCompletion):
updateDeletedStatus(noteId: noteId, deleteInProgress: deleteInProgress, onCompletion: onCompletion)
case .invalidateCache(let noteId, let onCompletion):
invalidateCache(noteId: noteId, onCompletion: onCompletion)
}
}
}
Expand Down Expand Up @@ -127,6 +129,9 @@ private extension NotificationStore {
/// - onCompletion: Closure to be executed on completion.
///
func synchronizeNotification(with noteId: Int64, onCompletion: @escaping (Error?) -> Void) {
print("==== synchronize notification ===")
print("starting sync with id ", noteId)
print("//// synchronize notification ===")
let remote = NotificationsRemote(network: network)

remote.loadNotes(noteIds: [noteId]) { notes, error in
Expand All @@ -135,6 +140,7 @@ private extension NotificationStore {
return
}

print("ready to update local notes ", notes)
self.updateLocalNotes(with: notes) {
onCompletion(nil)
}
Expand Down Expand Up @@ -192,6 +198,12 @@ private extension NotificationStore {
onCompletion(nil)
}
}

func invalidateCache(noteId: Int64, onCompletion: @escaping () -> Void) {
invalidateCache(for: [noteId]) {
onCompletion()
}
}
}


Expand Down Expand Up @@ -304,7 +316,13 @@ extension NotificationStore {
let derivedStorage = type(of: self).sharedDerivedStorage(with: storageManager)

derivedStorage.perform {
print("===== notifications to invalidate ids =====")
print(noteIds)
print("///// notifications to invalidate ids ====")
let notifications = noteIds.compactMap { derivedStorage.loadNotification(noteID: $0) }
print("===== notifications to invalidate =====")
print(notifications)
print("///// notifications to invalidate ====")
for note in notifications {
note.noteHash = Int64.min
}
Expand All @@ -314,7 +332,7 @@ extension NotificationStore {
guard let onCompletion = onCompletion else {
return
}

print("***** invalidation completed ====")
DispatchQueue.main.async(execute: onCompletion)
}
}
Expand Down