|
| 1 | +// |
| 2 | +// DeprecatedFlows.swift |
| 3 | +// RxFlow |
| 4 | +// |
| 5 | +// Created by Thibault Wittemberg on 2020-05-16. |
| 6 | +// Copyright © 2020 RxSwiftCommunity. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#if canImport(UIKit) |
| 10 | + |
| 11 | +import RxRelay |
| 12 | +import RxSwift |
| 13 | +import UIKit |
| 14 | + |
| 15 | +@available(*, deprecated, message: "You should use Flows.use()") |
| 16 | +public extension Flows { |
| 17 | + /// Allow to be triggered only when Flows given as parameters are ready to be displayed. |
| 18 | + /// Once it is the case, the block is executed |
| 19 | + /// |
| 20 | + /// - Parameters: |
| 21 | + /// - flows: Flow(s) to be observed |
| 22 | + /// - block: block to execute whenever the Flows are ready to use |
| 23 | + @available(*, deprecated, message: "You should use Flows.use()") |
| 24 | + static func whenReady<RootType: UIViewController>(flows: [Flow], |
| 25 | + block: @escaping ([RootType]) -> Void) { |
| 26 | + let flowsReadinesses = flows.map { $0.rxFlowReady } |
| 27 | + let roots = flows.compactMap { $0.root as? RootType } |
| 28 | + guard roots.count == flows.count else { |
| 29 | + fatalError("Type mismatch, Flows roots types do not match the types awaited in the block") |
| 30 | + } |
| 31 | + |
| 32 | + _ = Single.zip(flowsReadinesses) { _ in Void() } |
| 33 | + .asDriver(onErrorJustReturn: Void()) |
| 34 | + .drive(onNext: { _ in |
| 35 | + block(roots) |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + // swiftlint:disable function_parameter_count |
| 40 | + /// Allow to be triggered only when Flows given as parameters are ready to be displayed. |
| 41 | + /// Once it is the case, the block is executed |
| 42 | + /// |
| 43 | + /// - Parameters: |
| 44 | + /// - flow1: first Flow to be observed |
| 45 | + /// - flow2: second Flow to be observed |
| 46 | + /// - flow3: third Flow to be observed |
| 47 | + /// - flow4: fourth Flow to be observed |
| 48 | + /// - flow5: fifth Flow to be observed |
| 49 | + /// - block: block to execute whenever the Flows are ready to use |
| 50 | + @available(*, deprecated, message: "You should use Flows.use()") |
| 51 | + static func whenReady<RootType1, RootType2, RootType3, RootType4, RootType5>(flow1: Flow, |
| 52 | + flow2: Flow, |
| 53 | + flow3: Flow, |
| 54 | + flow4: Flow, |
| 55 | + flow5: Flow, |
| 56 | + block: @escaping (_ flow1Root: RootType1, |
| 57 | + _ flow2Root: RootType2, |
| 58 | + _ flow3Root: RootType3, |
| 59 | + _ flow4Root: RootType4, |
| 60 | + _ flow5Root: RootType5) -> Void) |
| 61 | + where |
| 62 | + RootType1: UIViewController, |
| 63 | + RootType2: UIViewController, |
| 64 | + RootType3: UIViewController, |
| 65 | + RootType4: UIViewController, |
| 66 | + RootType5: UIViewController { |
| 67 | + guard |
| 68 | + let root1 = flow1.root as? RootType1, |
| 69 | + let root2 = flow2.root as? RootType2, |
| 70 | + let root3 = flow3.root as? RootType3, |
| 71 | + let root4 = flow4.root as? RootType4, |
| 72 | + let root5 = flow5.root as? RootType5 else { |
| 73 | + fatalError("Type mismatch, Flows roots types do not match the types awaited in the block") |
| 74 | + } |
| 75 | + |
| 76 | + _ = Single.zip(flow1.rxFlowReady, |
| 77 | + flow2.rxFlowReady, |
| 78 | + flow3.rxFlowReady, |
| 79 | + flow4.rxFlowReady, |
| 80 | + flow5.rxFlowReady) { _, _, _, _, _ in Void() } |
| 81 | + .asDriver(onErrorJustReturn: Void()) |
| 82 | + .drive(onNext: { _ in |
| 83 | + block(root1, root2, root3, root4, root5) |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + // swiftlint:enable function_parameter_count |
| 88 | + /// Allow to be triggered only when Flows given as parameters are ready to be displayed. |
| 89 | + /// Once it is the case, the block is executed |
| 90 | + /// |
| 91 | + /// - Parameters: |
| 92 | + /// - flow1: first Flow to be observed |
| 93 | + /// - flow2: second Flow to be observed |
| 94 | + /// - flow3: third Flow to be observed |
| 95 | + /// - flow4: fourth Flow to be observed |
| 96 | + /// - block: block to execute whenever the Flows are ready to use |
| 97 | + @available(*, deprecated, message: "You should use Flows.use()") |
| 98 | + static func whenReady<RootType1, RootType2, RootType3, RootType4>(flow1: Flow, |
| 99 | + flow2: Flow, |
| 100 | + flow3: Flow, |
| 101 | + flow4: Flow, |
| 102 | + block: @escaping (_ flow1Root: RootType1, |
| 103 | + _ flow2Root: RootType2, |
| 104 | + _ flow3Root: RootType3, |
| 105 | + _ flow4Root: RootType4) -> Void) |
| 106 | + where |
| 107 | + RootType1: UIViewController, |
| 108 | + RootType2: UIViewController, |
| 109 | + RootType3: UIViewController, |
| 110 | + RootType4: UIViewController { |
| 111 | + guard |
| 112 | + let root1 = flow1.root as? RootType1, |
| 113 | + let root2 = flow2.root as? RootType2, |
| 114 | + let root3 = flow3.root as? RootType3, |
| 115 | + let root4 = flow4.root as? RootType4 else { |
| 116 | + fatalError("Type mismatch, Flows roots types do not match the types awaited in the block") |
| 117 | + } |
| 118 | + |
| 119 | + _ = Single.zip(flow1.rxFlowReady, |
| 120 | + flow2.rxFlowReady, |
| 121 | + flow3.rxFlowReady, |
| 122 | + flow4.rxFlowReady) { _, _, _, _ in Void() |
| 123 | + } |
| 124 | + .asDriver(onErrorJustReturn: Void()) |
| 125 | + .drive(onNext: { _ in |
| 126 | + block(root1, root2, root3, root4) |
| 127 | + }) |
| 128 | + } |
| 129 | + |
| 130 | + /// Allow to be triggered only when Flows given as parameters are ready to be displayed. |
| 131 | + /// Once it is the case, the block is executed |
| 132 | + /// |
| 133 | + /// - Parameters: |
| 134 | + /// - flow1: first Flow to be observed |
| 135 | + /// - flow2: second Flow to be observed |
| 136 | + /// - flow3: third Flow to be observed |
| 137 | + /// - block: block to execute whenever the Flows are ready to use |
| 138 | + @available(*, deprecated, message: "You should use Flows.use()") |
| 139 | + static func whenReady<RootType1, RootType2, RootType3>(flow1: Flow, |
| 140 | + flow2: Flow, |
| 141 | + flow3: Flow, |
| 142 | + block: @escaping (_ flow1Root: RootType1, |
| 143 | + _ flow2Root: RootType2, |
| 144 | + _ flow3Root: RootType3) -> Void) |
| 145 | + where |
| 146 | + RootType1: UIViewController, |
| 147 | + RootType2: UIViewController, |
| 148 | + RootType3: UIViewController { |
| 149 | + guard |
| 150 | + let root1 = flow1.root as? RootType1, |
| 151 | + let root2 = flow2.root as? RootType2, |
| 152 | + let root3 = flow3.root as? RootType3 else { |
| 153 | + fatalError("Type mismatch, Flows roots types do not match the types awaited in the block") |
| 154 | + } |
| 155 | + |
| 156 | + _ = Single.zip(flow1.rxFlowReady, |
| 157 | + flow2.rxFlowReady, |
| 158 | + flow3.rxFlowReady) { _, _, _ in Void() } |
| 159 | + .asDriver(onErrorJustReturn: Void()) |
| 160 | + .drive(onNext: { _ in |
| 161 | + block(root1, root2, root3) |
| 162 | + }) |
| 163 | + } |
| 164 | + |
| 165 | + /// Allow to be triggered only when Flows given as parameters are ready to be displayed. |
| 166 | + /// Once it is the case, the block is executed |
| 167 | + /// |
| 168 | + /// - Parameters: |
| 169 | + /// - flow1: first Flow to be observed |
| 170 | + /// - flow2: second Flow to be observed |
| 171 | + /// - block: block to execute whenever the Flows are ready to use |
| 172 | + @available(*, deprecated, message: "You should use Flows.use()") |
| 173 | + static func whenReady<RootType1: UIViewController, RootType2: UIViewController>(flow1: Flow, |
| 174 | + flow2: Flow, |
| 175 | + block: @escaping (_ flow1Root: RootType1, |
| 176 | + _ flow2Root: RootType2) -> Void) { |
| 177 | + guard let root1 = flow1.root as? RootType1, |
| 178 | + let root2 = flow2.root as? RootType2 else { |
| 179 | + fatalError("Type mismatch, Flows root types do not match the types awaited in the block") |
| 180 | + } |
| 181 | + |
| 182 | + _ = Single.zip(flow1.rxFlowReady, |
| 183 | + flow2.rxFlowReady) { _, _ in Void() } |
| 184 | + .asDriver(onErrorJustReturn: Void()) |
| 185 | + .drive(onNext: { _ in |
| 186 | + block(root1, root2) |
| 187 | + }) |
| 188 | + } |
| 189 | + |
| 190 | + /// Allow to be triggered only when Flow given as parameters is ready to be displayed. |
| 191 | + /// Once it is the case, the block is executed |
| 192 | + /// |
| 193 | + /// - Parameters: |
| 194 | + /// - flow1: Flow to be observed |
| 195 | + /// - block: block to execute whenever the Flow is ready to use |
| 196 | + @available(*, deprecated, message: "You should use Flows.use()") |
| 197 | + static func whenReady<RootType: UIViewController>(flow1: Flow, |
| 198 | + block: @escaping (_ flowRoot1: RootType) -> Void) { |
| 199 | + guard let root = flow1.root as? RootType else { |
| 200 | + fatalError("Type mismatch, Flow root type does not match the type awaited in the block") |
| 201 | + } |
| 202 | + |
| 203 | + _ = flow1 |
| 204 | + .rxFlowReady |
| 205 | + .asDriver(onErrorJustReturn: true) |
| 206 | + .drive(onNext: { _ in |
| 207 | + block(root) |
| 208 | + }) |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +#endif |
0 commit comments