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
 		elementByText("DISMISS ALL MODALS").click();
109
 		elementByText("DISMISS ALL MODALS").click();
110
 		assertMainShown();
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
 package com.reactnativenavigation.viewcontrollers;
1
 package com.reactnativenavigation.viewcontrollers;
2
 
2
 
3
 import android.app.Dialog;
3
 import android.app.Dialog;
4
+import android.content.DialogInterface;
4
 import android.support.annotation.Nullable;
5
 import android.support.annotation.Nullable;
6
+import android.view.KeyEvent;
5
 import android.view.View;
7
 import android.view.View;
6
 
8
 
7
 import com.facebook.react.bridge.Promise;
9
 import com.facebook.react.bridge.Promise;
59
 		return null;
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
 		private final Dialog dialog;
72
 		private final Dialog dialog;
65
 
73
 
66
 		Modal(final ViewController viewController) {
74
 		Modal(final ViewController viewController) {
67
 			this.viewController = viewController;
75
 			this.viewController = viewController;
68
 			dialog = new Dialog(viewController.getActivity(), R.style.Modal);
76
 			dialog = new Dialog(viewController.getActivity(), R.style.Modal);
77
+			dialog.setOnKeyListener(this);
69
 		}
78
 		}
70
 
79
 
71
 		void show() {
80
 		void show() {
86
 			View decorView = viewController.getActivity().getWindow().getDecorView();
95
 			View decorView = viewController.getActivity().getWindow().getDecorView();
87
 			viewController.getView().measure(makeMeasureSpec(decorView.getMeasuredWidth(), EXACTLY), makeMeasureSpec(decorView.getMeasuredHeight(), EXACTLY));
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
 
2
 
3
 import android.app.Activity;
3
 import android.app.Activity;
4
 import android.support.annotation.NonNull;
4
 import android.support.annotation.NonNull;
5
+import android.support.annotation.Nullable;
5
 import android.view.ViewGroup;
6
 import android.view.ViewGroup;
6
 import android.widget.FrameLayout;
7
 import android.widget.FrameLayout;
7
 
8
 
35
 	@NonNull
36
 	@NonNull
36
 	@Override
37
 	@Override
37
 	public Collection<ViewController> getChildControllers() {
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
 	@Override
42
 	@Override
173
 			promise.reject(new Throwable("Nothing to pop"));
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
 		this.parentController = parentController;
56
 		this.parentController = parentController;
57
 	}
57
 	}
58
 
58
 
59
-    void performOnParentStack(Task<StackController> task) {
59
+    boolean performOnParentStack(Task<StackController> task) {
60
 	    if (parentController instanceof StackController) {
60
 	    if (parentController instanceof StackController) {
61
             task.run((StackController) parentController);
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
     void performOnParentStack(Task<StackController> accept, Runnable  reject) {
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
             reject.run();
73
             reject.run();
70
         }
74
         }
71
     }
75
     }

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

94
 		StackController stack2 = newStack();
94
 		StackController stack2 = newStack();
95
 		stack1.push(child1);
95
 		stack1.push(child1);
96
 		stack2.push(child2);
96
 		stack2.push(child2);
97
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
97
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
98
 		uut.setRoot(bottomTabsController);
98
 		uut.setRoot(bottomTabsController);
99
 
99
 
100
 		SimpleViewController newChild = new SimpleViewController(activity, "new child");
100
 		SimpleViewController newChild = new SimpleViewController(activity, "new child");
121
 		stack2.push(child2);
121
 		stack2.push(child2);
122
 		stack2.push(child3);
122
 		stack2.push(child3);
123
 		stack2.push(child4);
123
 		stack2.push(child4);
124
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
124
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
125
 		uut.setRoot(bottomTabsController);
125
 		uut.setRoot(bottomTabsController);
126
 
126
 
127
 		uut.pop("child4");
127
 		uut.pop("child4");
138
 		stack2.push(child2);
138
 		stack2.push(child2);
139
 		stack2.push(child3);
139
 		stack2.push(child3);
140
 		stack2.push(child4);
140
 		stack2.push(child4);
141
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
141
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
142
 		uut.setRoot(bottomTabsController);
142
 		uut.setRoot(bottomTabsController);
143
 
143
 
144
 		uut.popSpecific(child2.getId());
144
 		uut.popSpecific(child2.getId());
156
 		stack2.push(child3);
156
 		stack2.push(child3);
157
 		stack2.push(child4);
157
 		stack2.push(child4);
158
 		stack2.push(child5);
158
 		stack2.push(child5);
159
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
159
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
160
 		uut.setRoot(bottomTabsController);
160
 		uut.setRoot(bottomTabsController);
161
 
161
 
162
 		uut.popTo(child2.getId());
162
 		uut.popTo(child2.getId());
175
 		stack2.push(child4);
175
 		stack2.push(child4);
176
 		stack2.push(child5);
176
 		stack2.push(child5);
177
 
177
 
178
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
178
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
179
 		uut.setRoot(bottomTabsController);
179
 		uut.setRoot(bottomTabsController);
180
 
180
 
181
 		uut.popToRoot(child3.getId());
181
 		uut.popToRoot(child3.getId());
272
 		stack2.push(child2);
272
 		stack2.push(child2);
273
 		stack2.push(child3);
273
 		stack2.push(child3);
274
 		stack2.push(child4);
274
 		stack2.push(child4);
275
-		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
275
+		bottomTabsController.setTabs(Arrays.asList(stack1, stack2));
276
 		uut.setRoot(bottomTabsController);
276
 		uut.setRoot(bottomTabsController);
277
 
277
 
278
 		uut.pop("child4", new MockPromise() {
278
 		uut.pop("child4", new MockPromise() {
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
     this.onClickDismissAllPreviousModals = this.onClickDismissAllPreviousModals.bind(this);
24
     this.onClickDismissAllPreviousModals = this.onClickDismissAllPreviousModals.bind(this);
25
     this.onClickDismissFirstInStack = this.onClickDismissFirstInStack.bind(this);
25
     this.onClickDismissFirstInStack = this.onClickDismissFirstInStack.bind(this);
26
     this.onClickDismissAllModals = this.onClickDismissAllModals.bind(this);
26
     this.onClickDismissAllModals = this.onClickDismissAllModals.bind(this);
27
+    this.onClickPushScreen = this.onClickPushScreen.bind(this);
27
   }
28
   }
28
 
29
 
29
   render() {
30
   render() {
35
         <Button title="Dismiss Modal" testID={testIDs.DISMISS_MODAL_BUTTON} onPress={this.onClickDismissModal} />
36
         <Button title="Dismiss Modal" testID={testIDs.DISMISS_MODAL_BUTTON} onPress={this.onClickDismissModal} />
36
         <Button title="Dismiss Unknown Modal" testID={testIDs.DISMISS_UNKNOWN_MODAL_BUTTON} onPress={this.onClickDismissUnknownModal} />
37
         <Button title="Dismiss Unknown Modal" testID={testIDs.DISMISS_UNKNOWN_MODAL_BUTTON} onPress={this.onClickDismissUnknownModal} />
37
         <Button title="Dismiss All Modals" testID={testIDs.DISMISS_ALL_MODALS_BUTTON} onPress={this.onClickDismissAllModals} />
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
         {this.getPreviousModalId() ? (<Button title="Dismiss Previous Modal" testID={testIDs.DISMISS_PREVIOUS_MODAL_BUTTON} onPress={this.onClickDismissPreviousModal} />) : undefined}
40
         {this.getPreviousModalId() ? (<Button title="Dismiss Previous Modal" testID={testIDs.DISMISS_PREVIOUS_MODAL_BUTTON} onPress={this.onClickDismissPreviousModal} />) : undefined}
39
         {this.props.previousModalIds ? (<Button title="Dismiss ALL Previous Modals" testID={testIDs.DISMISS_ALL_PREVIOUS_MODAL_BUTTON} onPress={this.onClickDismissAllPreviousModals} />) : undefined}
41
         {this.props.previousModalIds ? (<Button title="Dismiss ALL Previous Modals" testID={testIDs.DISMISS_ALL_PREVIOUS_MODAL_BUTTON} onPress={this.onClickDismissAllPreviousModals} />) : undefined}
40
         {this.props.previousModalIds ? (<Button title="Dismiss First In Stack" testID={testIDs.DISMISS_FIRST_MODAL_BUTTON} onPress={this.onClickDismissFirstInStack} />) : undefined}
42
         {this.props.previousModalIds ? (<Button title="Dismiss First In Stack" testID={testIDs.DISMISS_FIRST_MODAL_BUTTON} onPress={this.onClickDismissFirstInStack} />) : undefined}
79
     Navigation.dismissAllModals();
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
   getModalPosition() {
93
   getModalPosition() {
83
     return (this.props.modalPosition || 1);
94
     return (this.props.modalPosition || 1);
84
   }
95
   }