Browse Source

modals android

Daniel Zlotin 7 years ago
parent
commit
1eefe4bef5

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

@@ -15,7 +15,6 @@ public class ModalsTest extends BaseTest {
15 15
 		assertExists(By.text("Modal Screen"));
16 16
 	}
17 17
 
18
-	@Ignore
19 18
 	@Test
20 19
 	public void dismissModal() throws Exception {
21 20
 		launchTheApp();
@@ -28,7 +27,6 @@ public class ModalsTest extends BaseTest {
28 27
 
29 28
 	}
30 29
 
31
-	@Ignore
32 30
 	@Test
33 31
 	public void showMultipleModals() throws Exception {
34 32
 		launchTheApp();
@@ -46,7 +44,6 @@ public class ModalsTest extends BaseTest {
46 44
 		assertMainShown();
47 45
 	}
48 46
 
49
-	@Ignore
50 47
 	@Test
51 48
 	public void dismissUnknownContainerId() throws Exception {
52 49
 		launchTheApp();
@@ -61,7 +58,6 @@ public class ModalsTest extends BaseTest {
61 58
 		assertMainShown();
62 59
 	}
63 60
 
64
-	@Ignore
65 61
 	@Test
66 62
 	public void dismissModalByContainerIdWhenNotOnTop() throws Exception {
67 63
 		launchTheApp();
@@ -79,7 +75,6 @@ public class ModalsTest extends BaseTest {
79 75
 		assertMainShown();
80 76
 	}
81 77
 
82
-	@Ignore
83 78
 	@Test
84 79
 	public void dismissAllPreviousModalsByIdWhenTheyAreBelowTopPresented() throws Exception {
85 80
 		launchTheApp();
@@ -98,7 +93,6 @@ public class ModalsTest extends BaseTest {
98 93
 		assertMainShown();
99 94
 	}
100 95
 
101
-	@Ignore
102 96
 	@Test
103 97
 	public void dismissSomeModalByIdDeepInTheStack() throws Exception {
104 98
 		launchTheApp();
@@ -110,7 +104,7 @@ public class ModalsTest extends BaseTest {
110 104
 		elementByText("SHOW MODAL").click();
111 105
 		assertExists(By.text("Modal Stack Position: 3"));
112 106
 
113
-		elementByText("DISMISS FIRST IN THE STACK").click();
107
+		elementByText("DISMISS FIRST IN STACK").click();
114 108
 		assertExists(By.text("Modal Stack Position: 3"));
115 109
 
116 110
 		elementByText("DISMISS MODAL").click();

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

@@ -96,6 +96,16 @@ public class NavigationModule extends ReactContextBaseJavaModule {
96 96
 		});
97 97
 	}
98 98
 
99
+	@ReactMethod
100
+	public void dismissModal(final String containerId) {
101
+		handle(new Runnable() {
102
+			@Override
103
+			public void run() {
104
+				navigator().dismissModal(containerId);
105
+			}
106
+		});
107
+	}
108
+
99 109
 	private NavigationActivity activity() {
100 110
 		return (NavigationActivity) getCurrentActivity();
101 111
 	}

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

@@ -3,6 +3,8 @@ package com.reactnativenavigation.viewcontrollers;
3 3
 import android.app.Activity;
4 4
 import android.app.Dialog;
5 5
 import android.support.annotation.NonNull;
6
+import android.support.annotation.Nullable;
7
+import android.support.v4.util.Pair;
6 8
 import android.view.ViewGroup;
7 9
 import android.widget.FrameLayout;
8 10
 
@@ -11,6 +13,8 @@ import com.reactnativenavigation.utils.CompatUtils;
11 13
 
12 14
 import java.util.Collection;
13 15
 import java.util.Collections;
16
+import java.util.HashMap;
17
+import java.util.Map;
14 18
 
15 19
 import static android.view.View.MeasureSpec.EXACTLY;
16 20
 import static android.view.View.MeasureSpec.makeMeasureSpec;
@@ -18,6 +22,7 @@ import static android.view.View.MeasureSpec.makeMeasureSpec;
18 22
 public class Navigator extends ParentController {
19 23
 
20 24
 	private ViewController root;
25
+	private HashMap<ViewController, Dialog> modals = new HashMap<>();
21 26
 
22 27
 	public Navigator(final Activity activity) {
23 28
 		super(activity, "navigator" + CompatUtils.generateViewId());
@@ -107,5 +112,25 @@ public class Navigator extends ParentController {
107 112
 		Dialog dialog = new Dialog(getActivity(), R.style.Modal);
108 113
 		dialog.setContentView(viewController.getView());
109 114
 		dialog.show();
115
+		modals.put(viewController, dialog);
116
+	}
117
+
118
+	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
+		}
124
+	}
125
+
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;
110 135
 	}
111 136
 }