Browse Source

Support await navigator.push

Guy Carmeli 7 years ago
parent
commit
bc0d0c4544

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/bridge/NavigationReactModule.java View File

@@ -176,8 +176,8 @@ public class NavigationReactModule extends ReactContextBaseJavaModule {
176 176
     }
177 177
 
178 178
     @ReactMethod
179
-    public void push(final ReadableMap params) {
180
-        NavigationCommandsHandler.push(BundleConverter.toBundle(params));
179
+    public void push(final ReadableMap params, Promise onPushComplete) {
180
+        NavigationCommandsHandler.push(BundleConverter.toBundle(params), onPushComplete);
181 181
     }
182 182
 
183 183
     @ReactMethod

+ 3
- 2
android/app/src/main/java/com/reactnativenavigation/controllers/Modal.java View File

@@ -9,6 +9,7 @@ import android.view.Window;
9 9
 import android.view.WindowManager;
10 10
 
11 11
 import com.facebook.react.bridge.Callback;
12
+import com.facebook.react.bridge.Promise;
12 13
 import com.reactnativenavigation.NavigationApplication;
13 14
 import com.reactnativenavigation.R;
14 15
 import com.reactnativenavigation.layouts.Layout;
@@ -160,8 +161,8 @@ class Modal extends Dialog implements DialogInterface.OnDismissListener, ScreenS
160 161
     }
161 162
 
162 163
     @Override
163
-    public void push(ScreenParams params) {
164
-        layout.push(params);
164
+    public void push(ScreenParams params, Promise onPushComplete) {
165
+        layout.push(params, onPushComplete);
165 166
     }
166 167
 
167 168
     @Override

+ 3
- 2
android/app/src/main/java/com/reactnativenavigation/controllers/ModalController.java View File

@@ -5,6 +5,7 @@ import android.support.v7.app.AppCompatActivity;
5 5
 import android.view.Window;
6 6
 
7 7
 import com.facebook.react.bridge.Callback;
8
+import com.facebook.react.bridge.Promise;
8 9
 import com.reactnativenavigation.events.EventBus;
9 10
 import com.reactnativenavigation.events.ModalDismissedEvent;
10 11
 import com.reactnativenavigation.layouts.ScreenStackContainer;
@@ -58,8 +59,8 @@ class ModalController implements ScreenStackContainer, Modal.OnModalDismissedLis
58 59
         return !stack.empty();
59 60
     }
60 61
 
61
-    public void push(ScreenParams params) {
62
-        stack.peek().push(params);
62
+    public void push(ScreenParams params, Promise onPushComplete) {
63
+        stack.peek().push(params, onPushComplete);
63 64
     }
64 65
 
65 66
     @Override

+ 4
- 3
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java View File

@@ -12,6 +12,7 @@ import android.view.KeyEvent;
12 12
 import android.view.Window;
13 13
 
14 14
 import com.facebook.react.bridge.Callback;
15
+import com.facebook.react.bridge.Promise;
15 16
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
16 17
 import com.facebook.react.modules.core.PermissionAwareActivity;
17 18
 import com.facebook.react.modules.core.PermissionListener;
@@ -212,11 +213,11 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
212 213
         super.onConfigurationChanged(newConfig);
213 214
     }
214 215
 
