Browse Source

Static didAppear/didDisappear events

Guy Carmeli 6 years ago
parent
commit
8d3fbdf5f7

+ 17
- 0
AndroidE2E/app/src/androidTest/java/com/reactnativenavigation/e2e/androide2e/StaticLifecycleEvents.java View File

1
+package com.reactnativenavigation.e2e.androide2e;
2
+
3
+import android.support.test.uiautomator.By;
4
+
5
+import org.junit.Test;
6
+
7
+public class StaticLifecycleEvents extends BaseTest {
8
+    @Test
9
+    public void didAppearDidDisappear() throws Exception {
10
+        elementByText("STATIC LIFECYCLE EVENTS").click();
11
+        assertExists(By.text("Static Lifecycle Events"));
12
+        assertExists(By.text("didAppear | navigation.playground.StaticLifecycleOverlay"));
13
+        elementByText("PUSH").click();
14
+        assertExists(By.text("didAppear | navigation.playground.PushedScreen"));
15
+        assertExists(By.text("didDisappear | navigation.playground.WelcomeScreen"));
16
+    }
17
+}

+ 17
- 7
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationEvent.java View File

1
 package com.reactnativenavigation.react;
1
 package com.reactnativenavigation.react;
2
 
2
 
3
+import android.support.annotation.NonNull;
4
+
3
 import com.facebook.react.bridge.Arguments;
5
 import com.facebook.react.bridge.Arguments;
4
 import com.facebook.react.bridge.ReactContext;
6
 import com.facebook.react.bridge.ReactContext;
5
 import com.facebook.react.bridge.WritableMap;
7
 import com.facebook.react.bridge.WritableMap;
6
-import com.facebook.react.bridge.WritableNativeArray;
7
 import com.facebook.react.modules.core.DeviceEventManagerModule;
8
 import com.facebook.react.modules.core.DeviceEventManagerModule;
8
 
9
 
9
-import org.json.JSONException;
10
-import org.json.JSONObject;
11
-
12
 import static com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
10
 import static com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
13
 
11
 
14
 public class NavigationEvent {
12
 public class NavigationEvent {
16
 	private static final String componentDidAppear = "RNN.componentDidAppear";
14
 	private static final String componentDidAppear = "RNN.componentDidAppear";
17
 	private static final String componentDidDisappear = "RNN.componentDidDisappear";
15
 	private static final String componentDidDisappear = "RNN.componentDidDisappear";
18
 	private static final String onNavigationButtonPressed = "RNN.navigationButtonPressed";
16
 	private static final String onNavigationButtonPressed = "RNN.navigationButtonPressed";
17
+    private static final String componentLifecycle = "RNN.componentLifecycle";
19
 
18
 
20
 	private final RCTDeviceEventEmitter emitter;
19
 	private final RCTDeviceEventEmitter emitter;
21
 
20
 
27
 		emit(onAppLaunched);
26
 		emit(onAppLaunched);
28
 	}
27
 	}
29
 
28
 
30
-	public void componentDidDisappear(String id) {
29
+	public void componentDidDisappear(String id, String componentName) {
31
 		emit(componentDidDisappear, id);
30
 		emit(componentDidDisappear, id);
31
+		emit(componentLifecycle, getLifecycleEventData(id, componentName, "didDisappear"));
32
 	}
32
 	}
33
 
33
 
34
-	public void componentDidAppear(String id) {
34
+	public void componentDidAppear(String id, String componentName) {
35
 		emit(componentDidAppear, id);
35
 		emit(componentDidAppear, id);
36
+        emit(componentLifecycle, getLifecycleEventData(id, componentName, "didAppear"));
36
 	}
37
 	}
37
 
38
 
