Show message if remote copy was delete from server

This commit is contained in:
Angelo Stavrow 2020-09-17 14:33:21 -04:00
parent 2caa1e80b0
commit 8addc1fc5d
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
2 changed files with 59 additions and 5 deletions

View File

@ -244,20 +244,19 @@ private extension WriteFreelyModel {
func fetchUserPostsHandler(result: Result<[WFPost], Error>) {
do {
var postsToDelete = posts.userPosts
let fetchedPosts = try result.get()
for fetchedPost in fetchedPosts {
// For each fetched post, we
// 1. check to see if a matching post exists
if let managedPost = posts.userPosts.first(where: { $0.postId == fetchedPost.postId }) {
// If it exists, we set the hasNewerRemoteCopy flag as appropriate.
managedPost.wasDeletedFromServer = false
if let fetchedPostUpdatedDate = fetchedPost.updatedDate,
let localPostUpdatedDate = managedPost.updatedDate {
managedPost.hasNewerRemoteCopy = fetchedPostUpdatedDate > localPostUpdatedDate
} else {
print("Error: could not determine which copy of post is newer")
}
postsToDelete.removeAll(where: { $0.postId == fetchedPost.postId })
} else {
// If it doesn't exist, we create the managed object.
let managedPost = WFAPost(context: LocalStorageManager.persistentContainer.viewContext)
managedPost.postId = fetchedPost.postId
managedPost.slug = fetchedPost.slug
@ -270,8 +269,12 @@ private extension WriteFreelyModel {
managedPost.body = fetchedPost.body
managedPost.collectionAlias = fetchedPost.collectionAlias
managedPost.status = PostStatus.published.rawValue
managedPost.wasDeletedFromServer = false
}
}
for post in postsToDelete {
post.wasDeletedFromServer = true
}
DispatchQueue.main.async {
LocalStorageManager().saveContext()
self.posts.loadCachedPosts()
@ -377,7 +380,6 @@ private extension WriteFreelyModel {
var secItem: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &secItem)
guard status != errSecItemNotFound else {
return nil
}

View File

@ -3,6 +3,7 @@ import SwiftUI
struct PostEditorStatusToolbarView: View {
#if os(iOS)
@Environment(\.horizontalSizeClass) var horizontalSizeClass
@Environment(\.presentationMode) var presentationMode
#endif
@EnvironmentObject var model: WriteFreelyModel
@ -55,6 +56,57 @@ struct PostEditorStatusToolbarView: View {
})
}
#endif
} else if post.wasDeletedFromServer {
#if os(iOS)
if horizontalSizeClass == .compact {
VStack {
PostStatusBadgeView(post: post)
HStack {
Text("‼️ Post deleted from server. Delete local copy?")
.font(.caption)
.foregroundColor(.secondary)
Button(action: {
self.presentationMode.wrappedValue.dismiss()
model.selectedPost = nil
model.posts.remove(post)
}, label: {
Image(systemName: "trash")
})
}
.padding(.bottom)
}
.padding(.top)
} else {
HStack {
PostStatusBadgeView(post: post)
.padding(.trailing)
Text("‼️ Post deleted from server. Delete local copy?")
.font(.callout)
.foregroundColor(.secondary)
Button(action: {
self.presentationMode.wrappedValue.dismiss()
model.selectedPost = nil
model.posts.remove(post)
}, label: {
Image(systemName: "trash")
})
}
}
#else
HStack {
PostStatusBadgeView(post: post)
.padding(.trailing)
Text("‼️ Post deleted from server. Delete local copy?")
.font(.callout)
.foregroundColor(.secondary)
Button(action: {
model.selectedPost = nil
model.posts.remove(post)
}, label: {
Image(systemName: "trash")
})
}
#endif
} else {
PostStatusBadgeView(post: post)
}