Skip to content

Commit d1f558a

Browse files
committed
Check-in with routing structure for demos
1 parent 10b6919 commit d1f558a

File tree

9 files changed

+260
-6
lines changed

9 files changed

+260
-6
lines changed

app/components/List/List.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import React, { PropTypes as T } from 'react'
2+
3+
import {
4+
View,
5+
Text,
6+
StyleSheet,
7+
TouchableHighlight,
8+
ListView
9+
} from 'react-native'
10+
11+
import colors from '../../styles/colors'
12+
import appStyles from '../../styles/app';
13+
14+
export class List extends React.Component {
15+
static propTypes = {
16+
initialRows: T.array,
17+
onRowPress: T.func
18+
}
19+
20+
static defaultProps = {
21+
initialRows: []
22+
}
23+
24+
constructor(props, context) {
25+
super(props, context);
26+
27+
const dataSource = new ListView.DataSource({
28+
sectionHeaderHasChanged: this._sectionHeaderHasChanged.bind(this),
29+
rowHasChanged: this._rowHasChanged.bind(this)
30+
});
31+
32+
const {initialRows} = this.props;
33+
34+
this.state = {
35+
dataSource: dataSource.cloneWithRows(initialRows)
36+
}
37+
}
38+
39+
_sectionHeaderHasChanged(oldSection, newSection) {
40+
return oldSection !== newSection;
41+
}
42+
43+
_rowHasChanged(oldRow, newRow) {
44+
return oldRow !== newRow;
45+
}
46+
47+
render() {
48+
const {style} = this.props;
49+
const headerStyle = [styles.header, style];
50+
51+
return (
52+
<ListView
53+
ref="listView"
54+
scrollRenderAheadDistance={0}
55+
automaticallyAdjustContentInsets={true}
56+
dataSource={this.state.dataSource}
57+
renderSectionHeader={this._renderSectionHeader.bind(this)}
58+
renderRow={this._renderRow.bind(this)}
59+
renderFooter={this._renderFooter.bind(this)}
60+
renderSeparator={this._renderSeparator.bind(this)}
61+
/>
62+
)
63+
}
64+
65+
_renderSectionHeader(data, sectionId) {
66+
return <View></View>; // empty, for now
67+
return (
68+
<View style={styles.sectionHeader}>
69+
<Text style={styles.sectionHeaderText}>{sectionId}</Text>
70+
</View>
71+
)
72+
}
73+
74+
_renderSeparator(sectionID: number, rowID: number, adjacentRowHighlighted: bool) {
75+
return (
76+
<View
77+
key={`${sectionID}-${rowID}`}
78+
style={{
79+
height: adjacentRowHighlighted ? 4 : 1,
80+
backgroundColor: adjacentRowHighlighted ? '#3B5998' : '#CCCCCC',
81+
}}
82+
/>
83+
);
84+
}
85+
86+
_defaultRenderRow(rowData, sectionID, rowID, highlightRow) {
87+
return (
88+
<View style={appStyles.row}>
89+
<Text>{rowData.title}</Text>
90+
</View>
91+
)
92+
}
93+
94+
_renderRow(rowData, sectionID, rowID, highlightRow) {
95+
const {renderRow} = this.props;
96+
97+
return (
98+
<TouchableHighlight onPress={() => {
99+
this._pressRow(rowData, sectionID, rowID, highlightRow);
100+
}}>
101+
{renderRow ?
102+
renderRow(rowData, sectionID, rowID, highlightRow) :
103+
this._defaultRenderRow(rowData, sectionID, rowID, highlightRow)}
104+
</TouchableHighlight>
105+
)
106+
}
107+
108+
_pressRow(...args) {
109+
const {onRowPress} = this.props;
110+
if (onRowPress && typeof onRowPress === 'function') {
111+
onRowPress(...args);
112+
}
113+
}
114+
115+
_renderFooter() {
116+
return (
117+
<View style={[appStyles.container, styles.scrollSpinner]}>
118+
<Text></Text>
119+
</View>
120+
)
121+
}
122+
123+
}
124+
125+
const styles = StyleSheet.create({
126+
header: {
127+
}
128+
})
129+
130+
export default List;

app/components/Navigation/Navigator.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class FirestackNavigator extends React.Component {
5959
<Scene
6060
{...componentProps}
6161
key={scene.key}
62+
firestack={this.props.firestack}
6263
navigate={this._navigate.bind(this)}>
6364
<Component />
6465
</Scene>

app/components/Navigation/Scene.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ class Scene extends Component {
3333
};
3434

3535
render(): ReactElement<any> {
36-
const {scene, navigate, children} = this.props;
36+
const {scene, navigate, children, firestack} = this.props;
37+
const childProps = Object.assign({}, this.props, {firestack})
3738
return (
3839
<Animated.View
3940
style={[styles.scene, this._getAnimatedStyle()]}>
4041
{Children.map(children,
41-
c => React.cloneElement(c, this.props))}
42+
c => React.cloneElement(c, childProps))}
4243
</Animated.View>
4344
);
4445
}

app/containers/App.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ export class App extends React.Component {
1717
}
1818

