Browse Source

dismissAllModals android

Daniel Zlotin 7 years ago
parent
commit
690840353e

+ 0
- 2
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/ModalsTest.java View File

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

+ 10
- 0
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java View File

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
 	private NavigationActivity activity() {
119
 	private NavigationActivity activity() {
110
 		return (NavigationActivity) getCurrentActivity();
120
 		return (NavigationActivity) getCurrentActivity();
111
 	}
121
 	}

+ 78
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ModalStack.java View File

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 View File

1
 package com.reactnativenavigation.viewcontrollers;
1
 package com.reactnativenavigation.viewcontrollers;
2
 
2
 
3
 import android.app.Activity;
3
 import android.app.Activity;
4
-import android.app.Dialog;
5
 import android.support.annotation.NonNull;
4
 import android.support.annotation.NonNull;
6
-import android.support.annotation.Nullable;
7
-import android.support.v4.util.Pair;
8
 import android.view.ViewGroup;
5
 import android.view.ViewGroup;
9
 import android.widget.FrameLayout;
6
 import android.widget.FrameLayout;
10
 
7
 
11
-import com.reactnativenavigation.R;
12
 import com.reactnativenavigation.utils.CompatUtils;
8
 import com.reactnativenavigation.utils.CompatUtils;
13
 
9
 
14
 import java.util.Collection;
10
 import java.util.Collection;
15
 import java.util.Collections;
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
 public class Navigator extends ParentController {
13
 public class Navigator extends ParentController {
23
 
14
 
15
+	private final ModalStack modalStack = new ModalStack();
24
 	private ViewController root;
16
 	private ViewController root;
25
-	private HashMap<ViewController, Dialog> modals = new HashMap<>();
26
 
17
 
27
 	public Navigator(final Activity activity) {
18
 	public Navigator(final Activity activity) {
28
 		super(activity, "navigator" + CompatUtils.generateViewId());
19
 		super(activity, "navigator" + CompatUtils.generateViewId());
108
 	}
99
 	}
109
 
100
 
110
 	public void showModal(final ViewController viewController) {
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
 	public void dismissModal(final String containerId) {
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
 }