-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
233 lines (180 loc) · 7.91 KB
/
main.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
'use strict';
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text, ListView, Dimensions,
View, TextInput, AsyncStorage, Platform, StatusBar, TouchableOpacity, Image, AlertIOS
} from 'react-native';
import { Router, Scene, Actions } from 'react-native-router-flux';
var STORAGE_KEY = '@firstTime';
var {width: windowWidth, height:windowHeight} = Dimensions.get('window');
var NAVBAR_HEIGHT = (Platform.OS === 'ios') ? 64 : 54;
var moment = require('moment');
var ActivityIndicator = require('./CustomActivityIndicator2')
var SQLiteDemo = React.createClass({
getInitialState(){
return {
error: "",
data: null,
loading: true
};
},
componentWillMount() {
},
componentDidMount(){
this._loadInitialState().done();
this.getMotivation();
},
componentWillReceiveProps(newProps){
if (newProps.reload) this.getMotivation();
},
async _loadInitialState() {
try {
var value = await AsyncStorage.getItem(STORAGE_KEY);
if (value === null) {
Actions.welcome();
}
} catch (error) {
alert('AsyncStorage error: ' + error.message);
}
},
getMotivation(){
var _this = this;
this.props.db.transaction((tx) => {
console.log("Executing sql...");
tx.executeSql('SELECT entries.text, entries.title from pushes ' +
'LEFT JOIN entries ON pushes.entry_id = entries.id WHERE pushes.pushed_at = '+moment().startOf('day')+' LIMIT 1', [],
(tx, results) => {
var len = results.rows.length;
if (len > 0){
_this.setState({data: results.rows.item(0), loading: false});
}else{
tx.executeSql('SELECT * from entries WHERE active = 1 ORDER BY RANDOM() LIMIT 1', [],
(tx, results) => {
var len = results.rows.length;
if (len > 0){
tx.executeSql('INSERT INTO pushes (entry_id, pushed_at) VALUES ('+results.rows.item(0).id+', '+moment().startOf('day')+');', [], (tx, results) => {
//alert("Push Added Successfully!");
});
_this.setState({data: results.rows.item(0), loading: false});
}else{
_this.setState({data: null, loading: false});
}
}, this.errorCB);
}
}, this.errorCB);
});
},
deleteDatabase(){
this.state.progress = ["Deleting database"];
this.setState(this.state);
SQLite.deleteDatabase(database_name, this.deleteCB, this.errorCB);
},
closeDatabase(){
var that = this;
if (db) {
console.log("Closing database ...");
that.setState(that.state);
db.close(that.closeCB,that.errorCB);
} else {
that.setState(that.state);
}
},
renderProgressEntry(entry){
return (<View style={listStyles.li}>
<View>
<Text style={listStyles.title}>{entry}</Text>
</View>
</View>)
},
render(){
var date = moment().format('Do MMMM YYYY');
date = date.toUpperCase();
return (
<View style={[styles.container, {flex:1}]}>
<View style={{height: NAVBAR_HEIGHT, padding:6, flexDirection: "row", paddingTop: 20}}>
<View style={{height: 44, width: 54}}/>
<View style={{height: 44, flex:1, alignItems: "center", justifyContent: "center"}}>
<Text style={{color:"#A7A9AA", fontSize: 15, fontWeight: "600", textAlign: "center"}}>
TODAY'S MOTIVITATION
</Text>
</View>
<View style={{height: 44, width: 54}}/>
</View>
<View style={{paddingLeft:15, paddingRight:15, flex: 1}}>
<View style={{flex: 1, borderRadius: 8, backgroundColor: "#3A3A3A", padding: 22, paddingTop: 0, alignItems: "center", justifyContent: "center"}}>
{
(this.state.loading) ?
<ActivityIndicator/>
:
<View style={{flex: 1, alignItems: "center", justifyContent: "center"}}>
<Text style={{color:"#FAFAFA", fontSize: 19, fontWeight: "700", textAlign: "center", marginBottom: 6, marginTop: -60}}>
{date}
</Text>
{
(this.state.data !== null) ?
<Text style={{color:"#828385", fontSize: 18, fontWeight: "600", textAlign: "center"}}>
{this.state.data.title}
</Text>
:
null
}
<View style={{borderWidth: 2, borderColor: "#FAFAFA", backgroundColor: "#fff", height:2, width: 60, borderRadius: 2, margin: 20}}/>
{
(this.state.data !== null) ?
<Text style={{color:"#C1C1C1", fontSize: 18, lineHeight:22, fontWeight: "500", textAlign: "center"}}>
{this.state.data.text}
</Text>
:
<Text style={{color:"#C1C1C1", fontSize: 18, lineHeight:22, fontWeight: "500", textAlign: "center"}}>
No Data Available
</Text>
}
</View>
}
</View>
</View>
<View style={{height: NAVBAR_HEIGHT, padding:15, flexDirection: "row", alignItems: "center", justifyContent: "center"}}>
<TouchableOpacity style={[styles.navBtn]}
onPress={Actions.entries}>
<Image source={require("./images/List2.png")} style={{width: 27, height: 27}}/>
</TouchableOpacity>
<View style={{height: 44, flex:1, alignItems: "center"}}>
<TouchableOpacity style={[styles.navBtn]} onPress={Actions.new}>
<Image source={require("./images/Add-Filled.png")} style={{width: 39, height: 39}}/>
</TouchableOpacity>
</View>
<TouchableOpacity style={[styles.navBtn]} onPress={Actions.settings}>
<Image source={require("./images/Settings.png")} style={{width: 23, height: 23}}/>
</TouchableOpacity>
</View>
</View>
);
},
//Output Messages
errorCB(err) {
console.log("error: ",err);
this.state.progress.push("Error: "+ (err.message || err));
this.setState(this.state);
return false;
},
successCB() {
console.log("SQL executed ...");
},
openCB() {
this.state.progress.push("Database OPEN");
this.setState(this.state);
},
closeCB() {
this.state.progress.push("Database CLOSED");
this.setState(this.state);
},
deleteCB() {
console.log("Database DELETED");
this.state.progress.push("Database DELETED");
this.setState(this.state);
},
});
const styles = require('./styles');
module.exports = SQLiteDemo;