diff --git a/Sources/ScreenNavigatorKit/Internal/Helpers/Binding+when.swift b/Sources/ScreenNavigatorKit/Internal/Helpers/Binding+when.swift new file mode 100644 index 0000000..ac2293b --- /dev/null +++ b/Sources/ScreenNavigatorKit/Internal/Helpers/Binding+when.swift @@ -0,0 +1,21 @@ +// +// Binding+when.swift +// +// +// Created by Ernest Babayan on 12.06.2022. +// + +import SwiftUI + +extension Binding { + func when(_ isTrue: Bool) -> Binding where Value == Wrapped? { + Binding( + get: { + isTrue ? self.wrappedValue : nil + }, + set: { newValue, transaction in + self.transaction(transaction).wrappedValue = newValue + } + ) + } +} diff --git a/Sources/ScreenNavigatorKit/ModalStack/PresentScreen.swift b/Sources/ScreenNavigatorKit/ModalStack/PresentScreen.swift index fc79693..f095cf0 100644 --- a/Sources/ScreenNavigatorKit/ModalStack/PresentScreen.swift +++ b/Sources/ScreenNavigatorKit/ModalStack/PresentScreen.swift @@ -36,11 +36,8 @@ struct PresentScreenView: View { @ObservedObject var screen: PresentScreen var body: some View { - switch screen.presentationStyle { - case .sheet, .none: - content.sheet($screen.presentedView) - case .fullScreenCover: - content.fullScreenCover($screen.presentedView) - } + content + .sheet($screen.presentedView.when(screen.presentationStyle == .sheet)) + .fullScreenCover($screen.presentedView.when(screen.presentationStyle == .fullScreenCover)) } }