Skip to content

Instantly share code, notes, and snippets.

@iSevenDays
Last active October 12, 2021 10:40
Show Gist options
  • Select an option

  • Save iSevenDays/8a0d58dee7e7bcb5b04d3e9d54b57a49 to your computer and use it in GitHub Desktop.

Select an option

Save iSevenDays/8a0d58dee7e7bcb5b04d3e9d54b57a49 to your computer and use it in GitHub Desktop.

Revisions

  1. iSevenDays revised this gist Oct 12, 2021. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions ViewModelClosable.swift
    Original 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)
    }
    ZStack {
    }.onAppear {
    self.onAppearSetupCloseCallback(closableViewModel: &self.bussinessLogic.correctionDoseData)
    }
    }
    }
  2. iSevenDays revised this gist Oct 12, 2021. 1 changed file with 10 additions and 5 deletions.
    15 changes: 10 additions & 5 deletions ViewModelClosable.swift
    Original 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>(closableSource: inout Closable) -> some View {
    closableSource.closeCancellable = closableSource.shouldCloseView.sink(receiveValue: { shouldClose -> Void in
    @inlinable func onAppearSetupCloseCallback<Closable: ViewModelClosableProtocol>(closableViewModel: inout Closable) {
    closableViewModel.closeCancellable = closableViewModel.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
    .onAppear {
    self.onAppearSetupCloseCallback(closableViewModel: &self.bussinessLogic.correctionDoseData)
    }
    }
    }
  3. iSevenDays created this gist Oct 12, 2021.
    38 changes: 38 additions & 0 deletions ViewModelClosable.swift
    Original 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
    }
    }