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
     }
176
     }
177
 
177
 
178
     @ReactMethod
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
     @ReactMethod
183
     @ReactMethod

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

9
 import android.view.WindowManager;
9
 import android.view.WindowManager;
10
 
10
 
11
 import com.facebook.react.bridge.Callback;
11
 import com.facebook.react.bridge.Callback;
12
+import com.facebook.react.bridge.Promise;
12
 import com.reactnativenavigation.NavigationApplication;
13
 import com.reactnativenavigation.NavigationApplication;
13
 import com.reactnativenavigation.R;
14
 import com.reactnativenavigation.R;
14
 import com.reactnativenavigation.layouts.Layout;
15
 import com.reactnativenavigation.layouts.Layout;
160
     }
161
     }
161
 
162
 
162
     @Override
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
     @Override
168
     @Override

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

5
 import android.view.Window;
5
 import android.view.Window;
6
 
6
 
7
 import com.facebook.react.bridge.Callback;
7
 import com.facebook.react.bridge.Callback;
8
+import com.facebook.react.bridge.Promise;
8
 import com.reactnativenavigation.events.EventBus;
9
 import com.reactnativenavigation.events.EventBus;
9
 import com.reactnativenavigation.events.ModalDismissedEvent;
10
 import com.reactnativenavigation.events.ModalDismissedEvent;
10
 import com.reactnativenavigation.layouts.ScreenStackContainer;
11
 import com.reactnativenavigation.layouts.ScreenStackContainer;
58
         return !stack.empty();
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
     @Override
66
     @Override

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

12
 import android.view.Window;
12
 import android.view.Window;
13
 
13
 
14
 import com.facebook.react.bridge.Callback;
14
 import com.facebook.react.bridge.Callback;
15
+import com.facebook.react.bridge.Promise;
15
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
16
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
16
 import com.facebook.react.modules.core.PermissionAwareActivity;
17
 import com.facebook.react.modules.core.PermissionAwareActivity;
17
 import com.facebook.react.modules.core.PermissionListener;
18
 import com.facebook.react.modules.core.PermissionListener;
212
         super.onConfigurationChanged(newConfig);
213
         super.onConfigurationChanged(newConfig);
213
     }
214
     }
214
 
215
 