38
-	public void sendOnNavigationButtonPressed(String id, String buttonId) {
39
+    @NonNull
40
+    private WritableMap getLifecycleEventData(String id, String componentName, String didAppear) {
41
+        WritableMap map = Arguments.createMap();
42
+        map.putString("componentId", id);
43
+        map.putString("componentName", componentName);
44
+        map.putString("event", didAppear);
45
+        return map;
46
+    }
47
+
48
+    public void sendOnNavigationButtonPressed(String id, String buttonId) {
39
 		WritableMap map = Arguments.createMap();
49
 		WritableMap map = Arguments.createMap();
40
 		map.putString("componentId", id);
50
 		map.putString("componentId", id);
41
 		map.putString("buttonId", buttonId);
51
 		map.putString("buttonId", buttonId);

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/react/ReactView.java View File

60
 
60
 
61
 	@Override
61
 	@Override
62
 	public void sendComponentStart() {
62
 	public void sendComponentStart() {
63
-		new NavigationEvent(reactInstanceManager.getCurrentReactContext()).componentDidAppear(componentId);
63
+		new NavigationEvent(reactInstanceManager.getCurrentReactContext()).componentDidAppear(componentId, componentName);
64
 	}
64
 	}
65
 
65
 
66
 	@Override
66
 	@Override
67
 	public void sendComponentStop() {
67
 	public void sendComponentStop() {
68
-		new NavigationEvent(reactInstanceManager.getCurrentReactContext()).componentDidDisappear(componentId);
68
+		new NavigationEvent(reactInstanceManager.getCurrentReactContext()).componentDidDisappear(componentId, componentName);
69
 	}
69
 	}
70
 
70
 
71
     @Override
71
     @Override

+ 2
- 2
playground/src/screens/OptionsScreen.js View File

66
         <Button title="Custom Transition" onPress={this.onClickCustomTranstition} />
66
         <Button title="Custom Transition" onPress={this.onClickCustomTranstition} />
67
         <Button title="Show custom alert" testID={testIDs.SHOW_CUSTOM_ALERT_BUTTON} onPress={this.onClickAlert} />
67
         <Button title="Show custom alert" testID={testIDs.SHOW_CUSTOM_ALERT_BUTTON} onPress={this.onClickAlert} />
68
         <Button title="Show snackbar" testID={testIDs.SHOW_SNACKBAR_BUTTON} onPress={this.onClickSnackbar} />
68
         <Button title="Show snackbar" testID={testIDs.SHOW_SNACKBAR_BUTTON} onPress={this.onClickSnackbar} />
69
-        <Button title="Show overlay" testID={testIDs.SHOW_OVERLAY_BUTTON} onPress={() => this.onClickShowOverlay(true)} />
70
-        <Button title="Show touch through overlay" testID={testIDs.SHOW_TOUCH_THROUGH_OVERLAY_BUTTON} onPress={() => this.onClickShowOverlay(false)} />
69
+        <Button title="Show overlay inter" testID={testIDs.SHOW_OVERLAY_BUTTON} onPress={() => this.onClickShowOverlay(true)} />
70
+        <Button title="Show touch through overlay notInter" testID={testIDs.SHOW_TOUCH_THROUGH_OVERLAY_BUTTON} onPress={() => this.onClickShowOverlay(false)} />
71
         <Button title="Push Default Options Screen" testID={testIDs.PUSH_DEFAULT_OPTIONS_BUTTON} onPress={this.onClickPushDefaultOptionsScreen} />
71
         <Button title="Push Default Options Screen" testID={testIDs.PUSH_DEFAULT_OPTIONS_BUTTON} onPress={this.onClickPushDefaultOptionsScreen} />
72
         <Text style={styles.footer}>{`this.props.containerId = ${this.props.containerId}`}</Text>
72
         <Text style={styles.footer}>{`this.props.containerId = ${this.props.containerId}`}</Text>
73
       </View>
73
       </View>

+ 62
- 0
playground/src/screens/StaticLifecycleOverlay.js View File

1
+const React = require('react');
2
+const { Component } = require('react');
3
+const { View, Text } = require('react-native');
4
+const Navigation = require('react-native-navigation');
5
+
6
+class StaticLifecycleOverlay extends Component {
7
+  constructor(props) {
8
+    super(props);
9
+    this.state = {
10
+      text: 'nothing yet',
11
+      events: []
12
+    };
13
+    Navigation.events().componentLifecycle(({ event, componentName, componentId }) => {
14
+      console.log('guyca', `${event} ${componentName} [${componentId}]`);
15
+      this.setState({
16
+        events: [...this.state.events, { event, componentName, componentId }]
17
+      });
18
+    });
19
+  }
20
+
21
+  render() {
22
+    const events = this.state.events.map((event) =>
23
+      (
24
+        <View key={event.componentId}>
25
+          <Text style={styles.h2}>{`${event.event} | ${event.componentName}`}</Text>
26
+        </View>
27
+      ));
28
+    return (
29
+      <View style={styles.root}>
30
+        <Text style={styles.h1}>{`Static Lifecycle Events`}</Text>
31
+        <View style={styles.events}>
32
+          {events}
33
+        </View>
34
+      </View>
35
+    );
36
+  }
37
+}
38
+module.exports = StaticLifecycleOverlay;
39
+
40
+const styles = {
41
+  root: {
42
+    position: 'absolute',
43
+    bottom: 0,
44
+    left: 0,
45
+    right: 0,
46
+    height: 150,
47
+    backgroundColor: '#c1d5e0ae',
48
+    flexDirection: 'column'
49
+  },
50
+  events: {
51
+    flexDirection: 'column',
52
+    marginHorizontal: 2
53
+  },
54
+  h1: {
55
+    fontSize: 14,
56
+    textAlign: 'center',
57
+    margin: 4
58
+  },
59
+  h2: {
60
+    fontSize: 10
61
+  }
62
+};

+ 13
- 2
playground/src/screens/WelcomeScreen.js View File

23
     this.onClickPushOrientationMenuScreen = this.onClickPushOrientationMenuScreen.bind(this);
23
     this.onClickPushOrientationMenuScreen = this.onClickPushOrientationMenuScreen.bind(this);
24
     this.onClickBackHandler = this.onClickBackHandler.bind(this);
24
     this.onClickBackHandler = this.onClickBackHandler.bind(this);
25
     this.onClickPushTopTabsScreen = this.onClickPushTopTabsScreen.bind(this);
25
     this.onClickPushTopTabsScreen = this.onClickPushTopTabsScreen.bind(this);
26
+    this.onClickShowStaticLifecycleOverlay = this.onClickShowStaticLifecycleOverlay.bind(this);
26
   }
27
   }
