-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathApp.js
164 lines (137 loc) · 4.86 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import { useEffect, useState } from 'react';
import {
AppState,
Text,
Button
} from 'react-native';
import EventEmitter from 'eventemitter3';
var SQLite = require('react-native-sqlite-storage');
SQLite.enablePromise(false);
import PeerMessageDB from './model/PeerMessageDB';
import GroupMessageDB from './model/GroupMessageDB';
import Login from "./page/Login";
import Conversation from './page/Conversation';
// import Search from './Search';
//import PeerChat from "./PeerChat";
// import GroupChat from "./GroupChat"
// import Photo from './chat/Photo';
// import LocationPicker from './chat/LocationPicker';
import { NativeRouter, Route, Switch, useHistory } from "react-router-native";
import IMService from "./imsdk/im";
function NavigationBar() {
let history = useHistory();
const [count, setCount] = useState(1);
function onLocationUpdate(location, action) {
console.log("history len:", history.length);
console.log("on location update:", location, action);
if (action == "POP") {
setCount(history.length - 1);
} else {
setCount(history.length);
}
}
function onPress() {
history.goBack();
}
useEffect(() => {
history.listen(onLocationUpdate);
}, []);
if (count > 1){
return (<Button onPress = {onPress}
title = "Back"
color = "#841584"/>);
} else {
return null;
}
}
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
};
console.log("this props:", this.props);
this.emitter = new EventEmitter();
this.im = new IMService();
this.handleConnectivityChange = this.handleConnectivityChange.bind(this);
this.handleAppStateChange = this.handleAppStateChange.bind(this);
}
componentDidMount() {
var db = SQLite.openDatabase({name:"gobelieve.db", createFromLocation : 1},
function() {
console.log("db open success");
},
function(err) {
console.log("db open error:", err);
});
PeerMessageDB.getInstance().setDB(db);
GroupMessageDB.getInstance().setDB(db);
this.db = db;
//todo android native event
AppState.addEventListener('change', this.handleAppStateChange);
//this.startReachabilityNotifier();
}
componentWillUnmount() {
}
startReachabilityNotifier() {
var im = IMService.instance;
NetInfo.fetch().done((reach) => {
if (reach && (reach.toLowerCase() == 'wifi' || reach.toLowerCase() == 'cell')) {
im.reachable = true;
} else {
im.reachable = false;
}
console.log("reachable:", reach, im.reachable);
});
//never remove listener
NetInfo.addEventListener(
'change',
this.handleConnectivityChange
);
}
handleConnectivityChange(reach) {
var im = IMService.instance;
console.log('connectivity change: ' + reach);
im.handleConnectivityChange(reach);
}
handleAppStateChange(currentAppState) {
var im = IMService.instance;
console.log("app state:", currentAppState);
if (currentAppState == "background") {
im.enterBackground();
} else if (currentAppState == "active") {
im.enterForeground();
}
}
render() {
return (
<NativeRouter
initialEntries={["/"]}
initialIndex={0}>
<NavigationBar></NavigationBar>
<Switch>
<Route exact path="/" render={() => {
return <Login im={this.im}></Login>
}}/>
<Route path="/conversations" render={(routeProps) => {
console.log("route props:", routeProps, routeProps.location.state);
return (<Conversation
im={this.im}
navigator={this.props.navigator}
emitter={this.emitter}
testPeer={routeProps.location.state.testPeer}
uid={routeProps.location.state.uid}
token={routeProps.location.state.token}
></Conversation>);
}}>
</Route>
</Switch>
</NativeRouter>
);
}
}