Browse Source

added deep links

talkol 8 years ago
parent
commit
eeca8d766f
3 changed files with 67 additions and 8 deletions
  1. 21
    7
      example/src/screens/SecondTabScreen.js
  2. 32
    1
      example/src/screens/SideMenu.js
  3. 14
    0
      src/Screen.js

+ 21
- 7
example/src/screens/SecondTabScreen.js View File

64
     });
64
     });
65
   }
65
   }
66
   onNavigatorEvent(event) {
66
   onNavigatorEvent(event) {
67
-    if (event.id == 'edit') {
68
-      AlertIOS.alert('NavBar', 'Dynamic Edit button pressed');
67
+    // handle a deep link
68
+    if (event.type == 'DeepLink') {
69
+      const parts = event.link.split('/');
70
+      if (parts[0] == 'tab2') {
71
+        this.props.navigator.resetTo({
72
+          title: "Replaced Root",
73
+          screen: parts[1],
74
+          animated: true
75
+        });
76
+      }
69
     }
77
     }
70
-    if (event.id == 'add') {
71
-      AlertIOS.alert('NavBar', 'Dynamic Add button pressed');
72
-    }
73
-    if (event.id == 'save') {
74
-      AlertIOS.alert('NavBar', 'Dynamic Save button pressed');
78
+    // handle a button press
79
+    if (event.type == 'NavBarButtonPress') {
80
+      if (event.id == 'edit') {
81
+        AlertIOS.alert('NavBar', 'Dynamic Edit button pressed');
82
+      }
83
+      if (event.id == 'add') {
84
+        AlertIOS.alert('NavBar', 'Dynamic Add button pressed');
85
+      }
86
+      if (event.id == 'save') {
87
+        AlertIOS.alert('NavBar', 'Dynamic Save button pressed');
88
+      }
75
     }
89
     }
76
   }
90
   }
77
 }
91
 }

+ 32
- 1
example/src/screens/SideMenu.js View File

16
     return (
16
     return (
17
       <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
17
       <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
18
 
18
 
19
-        <Text>Side Menu</Text>
19
+        <Text style={styles.title}>Side Menu</Text>
20
+
21
+        <TouchableOpacity onPress={ this.onReplaceTab2Press.bind(this) }>
22
+          <Text style={styles.button}>Replace Tab#2 Root</Text>
23
+        </TouchableOpacity>
24
+
25
+        <TouchableOpacity onPress={ this.onModalPress.bind(this) }>
26
+          <Text style={styles.button}>Show Modal Screen</Text>
27
+        </TouchableOpacity>
20
 
28
 
21
       </View>
29
       </View>
22
     );
30
     );
23
   }
31
   }
32
+  onReplaceTab2Press() {
33
+    this.props.navigator.toggleDrawer({
34
+      to: 'closed',
35
+      side: 'left',
36
+      animated: true
37
+    });
38
+    this.props.navigator.handleDeepLink({
39
+      link: "tab2/example.PushedScreen"
40
+    });
41
+  }
42
+  onModalPress() {
43
+    this.props.navigator.showModal({
44
+      title: "Modal",
45
+      screen: "example.ModalScreen"
46
+    });
47
+  }
24
 }
48
 }
25
 
49
 
26
 const styles = StyleSheet.create({
50
 const styles = StyleSheet.create({
51
+  title: {
52
+    textAlign: 'center',
53
+    fontSize: 18,
54
+    marginBottom: 10,
55
+    marginTop:10,
56
+    fontWeight: '500'
57
+  },
27
   button: {
58
   button: {
28
     textAlign: 'center',
59
     textAlign: 'center',
29
     fontSize: 18,
60
     fontSize: 18,

+ 14
- 0
src/Screen.js View File

2
 import platformSpecific from './platformSpecific';
2
 import platformSpecific from './platformSpecific';
3
 import Navigation from './Navigation';
3
 import Navigation from './Navigation';
4
 
4
 
5
+const _allNavigatorEventHandlers = {};
6
+
5
 class Navigator {
7
 class Navigator {
6
   constructor(navigatorID, navigatorEventID) {
8
   constructor(navigatorID, navigatorEventID) {
7
     this.navigatorID = navigatorID;
9
     this.navigatorID = navigatorID;
40
     this.navigatorEventHandler = callback;
42
     this.navigatorEventHandler = callback;
41
     if (!this.navigatorEventSubscription) {
43
     if (!this.navigatorEventSubscription) {
42
       this.navigatorEventSubscription = NativeAppEventEmitter.addListener(this.navigatorEventID, (event) => this.onNavigatorEvent(event));
44
       this.navigatorEventSubscription = NativeAppEventEmitter.addListener(this.navigatorEventID, (event) => this.onNavigatorEvent(event));
45
+      _allNavigatorEventHandlers[this.navigatorEventID] = (event) => this.onNavigatorEvent(event);
46
+    }
47
+  }
48
+  handleDeepLink(params = {}) {
49
+    if (!params.link) return;
50
+    const event = {
51
+      type: 'DeepLink',
52
+      link: params.link
53
+    };
54
+    for (let i in _allNavigatorEventHandlers) {
55
+      _allNavigatorEventHandlers[i](event);
43
     }
56
     }
44
   }
57
   }
45
   onNavigatorEvent(event) {
58
   onNavigatorEvent(event) {
50
   cleanup() {
63
   cleanup() {
51
     if (this.navigatorEventSubscription) {
64
     if (this.navigatorEventSubscription) {
52
       this.navigatorEventSubscription.remove();
65
       this.navigatorEventSubscription.remove();
66
+      delete _allNavigatorEventHandlers[this.navigatorEventID];
53
     }
67
     }
54
   }
68
   }
55
 }
69
 }