-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/confirmationdialog' into develop
- Loading branch information
Showing
5 changed files
with
251 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
code/frontend/MakeItSo/Shared/Extensions/View+ConfirmationDialog.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// View+ConfirmationDialog.swift | ||
// MakeItSo (iOS) | ||
// | ||
// Created by Peter Friese on 23.11.21. | ||
// Copyright © 2021 Google LLC. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import SwiftUI | ||
|
||
struct ConfirmationDialog: ViewModifier { | ||
@Environment(\.dismiss) private var dismiss | ||
@State private var presentingConfirmationDialog: Bool = false | ||
|
||
var isModified: Bool | ||
var onCancel: (() -> Void)? | ||
var onCommit: () -> Void | ||
|
||
private func doCancel() { | ||
onCancel?() | ||
dismiss() | ||
} | ||
|
||
private func doCommit() { | ||
onCommit() | ||
dismiss() | ||
} | ||
|
||
func body(content: Content) -> some View { | ||
NavigationView { | ||
content | ||
.toolbar { | ||
ToolbarItem(placement: .cancellationAction) { | ||
Button("Cancel", role: .cancel) { | ||
if isModified { | ||
presentingConfirmationDialog.toggle() | ||
} | ||
else { | ||
doCancel() | ||
} | ||
} | ||
} | ||
ToolbarItem(placement: .confirmationAction) { | ||
Button("Done", action: doCommit) | ||
} | ||
} | ||
.confirmationDialog("", isPresented: $presentingConfirmationDialog) { | ||
Button("Discard Changes", role: .destructive, action: doCancel) | ||
Button("Cancel", role: .cancel, action: { }) | ||
} | ||
} | ||
// Option 1: use a closure to handle the attempt to dismiss | ||
// .interactiveDismissDisabled(isModified) { | ||
// presentingConfirmationDialog.toggle() | ||
// } | ||
// Option 2: bind attempt to dismiss to a boolean state variable that drives the UI | ||
.interactiveDismissDisabled(isModified, attemptToDismiss: $presentingConfirmationDialog) | ||
} | ||
} | ||
|
||
extension View { | ||
func confirmationDialog(isModified: Bool, onCancel: (() -> Void)? = nil, onCommit: @escaping () -> Void) -> some View { | ||
self.modifier(ConfirmationDialog(isModified: isModified, onCancel: onCancel, onCommit: onCommit)) | ||
} | ||
} | ||
|
141 changes: 141 additions & 0 deletions
141
code/frontend/MakeItSo/Shared/Extensions/View+InteractiveDismissDisable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// | ||
// View+InteractiveDismissDisable.swift | ||
// MakeItSo (iOS) | ||
// | ||
// Created by Peter Friese on 25.11.21. | ||
// Copyright © 2021 Google LLC. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import SwiftUI | ||
|
||
extension View { | ||
public func interactiveDismissDisabled(_ isDisabled: Bool = true, onAttemptToDismiss: (() -> Void)? = nil) -> some View { | ||
InteractiveDismissableView(view: self, isDisabled: isDisabled, onAttemptToDismiss: onAttemptToDismiss) | ||
} | ||
|
||
public func interactiveDismissDisabled(_ isDisabled: Bool = true, attemptToDismiss: Binding<Bool>) -> some View { | ||
InteractiveDismissableView(view: self, isDisabled: isDisabled) { | ||
attemptToDismiss.wrappedValue.toggle() | ||
} | ||
} | ||
} | ||
|
||
private struct InteractiveDismissableView<T: View>: UIViewControllerRepresentable { | ||
let view: T | ||
let isDisabled: Bool | ||
let onAttemptToDismiss: (() -> Void)? | ||
|
||
func makeUIViewController(context: Context) -> UIHostingController<T> { | ||
UIHostingController(rootView: view) | ||
} | ||
|
||
func updateUIViewController(_ uiViewController: UIHostingController<T>, context: Context) { | ||
context.coordinator.dismissableView = self | ||
uiViewController.rootView = view | ||
uiViewController.parent?.presentationController?.delegate = context.coordinator | ||
} | ||
|
||
func makeCoordinator() -> Coordinator { | ||
Coordinator(self) | ||
} | ||
|
||
class Coordinator: NSObject, UIAdaptivePresentationControllerDelegate { | ||
var dismissableView: InteractiveDismissableView | ||
|
||
init(_ dismissableView: InteractiveDismissableView) { | ||
self.dismissableView = dismissableView | ||
} | ||
|
||
func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool { | ||
!dismissableView.isDisabled | ||
} | ||
|
||
func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController) { | ||
dismissableView.onAttemptToDismiss?() | ||
} | ||
} | ||
} | ||
|
||
struct ContentView: View { | ||
@State var showingSheet = false | ||
@State var name: String = "Johnny Appleseed" | ||
var body: some View { | ||
Form { | ||
Section("User Profile") { | ||
Text(name) | ||
} | ||
Button("Edit", action: { showingSheet.toggle() }) | ||
} | ||
.sheet(isPresented: $showingSheet) { | ||
EditView(name: $name) | ||
} | ||
} | ||
} | ||
|
||
private class ViewModel: ObservableObject { | ||
@Published var name: String | ||
private var original: String | ||
|
||
var isModified: Bool { | ||
print("\(name) - \(original)") | ||
return name != original | ||
} | ||
|
||
init(name: String) { | ||
self.name = name | ||
self.original = name | ||
} | ||
} | ||
|
||
private struct EditView: View { | ||
@Environment(\.dismiss) var dismiss | ||
@Binding var name: String | ||
|
||
@StateObject private var viewModel: ViewModel | ||
@State var showingConfirmationDialog = false | ||
|
||
init(name: Binding<String>) { | ||
self._name = name | ||
self._viewModel = StateObject(wrappedValue: ViewModel(name: name.wrappedValue)) | ||
} | ||
|
||
var body: some View { | ||
NavigationView { | ||
Form { | ||
TextField("Enter your name", text: $viewModel.name) | ||
} | ||
.navigationTitle("Edit") | ||
.navigationBarTitleDisplayMode(.inline) | ||
} | ||
.interactiveDismissDisabled(viewModel.isModified) { | ||
showingConfirmationDialog.toggle() | ||
} | ||
.confirmationDialog("", isPresented: $showingConfirmationDialog) { | ||
Button("Save") { | ||
name = viewModel.name | ||
dismiss() | ||
} | ||
Button("Discard", role: .destructive) { | ||
dismiss() | ||
} | ||
Button("Cancel", role: .cancel) { } | ||
} | ||
} | ||
} | ||
|
||
struct ContentView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
ContentView() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters