Present alert on macOS if using Edit > Delete as confirmation

This commit is contained in:
Angelo Stavrow 2020-09-17 13:23:44 -04:00
parent 3b4f50c601
commit 5af1d38a2d
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
4 changed files with 25 additions and 7 deletions

View File

@ -12,7 +12,8 @@ class WriteFreelyModel: ObservableObject {
@Published var isLoggingIn: Bool = false
@Published var hasNetworkConnection: Bool = false
@Published var selectedPost: WFAPost?
@Published var isPresentingDeleteAlert: Bool = false
@Published var postToDelete: WFAPost?
#if os(iOS)
@Published var isPresentingSettingsView: Bool = false
#endif

View File

@ -13,6 +13,24 @@ struct ContentView: View {
.foregroundColor(.secondary)
}
.environmentObject(model)
.alert(isPresented: $model.isPresentingDeleteAlert) {
Alert(
title: Text("Delete Post?"),
message: Text("This action cannot be undone."),
primaryButton: .destructive(Text("Delete"), action: {
if let postToDelete = model.postToDelete {
model.selectedPost = nil
withAnimation {
model.posts.remove(postToDelete)
}
model.postToDelete = nil
}
}),
secondaryButton: .cancel() {
model.postToDelete = nil
}
)
}
#if os(iOS)
EmptyView()

View File

@ -70,18 +70,17 @@ struct PostListFilteredView: View {
.onDeleteCommand(perform: {
guard let selectedPost = model.selectedPost else { return }
if selectedPost.status == PostStatus.local.rawValue {
delete(selectedPost)
model.postToDelete = selectedPost
model.isPresentingDeleteAlert = true
}
})
#endif
}
func delete(_ post: WFAPost) {
withAnimation {
model.posts.remove(post)
}
}
}
struct PostListFilteredView_Previews: PreviewProvider {
static var previews: some View {

View File

@ -7,12 +7,12 @@
<key>WriteFreely-MultiPlatform (iOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>1</integer>
</dict>
<key>WriteFreely-MultiPlatform (macOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>0</integer>
</dict>
</dict>
</dict>