Last active
October 12, 2021 10:40
-
-
Save iSevenDays/8a0d58dee7e7bcb5b04d3e9d54b57a49 to your computer and use it in GitHub Desktop.
Revisions
-
iSevenDays revised this gist
Oct 12, 2021 . 1 changed file with 4 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -34,10 +34,9 @@ public extension View where Self: ViewWithPresentationMode { var SomeView: View, ViewWithPresentationMode { var body: some View { ZStack { }.onAppear { self.onAppearSetupCloseCallback(closableViewModel: &self.bussinessLogic.correctionDoseData) } } } -
iSevenDays revised this gist
Oct 12, 2021 . 1 changed file with 10 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,7 @@ import Foundation import Combine import SwiftUI /// View Model closable protocol - conform to this protocol if your view model should emit close block to close the screen that uses this view model public protocol ViewModelClosableProtocol { var closeCancellable: AnyCancellable? { get set } @@ -19,20 +23,21 @@ public protocol ViewWithPresentationMode { public extension View where Self: ViewWithPresentationMode { @inlinable func onAppearSetupCloseCallback<Closable: ViewModelClosableProtocol>(closableViewModel: inout Closable) { closableViewModel.closeCancellable = closableViewModel.shouldCloseView.sink(receiveValue: { shouldClose -> Void in if shouldClose { presentationMode.wrappedValue.dismiss() } }) } } var SomeView: View, ViewWithPresentationMode { var body: some View { ZStack { } .onAppear { self.onAppearSetupCloseCallback(closableViewModel: &self.bussinessLogic.correctionDoseData) } } } -
iSevenDays created this gist
Oct 12, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ /// View Model closable protocol - conform to this protocol if your view model should emit close block to close the screen that uses this view model public protocol ViewModelClosableProtocol { var closeCancellable: AnyCancellable? { get set } // when inheriting protocol, set @Published // var shouldCloseView: Bool { get set } // @Published ~== CurrentValueSubject<Bool, Never>, but we can't use property wrappers in protocol var shouldCloseView: CurrentValueSubject<Bool, Never> { get set } } extension ViewModelClosableProtocol { mutating func closeAction() { shouldCloseView.value = true } } public protocol ViewWithPresentationMode { var presentationMode: Binding<PresentationMode> { get } } public extension View where Self: ViewWithPresentationMode { @inlinable func onAppearSetupCloseCallback<Closable: ViewModelClosableProtocol>(closableSource: inout Closable) -> some View { closableSource.closeCancellable = closableSource.shouldCloseView.sink(receiveValue: { shouldClose -> Void in if shouldClose { presentationMode.wrappedValue.dismiss() } }) return self } } var SomeView: View, ViewWithPresentationMode { var body: some View { ZStack { } .onAppearSetupCloseCallback(closableSource: self.viewModel that conforms to ViewModelClosableProtocol) // TODO: the method above should be fixed } }