Skip to content

Commit 9b7a6b5

Browse files
authored
fluidPop supports to remove all view controllers from target (#84)
* Patch * Fix test * Update log message
1 parent 08bb975 commit 9b7a6b5

File tree

7 files changed

+219
-151
lines changed

7 files changed

+219
-151
lines changed

Sources/FluidInterfaceKit-Demo/DemoStackingViewController.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ private final class ContentViewController: UIViewController {
268268

269269
UIButton.make(title: "Remove all", color: .white) { [unowned self] in
270270

271-
fluidStackContext?.removeAllViewController(transition: .springFlourish())
271+
fluidStackContext?.removeAllViewController(transition: .springFlourish)
272272

273273
}
274274

@@ -279,6 +279,10 @@ private final class ContentViewController: UIViewController {
279279
UIButton.make(title: "Remove self", color: .white) { [unowned self] in
280280
fluidPop()
281281
}
282+
283+
UIButton.make(title: "Remove self from parent", color: .white) { [unowned self] in
284+
fluidStackContext?.fluidStackController?.viewController(before: self.parent!)?.fluidPop()
285+
}
282286

283287
UIButton.make(title: "Set title", color: .white) { [unowned self] in
284288
self.title = "Fluid!"

Sources/FluidInterfaceKit/Helper/FluidExtentionViewController.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,11 @@ extension FluidExtentionViewController {
105105
*/
106106
public func fluidPop(
107107
transition: AnyRemovingTransition? = nil,
108+
transitionForBatch: AnyBatchRemovingTransition? = .crossDissolve,
108109
forwardingToParent: Bool = true,
109110
completion: ((RemovingTransitionContext.CompletionEvent) -> Void)? = nil
110111
) {
111-
112-
guard next != nil else {
113-
// got the end of tree.
114-
return
115-
}
116-
112+
117113
guard
118114
let fluidStackContext = fluidStackContext,
119115
let _ = fluidStackContext.fluidStackController
@@ -123,22 +119,23 @@ extension FluidExtentionViewController {
123119
return
124120
}
125121

126-
_fluidPop(transition: transition, forwardingToParent: forwardingToParent, completion: completion)
122+
_fluidPop(
123+
transition: transition,
124+
transitionForBatch: transitionForBatch,
125+
forwardingToParent: forwardingToParent,
126+
completion: completion
127+
)
127128

128129
}
129130

130131
private func _fluidPop(
131132
transition: AnyRemovingTransition?,
133+
transitionForBatch: AnyBatchRemovingTransition?,
132134
forwardingToParent: Bool,
133135
completion: ((RemovingTransitionContext.CompletionEvent) -> Void)? = nil
134136
) {
135-
136-
guard next != nil else {
137-
// got the end of tree.
138-
return
139-
}
140-
141-
guard
137+
138+
guard
142139
let fluidStackContext = fluidStackContext,
143140
let stack = fluidStackContext.fluidStackController
144141
else {
@@ -157,16 +154,19 @@ extension FluidExtentionViewController {
157154

158155
stack._fluidPop(
159156
transition: transition,
157+
transitionForBatch: transitionForBatch,
160158
forwardingToParent: forwardingToParent,
161159
completion: completion
162160
)
163161

164162
} else {
165163

166-
fluidStackContext.removeSelf(
167-
transition: transition,
168-
completion: completion
169-
)
164+
fluidStackContext
165+
.removeSelf(
166+
transition: transition,
167+
transitionForBatch: transitionForBatch,
168+
completion: completion
169+
)
170170

171171
}
172172

Sources/FluidInterfaceKit/Transition/AnyBatchRemovingTransition.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extension AnyBatchRemovingTransition {
3131
return disabled
3232
}
3333

34-
public static func vanishing(duration: TimeInterval = 0.6) -> Self {
34+
public static var crossDissolve: Self {
3535

3636
return .init { context in
3737

@@ -42,7 +42,7 @@ extension AnyBatchRemovingTransition {
4242
$0.view.isHidden = true
4343
}
4444

45-
let animator = UIViewPropertyAnimator(duration: duration, dampingRatio: 1) {
45+
let animator = UIViewPropertyAnimator(duration: 0.6, dampingRatio: 1) {
4646

4747
topViewController.view.alpha = 0
4848

@@ -71,7 +71,7 @@ extension AnyBatchRemovingTransition {
7171

7272
}
7373

74-
public static func springFlourish() -> Self {
74+
public static var springFlourish: Self {
7575

7676
return .init { context in
7777

Sources/FluidInterfaceKit/Transition/BatchRemovingTransitionContext.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ public final class BatchRemovingTransitionContext: TransitionContext {
4949
super.init(contentView: contentView)
5050
}
5151

52+
deinit {
53+
assert(
54+
isInvalidated == true || isCompleted == true,
55+
"\(self) is deallocated without appropriate operation. Call `notifyAnimationCompleted()` or `notifyCancelled()`"
56+
)
57+
}
58+
5259
public func notifyCompleted() {
5360
assert(Thread.isMainThread)
5461
isCompleted = true
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import UIKit
2+
3+
/// A context object that communicates with ``FluidStackController``.
4+
/// Associated with the view controller displayed on the stack.
5+
public final class FluidStackContext: Equatable {
6+
7+
public static func == (lhs: FluidStackContext, rhs: FluidStackContext) -> Bool {
8+
lhs === rhs
9+
}
10+
11+
public private(set) weak var fluidStackController: FluidStackController?
12+
public private(set) weak var targetViewController: UIViewController?
13+
14+
init(
15+
fluidStackController: FluidStackController,
16+
targetViewController: UIViewController
17+
) {
18+
self.fluidStackController = fluidStackController
19+
self.targetViewController = targetViewController
20+
}
21+
22+
/**
23+
Adds view controller to parent container if it presents.
24+
*/
25+
public func addContentViewController(
26+
_ viewController: UIViewController,
27+
transition: AnyAddingTransition?,
28+
completion: @escaping (AddingTransitionContext.CompletionEvent) -> Void = { _ in }
29+
) {
30+
fluidStackController?.addContentViewController(
31+
viewController,
32+
transition: transition,
33+
completion: completion
34+
)
35+
}
36+
37+
public func addContentView(
38+
_ view: UIView,
39+
transition: AnyAddingTransition?,
40+
completion: @escaping (AddingTransitionContext.CompletionEvent) -> Void = { _ in }
41+
) {
42+
fluidStackController?.addContentView(
43+
view,
44+
transition: transition,
45+
completion: completion
46+
)
47+
}
48+
49+
/// Removes the target view controller in ``FluidStackController``.
50+
/// - Parameter transition: if not nil, it would be used override parameter.
51+
///
52+
/// See detail in ``FluidStackController/removeViewController(_:transition:)``
53+
public func removeSelf(
54+
transition: AnyRemovingTransition?,
55+
transitionForBatch: AnyBatchRemovingTransition? = .crossDissolve,
56+
completion: ((RemovingTransitionContext.CompletionEvent) -> Void)? = nil
57+
) {
58+
guard let targetViewController = targetViewController else {
59+
return
60+
}
61+
fluidStackController?.removeViewController(
62+
targetViewController,
63+
transition: transition,
64+
transitionForBatch: transitionForBatch,
65+
completion: completion
66+
)
67+
}
68+
69+
/**
70+
Starts transition for removing if parent container presents.
71+
72+
See detail in ``FluidStackController/startRemovingForInteraction(_:)``
73+
*/
74+
public func startRemovingForInteraction() -> RemovingTransitionContext? {
75+
guard let targetViewController = targetViewController else {
76+
return nil
77+
}
78+
return fluidStackController?.startRemovingForInteraction(targetViewController)
79+
}
80+
81+
/**
82+
See detail in ``FluidStackController/removeAllViewController(transition:)``
83+
*/
84+
public func removeAllViewController(
85+
transition: AnyBatchRemovingTransition?
86+
) {
87+
fluidStackController?.removeAllViewController(transition: transition)
88+
}
89+
90+
}
91+

0 commit comments

Comments
 (0)