Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[interop-abandon] - support workflow abandon with interop and navcontroller #214

Merged
merged 1 commit into from Aug 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions Sources/SwiftCurrent_SwiftUI/Views/ViewControllerWrapper.swift
Expand Up @@ -9,6 +9,7 @@

#if (os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)) && canImport(UIKit)
import SwiftUI
import Combine
import SwiftCurrent

/// A wrapper for exposing `UIViewController`s that are `FlowRepresentable` to SwiftUI.
Expand All @@ -27,6 +28,7 @@ public struct ViewControllerWrapper<F: FlowRepresentable & UIViewController>: Vi
private var vc: F

@StateObject private var model: Model
@EnvironmentObject private var workflowModel: WorkflowViewModel

public init(with args: F.WorkflowInput) {
let vc = F._factory(F.self, with: args)
Expand All @@ -42,6 +44,16 @@ public struct ViewControllerWrapper<F: FlowRepresentable & UIViewController>: Vi

public func makeUIViewController(context: Context) -> F {
model.vc._workflowPointer = _workflowPointer
workflowModel.onAbandonPublisher
.receive(on: RunLoop.main)
.sink {
if let navController = model.vc.navigationController,
let parent = model.vc.parent {
navController.popToViewController(parent, animated: true)
print("")
}
}
.store(in: &model.subscribers)
return model.vc
}

Expand All @@ -57,6 +69,7 @@ extension ViewControllerWrapper {
@available(iOS 14.0, macOS 11, tvOS 14.0, *)
final class Model: ObservableObject {
var vc: F
var subscribers = Set<AnyCancellable>()

init(vc: F) {
self.vc = vc
Expand Down