Pārlūkot izejas kodu

dismissAllModals android

Daniel Zlotin 7 gadus atpakaļ
vecāks
revīzija
690840353e

+ 0
- 2
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/ModalsTest.java Parādīt failu

@@ -2,7 +2,6 @@ package com.reactnativenavigation.e2e.androide2e;
2 2
 
3 3
 import android.support.test.uiautomator.By;
4 4
 
5
-import org.junit.Ignore;
6 5
 import org.junit.Test;
7 6
 
8 7
 public class ModalsTest extends BaseTest {
@@ -100,7 +99,6 @@ public class ModalsTest extends BaseTest {
100 99
 		assertMainShown();
101 100
 	}
102 101
 
103
-	@Ignore
104 102
 	@Test
105 103
 	public void dismissAllModals() throws Exception {
106 104
 		elementByText("SHOW MODAL").click();

+ 10
- 0
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java Parādīt failu

@@ -106,6 +106,16 @@ public class NavigationModule extends ReactContextBaseJavaModule {
106 106
 		});
107 107
 	}
108 108
 
109
+	@ReactMethod
110
+	public void dismissAllModals() {
111
+		handle(new Runnable() {
112
+			@Override
113
+			public void run() {
114
+				navigator().dismissAllModals();
115
+			}
116
+		});
117
+	}
118
+
109 119
 	private NavigationActivity activity() {
110 120
 		return (NavigationActivity) getCurrentActivity();
111 121
 	}

+ 78
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ModalStack.java Parādīt failu

@@ -0,0 +1,78 @@
1
+package com.reactnativenavigation.viewcontrollers;
2
+
3
+import android.app.Dialog;
4
+import android.support.annotation.Nullable;
5
+import android.view.View;
6
+
7
+import com.reactnativenavigation.R;
8
+
9
+import java.util.ArrayList;
10
+import java.util.List;
11
+
12
+import static android.view.View.MeasureSpec.EXACTLY;
13
+import static android.view.View.MeasureSpec.makeMeasureSpec;
14
+
15
+public class ModalStack {
16
+
17
+	private List<Modal> modals = new ArrayList<>();
18
+
19
+	public void showModal(final ViewController viewController) {
20
+		Modal modal = new Modal(viewController);
21
+		modals.add(modal);
22
+		modal.show();
23
+	}
24
+
25
+	public void dismissModal(final String containerId) {
26
+		Modal modal = findModalByContainerId(containerId);
27
+		if (modal != null) {
28
+			modal.dismiss();
29
+			modals.remove(modal);
30
+		}
31
+	}
32
+
33
+	public void dismissAll() {
34
+		for (Modal modal : modals) {
35
+			modal.dismiss();
36
+		}
37
+		modals.clear();
38
+	}
39
+
40
+	@Nullable
41
+	private Modal findModalByContainerId(String containerId) {
42
+		for (Modal modal : modals) {
43
+			if (modal.containsDeepContainerId(containerId)) {
44
+				return modal;
45
+			}
46
+		}
47
+		return null;
48
+	}
49
+
50
+	private static class Modal {
51
+		private final ViewController viewController;
52
+		private final Dialog dialog;
53
+
54
+		Modal(final ViewController viewController) {
55
+			this.viewController = viewController;
56
+			dialog = new Dialog(viewController.getActivity(), R.style.Modal);
57
+		}
58
+
59
+		void show() {
60
+			preMeasureView();
61
+			dialog.setContentView(viewController.getView());
62
+			dialog.show();
63
+		}
64
+
65
+		void dismiss() {
66
+			dialog.dismiss();
67
+		}
68
+
69
+		boolean containsDeepContainerId(String containerId) {
70
+			return viewController.findControllerById(containerId) != null;
71
+		}
72
+
73
+		private void preMeasureView() {
74
+			View decorView = viewController.getActivity().getWindow().getDecorView();
75
+			viewController.getView().measure(makeMeasureSpec(decorView.getMeasuredWidth(), EXACTLY), makeMeasureSpec(decorView.getMeasuredHeight(), EXACTLY));
76
+		}
77
+	}
78
+}

+ 5
- 29
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java Parādīt failu

@@ -1,28 +1,19 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4
-import android.app.Dialog;
5 4
 import android.support.annotation.NonNull;
6
-import android.support.annotation.Nullable;
7
-import android.support.v4.util.Pair;
8 5
 import android.view.ViewGroup;
9 6
 import android.widget.FrameLayout;
10 7
 
11
-import com.reactnativenavigation.R;
12 8
 import com.reactnativenavigation.utils.CompatUtils;
13 9
 
14 10
 import java.util.Collection;
15 11
 import java.util.Collections;
16
-import java.util.HashMap;
17
-import java.util.Map;
18
-
19
-import static android.view.View.MeasureSpec.EXACTLY;
20
-import static android.view.View.MeasureSpec.makeMeasureSpec;
21 12
 
22 13
 public class Navigator extends ParentController {
23 14
 
15
+	private final ModalStack modalStack = new ModalStack();
24 16
 	private ViewController root;
25
-	private HashMap<ViewController, Dialog> modals = new HashMap<>();
26 17
 
27 18
 	public Navigator(final Activity activity) {
28 19
 		super(activity, "navigator" + CompatUtils.generateViewId());
@@ -108,29 +99,14 @@ public class Navigator extends ParentController {
108 99
 	}
109 100
 
110 101
 	public void showModal(final ViewController viewController) {
111
-		viewController.getView().measure(makeMeasureSpec(getView().getWidth(), EXACTLY), makeMeasureSpec(getView().getHeight(), EXACTLY));
112
-		Dialog dialog = new Dialog(getActivity(), R.style.Modal);
113
-		dialog.setContentView(viewController.getView());
114
-		dialog.show();
115
-		modals.put(viewController, dialog);
102
+		modalStack.showModal(viewController);
116 103
 	}
117 104
 
118 105
 	public void dismissModal(final String containerId) {
119
-		Pair<ViewController, Dialog> pair = findModalByContainerId(containerId);
120
-		if (pair != null) {
121
-			pair.second.dismiss();
122
-			modals.remove(pair.first);
123
-		}
106
+		modalStack.dismissModal(containerId);
124 107
 	}
125 108
 
126
-	@Nullable
127
-	private Pair<ViewController, Dialog> findModalByContainerId(String containerId) {
128
-		for (Map.Entry<ViewController, Dialog> entry : modals.entrySet()) {
129
-			ViewController vc = entry.getKey().findControllerById(containerId);
130
-			if (vc != null) {
131
-				return Pair.create(entry.getKey(), entry.getValue());
132
-			}
133
-		}
134
-		return null;
109
+	public void dismissAllModals() {
110
+		modalStack.dismissAll();
135 111
 	}
136 112
 }