27
 
28
 
28
   render() {
29
   render() {
29
     return (
30
     return (
30
-      <View style={styles.root}>
31
+      <View style={styles.root} key={'root'}>
31
         <Text testID={testIDs.WELCOME_SCREEN_HEADER} style={styles.h1}>{`React Native Navigation!`}</Text>
32
         <Text testID={testIDs.WELCOME_SCREEN_HEADER} style={styles.h1}>{`React Native Navigation!`}</Text>
32
         <Button title="Switch to tab based app" testID={testIDs.TAB_BASED_APP_BUTTON} onPress={this.onClickSwitchToTabs} />
33
         <Button title="Switch to tab based app" testID={testIDs.TAB_BASED_APP_BUTTON} onPress={this.onClickSwitchToTabs} />
33
         <Button title="Switch to app with side menus" testID={testIDs.TAB_BASED_APP_SIDE_BUTTON} onPress={this.onClickSwitchToSideMenus} />
34
         <Button title="Switch to app with side menus" testID={testIDs.TAB_BASED_APP_SIDE_BUTTON} onPress={this.onClickSwitchToSideMenus} />
34
         <Button title="Push Lifecycle Screen" testID={testIDs.PUSH_LIFECYCLE_BUTTON} onPress={this.onClickLifecycleScreen} />
35
         <Button title="Push Lifecycle Screen" testID={testIDs.PUSH_LIFECYCLE_BUTTON} onPress={this.onClickLifecycleScreen} />
36
+        <Button title="Static Lifecycle Events" testID={testIDs.PUSH_STATIC_LIFECYCLE_BUTTON} onPress={this.onClickShowStaticLifecycleOverlay} />
35
         <Button title="Push" testID={testIDs.PUSH_BUTTON} onPress={this.onClickPush} />
37
         <Button title="Push" testID={testIDs.PUSH_BUTTON} onPress={this.onClickPush} />
36
         <Button title="Push Options Screen" testID={testIDs.PUSH_OPTIONS_BUTTON} onPress={this.onClickPushOptionsScreen} />
38
         <Button title="Push Options Screen" testID={testIDs.PUSH_OPTIONS_BUTTON} onPress={this.onClickPushOptionsScreen} />
37
         <Button title="Push Top Tabs screen" testID={testIDs.PUSH_TOP_TABS_BUTTON} onPress={this.onClickPushTopTabsScreen} />
39
         <Button title="Push Top Tabs screen" testID={testIDs.PUSH_TOP_TABS_BUTTON} onPress={this.onClickPushTopTabsScreen} />
192
     });
194
     });
193
   }
195
   }
194
 
196
 
