-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathModalStack.java
More file actions
204 lines (172 loc) · 6.74 KB
/
ModalStack.java
File metadata and controls
204 lines (172 loc) · 6.74 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
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
package com.reactnativenavigation.viewcontrollers.modal;
import android.content.Context;
import android.content.res.Configuration;
import android.view.ViewGroup;
import com.reactnativenavigation.options.ModalPresentationStyle;
import com.reactnativenavigation.options.Options;
import com.reactnativenavigation.react.CommandListener;
import com.reactnativenavigation.react.CommandListenerAdapter;
import com.reactnativenavigation.react.events.EventEmitter;
import com.reactnativenavigation.viewcontrollers.viewcontroller.ViewController;
import com.reactnativenavigation.viewcontrollers.viewcontroller.overlay.ModalOverlay;
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.List;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import static com.reactnativenavigation.react.Constants.HARDWARE_BACK_BUTTON_ID;
import static com.reactnativenavigation.utils.ObjectUtils.perform;
public class ModalStack {
private final List<ViewController<?>> modals = new ArrayList<>();
private final ModalPresenter presenter;
private final ModalOverlay overlay;
private EventEmitter eventEmitter;
public void setEventEmitter(EventEmitter eventEmitter) {
this.eventEmitter = eventEmitter;
}
public ModalStack(Context context) {
this.presenter = new ModalPresenter(new ModalAnimator(context));
overlay = new ModalOverlay(context);
}
@RestrictTo(RestrictTo.Scope.TESTS)
ModalStack(Context context, ModalPresenter presenter) {
this.presenter = presenter;
overlay = new ModalOverlay(context);
}
public void setModalsLayout(CoordinatorLayout modalsLayout) {
presenter.setModalsLayout(modalsLayout);
overlay.setModalsLayout(modalsLayout);
}
public void setRootLayout(ViewGroup rootLayout) {
presenter.setRootLayout(rootLayout);
}
public void setDefaultOptions(Options defaultOptions) {
presenter.setDefaultOptions(defaultOptions);
}
public void showModal(ViewController<?> viewController, ViewController<?> root, CommandListener listener) {
ViewController<?> toRemove = isEmpty() ? root : peek();
modals.add(viewController);
viewController.setOverlay(overlay);
presenter.showModal(viewController, toRemove, listener);
}
public boolean dismissModal(String componentId, @Nullable ViewController<?> root, CommandListener listener) {
ViewController<?> toDismiss = findModalByComponentId(componentId);
if (toDismiss != null) {
boolean isDismissingTopModal = isTop(toDismiss);
modals.remove(toDismiss);
@Nullable ViewController<?> toAdd = isEmpty() ? root : isDismissingTopModal ? get(size() - 1) : null;
if (isDismissingTopModal) {
if (toAdd == null) {
listener.onError("Could not dismiss modal");
return false;
}
}
presenter.dismissModal(toDismiss, toAdd, root, new CommandListenerAdapter(listener) {
@Override
public void onSuccess(String childId) {
eventEmitter.emitModalDismissed(toDismiss.getId(), toDismiss.getCurrentComponentName(), 1);
super.onSuccess(toDismiss.getId());
}
});
return true;
} else {
listener.onError("Nothing to dismiss");
return false;
}
}
public void dismissAllModals(@Nullable ViewController<?> root, Options mergeOptions, CommandListener listener) {
if (modals.isEmpty()) {
listener.onSuccess(perform(root, "", ViewController::getId));
return;
}
String topModalId = peek().getId();
String topModalName = peek().getCurrentComponentName();
int modalsDismissed = size();
peek().mergeOptions(mergeOptions);
while (!modals.isEmpty()) {
if (modals.size() == 1) {
dismissModal(modals.get(0).getId(), root, new CommandListenerAdapter(listener) {
@Override
public void onSuccess(String childId) {
eventEmitter.emitModalDismissed(topModalId, topModalName, modalsDismissed);
super.onSuccess(childId);
}
});
} else {
modals.get(0).destroy();
modals.remove(0);
}
}
}
public boolean handleBack(CommandListener listener, ViewController<?> root) {
if (isEmpty()) return false;
if (peek().handleBack(listener)) {
return true;
}
if (presenter.shouldDismissModal(peek())) return dismissModal(peek().getId(), root, listener);
else {
peek().sendOnNavigationButtonPressed(HARDWARE_BACK_BUTTON_ID);
return true;
}
}
ViewController<?> peek() {
if (modals.isEmpty()) throw new EmptyStackException();
return modals.get(modals.size() - 1);
}
public ViewController<?> get(int index) {
return modals.get(index);
}
public boolean isEmpty() {
return modals.isEmpty();
}
public int size() {
return modals.size();
}
private boolean isTop(ViewController<?> modal) {
return !isEmpty() && peek().equals(modal);
}
@Nullable
private ViewController<?> findModalByComponentId(String componentId) {
for (ViewController<?> modal : modals) {
if (modal.findController(componentId) != null) {
return modal;
}
}
return null;
}
@Nullable
public ViewController<?> findControllerById(String componentId) {
for (ViewController<?> modal : modals) {
ViewController<?> controllerById = modal.findController(componentId);
if (controllerById != null) {
return controllerById;
}
}
return null;
}
public void destroy() {
for (ViewController<?> modal : modals) {
modal.destroy();
}
modals.clear();
}
public void onConfigurationChanged(Configuration newConfig) {
for (ViewController<?> controller : modals) {
controller.onConfigurationChanged(newConfig);
}
}
public void onHostPause() {
if (!isEmpty()) {
peek().onViewDisappear();
}
}
public void onHostResume() {
if (!isEmpty()) {
peek().onViewDidAppear();
}
}
public boolean peekDisplayedOverCurrentContext() {
return !isEmpty() && presenter.resolveOptions(peek()).modal.presentationStyle == ModalPresentationStyle.OverCurrentContext;
}
}