|
|
@@ -1,27 +1,26 @@
|
|
1
|
1
|
package com.reactnativenavigation.viewcontrollers;
|
|
2
|
2
|
|
|
3
|
|
-import android.app.Dialog;
|
|
4
|
|
-import android.content.DialogInterface;
|
|
5
|
3
|
import android.support.annotation.Nullable;
|
|
6
|
|
-import android.view.KeyEvent;
|
|
7
|
|
-import android.view.View;
|
|
8
|
4
|
|
|
9
|
5
|
import com.facebook.react.bridge.Promise;
|
|
10
|
|
-import com.reactnativenavigation.R;
|
|
|
6
|
+import com.reactnativenavigation.viewcontrollers.modal.Modal;
|
|
|
7
|
+import com.reactnativenavigation.viewcontrollers.modal.ModalCreator;
|
|
11
|
8
|
|
|
12
|
9
|
import java.util.ArrayList;
|
|
13
|
10
|
import java.util.List;
|
|
14
|
11
|
|
|
15
|
|
-import static android.view.View.MeasureSpec.EXACTLY;
|
|
16
|
|
-import static android.view.View.MeasureSpec.makeMeasureSpec;
|
|
17
|
|
-
|
|
18
|
12
|
public class ModalStack {
|
|
19
|
13
|
|
|
20
|
14
|
private List<Modal> modals = new ArrayList<>();
|
|
|
15
|
+ private ModalCreator creator;
|
|
|
16
|
+
|
|
|
17
|
+ public ModalStack(ModalCreator creator) {
|
|
|
18
|
+ this.creator = creator;
|
|
|
19
|
+ }
|
|
21
|
20
|
|
|
22
|
|
- public void showModal(final ViewController viewController, Promise promise) {
|
|
23
|
|
- Modal modal = new Modal(viewController);
|
|
24
|
|
- modals.add(modal);
|
|
|
21
|
+ public void showModal(final ViewController viewController, Promise promise) {
|
|
|
22
|
+ Modal modal = creator.create(viewController);
|
|
|
23
|
+ modals.add(modal);
|
|
25
|
24
|
modal.show();
|
|
26
|
25
|
if (promise != null) {
|
|
27
|
26
|
promise.resolve(viewController.getId());
|
|
|
@@ -52,7 +51,7 @@ public class ModalStack {
|
|
52
|
51
|
}
|
|
53
|
52
|
|
|
54
|
53
|
@Nullable
|
|
55
|
|
- private Modal findModalByComponentId(String componentId) {
|
|
|
54
|
+ public Modal findModalByComponentId(String componentId) {
|
|
56
|
55
|
for (Modal modal : modals) {
|
|
57
|
56
|
if (modal.containsDeepComponentId(componentId)) {
|
|
58
|
57
|
return modal;
|
|
|
@@ -64,49 +63,6 @@ public class ModalStack {
|
|
64
|
63
|
@Nullable
|
|
65
|
64
|
ViewController findControllerById(String id) {
|
|
66
|
65
|
Modal modal = findModalByComponentId(id);
|
|
67
|
|
- return modal != null ? modal.viewController : null;
|
|
68
|
|
- }
|
|
69
|
|
-
|
|
70
|
|
- private static class Modal implements DialogInterface.OnKeyListener {
|
|
71
|
|
- public final ViewController viewController;
|
|
72
|
|
- private final Dialog dialog;
|
|
73
|
|
-
|
|
74
|
|
- Modal(final ViewController viewController) {
|
|
75
|
|
- this.viewController = viewController;
|
|
76
|
|
- dialog = new Dialog(viewController.getActivity(), R.style.Modal);
|
|
77
|
|
- dialog.setOnKeyListener(this);
|
|
78
|
|
- }
|
|
79
|
|
-
|
|
80
|
|
- void show() {
|
|
81
|
|
- preMeasureView();
|
|
82
|
|
- dialog.setContentView(viewController.getView());
|
|
83
|
|
- dialog.show();
|
|
84
|
|
- }
|
|
85
|
|
-
|
|
86
|
|
- void dismiss() {
|
|
87
|
|
- dialog.dismiss();
|
|
88
|
|
- }
|
|
89
|
|
-
|
|
90
|
|
- boolean containsDeepComponentId(String componentId) {
|
|
91
|
|
- return viewController.findControllerById(componentId) != null;
|
|
92
|
|
- }
|
|
93
|
|
-
|
|
94
|
|
- private void preMeasureView() {
|
|
95
|
|
- View decorView = viewController.getActivity().getWindow().getDecorView();
|
|
96
|
|
- viewController.getView().measure(makeMeasureSpec(decorView.getMeasuredWidth(), EXACTLY), makeMeasureSpec(decorView.getMeasuredHeight(), EXACTLY));
|
|
97
|
|
- }
|
|
98
|
|
-
|
|
99
|
|
- @Override
|
|
100
|
|
- public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
|
|
101
|
|
- if (keyCode == KeyEvent.KEYCODE_BACK) {
|
|
102
|
|
- if (event.getAction() == KeyEvent.ACTION_UP) {
|
|
103
|
|
- if (viewController.handleBack()) {
|
|
104
|
|
- return true;
|
|
105
|
|
- }
|
|
106
|
|
- dialog.dismiss();
|
|
107
|
|
- }
|
|
108
|
|
- }
|
|
109
|
|
- return false;
|
|
110
|
|
- }
|
|
|
66
|
+ return modal != null ? modal.viewController.findControllerById(id) : null;
|
|
111
|
67
|
}
|
|
112
|
68
|
}
|