Browse Source

Merge branch 'v2' into newAPI

Daniel Zlotin 6 years ago
parent
commit
70e66508ed

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

@@ -109,4 +109,20 @@ public class ModalsTest extends BaseTest {
109 109
 		elementByText("DISMISS ALL MODALS").click();
110 110
 		assertMainShown();
111 111
 	}
112
+
113
+	@Test
114
+    public void pushIntoModal() throws Exception {
115
+        elementByText("SHOW MODAL").click();
116
+        elementByText("PUSH SCREEN").click();
117
+        assertExists(By.text("Pushed Screen"));
118
+    }
119
+
120
+    @Test
121
+    public void backPopsModalStack() throws Exception {
122
+        elementByText("SHOW MODAL").click();
123
+        elementByText("PUSH SCREEN").click();
124
+        elementByText("PUSH").click();
125
+        device().pressBack();
126
+        assertExists(By.text("Pushed Screen"));
127
+    }
112 128
 }

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

@@ -1,7 +1,9 @@
1 1
 package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Dialog;
4
+import android.content.DialogInterface;
4 5
 import android.support.annotation.Nullable;
6
+import android.view.KeyEvent;
5 7
 import android.view.View;
6 8
 
7 9
 import com.facebook.react.bridge.Promise;
@@ -59,13 +61,20 @@ public class ModalStack {
59 61
 		return null;
60 62
 	}
61 63
 
62
-	private static class Modal {
63
-		private final ViewController viewController;
64
+	@Nullable
65
+    ViewController findControllerById(String id) {
66
+        Modal modal = findModalByContainerId(id);
67
+        return modal != null ? modal.viewController : null;
68
+    }
69
+
70
+    private static class Modal implements DialogInterface.OnKeyListener {
71
+		public final ViewController viewController;
64 72
 		private final Dialog dialog;
65 73
 
66 74
 		Modal(final ViewController viewController) {
67 75
 			this.viewController = viewController;
68 76
 			dialog = new Dialog(viewController.getActivity(), R.style.Modal);
77
+			dialog.setOnKeyListener(this);
69 78
 		}
70 79
 
71 80
 		void show() {
@@ -86,5 +95,18 @@ public class ModalStack {
86 95
 			View decorView = viewController.getActivity().getWindow().getDecorView();
87 96
 			viewController.getView().measure(makeMeasureSpec(decorView.getMeasuredWidth(), EXACTLY), makeMeasureSpec(decorView.getMeasuredHeight(), EXACTLY));
88 97
 		}
89
-	}
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
+        }
111
+    }
90 112
 }

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

@@ -2,6 +2,7 @@ package com.reactnativenavigation.viewcontrollers;
2 2
 
3 3
 import android.app.Activity;
4 4
 import android.support.annotation.NonNull;
5
+import android.support.annotation.Nullable;
5 6
 import android.view.ViewGroup;
6 7
 import android.widget.FrameLayout;
7 8
 
@@ -35,7 +36,7 @@ public class Navigator extends ParentController {
35 36
 	@NonNull
36 37
 	@Override
37 38
 	public Collection<ViewController> getChildControllers() {
38
-		return root == null ? Collections.<ViewController>emptyList() : Collections.singletonList(root);
39
+		return root == null ? Collections.emptyList() : Collections.singletonList(root);
39 40
 	}
40 41
 
41 42
 	@Override
@@ -173,4 +174,11 @@ public class Navigator extends ParentController {
173 174
 			promise.reject(new Throwable("Nothing to pop"));
174 175
 		}
175 176
 	}
177
+
178
+    @Nullable
179
+    @Override
180
+    public ViewController findControllerById(String id) {
181
+        ViewController controllerById = super.findControllerById(id);
182
+        return controllerById != null ? controllerById : modalStack.findControllerById(id);
183
+    }
176 184
 }

+ 8
- 4
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java View File

@@ -56,16 +56,20 @@ public abstract class ViewController implements ViewTreeObserver.OnGlobalLayoutL
56 56
 		this.parentController = parentController;
57 57
 	}
58 58
 
