Skip to content

Commit 171b1b0

Browse files
kenjitayamamuukii
andauthored
Add param keepViewControllersStackedAbove to fluidPop/removeSelf/removeViewController (#170)
keepViewControllersStackedAbove: if true and if there are view controllers stacked above this view controller, they will be kept --------- Co-authored-by: Muukii <[email protected]>
1 parent 6d0f247 commit 171b1b0

File tree

3 files changed

+60
-32
lines changed

3 files changed

+60
-32
lines changed

Sources/FluidStack/Helper/FluidExtentionViewController.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ extension FluidExtentionViewController {
209209
- Parameters:
210210
- transition: You may set ``AnyRemovingTransition/noAnimation`` to disable animation, nil runs transition given view controller provides (if it's ``FluidTransitionViewController``).
211211
- fowardingToParent: Forwards to parent to pop if current stack do not have view controller to pop. No effects if the current stack prevents it by ``FluidStackController/Configuration-swift.struct/preventsFowardingPop``
212-
212+
- cascadesToChildren: if true and if there are view controllers stacked above this view controller, they will be removed as well
213+
213214
- Warning: To run this method to ``FluidStackController`` does not mean to pop the current top view controller.
214215
A way to pop the top view controller:
215216
```
@@ -220,6 +221,7 @@ extension FluidExtentionViewController {
220221
transition: AnyRemovingTransition? = nil,
221222
transitionForBatch: AnyBatchRemovingTransition? = .crossDissolve,
222223
forwardingToParent: Bool = true,
224+
removingRule: RemovingRule = .cascade,
223225
completion: ((RemovingTransitionContext.CompletionEvent) -> Void)? = nil
224226
) {
225227

@@ -235,7 +237,8 @@ extension FluidExtentionViewController {
235237
_fluidPop(
236238
transition: transition,
237239
transitionForBatch: transitionForBatch,
238-
forwardingToParent: forwardingToParent,
240+
forwardingToParent: forwardingToParent,
241+
removingRule: removingRule,
239242
completion: completion
240243
)
241244

@@ -247,7 +250,8 @@ extension FluidExtentionViewController {
247250
- Parameters:
248251
- transition: You may set ``AnyRemovingTransition/noAnimation`` to disable animation, nil runs transition given view controller provides (if it's ``FluidTransitionViewController``).
249252
- fowardingToParent: Forwards to parent to pop if current stack do not have view controller to pop. No effects if the current stack prevents it by ``FluidStackController/Configuration-swift.struct/preventsFowardingPop``
250-
253+
- cascadesToChildren: if true and if there are view controllers stacked above this view controller, they will be removed as well
254+
251255
- Warning: To run this method to ``FluidStackController`` does not mean to pop the current top view controller.
252256
A way to pop the top view controller:
253257
```
@@ -258,7 +262,8 @@ extension FluidExtentionViewController {
258262
public func fluidPop(
259263
transition: AnyRemovingTransition? = nil,
260264
transitionForBatch: AnyBatchRemovingTransition? = .crossDissolve,
261-
forwardingToParent: Bool = true
265+
forwardingToParent: Bool = true,
266+
removingRule: RemovingRule = .cascade
262267
) async -> RemovingTransitionContext.CompletionEvent {
263268

264269
await withCheckedContinuation { continuation in
@@ -267,6 +272,7 @@ extension FluidExtentionViewController {
267272
transition: transition,
268273
transitionForBatch: transitionForBatch,
269274
forwardingToParent: forwardingToParent,
275+
removingRule: removingRule,
270276
completion: { event in
271277
continuation.resume(returning: event)
272278
})
@@ -278,6 +284,7 @@ extension FluidExtentionViewController {
278284
transition: AnyRemovingTransition?,
279285
transitionForBatch: AnyBatchRemovingTransition?,
280286
forwardingToParent: Bool,
287+
removingRule: RemovingRule,
281288
completion: ((RemovingTransitionContext.CompletionEvent) -> Void)?
282289
) {
283290

@@ -302,13 +309,15 @@ extension FluidExtentionViewController {
302309
transition: transition,
303310
transitionForBatch: transitionForBatch,
304311
forwardingToParent: forwardingToParent,
312+
removingRule: removingRule,
305313
completion: completion
306314
)
307315

308316
} else {
309317

310318
fluidStackContext
311319
.removeSelf(
320+
removingRule: removingRule,
312321
transition: transition,
313322
transitionForBatch: transitionForBatch,
314323
completion: completion

Sources/FluidStack/ViewController/FluidStackContext.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public final class FluidStackContext: Equatable {
4949
/// Removes the target view controller in ``FluidStackController``.
5050
/// - Parameter transition: if not nil, it would be used override parameter.
5151
///
52-
/// See detail in ``FluidStackController/removeViewController(_:transition:)``
52+
/// See detail in ``FluidStackController/removeViewController(_:transition:transitionForBatch:cascadesToChildren:completion:)``
5353
public func removeSelf(
54+
removingRule: RemovingRule = .cascade,
5455
transition: AnyRemovingTransition?,
5556
transitionForBatch: AnyBatchRemovingTransition? = .crossDissolve,
5657
completion: ((RemovingTransitionContext.CompletionEvent) -> Void)? = nil
@@ -60,6 +61,7 @@ public final class FluidStackContext: Equatable {
6061
}
6162
fluidStackController?.removeViewController(
6263
targetViewController,
64+
removingRule: removingRule,
6365
transition: transition,
6466
transitionForBatch: transitionForBatch,
6567
completion: completion

Sources/FluidStack/ViewController/FluidStackController.swift

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ public enum FluidStackAction {
2424
case willBecomeTop
2525
}
2626

27+
public enum RemovingRule {
28+
/// Removes all of view controllers above itself.
29+
case cascade
30+
/// Removes only itself, leaving view controllers above itself.
31+
case single
32+
}
33+
2734
/// A struct that configures how to display in ``FluidStackController``
2835
public struct FluidStackContentConfiguration {
2936

@@ -223,6 +230,7 @@ open class FluidStackController: UIViewController {
223230

224231
removeViewController(
225232
wrapperView.viewController,
233+
removingRule: .cascade,
226234
transition: transition,
227235
completion: completion
228236
)
@@ -556,9 +564,11 @@ open class FluidStackController: UIViewController {
556564
Removes given view controller with transition.
557565

558566
Switches to batch removing if there are multiple view controllers on top of the given view controller.
567+
- Parameters:
559568
*/
560569
public func removeViewController(
561570
_ viewControllerToRemove: UIViewController,
571+
removingRule: RemovingRule = .cascade,
562572
transition: AnyRemovingTransition?,
563573
transitionForBatch: @autoclosure @escaping () -> AnyBatchRemovingTransition? = .crossDissolve,
564574
completion: (@MainActor (RemovingTransitionContext.CompletionEvent) -> Void)? = nil
@@ -578,35 +588,42 @@ open class FluidStackController: UIViewController {
578588
assertionFailure("Not found wrapper view to manage \(viewControllerToRemove)")
579589
return
580590
}
581-
582-
if stackingItems.last?.viewController != viewControllerToRemove {
583-
584-
// Removes view controllers with batch
585-
586-
let transition = transitionForBatch()
587-
588-
Log.debug(
589-
.stack,
590-
"The removing view controller is not displaying on top. it's behind of the other view controllers. Switches to batch-removing using transition: \(transition as Any)"
591-
)
592-
593-
removeAllViewController(
594-
from: viewToRemove.viewController,
595-
transition: transition,
596-
completion: { event in
597-
598-
switch event {
599-
case .succeeded:
600-
completion?(.succeeded)
601-
case .interrupted:
602-
completion?(.interrupted)
591+
592+
switch removingRule {
593+
case .cascade:
594+
595+
if stackingItems.last?.viewController != viewControllerToRemove {
596+
597+
// Removes view controllers with batch
598+
599+
let transition = transitionForBatch()
600+
601+
Log.debug(
602+
.stack,
603+
"The removing view controller is not displaying on top. it's behind of the other view controllers. Switches to batch-removing using transition: \(transition as Any)"
604+
)
605+
606+
removeAllViewController(
607+
from: viewToRemove.viewController,
608+
transition: transition,
609+
completion: { event in
610+
611+
switch event {
612+
case .succeeded:
613+
completion?(.succeeded)
614+
case .interrupted:
615+
completion?(.interrupted)
616+
}
617+
603618
}
604-
605-
}
606-
)
607-
return
619+
)
620+
return
621+
}
622+
623+
case .single:
624+
break
608625
}
609-
626+
610627
// Removes view controller
611628

612629
let transitionContext = _startRemoving(viewToRemove, completion: completion)

0 commit comments

Comments
 (0)