215
-    void push(ScreenParams params) {
216
+    void push(ScreenParams params, Promise onPushComplete) {
216 217
         if (modalController.containsNavigator(params.getNavigatorId())) {
217
-            modalController.push(params);
218
+            modalController.push(params, onPushComplete);
218 219
         } else {
219
-            layout.push(params);
220
+            layout.push(params, onPushComplete);
220 221
         }
221 222
     }
222 223
 

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationCommandsHandler.java View File

@@ -47,7 +47,7 @@ public class NavigationCommandsHandler {
47 47
         NavigationApplication.instance.startActivity(intent);
48 48
     }
49 49
 
50
-    public static void push(Bundle screenParams) {
50
+    public static void push(Bundle screenParams, final Promise onPushComplete) {
51 51
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;
52 52
         if (currentActivity == null) {
53 53
             return;
@@ -57,7 +57,7 @@ public class NavigationCommandsHandler {
57 57
         NavigationApplication.instance.runOnMainThread(new Runnable() {
58 58
             @Override
59 59
             public void run() {
60
-                currentActivity.push(params);
60
+                currentActivity.push(params, onPushComplete);
61 61
             }
62 62
         });
63 63
     }

+ 3
- 2
android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java View File

@@ -13,6 +13,7 @@ import android.widget.RelativeLayout;
13 13
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
14 14
 import com.facebook.react.bridge.Arguments;
15 15
 import com.facebook.react.bridge.Callback;
16
+import com.facebook.react.bridge.Promise;
16 17
 import com.facebook.react.bridge.WritableMap;
17 18
 import com.reactnativenavigation.NavigationApplication;
18 19
 import com.reactnativenavigation.events.EventBus;
@@ -364,11 +365,11 @@ public class BottomTabsLayout extends BaseLayout implements AHBottomNavigation.O
364 365
     }
365 366
 
366 367
     @Override
367
-    public void push(final ScreenParams params) {
368
+    public void push(final ScreenParams params, final Promise onPushComplete) {
368 369
         performOnStack(params.getNavigatorId(), new Task<ScreenStack>() {
369 370
             @Override
370 371
             public void run(ScreenStack screenStack) {
371
-                screenStack.push(params, createScreenLayoutParams(params));
372
+                screenStack.push(params, createScreenLayoutParams(params), onPushComplete);
372 373
                 if (isCurrentStack(screenStack)) {
373 374
                     setStyleFromScreen(params.styleParams);
374 375
                     EventBus.instance.post(new ScreenChangedEvent(params));

+ 2
- 1
android/app/src/main/java/com/reactnativenavigation/layouts/ScreenStackContainer.java View File

@@ -1,10 +1,11 @@
1 1
 package com.reactnativenavigation.layouts;
2 2
 
3
+import com.facebook.react.bridge.Promise;
3 4
 import com.reactnativenavigation.params.ScreenParams;
4 5
 import com.reactnativenavigation.views.LeftButtonOnClickListener;
5 6
 
6 7
 public interface ScreenStackContainer extends LeftButtonOnClickListener {
7
-    void push(ScreenParams screenParams);
8
+    void push(ScreenParams screenParams, Promise onPushComplete);
8 9
 
9 10
     void pop(ScreenParams screenParams);
10 11
 

+ 3
- 2
android/app/src/main/java/com/reactnativenavigation/layouts/SingleScreenLayout.java View File

@@ -7,6 +7,7 @@ import android.view.View;
7 7
 import android.widget.RelativeLayout;
8 8
 
9 9
 import com.facebook.react.bridge.Callback;
10
+import com.facebook.react.bridge.Promise;
10 11
 import com.reactnativenavigation.NavigationApplication;
11 12
 import com.reactnativenavigation.events.EventBus;
12 13
 import com.reactnativenavigation.events.ScreenChangedEvent;
@@ -144,8 +145,8 @@ public class SingleScreenLayout extends BaseLayout {
144 145
     }
145 146
 
146 147
     @Override
147
-    public void push(ScreenParams params) {
148
-        stack.push(params, new LayoutParams(MATCH_PARENT, MATCH_PARENT));
148
+    public void push(ScreenParams params, Promise onPushComplete) {
149
+        stack.push(params, new LayoutParams(MATCH_PARENT, MATCH_PARENT), onPushComplete);
149 150
         EventBus.instance.post(new ScreenChangedEvent(params));
150 151
     }
151 152
 

+ 22
- 10
android/app/src/main/java/com/reactnativenavigation/screens/ScreenStack.java View File

@@ -9,6 +9,7 @@ import android.widget.RelativeLayout;
9 9
 import android.widget.RelativeLayout.LayoutParams;
10 10
 
11 11
 import com.facebook.react.bridge.Callback;
12
+import com.facebook.react.bridge.Promise;
12 13
 import com.reactnativenavigation.NavigationApplication;
13 14
 import com.reactnativenavigation.params.ContextualMenuParams;
14 15
 import com.reactnativenavigation.params.FabParams;
@@ -57,14 +58,14 @@ public class ScreenStack {
57 58
         final Screen nextScreen = ScreenFactory.create(activity, params, leftButtonOnClickListener);
58 59
         final Screen previousScreen = stack.peek();
59 60
         if (isStackVisible) {
60
-            pushScreenToVisibleStack(layoutParams, nextScreen, previousScreen, new Screen.OnDisplayListener() {
61
+            pushScreenToVisibleStack(layoutParams, nextScreen, previousScreen, null, new Screen.OnDisplayListener() {
61 62
                 @Override
62 63
                 public void onDisplay() {
63 64
                     removeElementsBelowTop();
64 65
                 }
65 66
             });
66 67
         } else {
67
-            pushScreenToInvisibleStack(layoutParams, nextScreen, previousScreen);
68
+            pushScreenToInvisibleStack(layoutParams, nextScreen, previousScreen, null);
68 69
             removeElementsBelowTop();
69 70
         }
70 71
     }
@@ -97,28 +98,29 @@ public class ScreenStack {
97 98
         addScreen(initialScreen, params);
98 99
     }
99 100
 
100
-    public void push(final ScreenParams params, LayoutParams layoutParams) {
101
+    public void push(final ScreenParams params, LayoutParams layoutParams, Promise onPushComplete) {
101 102
         Screen nextScreen = ScreenFactory.create(activity, params, leftButtonOnClickListener);
102 103
         final Screen previousScreen = stack.peek();
103 104
         if (isStackVisible) {
104 105
             if (nextScreen.screenParams.sharedElementsTransitions.isEmpty()) {
105
-                pushScreenToVisibleStack(layoutParams, nextScreen, previousScreen);
106
+                pushScreenToVisibleStack(layoutParams, nextScreen, previousScreen, onPushComplete);
106 107
             } else {
107
-                pushScreenToVisibleStackWithSharedElementTransition(layoutParams, nextScreen, previousScreen);
108
+                pushScreenToVisibleStackWithSharedElementTransition(layoutParams, nextScreen, previousScreen, onPushComplete);
108 109
             }
109 110
         } else {
110
-            pushScreenToInvisibleStack(layoutParams, nextScreen, previousScreen);
111
+            pushScreenToInvisibleStack(layoutParams, nextScreen, previousScreen, onPushComplete);
111 112
         }
112 113
     }
113 114
 
114 115
     private void pushScreenToVisibleStack(LayoutParams layoutParams, final Screen nextScreen,
115
-                                          final Screen previousScreen) {
116
-        pushScreenToVisibleStack(layoutParams, nextScreen, previousScreen, null);
116
+                                          final Screen previousScreen, Promise onPushComplete) {
117
+        pushScreenToVisibleStack(layoutParams, nextScreen, previousScreen, onPushComplete, null);
117 118
     }
118 119
 
119 120
     private void pushScreenToVisibleStack(LayoutParams layoutParams,
120 121
                                           final Screen nextScreen,
121 122
                                           final Screen previousScreen,
123
+                                          @Nullable final Promise onPushComplete,
122 124
                                           @Nullable final Screen.OnDisplayListener onDisplay) {
123 125
         nextScreen.setVisibility(View.INVISIBLE);
124 126
         addScreen(nextScreen, layoutParams);
@@ -130,6 +132,7 @@ public class ScreenStack {
130 132
                     @Override
131 133
                     public void run() {
132 134
                         if (onDisplay != null) onDisplay.onDisplay();
135
+                        if (onPushComplete != null) onPushComplete.resolve(null);
133 136
                         NavigationApplication.instance.getEventEmitter().sendDidDisappearEvent(previousScreen.getScreenParams(), NavigationType.Push);
134 137
                         parent.removeView(previousScreen);
135 138
                     }
@@ -138,7 +141,8 @@ public class ScreenStack {
138 141
         });
139 142
     }
140 143
 
141
-    private void pushScreenToVisibleStackWithSharedElementTransition(LayoutParams layoutParams, final Screen nextScreen, final Screen previousScreen) {
144
+    private void pushScreenToVisibleStackWithSharedElementTransition(LayoutParams layoutParams, final Screen nextScreen,
145
+                                                                     final Screen previousScreen, @Nullable final Promise onPushComplete) {
142 146
         nextScreen.setVisibility(View.INVISIBLE);
143 147
         nextScreen.setOnDisplayListener(new Screen.OnDisplayListener() {
144 148
             @Override
@@ -146,6 +150,7 @@ public class ScreenStack {
146 150
                 nextScreen.showWithSharedElementsTransitions(previousScreen.sharedElements.getToElements(), new Runnable() {
147 151
                     @Override
148 152
                     public void run() {
153
+                        if (onPushComplete != null) onPushComplete.resolve(null);
149 154
                         parent.removeView(previousScreen);
150 155
                     }
151 156
                 });
@@ -154,8 +159,15 @@ public class ScreenStack {
154 159
         addScreen(nextScreen, layoutParams);
155 160
     }
156 161
 
157
-    private void pushScreenToInvisibleStack(LayoutParams layoutParams, Screen nextScreen, Screen previousScreen) {
162
+    private void pushScreenToInvisibleStack(LayoutParams layoutParams, Screen nextScreen, Screen previousScreen,
163
+                                            @Nullable final Promise onPushComplete) {
158 164
         nextScreen.setVisibility(View.INVISIBLE);
165
+        nextScreen.setOnDisplayListener(new Screen.OnDisplayListener() {
166
+            @Override
167
+            public void onDisplay() {
168
+                if (onPushComplete != null) onPushComplete.resolve(null);
169
+            }
170
+        });
159 171
         addScreen(nextScreen, layoutParams);
160 172
         parent.removeView(previousScreen);
161 173
     }

+ 1
- 1
src/deprecated/platformSpecificDeprecated.android.js View File

@@ -80,7 +80,7 @@ function navigatorPush(navigator, params) {
80 80
   adapted.overrideBackPress = params.overrideBackPress;
81 81
   adapted.timestamp = Date.now();
82 82
 
83
-  newPlatformSpecific.push(adapted);
83
+  return newPlatformSpecific.push(adapted);
84 84
 }
85 85
 
86 86
 function navigatorPop(navigator, params) {

+ 1
- 1
src/platformSpecific.android.js View File

@@ -12,7 +12,7 @@ function startApp(activityParams) {
12 12
 
13 13
 function push(screenParams) {
14 14
   savePassProps(screenParams);
15
-  NativeReactModule.push(screenParams);
15
+  return NativeReactModule.push(screenParams);
16 16
 }
17 17
 
18 18
 function pop(screenParams) {