Browse Source

Feature/view lifecycle (#798)

* Adds iOS callback for viewWillAppear/didAppear e.t.c.

* Adds in screen events on Android

* Fixed issue with rightButtons getting ignored in preference for empty Screen buttons

* Android screen lifecycle

This commit is contains a few changes and enhancements to the PR made by simon:

* Use getEventEmitter instead of sendNavigatorEvent
* Dispatch willDisappear and didDisappear events when pushing screens
* Dispatch willAppear and didAppear events when popping screen
Simon Mitchell 7 years ago
parent
commit
d1febd3c23

+ 9
- 0
android/app/src/main/java/com/reactnativenavigation/screens/Screen.java View File

9
 import android.widget.RelativeLayout;
9
 import android.widget.RelativeLayout;
10
 
10
 
11
 import com.facebook.react.bridge.Callback;
11
 import com.facebook.react.bridge.Callback;
12
+import com.reactnativenavigation.NavigationApplication;
12
 import com.reactnativenavigation.animation.VisibilityAnimator;
13
 import com.reactnativenavigation.animation.VisibilityAnimator;
13
 import com.reactnativenavigation.events.ContextualMenuHiddenEvent;
14
 import com.reactnativenavigation.events.ContextualMenuHiddenEvent;
14
 import com.reactnativenavigation.events.Event;
15
 import com.reactnativenavigation.events.Event;
218
     public abstract void setOnDisplayListener(OnDisplayListener onContentViewDisplayedListener);
219
     public abstract void setOnDisplayListener(OnDisplayListener onContentViewDisplayedListener);
219
 
220
 
220
     public void show() {
221
     public void show() {
222
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("willAppear", screenParams.getNavigatorEventId());
223
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("didAppear", screenParams.getNavigatorEventId());
221
         screenAnimator.show(screenParams.animateScreenTransitions);
224
         screenAnimator.show(screenParams.animateScreenTransitions);
222
     }
225
     }
223
 
226
 
224
     public void show(boolean animated) {
227
     public void show(boolean animated) {
228
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("willAppear", screenParams.getNavigatorEventId());
229
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("didAppear", screenParams.getNavigatorEventId());
225
         screenAnimator.show(animated);
230
         screenAnimator.show(animated);
226
     }
231
     }
227
 
232
 
228
     public void show(boolean animated, Runnable onAnimationEnd) {
233
     public void show(boolean animated, Runnable onAnimationEnd) {
234
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("willAppear", screenParams.getNavigatorEventId());
235
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("didAppear", screenParams.getNavigatorEventId());
229
         setStyle();
236
         setStyle();
230
         screenAnimator.show(animated, onAnimationEnd);
237
         screenAnimator.show(animated, onAnimationEnd);
231
     }
238
     }
232
 
239
 
233
     public void hide(boolean animated, Runnable onAnimatedEnd) {
240
     public void hide(boolean animated, Runnable onAnimatedEnd) {
241
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("willDisappear", screenParams.getNavigatorEventId());
242
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("didDisappear", screenParams.getNavigatorEventId());
234
         screenAnimator.hide(animated, onAnimatedEnd);
243
         screenAnimator.hide(animated, onAnimatedEnd);
235
     }
244
     }
236
 
245
 

+ 8
- 0
android/app/src/main/java/com/reactnativenavigation/screens/ScreenStack.java View File

117
                                           @Nullable final Screen.OnDisplayListener onDisplay) {
117
                                           @Nullable final Screen.OnDisplayListener onDisplay) {
118
         nextScreen.setVisibility(View.INVISIBLE);
118
         nextScreen.setVisibility(View.INVISIBLE);
119
         addScreen(nextScreen, layoutParams);
119
         addScreen(nextScreen, layoutParams);
120
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("willDisappear", previousScreen.getNavigatorEventId());
120
         nextScreen.setOnDisplayListener(new Screen.OnDisplayListener() {
121
         nextScreen.setOnDisplayListener(new Screen.OnDisplayListener() {
121
             @Override
122
             @Override
122
             public void onDisplay() {
123
             public void onDisplay() {
124
                     @Override
125
                     @Override
125
                     public void run() {
126
                     public void run() {
126
                         if (onDisplay != null) onDisplay.onDisplay();
127
                         if (onDisplay != null) onDisplay.onDisplay();
128
+                        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("didDisappear", previousScreen.getNavigatorEventId());
127
                         parent.removeView(previousScreen);
129
                         parent.removeView(previousScreen);
128
                     }
130
                     }
129
                 });
131
                 });
196
 
198
 
197
     private void readdPrevious(Screen previous) {
199
     private void readdPrevious(Screen previous) {
198
         previous.setVisibility(View.VISIBLE);
200
         previous.setVisibility(View.VISIBLE);
201
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("willAppear", previous.getNavigatorEventId());
202
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("didAppear", previous.getNavigatorEventId());
199
         parent.addView(previous, 0);
203
         parent.addView(previous, 0);
200
     }
204
     }
201
 
205
 
347
         isStackVisible = true;
351
         isStackVisible = true;
348
         stack.peek().setStyle();
352
         stack.peek().setStyle();
349
         stack.peek().setVisibility(View.VISIBLE);
353
         stack.peek().setVisibility(View.VISIBLE);
354
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("willAppear", stack.peek().getNavigatorEventId());
355
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("didAppear", stack.peek().getNavigatorEventId());
350
     }
356
     }
351
 
357
 
352
     public void hide() {
358
     public void hide() {
359
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("willDisappear", stack.peek().getNavigatorEventId());
360
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("didDisappear", stack.peek().getNavigatorEventId());
353
         isStackVisible = false;
361
         isStackVisible = false;
354
         stack.peek().setVisibility(View.INVISIBLE);
362
         stack.peek().setVisibility(View.INVISIBLE);
355
     }
363
     }

+ 32
- 2
ios/RCCViewController.m View File

6
 #import <React/RCTRootView.h>
6
 #import <React/RCTRootView.h>
7
 #import "RCCManager.h"
7
 #import "RCCManager.h"
8
 #import <React/RCTConvert.h>
8
 #import <React/RCTConvert.h>
9
+#import <React/RCTEventDispatcher.h>
9
 #import "RCCExternalViewControllerProtocol.h"
10
 #import "RCCExternalViewControllerProtocol.h"
10
 #import "RCTHelpers.h"
11
 #import "RCTHelpers.h"
11
 #import "RCCTitleViewHelper.h"
12
 #import "RCCTitleViewHelper.h"
173
     }
