-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewStack.cs
194 lines (161 loc) · 5.14 KB
/
ViewStack.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
using System;
using System.Collections.Generic;
using UnityEngine;
namespace UnityNavigator
{
public class ViewStack : MonoBehaviour
{
public delegate void ViewTransition(GameObject from, GameObject to, Action onComplete = null);
public interface ITransitionStartedHandler { void HandleTransitionStarted(); }
public interface ITransitionCompleteHandler { void HandleTransitionComplete(); }
private class ViewStackEntry
{
public string ViewID { get; private set; }
public GameObject View { get; private set; }
public ViewStackEntry(string id, GameObject view)
{
ViewID = id;
View = view;
}
public void NotifyView<T>(Action<T> notify, bool includeChildren = false)
{
var components = includeChildren
? View.GetComponents<T>() : View.GetComponentsInChildren<T>();
foreach (var component in components)
{
notify(component);
}
}
}
public ViewTransition DefaultTransition { get; set; }
public string TopViewID => TopViewEntry?.ViewID;
private ViewStackEntry TopViewEntry => viewStack.Count > 0 ? viewStack[viewStack.Count - 1] : null;
private List<ViewStackEntry> viewStack;
private IViewCreator viewCreator;
private bool transitionIsInProgress;
private bool isInitialized;
public event Action<string, GameObject> OnViewCreated;
public event Action<string, GameObject> OnViewDestroyed;
public event Action<string, GameObject> OnViewShown;
public event Action<string, GameObject> OnViewHidden;
// From ID -> To ID
public event Action<string, string> OnTransitionStarted;
public event Action<string, string> OnTransitionComplete;
private void Awake()
{
viewStack = new List<ViewStackEntry>();
}
private void Start()
{
if (!isInitialized)
{
Init();
}
}
public void Init()
{
viewCreator = GetComponent<IViewCreator>();
if (viewCreator == null)
{
throw new Exception("ViewStack doens't have an IViewCreator attached to it.");
}
isInitialized = true;
}
public void Push(
string newScreenId,
Action<GameObject> initView = null,
ViewTransition transition = null,
Action onCompete = null)
{
if (!isInitialized) throw new Exception("ViewStack is not yet initialized.");
if (transitionIsInProgress)
{
// TODO: How to handle this?
// for now just return and prevent starting a new one whilst one is in progress
return;
}
var newView = CreateView(newScreenId);
if (initView != null) initView(newView);
PerformTransition(
transition,
() => viewStack.Add(new ViewStackEntry(newScreenId, newView)),
onCompete);
}
public void Pop(ViewTransition transition = null, Action onComplete = null)
{
if (!isInitialized) throw new Exception("ViewStack is not yet initialized. Listen for OnInitialized before you start navigating");
if (transitionIsInProgress)
{
// TODO: How to handle this?
// for now just return and prevent starting a new one whilst one is in progress
return;
}
var oldTopViewEntry = TopViewEntry;
PerformTransition(
transition,
() => viewStack.Remove(oldTopViewEntry),
() =>
{
DestroyView(oldTopViewEntry);
if (onComplete != null) onComplete();
}
);
}
// TODO: add other operations like replace, pop-replace, reset etc...
private void PerformTransition(ViewTransition transition, Action manipulateState, Action onComplete = null)
{
transition = transition ?? DefaultTransition ?? Transitions.Instant;
var oldTopViewEntry = TopViewEntry;
manipulateState();
if (OnTransitionStarted != null)
{
OnTransitionStarted.Invoke(
oldTopViewEntry != null ? oldTopViewEntry.ViewID : null,
TopViewEntry != null ? TopViewEntry.ViewID : null
);
}
if (oldTopViewEntry != null)
{
// notify old view that transition has started
oldTopViewEntry.NotifyView<ITransitionStartedHandler>(
t => t.HandleTransitionStarted());
}
var oldView = oldTopViewEntry != null ? oldTopViewEntry.View : null;
transitionIsInProgress = true;
transition(oldView, TopViewEntry != null ? TopViewEntry.View : null, () =>
{
// notify the new view that transition has completed
if (TopViewEntry != null)
{
TopViewEntry.NotifyView<ITransitionCompleteHandler>(
t => t.HandleTransitionComplete());
}
if (OnTransitionComplete != null)
{
OnTransitionComplete(oldTopViewEntry == null ? null : oldTopViewEntry.ViewID, TopViewEntry.ViewID);
}
if (OnViewHidden != null && oldTopViewEntry != null)
{
OnViewHidden(oldTopViewEntry.ViewID, oldTopViewEntry.View);
}
if (OnViewShown != null)
{
OnViewShown(TopViewEntry.ViewID, TopViewEntry.View);
}
transitionIsInProgress = false;
if (onComplete != null) onComplete();
});
}
private GameObject CreateView(string newScreenId)
{
var view = viewCreator.Create(newScreenId);
if (OnViewCreated != null) OnViewCreated(newScreenId, view);
return view;
}
private void DestroyView(ViewStackEntry oldTopViewEntry)
{
if (OnViewDestroyed != null) OnViewDestroyed(oldTopViewEntry.ViewID, oldTopViewEntry.View);
viewCreator.Destroy(oldTopViewEntry.ViewID, oldTopViewEntry.View);
}
}
}