1919
render() {
20-
const {navigationState} = this.props;
20+
const {firestack, navigationState} = this.props;
2121
return (
2222
<View style={styles.container}>
2323
<FirestackNavigator
2424
{...this.props}
25+
firestack={firestack}
2526
navigationState={navigationState}
2627
navigate={this._navigate.bind(this)} />
2728
</View>
@@ -37,6 +38,7 @@ const styles = StyleSheet.create({
3738

3839
export default connect(state => {
3940
return {
40-
navigationState: state.navigation
41+
navigationState: state.navigation,
42+
firestack: state.firestack
4143
}
4244
})(App);

app/routes.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import appStyles from './styles/app';
99
import Home from './views/Home';
1010
import Database from './views/Database';
1111
import Authentication from './views/Authentication';
12+
import Messaging, { Routes as MessagingRoutes } from './views/Messaging';
1213

1314
export type Route = {
1415
key: String,
@@ -28,6 +29,13 @@ export const exampleRoutes = {
2829
title: 'Authentication',
2930
Component: Authentication
3031
}
32+
},
33+
'messaging': {
34+
route: {
35+
title: 'Messaging',
36+
Component: Messaging
37+
},
38+
children: MessagingRoutes
3139
}
3240
}
3341

@@ -43,4 +51,5 @@ export const routes = toList({
4351
},
4452
});
4553

54+
console.log('routes ->', routes);
4655
export default routes;

app/views/Database/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ export class Database extends React.Component {
1919
}
2020

2121
componentDidMount() {
22-
const ref = this.db.ref('chat-messages').child('roomId');
22+
const roomId = 'roomId';
23+
const ref = this.db.ref('chat-messages').child(roomId);
24+
ref.keepSynced(true);
2325
ref.orderByKey().limitToLast(3).once('value').then(snapshot => {
2426
console.log('snapshot ->', snapshot);
2527
});

app/views/Home.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class Home extends React.Component {
105105
_pressRow(rowKey) {
106106
const {actions} = this.props;
107107
const {navigation} = actions;
108-
navigation.push(rowKey);
108+
navigation.push(rowKey, this.props);
109109
}
110110

111111
_renderFooter() {

app/views/Messaging/Demos/Setup.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from 'react'
2+
3+
import {
4+
View,
5+
Text,
6+
} from 'react-native'
7+
8+
import appStyles from '../../../styles/app';
9+
10+
export class Setup extends React.Component {
11+
12+
componentWillMount() {
13+
const {firestack} = this.props;
14+
15+
firestack.cloudMessaging.getToken().then(function (token) {
16+
console.log('device token', token);
17+
});
18+
19+
firestack.cloudMessaging.subscribeToTopic("setup_topic").then(function (topic) {
20+
console.log('Subscribe:'+topic);
21+
}).catch(function(err){
22+
console.error(err);
23+
});
24+
25+
firestack.cloudMessaging.listenForReceiveNotification((msg) =>{
26+
console.log('Receive Messages:'+msg.data);
27+
console.log('Receive Messages:'+msg.notification);
28+
});
29+
}
30+
31+
_sendHi() {
32+
const {firestack} = this.props;
33+
firestack.cloudMessaging.send("hi", "bee", 2, "Alerting")
34+
}
35+
36+
render() {
37+
return (
38+
<View>
39+
<Text>Hi</Text>
40+
<View style={appStyles.row}>
41+
<Text onPress={this._sendHi.bind(this)}>Hi</Text>
42+
</View>
43+
</View>
44+
)
45+
}
46+
47+
}
48+
49+
export default Setup;

app/views/Messaging/index.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React from 'react';
2+
import {connect} from 'react-redux'
3+
import {
4+
View,
5+
Text
6+
} from 'react-native'
7+
8+
import appStyles from '../../styles/app';
9+
10+
import List from '../../components/List/List'
11+
12+
// Demos
13+
import Setup from './Demos/Setup';
14+
15+
export const Routes = {
16+
'setup': {
17+
route: {
18+
title: 'Setup',
19+
Component: Setup
20+
}
21+
}
22+
}
23+
24+
export class Messaging extends React.Component {
25+
render() {
26+
const initialRows = [
27+
{ title: 'Setup example', key: 'messaging.setup' }
28+
];
29+
return (
30+
<View style={appStyles.container}>
31+
<List
32+
initialRows={initialRows}
33+
renderRow={this._renderRow.bind(this)}
34+
onRowPress={this._onRowPress.bind(this)}
35+
/>
36+
</View>
37+
)
38+
}
39+
40+
_renderRow(rowData, sectionID, rowID, highlightRow) {
41+
console.log(rowData)
42+
return (
43+
<View style={[appStyles.row]}>
44+
<Text>{rowData.title}</Text>
45+
</View>
46+
)
47+
}
48+
49+
_onRowPress(rowData) {
50+
const rowKey = rowData.key;
51+
const {actions} = this.props;
52+
const {navigation} = actions;
53+
navigation.push(rowKey, this.props);
54+
}
55+
}
56+
57+
const mapStateToProps = (state) => ({
58+
firestack: state.firestack
59+
})
60+
export default connect(mapStateToProps)(Messaging)

0 commit comments

Comments
 (0)