Przeglądaj źródła

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 lat temu
rodzic
commit
d1febd3c23

+ 9
- 0
android/app/src/main/java/com/reactnativenavigation/screens/Screen.java Wyświetl plik

@@ -9,6 +9,7 @@ import android.view.Window;
9 9
 import android.widget.RelativeLayout;
10 10
 
11 11
 import com.facebook.react.bridge.Callback;
12
+import com.reactnativenavigation.NavigationApplication;
12 13
 import com.reactnativenavigation.animation.VisibilityAnimator;
13 14
 import com.reactnativenavigation.events.ContextualMenuHiddenEvent;
14 15
 import com.reactnativenavigation.events.Event;
@@ -218,19 +219,27 @@ public abstract class Screen extends RelativeLayout implements Subscriber {
218 219
     public abstract void setOnDisplayListener(OnDisplayListener onContentViewDisplayedListener);
219 220
 
220 221
     public void show() {
222
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("willAppear", screenParams.getNavigatorEventId());
223
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("didAppear", screenParams.getNavigatorEventId());
221 224
         screenAnimator.show(screenParams.animateScreenTransitions);
222 225
     }
223 226
 
224 227
     public void show(boolean animated) {
228
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("willAppear", screenParams.getNavigatorEventId());
229
+        NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("didAppear", screenParams.getNavigatorEventId());
225 230
         screenAnimator.show(animated);
226 231
     }
227 232
 
228 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 236
         setStyle();
230 237
         screenAnimator.show(animated, onAnimationEnd);
231 238
     }
232 239
 
233 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 243
         screenAnimator.hide(animated, onAnimatedEnd);
235 244
     }
236 245
 

+ 8
- 0
android/app/src/main/java/com/reactnativenavigation/screens/ScreenStack.java Wyświetl plik

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

+ 32
- 2
ios/RCCViewController.m Wyświetl plik

@@ -6,6 +6,7 @@
6 6
 #import <React/RCTRootView.h>
7 7
 #import "RCCManager.h"
8 8
 #import <React/RCTConvert.h>
9
+#import <React/RCTEventDispatcher.h>
9 10
 #import "RCCExternalViewControllerProtocol.h"
10 11
 #import "RCTHelpers.h"
11 12
 #import "RCCTitleViewHelper.h"
@@ -173,17 +174,46 @@ const NSInteger TRANSPARENT_NAVBAR_TAG = 78264803;
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 200
 - (void)viewWillAppear:(BOOL)animated
177 201
 {
178 202
     [super viewWillAppear:animated];
179
-    
203
+    [self sendScreenChangedEvent:@"willAppear"];
180 204
     [self setStyleOnAppear];
181 205
 }
182 206
 
207
+- (void)viewDidDisappear:(BOOL)animated
208
+{
209
+    [super viewDidDisappear:animated];
210
+    [self sendScreenChangedEvent:@"didDisappear"];
211
+}
212
+
183 213
 - (void)viewWillDisappear:(BOOL)animated
184 214
 {
185 215
     [super viewWillDisappear:animated];
186
-    
216
+    [self sendScreenChangedEvent:@"willDisappear"];
187 217
     [self setStyleOnDisappear];
188 218
 }
189 219