|
| 1 | +// |
| 2 | +// AnyViewController.swift |
| 3 | +// ViewController |
| 4 | +// |
| 5 | +// Created by Helge Heß. |
| 6 | +// Copyright © 2022 ZeeZide GmbH. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Combine |
| 10 | +import SwiftUI |
| 11 | + |
| 12 | +/** |
| 13 | + * A type erased version of a (statically typed) ``ViewController``. |
| 14 | + * |
| 15 | + * If possible type erasure should be avoided. |
| 16 | + * |
| 17 | + * When a ``ViewController`` is pushed into the environment, it is pushed |
| 18 | + * as an `@EnvironmentObject` of its concrete type, but also as an |
| 19 | + * ``AnyViewController``. This allows access of all common methods |
| 20 | + * (e.g. ``ViewController/dismiss``). |
| 21 | + * |
| 22 | + * Example access: |
| 23 | + * ```swift |
| 24 | + * struct TitleLabel: View { |
| 25 | + * |
| 26 | + * @EnvironmentObject private var viewController : AnyViewController |
| 27 | + * |
| 28 | + * var body: some View { |
| 29 | + * Text(verbatim: viewController.title) |
| 30 | + * .font(.title) |
| 31 | + * } |
| 32 | + * } |
| 33 | + * ``` |
| 34 | + */ |
| 35 | +public final class AnyViewController: ViewController { |
| 36 | + |
| 37 | + public var id : ObjectIdentifier { ObjectIdentifier(viewController) } |
| 38 | + |
| 39 | + public let viewController : _ViewController |
| 40 | + private var subscription : AnyCancellable? |
| 41 | + |
| 42 | + @usableFromInline |
| 43 | + init<VC>(_ viewController: VC) where VC: ViewController { |
| 44 | + assert(!(viewController is AnyViewController), |
| 45 | + "Attempt to nest an AnyVC into another \(viewController)") |
| 46 | + |
| 47 | + self.viewController = viewController |
| 48 | + |
| 49 | + subscription = viewController.objectWillChange.sink { [weak self] _ in |
| 50 | + self?.objectWillChange.send() |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * An initializer that avoids nesting `AnyViewController`s into themselves. |
| 56 | + */ |
| 57 | + @usableFromInline |
| 58 | + init(_ viewController: AnyViewController) { |
| 59 | + self.viewController = viewController.viewController |
| 60 | + |
| 61 | + // TBD: Can't unwrap this? |
| 62 | + subscription = viewController.objectWillChange.sink { |
| 63 | + [weak self] _ in |
| 64 | + self?.objectWillChange.send() |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + // MARK: - All the any |
| 70 | + // Those are typed erased by the base protocol already (_ViewController). |
| 71 | + |
| 72 | + @inlinable |
| 73 | + public var contentView : AnyView { anyContentView } |
| 74 | + @inlinable |
| 75 | + public var anyContentView : AnyView { viewController.anyContentView } |
| 76 | + @inlinable |
| 77 | + public var controlledContentView : AnyView { anyControlledContentView } |
| 78 | + @inlinable |
| 79 | + public var anyControlledContentView : AnyView { |
| 80 | + viewController.anyControlledContentView |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + // MARK: - Titles |
| 85 | + |
| 86 | + @inlinable |
| 87 | + public var title : String? { |
| 88 | + set { viewController.title = newValue } |
| 89 | + get { viewController.title } |
| 90 | + } |
| 91 | + |
| 92 | + @inlinable |
| 93 | + public var navigationTitle : String { viewController.navigationTitle } |
| 94 | + |
| 95 | + |
| 96 | + // MARK: - Represented Object |
| 97 | + |
| 98 | + @inlinable |
| 99 | + public var representedObject : Any? { |
| 100 | + set { anyRepresentedObject = newValue } |
| 101 | + get { anyRepresentedObject } |
| 102 | + } |
| 103 | + @inlinable |
| 104 | + public var anyRepresentedObject : Any? { |
| 105 | + set { viewController.anyRepresentedObject = newValue } |
| 106 | + get { viewController.anyRepresentedObject } |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + // MARK: - Presentation |
| 111 | + |
| 112 | + @inlinable |
| 113 | + public var presentedViewController : _ViewController? { |
| 114 | + get { viewController.presentedViewController } |
| 115 | + } |
| 116 | + @inlinable |
| 117 | + public var activePresentations : [ ViewControllerPresentation ] { |
| 118 | + set { viewController.activePresentations = newValue } |
| 119 | + get { viewController.activePresentations } |
| 120 | + } |
| 121 | + @inlinable |
| 122 | + public var presentingViewController : _ViewController? { |
| 123 | + set { viewController.presentingViewController = newValue } |
| 124 | + get { viewController.presentingViewController } |
| 125 | + } |
| 126 | + |
| 127 | + @inlinable |
| 128 | + public func willAppear() { viewController.willAppear() } |
| 129 | + @inlinable |
| 130 | + public func willDisappear() { viewController.willDisappear() } |
| 131 | + |
| 132 | + @inlinable |
| 133 | + public func present<VC>(_ viewController: VC, |
| 134 | + mode: ViewControllerPresentationMode) |
| 135 | + where VC: ViewController |
| 136 | + { |
| 137 | + self.viewController.present(viewController, mode: mode) |
| 138 | + } |
| 139 | + @inlinable |
| 140 | + public func present<VC: ViewController>(_ viewController: VC) { |
| 141 | + self.viewController.present(viewController) |
| 142 | + } |
| 143 | + @inlinable |
| 144 | + public func dismiss() { viewController.dismiss() } // TBD: really unwrap? |
| 145 | + |
| 146 | + |
| 147 | + @inlinable |
| 148 | + public func show<VC: ViewController>(_ viewController: VC) { |
| 149 | + self.viewController.show(viewController) |
| 150 | + } |
| 151 | + |
| 152 | + @inlinable |
| 153 | + public func showDetail<VC: ViewController>(_ viewController: VC){ |
| 154 | + self.viewController.showDetail(viewController) |
| 155 | + } |
| 156 | + |
| 157 | + @inlinable |
| 158 | + public func show<VC, OwnerVC>(_ viewController: VC, in owner: OwnerVC) |
| 159 | + where VC: ViewController, OwnerVC: _ViewController |
| 160 | + { |
| 161 | + self.viewController.show(viewController, in: owner) |
| 162 | + } |
| 163 | + @inlinable |
| 164 | + public func showDetail<VC, OwnerVC>(_ viewController: VC, in owner: OwnerVC) |
| 165 | + where VC: ViewController, OwnerVC: _ViewController |
| 166 | + { |
| 167 | + self.viewController.showDetail(viewController, in: owner) |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + // MARK: - Hierarchy |
| 172 | + |
| 173 | + @inlinable |
| 174 | + public var children : [ _ViewController ] { |
| 175 | + set { viewController.children = newValue } |
| 176 | + get { viewController.children } |
| 177 | + } |
| 178 | + |
| 179 | + @inlinable |
| 180 | + public var parent : _ViewController? { |
| 181 | + set { viewController.parent = newValue } |
| 182 | + get { viewController.parent } |
| 183 | + } |
| 184 | + |
| 185 | + @inlinable |
| 186 | + public func willMove(toParent parent: _ViewController?) { |
| 187 | + viewController.willMove(toParent: parent) |
| 188 | + } |
| 189 | + |
| 190 | + @inlinable |
| 191 | + public func didMove(toParent parent: _ViewController?) { |
| 192 | + viewController.didMove(toParent: parent) |
| 193 | + } |
| 194 | + |
| 195 | + @inlinable |
| 196 | + public func addChild<VC: ViewController>(_ viewController: VC) { |
| 197 | + self.viewController.addChild(viewController) |
| 198 | + } |
| 199 | + @inlinable |
| 200 | + public func removeFromParent() { |
| 201 | + viewController.removeFromParent() // TBD: really unwrap? |
| 202 | + } |
| 203 | + |
| 204 | + |
| 205 | + // MARK: - Better Description |
| 206 | + |
| 207 | + @inlinable |
| 208 | + public var description: String { "<Any: \(viewController)>" } |
| 209 | + @inlinable |
| 210 | + public func appendAttributes(to description: inout String) {} |
| 211 | +} |
0 commit comments