Skip to content

Commit fe59937

Browse files
author
darya-gurinovich
authored
Merge pull request #6 from shortcut/feature/modal-view-customisation
Added a posibility to change the modal view options
2 parents 90514ab + cdd1820 commit fe59937

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

Sources/ShortcutSwiftUI/SwiftUI/ModalView/ModalPresenter.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protocol ModalPresenter {
5151
func closeModal()
5252
}
5353

54-
public class ModalViewRouter<PresentationState: ModalPresentationState>: ObservableObject, ModalPresenter {
54+
open class ModalViewRouter<PresentationState: ModalPresentationState>: ObservableObject, ModalPresenter {
5555
@Published var customSheetPresentationState: PresentationState? {
5656
didSet {
5757
handlePresentationStateChange(oldValue: oldValue, newValue: customSheetPresentationState)

Sources/ShortcutSwiftUI/SwiftUI/ModalView/ModalViewPresenterViewModifier.swift

+29-5
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ import SwiftUI
1212
struct ModalViewPresenterViewModifier<PresentationState: ModalPresentationState>: ViewModifier {
1313
@EnvironmentObject var modalViewRouter: ModalViewRouter<PresentationState>
1414

15-
private let options: [BottomSheet.Options]
15+
@Binding private var options: [BottomSheet.Options]
1616

1717
init(options: [BottomSheet.Options] = []) {
18-
self.options = options
18+
self._options = .constant(options)
19+
}
20+
21+
init(options: Binding<[BottomSheet.Options]>) {
22+
self._options = options
1923
}
2024

2125
public func body(content: Content) -> some View {
@@ -44,8 +48,26 @@ public extension View {
4448
/// Works with the implementation of the `ModalPresentationState` protocol that represents all modals that can be shown with this presenter.
4549
/// Need to set an environmentObject of `ModalViewRouter<S: ModalPresentationState>` before using this modifier otherwise an error will occur.
4650
///
51+
/// - parameters:
52+
/// - presentationStateType: A type that represent possible states to show
53+
/// - options: Modal view options to customise it
54+
///
55+
func modalViewPresenter<PresentationState: ModalPresentationState>(presentationStateType: PresentationState.Type,
56+
options: [BottomSheet.Options] = []) -> some View {
57+
self.modifier(ModalViewPresenterViewModifier<PresentationState>(options: options))
58+
}
59+
60+
/// A view modifier to present modal views from any view.
61+
///
62+
/// Works with the implementation of the `ModalPresentationState` protocol that represents all modals that can be shown with this presenter.
63+
/// Need to set an environmentObject of `ModalViewRouter<S: ModalPresentationState>` before using this modifier otherwise an error will occur.
64+
///
65+
/// - parameters:
66+
/// - presentationStateType: A type that represent possible states to show
67+
/// - options: A options binding to allow to change the modal options
68+
///
4769
func modalViewPresenter<PresentationState: ModalPresentationState>(presentationStateType: PresentationState.Type,
48-
options: [BottomSheet.Options]) -> some View {
70+
options: Binding<[BottomSheet.Options]>) -> some View {
4971
self.modifier(ModalViewPresenterViewModifier<PresentationState>(options: options))
5072
}
5173
}
@@ -61,6 +83,7 @@ struct ModalViewPresenterViewModifier_Previews: PreviewProvider {
6183
@StateObject private var testModalViewRouter = TestModalViewRouter()
6284

6385
@State var color: Color = .red
86+
@State var options: [BottomSheet.Options] = [.disableSwipeToDismiss, .tapToDismiss]
6487

6588
var body: some View {
6689
ZStack {
@@ -70,12 +93,13 @@ struct ModalViewPresenterViewModifier_Previews: PreviewProvider {
7093
testModalViewRouter.setModal(state: .text("Hello World!"),
7194
type: .customSheet) {
7295
color = [Color.red, .purple, .blue, .yellow, .green].randomElement() ?? .orange
96+
97+
options = [.tapToDismiss]
7398
}
7499
}
75100
}
76101
.modalViewPresenter(presentationStateType: TestModalPresentationState.self,
77-
options: [.tapToDismiss,
78-
.maxHeight(500)])
102+
options: $options)
79103
.environmentObject(testModalViewRouter)
80104
}
81105
}

0 commit comments

Comments
 (0)