-
Notifications
You must be signed in to change notification settings - Fork 10
/
AnimatedSwitcher.cs
263 lines (215 loc) · 8.25 KB
/
AnimatedSwitcher.cs
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System.Linq;
using System.Collections.Generic;
using UniMob.UI.Internal;
using UnityEngine.Assertions;
namespace UniMob.UI.Widgets
{
public class AnimatedSwitcher : StatefulWidget
{
public Widget Child { get; set; }
public float Duration { get; set; }
public float ReverseDuration { get; set; }
public AnimatedSwitcherTransitionMode TransitionMode { get; set; } = AnimatedSwitcherTransitionMode.Parallel;
public AnimatedSwitcherTransitionBuilder TransitionBuilder { get; set; } = DefaultTransitionBuilder;
public AnimatedSwitcherLayoutBuilder LayoutBuilder { get; set; } = DefaultLayoutBuilder;
public override State CreateState() => new AnimatedSwitcherState();
private static Widget DefaultTransitionBuilder(IAnimation<float> animation, Widget child)
{
return new CompositeTransition
{
Opacity = animation,
Child = child,
};
}
private static Widget DefaultLayoutBuilder(Widget currentChild, IEnumerable<Widget> previousChildren)
{
var stack = new ZStack
{
Alignment = Alignment.Center,
Children = {previousChildren}
};
if (currentChild != null)
{
stack.Children.Add(currentChild);
}
return stack;
}
}
public class AnimatedSwitcherState : HocState<AnimatedSwitcher>
{
private readonly MutableAtom<int> _version = Atom.Value(int.MinValue);
private readonly List<Entry> _outgoingEntries = new List<Entry>();
private readonly Queue<AnimationController> _pendingAnimations = new Queue<AnimationController>();
private List<Widget> _outgoingWidgets = new List<Widget>();
private Entry _currentEntry;
private int _childNumber;
public override void InitState()
{
base.InitState();
AddEntryForNewChild(animate: false);
}
public override Widget Build(BuildContext context)
{
_version.Get();
RebuildOutgoingWidgetsIfNeed();
var previousChildren = _outgoingWidgets.Where(w => w.Key != _currentEntry?.Transition.Key);
return Widget.LayoutBuilder.Invoke(_currentEntry?.Transition, previousChildren);
}
public override void DidUpdateWidget(AnimatedSwitcher oldWidget)
{
base.DidUpdateWidget(oldWidget);
if (Widget.TransitionBuilder != oldWidget.TransitionBuilder)
{
foreach (var outgoingEntry in _outgoingEntries)
{
UpdateTransitionForEntry(outgoingEntry);
}
if (_currentEntry != null)
{
UpdateTransitionForEntry(_currentEntry);
}
MarkChildWidgetCacheAsDirty();
}
var hasNewChild = Widget.Child != null;
var hasOldChild = _currentEntry != null;
if (hasNewChild != hasOldChild ||
hasNewChild && !StateUtilities.CanUpdateWidget(Widget.Child, _currentEntry.Child))
{
_childNumber += 1;
AddEntryForNewChild(animate: true);
}
else if (_currentEntry != null)
{
Assert.IsTrue(hasOldChild && hasNewChild);
Assert.IsTrue(StateUtilities.CanUpdateWidget(Widget.Child, _currentEntry.Child));
_currentEntry.Child = Widget.Child;
UpdateTransitionForEntry(_currentEntry);
MarkChildWidgetCacheAsDirty();
}
}
private void AddEntryForNewChild(bool animate)
{
Assert.IsTrue(animate || _currentEntry == null);
var hasCurrentEntry = _currentEntry != null;
if (hasCurrentEntry)
{
Assert.IsTrue(animate);
Assert.IsTrue(!_outgoingEntries.Contains(_currentEntry));
_outgoingEntries.Add(_currentEntry);
if (_currentEntry.AnimationController.Status != AnimationStatus.Dismissed)
{
_currentEntry.AnimationController.Reverse();
}
else
{
_currentEntry.LifetimeController.Dispose();
}
_currentEntry = null;
MarkChildWidgetCacheAsDirty();
}
if (Widget.Child == null)
{
return;
}
var lc = StateLifetime.CreateNested();
var controller = new AnimationController(lc.Lifetime, Widget.Duration, Widget.ReverseDuration);
_currentEntry = NewEntry(lc, Widget.Child, controller, Widget.TransitionBuilder);
if (animate)
{
if (hasCurrentEntry && Widget.TransitionMode == AnimatedSwitcherTransitionMode.Sequential)
{
_pendingAnimations.Enqueue(controller);
}
else
{
controller.Forward();
}
}
else
{
Assert.IsTrue(_outgoingEntries.Count == 0);
controller.Complete();
}
}
private Entry NewEntry(ILifetimeController lc, Widget child, AnimationController controller,
AnimatedSwitcherTransitionBuilder transition)
{
var entry = new Entry
{
Child = child,
Transition = MakeTransition(child, controller, transition),
AnimationController = controller,
LifetimeController = lc,
};
Atom.Reaction(lc.Lifetime, () => controller.Status, status =>
{
if (status == AnimationStatus.Dismissed)
{
Zone.Current.NextFrame(lc.Dispose);
}
}, fireImmediately: false);
lc.Register(() =>
{
_outgoingEntries.Remove(entry);
_version.Value++;
MarkChildWidgetCacheAsDirty();
ForwardPendingAnimation();
});
return entry;
}
private void ForwardPendingAnimation()
{
while (_pendingAnimations.Count > 0)
{
var controller = _pendingAnimations.Dequeue();
if (controller.Lifetime.IsDisposed)
{
continue;
}
controller.Forward();
break;
}
}
private void UpdateTransitionForEntry(Entry entry)
{
entry.Transition = MakeTransition(entry.Child, entry.AnimationController, Widget.TransitionBuilder);
}
private Builder MakeTransition(Widget child, IAnimation<float> animation,
AnimatedSwitcherTransitionBuilder transition)
{
return new Builder(_ => transition.Invoke(animation, child))
{
Key = child.Key ?? Key.Of(_childNumber)
};
}
private void MarkChildWidgetCacheAsDirty()
{
_outgoingWidgets = null;
}
private void RebuildOutgoingWidgetsIfNeed()
{
if (_outgoingWidgets == null)
{
_outgoingWidgets = _outgoingEntries
.Select(entry => entry.Transition)
.ToList();
}
Assert.IsTrue(_outgoingEntries.Count == _outgoingWidgets.Count);
Assert.IsTrue(_outgoingEntries.Count == 0 || _outgoingEntries.Last().Transition == _outgoingWidgets.Last());
}
private class Entry
{
public ILifetimeController LifetimeController;
public AnimationController AnimationController;
public Widget Transition;
public Widget Child;
}
}
public enum AnimatedSwitcherTransitionMode
{
Parallel,
Sequential,
}
public delegate Widget AnimatedSwitcherTransitionBuilder(IAnimation<float> animation, Widget child);
public delegate Widget AnimatedSwitcherLayoutBuilder(Widget currentChild, IEnumerable<Widget> previousWidget);
}