59
-    void performOnParentStack(Task<StackController> task) {
59
+    boolean performOnParentStack(Task<StackController> task) {
60 60
 	    if (parentController instanceof StackController) {
61 61
             task.run((StackController) parentController);
62
+            return true;
62 63
         }
64
+        if (this instanceof StackController) {
65
+	        task.run((StackController) this);
66
+            return true;
67
+        }
68
+        return false;
63 69
     }
64 70
 
65 71
     void performOnParentStack(Task<StackController> accept, Runnable  reject) {
66
-        if (parentController instanceof StackController) {
67
-            accept.run((StackController) parentController);
68
-        } else {
72
+        if (!performOnParentStack(accept)) {
69 73
             reject.run();
70 74
         }
71 75
     }

+ 15
- 6
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigatorTest.java View File

@@ -94,7 +94,7 @@ public class NavigatorTest extends BaseTest {
94 94
 		StackController stack2 = newStack();
95 95
 		stack1.push(child1);
96 96
 		stack2.push(child2);
97
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
97
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
98 98
 		uut.setRoot(bottomTabsController);
99 99
 
100 100
 		SimpleViewController newChild = new SimpleViewController(activity, "new child");
@@ -121,7 +121,7 @@ public class NavigatorTest extends BaseTest {
121 121
 		stack2.push(child2);
122 122
 		stack2.push(child3);
123 123
 		stack2.push(child4);
124
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
124
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
125 125
 		uut.setRoot(bottomTabsController);
126 126
 
127 127
 		uut.pop("child4");
@@ -138,7 +138,7 @@ public class NavigatorTest extends BaseTest {
138 138
 		stack2.push(child2);
139 139
 		stack2.push(child3);
140 140
 		stack2.push(child4);
141
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
141
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
142 142
 		uut.setRoot(bottomTabsController);
143 143
 
144 144
 		uut.popSpecific(child2.getId());
@@ -156,7 +156,7 @@ public class NavigatorTest extends BaseTest {
156 156
 		stack2.push(child3);
157 157
 		stack2.push(child4);
158 158
 		stack2.push(child5);
159
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
159
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
160 160
 		uut.setRoot(bottomTabsController);
161 161
 
162 162
 		uut.popTo(child2.getId());
@@ -175,7 +175,7 @@ public class NavigatorTest extends BaseTest {
175 175
 		stack2.push(child4);
176 176
 		stack2.push(child5);
177 177
 
178
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
178
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
179 179
 		uut.setRoot(bottomTabsController);
180 180
 
181 181
 		uut.popToRoot(child3.getId());
@@ -272,7 +272,7 @@ public class NavigatorTest extends BaseTest {
272 272
 		stack2.push(child2);
273 273
 		stack2.push(child3);
274 274
 		stack2.push(child4);
275
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
275
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
276 276
 		uut.setRoot(bottomTabsController);
277 277
 
278 278
 		uut.pop("child4", new MockPromise() {
@@ -282,4 +282,13 @@ public class NavigatorTest extends BaseTest {
282 282
 			}
283 283
 		});
284 284
 	}
285
+
286
+	@Test
287
+    public void pushIntoModal() throws Exception {
288
+        StackController stackController = newStack();
289
+        stackController.push(child1);
290
+        uut.showModal(stackController, new MockPromise());
291
+        uut.push(stackController.getId(), child2);
292
+        assertIsChildById(stackController.getView(), child2.getView());
293
+    }
285 294
 }

+ 11
- 0
playground/src/screens/ModalScreen.js View File

@@ -24,6 +24,7 @@ class ModalScreen extends Component {
24 24
     this.onClickDismissAllPreviousModals = this.onClickDismissAllPreviousModals.bind(this);
25 25
     this.onClickDismissFirstInStack = this.onClickDismissFirstInStack.bind(this);
26 26
     this.onClickDismissAllModals = this.onClickDismissAllModals.bind(this);
27
+    this.onClickPushScreen = this.onClickPushScreen.bind(this);
27 28
   }
28 29
 
29 30
   render() {
@@ -35,6 +36,7 @@ class ModalScreen extends Component {
35 36
         <Button title="Dismiss Modal" testID={testIDs.DISMISS_MODAL_BUTTON} onPress={this.onClickDismissModal} />
36 37
         <Button title="Dismiss Unknown Modal" testID={testIDs.DISMISS_UNKNOWN_MODAL_BUTTON} onPress={this.onClickDismissUnknownModal} />
37 38
         <Button title="Dismiss All Modals" testID={testIDs.DISMISS_ALL_MODALS_BUTTON} onPress={this.onClickDismissAllModals} />
39
+        <Button title="Push screen" testID={testIDs.PUSH_BUTTON} onPress={this.onClickPushScreen} />
38 40
         {this.getPreviousModalId() ? (<Button title="Dismiss Previous Modal" testID={testIDs.DISMISS_PREVIOUS_MODAL_BUTTON} onPress={this.onClickDismissPreviousModal} />) : undefined}
39 41
         {this.props.previousModalIds ? (<Button title="Dismiss ALL Previous Modals" testID={testIDs.DISMISS_ALL_PREVIOUS_MODAL_BUTTON} onPress={this.onClickDismissAllPreviousModals} />) : undefined}
40 42
         {this.props.previousModalIds ? (<Button title="Dismiss First In Stack" testID={testIDs.DISMISS_FIRST_MODAL_BUTTON} onPress={this.onClickDismissFirstInStack} />) : undefined}
@@ -79,6 +81,15 @@ class ModalScreen extends Component {
79 81
     Navigation.dismissAllModals();
80 82
   }
81 83
 
84
+  onClickPushScreen() {
85
+    Navigation.push(this.props.containerId, {
86
+      name: `navigation.playground.PushedScreen`,
87
+      passProps: {
88
+        text: 'Pushed from modal'
89
+      }
90
+    });
91
+  }
92
+
82 93
   getModalPosition() {
83 94
     return (this.props.modalPosition || 1);
84 95
   }