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 isLoggingIn: Bool = false
@Published var hasNetworkConnection: Bool = false @Published var hasNetworkConnection: Bool = false
@Published var selectedPost: WFAPost? @Published var selectedPost: WFAPost?
@Published var isPresentingDeleteAlert: Bool = false
@Published var postToDelete: WFAPost?
#if os(iOS) #if os(iOS)
@Published var isPresentingSettingsView: Bool = false @Published var isPresentingSettingsView: Bool = false
#endif #endif

View File

@ -13,6 +13,24 @@ struct ContentView: View {
.foregroundColor(.secondary) .foregroundColor(.secondary)
} }
.environmentObject(model) .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) #if os(iOS)
EmptyView() EmptyView()

View File

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

View File

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