-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRemovingTransitionContext.swift
More file actions
131 lines (105 loc) · 3.84 KB
/
Copy pathRemovingTransitionContext.swift
File metadata and controls
131 lines (105 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import UIKit
import FluidPortal
/// A context object to interact with container view controller for transitions.
@MainActor
public final class RemovingTransitionContext: TransitionContext {
public enum CompletionEvent: Sendable {
/// Transition has been finished (no interruption was in there)
case succeeded
/// Transition has been interrupted
case interrupted
/// Transition has been cancelled in interaction
case cancelled
}
public private(set) var isCompleted: Bool = false
/// A view controller for removing
public let fromViewController: UIViewController
/// A view controller that displays after removed
public let toViewController: UIViewController?
private let onAnimationCompleted: (RemovingTransitionContext) -> Void
private let onRequestedDisplayOnTop:
(DisplaySource) -> FluidStackController.DisplayingOnTopSubscription
private var hasNotifiedCompletion: Bool = false
private var callbacks: [(CompletionEvent) -> Void] = []
init(
contentView: FluidStackController.StackingPlatterView,
fromViewController: UIViewController,
toViewController: UIViewController?,
onAnimationCompleted: @escaping (RemovingTransitionContext) -> Void,
onRequestedDisplayOnTop: @escaping (DisplaySource) -> FluidStackController
.DisplayingOnTopSubscription
) {
self.fromViewController = fromViewController
self.toViewController = toViewController
self.onAnimationCompleted = onAnimationCompleted
self.onRequestedDisplayOnTop = onRequestedDisplayOnTop
super.init(contentView: contentView)
}
/**
Notifies controller transition has been completed.
*/
public func notifyAnimationCompleted() {
assert(Thread.isMainThread)
guard isInvalidated == false, isCompleted == false else { return }
isCompleted = true
onAnimationCompleted(self)
}
public func notifyCancelled() {
assert(Thread.isMainThread)
guard isInvalidated == false, isCompleted == false else { return }
isInvalidated = true
hasNotifiedCompletion = true
callbacks.forEach { $0(.cancelled) }
onAnimationCompleted(self)
}
/**
Makes toViewController disabled in user-interaction until finish transition.
*/
public func disableUserInteractionUntileFinish() {
func run(viewController: UIViewController) {
// TODO: there is re-entrancy issue in running transitions simultaneously.
viewController.view.isUserInteractionEnabled = false
addCompletionEventHandler { [weak viewController] _ in
viewController?.view.isUserInteractionEnabled = true
}
}
toViewController.map {
run(viewController: $0)
}
run(viewController: fromViewController)
}
public func requestDisplayOnTop(_ source: DisplaySource)
-> FluidStackController.DisplayingOnTopSubscription
{
onRequestedDisplayOnTop(source)
}
/// Marks as this current transition has been outdated.
/// Another transition's started by owner.
/// Triggers ``addCompletionEventHandler(_:)`` with ``TransitionContext/CompletionEvent/interrupted``
override func invalidate() {
assert(Thread.isMainThread)
isInvalidated = true
guard hasNotifiedCompletion == false else { return }
hasNotifiedCompletion = true
callbacks.forEach { $0(.interrupted) }
}
/**
Adds closure that handles completion events (``CompletionEvent``)
Use-cases:
- Trigger to clean up transient resources for this transition.
*/
public func addCompletionEventHandler(_ closure: @escaping (CompletionEvent) -> Void) {
assert(Thread.isMainThread)
callbacks.append(closure)
}
/**
Triggers ``addCompletionEventHandler(_:)`` with ``TransitionContext/CompletionEvent/succeeded``
*/
func transitionSucceeded() {
guard hasNotifiedCompletion == false else { return }
hasNotifiedCompletion = true
callbacks.forEach { $0(.succeeded) }
}
deinit {
}
}