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 characters
| struct ContentView_Succeed: View { | |
| @State var dismissed = false | |
| var body: some View { | |
| Button(action: { self.dismissed = true }) { | |
| Text("Dismiss Button") | |
| } | |
| .dismissed(dismissed) | |
| } | |
| } |
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 characters
| extension View { | |
| func dismissed( | |
| _ dismissed: Bool, | |
| animated: Bool = true, | |
| completion: @escaping() -> Void = {} | |
| ) -> some View { | |
| modifier(DismissModifier(dismissed: dismissed, animated: animated, completion: completion)) | |
| } | |
| } |
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 characters
| struct DismissModifier: ViewModifier { | |
| let dismissed: Bool | |
| let animated: Bool | |
| let completion: () -> Void | |
| func body(content: Content) -> some View { | |
| content.overlay( | |
| DismissController(dismissed: dismissed, animated: animated, completion: completion) | |
| .frame(width: 0, height: 0) | |
| ) | |
| } |
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 characters
| struct DismissController: UIViewControllerRepresentable { | |
| let dismissed: Bool | |
| let animated: Bool | |
| let completion: () -> Void | |
| func makeUIViewController(context _: Context) -> UIViewController { | |
| let vc = UIViewController() | |
| vc.view.isHidden = true | |
| return vc | |
| } | |
| func updateUIViewController( |
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 characters
| import SwiftUI | |
| struct ContentView_Failed: View { | |
| @Environment(\.presentationMode) var presentationMode | |
| var body: some View { | |
| Button(action: { self.presentationMode.wrappedValue.dismiss() }) { | |
| Text("Dismiss Button") | |
| } | |
| } | |
| } | |
| struct ContentView_Failed_Previews: PreviewProvider { |
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 characters
| struct SampleView_General: View { | |
| @ObservedObject private var viewModel: ObservableSampleViewModel | |
| init(viewModel: SampleViewModelType) { | |
| self.viewModel = .init(viewModel) | |
| } | |
| var body: some View { | |
| Button(action: viewModel.inputs.buttonTapped) { | |
| Text(viewModel.outputs.title) | |
| } |
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 characters
| // ObservableViewModel.swift | |
| final class ObservableViewModel<ViewModelInputs, ViewModelOutputs>: ObservableObject { | |
| let inputs: ViewModelInputs | |
| let outputs: ObservableOutputs<ViewModelOutputs> | |
| private var cancellables = Set<AnyCancellable>() | |
| init(inputs: ViewModelInputs, outputs: ViewModelOutputs) { | |
| self.inputs = inputs | |
| self.outputs = .init(outputs) | |
| self.outputs.objectWillChange |
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 characters
| struct SampleView: View { | |
| @ObservedObject private var viewModel: ViewModel | |
| init(viewModel: SampleViewModelType) { | |
| self.viewModel = .init(viewModel) | |
| } | |
| var body: some View { | |
| Button(action: viewModel.inputs.buttonTapped) { | |
| Text(viewModel.outputs.title) | |
| } |
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 characters
| // SampleView.swift | |
| // Usage: | |
| // UIHostingController(rootView: SampleView(viewModel: viewModel)) | |
| struct SampleView: View { | |
| private let viewModel: SampleViewModelType | |
| private let disposeBag = DisposeBag() | |
| init(viewModel: SampleViewModelType) { | |
| self.viewModel = viewModel | |
| bindViewModel() |
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 characters
| // SampleViewController.swift | |
| final class SampleViewController: UIViewController { | |
| private let viewModel: SampleViewModelType | |
| init(viewModel: SampleViewModelType) { | |
| self.viewModel = viewModel | |
| super.init(nibName: nil, bundle: nil) | |
| } | |
| private let disposeBag = DisposeBag() | |
| @IBAction let button: UIButton! | |
| override func viewDidLoad() { |
NewerOlder