197
+  onClickShowStaticLifecycleOverlay() {
198
+    Navigation.showOverlay({
199
+      component: {
200
+        name: 'navigation.playground.StaticLifecycleOverlay'
201
+      }
202
+    });
203
+  }
204
+
195
   async onClickShowModal() {
205
   async onClickShowModal() {
196
     await Navigation.showModal({
206
     await Navigation.showModal({
197
       stack: {
207
       stack: {
312
   root: {
322
   root: {
313
     flexGrow: 1,
323
     flexGrow: 1,
314
     justifyContent: 'center',
324
     justifyContent: 'center',
315
-    alignItems: 'center'
325
+    alignItems: 'center',
326
+    backgroundColor: 'white'
316
   },
327
   },
317
   h1: {
328
   h1: {
318
     fontSize: 24,
329
     fontSize: 24,

+ 2
- 0
playground/src/screens/index.js View File

3
 const TextScreen = require('./TextScreen');
3
 const TextScreen = require('./TextScreen');
4
 const PushedScreen = require('./PushedScreen');
4
 const PushedScreen = require('./PushedScreen');
5
 const LifecycleScreen = require('./LifecycleScreen');
5
 const LifecycleScreen = require('./LifecycleScreen');
6
+const StaticLifecycleOverlay = require('./StaticLifecycleOverlay');
6
 const ModalScreen = require('./ModalScreen');
7
 const ModalScreen = require('./ModalScreen');
7
 const OptionsScreen = require('./OptionsScreen');
8
 const OptionsScreen = require('./OptionsScreen');
8
 const OrientationSelectScreen = require('./OrientationSelectScreen');
9
 const OrientationSelectScreen = require('./OrientationSelectScreen');
23
   Navigation.registerComponent(`navigation.playground.WelcomeScreen`, () => WelcomeScreen);
24
   Navigation.registerComponent(`navigation.playground.WelcomeScreen`, () => WelcomeScreen);
24
   Navigation.registerComponent(`navigation.playground.ModalScreen`, () => ModalScreen);
25
   Navigation.registerComponent(`navigation.playground.ModalScreen`, () => ModalScreen);
25
   Navigation.registerComponent(`navigation.playground.LifecycleScreen`, () => LifecycleScreen);
26
   Navigation.registerComponent(`navigation.playground.LifecycleScreen`, () => LifecycleScreen);
27
+  Navigation.registerComponent(`navigation.playground.StaticLifecycleOverlay`, () => StaticLifecycleOverlay);
26
   Navigation.registerComponent(`navigation.playground.TextScreen`, () => TextScreen);
28
   Navigation.registerComponent(`navigation.playground.TextScreen`, () => TextScreen);
27
   Navigation.registerComponent(`navigation.playground.PushedScreen`, () => PushedScreen);
29
   Navigation.registerComponent(`navigation.playground.PushedScreen`, () => PushedScreen);
28
   Navigation.registerComponent(`navigation.playground.OptionsScreen`, () => OptionsScreen);
30
   Navigation.registerComponent(`navigation.playground.OptionsScreen`, () => OptionsScreen);

+ 1
- 0
playground/src/testIDs.js View File

5
   TAB_BASED_APP_BUTTON: `TAB_BASED_APP_BUTTON`,
5
   TAB_BASED_APP_BUTTON: `TAB_BASED_APP_BUTTON`,
6
   TAB_BASED_APP_SIDE_BUTTON: `TAB_BASED_APP_SIDE_BUTTON`,
6
   TAB_BASED_APP_SIDE_BUTTON: `TAB_BASED_APP_SIDE_BUTTON`,
7
   PUSH_LIFECYCLE_BUTTON: `PUSH_LIFECYCLE_BUTTON`,
7
   PUSH_LIFECYCLE_BUTTON: `PUSH_LIFECYCLE_BUTTON`,
8
+  PUSH_STATIC_LIFECYCLE_BUTTON: `PUSH_STATIC_LIFECYCLE_BUTTON`,
8
   PUSH_BUTTON: `PUSH_BUTTON`,
9
   PUSH_BUTTON: `PUSH_BUTTON`,
9
   PUSH_OPTIONS_BUTTON: `PUSH_OPTIONS_BUTTON`,
10
   PUSH_OPTIONS_BUTTON: `PUSH_OPTIONS_BUTTON`,
10
   PUSH_DEFAULT_OPTIONS_BUTTON: `PUSH_DEFAULT_OPTIONS_BUTTON`,
11
   PUSH_DEFAULT_OPTIONS_BUTTON: `PUSH_DEFAULT_OPTIONS_BUTTON`,