Skip to content

Commit

Permalink
✨ Implement confirmation dialog
Browse files Browse the repository at this point in the history
Show a confirmation dialog when the user tries to leave the reminder details dialog after modifying the reminder.

This resolves #70

Signed-off-by: Peter Friese <[email protected]>
  • Loading branch information
peterfriese committed Nov 23, 2021
1 parent 3a84b6d commit 6d60c42
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ import Combine

class ReminderDetailsViewModel: ObservableObject {
@Published var reminder: Reminder
private var original: Reminder

init(reminder: Reminder) {
self.reminder = reminder
original = reminder
}

var isModified: Bool {
original != reminder
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,26 @@ struct ReminderDetailsView: View {
@Environment(\.dismiss) private var dismiss
@ObservedObject private var viewModel: ReminderDetailsViewModel

private var onCancel: (() -> Void)?
private var onCommit: (Reminder) -> Void

init(reminder: Reminder, onCommit: @escaping (Reminder) -> Void) {
@State private var presentingConfirmationDialog: Bool = false

init(reminder: Reminder, onCancel: (() -> Void)? = nil, onCommit: @escaping (Reminder) -> Void) {
self.viewModel = ReminderDetailsViewModel(reminder: reminder)
self.onCommit = onCommit
}

func doCancel() {
onCancel?()
dismiss()
}

func doCommit() {
onCommit(viewModel.reminder)
dismiss()
}

var body: some View {
NavigationView {
Form {
Expand All @@ -54,16 +67,23 @@ struct ReminderDetailsView: View {
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel", role: .cancel) {
dismiss()
if viewModel.isModified {
presentingConfirmationDialog.toggle()
}
else {
doCancel()
}
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Done") {
onCommit(viewModel.reminder)
dismiss()
}
Button("Done", action: doCommit)
}
}
.interactiveDismissDisabled(viewModel.isModified)
.confirmationDialog("", isPresented: $presentingConfirmationDialog) {
Button("Discard Changes", role: .destructive, action: doCancel)
Button("Cancel", role: .cancel, action: { })
}
}
}
}
Expand Down

0 comments on commit 6d60c42

Please sign in to comment.