215
-    void push(ScreenParams params) {
216
+    void push(ScreenParams params, Promise onPushComplete) {
216
         if (modalController.containsNavigator(params.getNavigatorId())) {
217
         if (modalController.containsNavigator(params.getNavigatorId())) {
217
-            modalController.push(params);
218
+            modalController.push(params, onPushComplete);
218
         } else {
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
         NavigationApplication.instance.startActivity(intent);
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
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;
51
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;
52
         if (currentActivity == null) {
52
         if (currentActivity == null) {
53
             return;
53
             return;
57
         NavigationApplication.instance.runOnMainThread(new Runnable() {
57
         NavigationApplication.instance.runOnMainThread(new Runnable() {
58
             @Override
58
             @Override
59
             public void run() {
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
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
13
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
14
 import com.facebook.react.bridge.Arguments;
14
 import com.facebook.react.bridge.Arguments;
15
 import com.facebook.react.bridge.Callback;
15
 import com.facebook.react.bridge.Callback;
16
+import com.facebook.react.bridge.Promise;
16
 import com.facebook.react.bridge.WritableMap;
17
 import com.facebook.react.bridge.WritableMap;
17
 import com.reactnativenavigation.NavigationApplication;
18
 import com.reactnativenavigation.NavigationApplication;
18
 import com.reactnativenavigation.events.EventBus;
19
 import com.reactnativenavigation.events.EventBus;
364
     }
365
     }
365
 
366
 
366
     @Override
367
     @Override
367
-    public void push(final ScreenParams params) {
368
+    public void push(final ScreenParams params, final Promise onPushComplete) {
368
         performOnStack(params.getNavigatorId(), new Task<ScreenStack>() {
369
         performOnStack(params.getNavigatorId(), new Task<ScreenStack>() {
369
             @Override
370
             @Override
370
             public void run(ScreenStack screenStack) {
371
             public void run(ScreenStack screenStack) {
371
-                screenStack.push(params, createScreenLayoutParams(params));
372
+                screenStack.push(params, createScreenLayoutParams(params), onPushComplete);
372
                 if (isCurrentStack(screenStack)) {
373
                 if (isCurrentStack(screenStack)) {
373
                     setStyleFromScreen(params.styleParams);
374
                     setStyleFromScreen(params.styleParams);
374
                     EventBus.instance.post(new ScreenChangedEvent(params));
375
                     EventBus.instance.post(new ScreenChangedEvent(params));

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

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

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

7
 import android.widget.RelativeLayout;
7
 import android.widget.RelativeLayout;
8
 
8
 
9
 import com.facebook.react.bridge.Callback;
9
 import com.facebook.react.bridge.Callback;
10
+import com.facebook.react.bridge.Promise;
10
 import com.reactnativenavigation.NavigationApplication;
11
 import com.reactnativenavigation.NavigationApplication;
11
 import com.reactnativenavigation.events.EventBus;
12
 import com.reactnativenavigation.events.EventBus;
12
 import com.reactnativenavigation.events.ScreenChangedEvent;
13
 import com.reactnativenavigation.events.ScreenChangedEvent;
144
     }
145
     }
145
 
146
 
146
     @Override
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
         EventBus.instance.post(new ScreenChangedEvent(params));
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
 import android.widget.RelativeLayout.LayoutParams;
9
 import android.widget.RelativeLayout.LayoutParams;
10
 
10
 
11
 import com.facebook.react.bridge.Callback;
11
 import com.facebook.react.bridge.Callback;
12
+import com.facebook.react.bridge.Promise;
12
 import com.reactnativenavigation.NavigationApplication;
13
 import com.reactnativenavigation.NavigationApplication;
13
 import com.reactnativenavigation.params.ContextualMenuParams;
14
 import com.reactnativenavigation.params.ContextualMenuParams;
14
 import com.reactnativenavigation.params.FabParams;
15
 import com.reactnativenavigation.params.FabParams;
57
         final Screen nextScreen = ScreenFactory.create(activity, params, leftButtonOnClickListener);
58
         final Screen nextScreen = ScreenFactory.create(activity, params, leftButtonOnClickListener);
58
         final Screen previousScreen = stack.peek();
59
         final Screen previousScreen = stack.peek();
59
         if (isStackVisible) {
60
         if (isStackVisible) {
60
-            pushScreenToVisibleStack(layoutParams, nextScreen, previousScreen, new Screen.OnDisplayListener() {
61
+            pushScreenToVisibleStack(layoutParams, nextScreen, previousScreen, null, new Screen.OnDisplayListener() {
61
                 @Override
62
                 @Override
62
                 public void onDisplay() {
63
                 public void onDisplay() {
63
                     removeElementsBelowTop();
64
                     removeElementsBelowTop();
64
                 }
65
                 }
65
             });
66
             });
66
         } else {
67
         } else {
67
-            pushScreenToInvisibleStack(layoutParams, nextScreen, previousScreen);
68
+            pushScreenToInvisibleStack(layoutParams, nextScreen, previousScreen, null);
68
             removeElementsBelowTop();
69
             removeElementsBelowTop();
69
         }
70
         }
70
     }
71
     }
97
         addScreen(initialScreen, params);
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
         Screen nextScreen = ScreenFactory.create(activity, params, leftButtonOnClickListener);
102
         Screen nextScreen = ScreenFactory.create(activity, params, leftButtonOnClickListener);
102
         final Screen previousScreen = stack.peek();
103
         final Screen previousScreen = stack.peek();
103
         if (isStackVisible) {
104
         if (isStackVisible) {
104
             if (nextScreen.screenParams.sharedElementsTransitions.isEmpty()) {
105
             if (nextScreen.screenParams.sharedElementsTransitions.isEmpty()) {
105
-                pushScreenToVisibleStack(layoutParams, nextScreen, previousScreen);
106
+                pushScreenToVisibleStack(layoutParams, nextScreen, previousScreen, onPushComplete);
106
             } else {
107
             } else {
107
-                pushScreenToVisibleStackWithSharedElementTransition(layoutParams, nextScreen, previousScreen);
108
+                pushScreenToVisibleStackWithSharedElementTransition(layoutParams, nextScreen, previousScreen, onPushComplete);
108
             }
109
             }
109
         } else {
110
         } else {
110
-            pushScreenToInvisibleStack(layoutParams, nextScreen, previousScreen);
111
+            pushScreenToInvisibleStack(layoutParams, nextScreen, previousScreen, onPushComplete);
111
         }
112
         }
112
     }
113
     }
113
 
114
 
114
     private void pushScreenToVisibleStack(LayoutParams layoutParams, final Screen nextScreen,
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
     private void pushScreenToVisibleStack(LayoutParams layoutParams,
120
     private void pushScreenToVisibleStack(LayoutParams layoutParams,
120
                                           final Screen nextScreen,
121
                                           final Screen nextScreen,
121
                                           final Screen previousScreen,
122
                                           final Screen previousScreen,
123
+                                          @Nullable final Promise onPushComplete,
122
                                           @Nullable final Screen.OnDisplayListener onDisplay) {
124
                                           @Nullable final Screen.OnDisplayListener onDisplay) {
123
         nextScreen.setVisibility(View.INVISIBLE);
125
         nextScreen.setVisibility(View.INVISIBLE);
124
         addScreen(nextScreen, layoutParams);
126
         addScreen(nextScreen, layoutParams);
130
                     @Override
132
                     @Override
131
                     public void run() {
133
                     public void run() {
132
                         if (onDisplay != null) onDisplay.onDisplay();
134
                         if (onDisplay != null) onDisplay.onDisplay();
135
+                        if (onPushComplete != null) onPushComplete.resolve(null);
133
                         NavigationApplication.instance.getEventEmitter().sendDidDisappearEvent(previousScreen.getScreenParams(), NavigationType.Push);
136
                         NavigationApplication.instance.getEventEmitter().sendDidDisappearEvent(previousScreen.getScreenParams(), NavigationType.Push);
134
                         parent.removeView(previousScreen);
137
                         parent.removeView(previousScreen);
135
                     }
138
                     }
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
         nextScreen.setVisibility(View.INVISIBLE);
146
         nextScreen.setVisibility(View.INVISIBLE);
143
         nextScreen.setOnDisplayListener(new Screen.OnDisplayListener() {
147
         nextScreen.setOnDisplayListener(new Screen.OnDisplayListener() {
144
             @Override
148
             @Override
146
                 nextScreen.showWithSharedElementsTransitions(previousScreen.sharedElements.getToElements(), new Runnable() {
150
                 nextScreen.showWithSharedElementsTransitions(previousScreen.sharedElements.getToElements(), new Runnable() {
147
                     @Override
151
                     @Override
148
                     public void run() {
152
                     public void run() {
153
+                        if (onPushComplete != null) onPushComplete.resolve(null);
149
                         parent.removeView(previousScreen);
154
                         parent.removeView(previousScreen);
150
                     }
155
                     }
151
                 });
156
                 });
154
         addScreen(nextScreen, layoutParams);
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
         nextScreen.setVisibility(View.INVISIBLE);
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
         addScreen(nextScreen, layoutParams);
171
         addScreen(nextScreen, layoutParams);
160
         parent.removeView(previousScreen);
172
         parent.removeView(previousScreen);
161
     }
173
     }

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

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

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

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