174
     }
174
 }
175
 }
175
 
176
 
177
+- (void)sendScreenChangedEvent:(NSString *)eventName
178
+{
179
+    if ([self.view isKindOfClass:[RCTRootView class]]){
180
+        
181
+        RCTRootView *rootView = (RCTRootView *)self.view;
182
+        
183
+        if (rootView.appProperties && rootView.appProperties[@"navigatorEventID"]) {
184
+            
185
+            [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:rootView.appProperties[@"navigatorEventID"] body:@
186
+             {
187
+                 @"type": @"ScreenChangedEvent",
188
+                 @"method": eventName
189
+             }];
190
+        }
191
+    }
192
+}
193
+
194
+- (void)viewDidAppear:(BOOL)animated
195
+{
196
+    [super viewDidAppear:animated];
197
+    [self sendScreenChangedEvent:@"didAppear"];
198
+}
199
+
176
 - (void)viewWillAppear:(BOOL)animated
200
 - (void)viewWillAppear:(BOOL)animated
177
 {
201
 {
178
     [super viewWillAppear:animated];
202
     [super viewWillAppear:animated];
179
-    
203
+    [self sendScreenChangedEvent:@"willAppear"];
180
     [self setStyleOnAppear];
204
     [self setStyleOnAppear];
181
 }
205
 }
182
 
206
 
207
+- (void)viewDidDisappear:(BOOL)animated
208
+{
209
+    [super viewDidDisappear:animated];
210
+    [self sendScreenChangedEvent:@"didDisappear"];
211
+}
212
+
183
 - (void)viewWillDisappear:(BOOL)animated
213
 - (void)viewWillDisappear:(BOOL)animated
184
 {
214
 {
185
     [super viewWillDisappear:animated];
215
     [super viewWillDisappear:animated];
186
-    
216
+    [self sendScreenChangedEvent:@"willDisappear"];
187
     [self setStyleOnDisappear];
217
     [self setStyleOnDisappear];
188
 }
218
 }
189
 
219