Skip to content

Commit

Permalink
✨ Implement custom version of interactiveDismissDisabled
Browse files Browse the repository at this point in the history
This enables us to detect when the user wants to dismiss the sheet, and ask them if they want to discard their changes.

Signed-off-by: Peter Friese <[email protected]>
  • Loading branch information
peterfriese committed Nov 25, 2021
1 parent f53652a commit d8756cf
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 37 deletions.
4 changes: 4 additions & 0 deletions code/frontend/MakeItSo/MakeItSo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
88E7B7BE274CF30A00AF477D /* ReminderDetailsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88E7B7BD274CF30A00AF477D /* ReminderDetailsView.swift */; };
88E7B7BF274CF30A00AF477D /* ReminderDetailsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88E7B7BD274CF30A00AF477D /* ReminderDetailsView.swift */; };
88FA998D274D63A400670474 /* View+ConfirmationDialog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88FA998C274D63A400670474 /* View+ConfirmationDialog.swift */; };
88FA99922750090200670474 /* View+InteractiveDismissDisable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88FA99912750090200670474 /* View+InteractiveDismissDisable.swift */; };
88FEECDA27275ABD00ED368C /* MakeItSoApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88FEECCA27275ABC00ED368C /* MakeItSoApp.swift */; };
88FEECDB27275ABD00ED368C /* MakeItSoApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88FEECCA27275ABC00ED368C /* MakeItSoApp.swift */; };
88FEECDE27275ABD00ED368C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 88FEECCC27275ABD00ED368C /* Assets.xcassets */; };
Expand All @@ -35,6 +36,7 @@
88C30CD5274D1B4500E6694D /* ReminderDetailsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReminderDetailsViewModel.swift; sourceTree = "<group>"; };
88E7B7BD274CF30A00AF477D /* ReminderDetailsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReminderDetailsView.swift; sourceTree = "<group>"; };
88FA998C274D63A400670474 /* View+ConfirmationDialog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+ConfirmationDialog.swift"; sourceTree = "<group>"; };
88FA99912750090200670474 /* View+InteractiveDismissDisable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+InteractiveDismissDisable.swift"; sourceTree = "<group>"; };
88FEECCA27275ABC00ED368C /* MakeItSoApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MakeItSoApp.swift; sourceTree = "<group>"; };
88FEECCC27275ABD00ED368C /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
88FEECD127275ABD00ED368C /* MakeItSo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MakeItSo.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -69,6 +71,7 @@
children = (
881EF5BC272DC399004761E5 /* View+Focus.swift */,
88FA998C274D63A400670474 /* View+ConfirmationDialog.swift */,
88FA99912750090200670474 /* View+InteractiveDismissDisable.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -277,6 +280,7 @@
88FEECF02727FEFF00ED368C /* Reminder.swift in Sources */,
88FA998D274D63A400670474 /* View+ConfirmationDialog.swift in Sources */,
88FEECFA27280F3D00ED368C /* ReminderListRowView.swift in Sources */,
88FA99922750090200670474 /* View+InteractiveDismissDisable.swift in Sources */,
887B6FAD273ED4180028263D /* EmptyStateView.swift in Sources */,
88E7B7BE274CF30A00AF477D /* ReminderDetailsView.swift in Sources */,
88C30CD6274D1B4500E6694D /* ReminderDetailsViewModel.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,34 @@ struct ConfirmationDialog: ViewModifier {
}

func body(content: Content) -> some View {
content
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel", role: .cancel) {
if isModified {
presentingConfirmationDialog.toggle()
}
else {
doCancel()
NavigationView {
content
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel", role: .cancel) {
if isModified {
presentingConfirmationDialog.toggle()
}
else {
doCancel()
}
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Done", action: doCommit)
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Done", action: doCommit)
.confirmationDialog("", isPresented: $presentingConfirmationDialog) {
Button("Discard Changes", role: .destructive, action: doCancel)
Button("Cancel", role: .cancel, action: { })
}
}
.interactiveDismissDisabled(isModified)
.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)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// 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
//
// http: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 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?()
}
}
}

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()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,27 @@ struct ReminderDetailsView: View {
}

var body: some View {
NavigationView {
Form {
Section {
TextField("Title", text: $viewModel.reminder.title)
}
Section {
Toggle(isOn: $viewModel.reminder.flagged) {
HStack {
Image(systemName: "flag.fill")
.frame(width: 26, height: 26, alignment: .center)
.background(.orange)
.foregroundColor(.white)
.cornerRadius(4)
Text("Flag")
}
Form {
Section {
TextField("Title", text: $viewModel.reminder.title)
}
Section {
Toggle(isOn: $viewModel.reminder.flagged) {
HStack {
Image(systemName: "flag.fill")
.frame(width: 26, height: 26, alignment: .center)
.background(.orange)
.foregroundColor(.white)
.cornerRadius(4)
Text("Flag")
}
}
}
.navigationTitle("Details")
.navigationBarTitleDisplayMode(.inline)
.confirmationDialog(isModified: viewModel.isModified) {
onCommit(viewModel.reminder)
}
}
.navigationTitle("Details")
.navigationBarTitleDisplayMode(.inline)
.confirmationDialog(isModified: viewModel.isModified) {
onCommit(viewModel.reminder)
}
}
}
Expand Down

0 comments on commit d8756cf

Please sign in to comment.