Skip to content

Commit

Permalink
added daily notification and show case of the quote
Browse files Browse the repository at this point in the history
  • Loading branch information
TangMichael committed Jan 7, 2019
1 parent 4938e22 commit 997268c
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 41 deletions.
Binary file modified android/app/src/main/assets/quotes.db
Binary file not shown.
176 changes: 143 additions & 33 deletions src/containers/display-daily/display-daily.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,183 @@
import React, { Component } from "react";
import { StyleSheet, View, Text, Button, TouchableOpacity} from "react-native";
import { StyleSheet, View, Text, Button, TouchableOpacity } from "react-native";
import { connect } from "react-redux";
import { getFavorites } from "../../actions/index";
import { TextInput } from "react-native-gesture-handler";
import NotifService from './../../service/NotifService';
import { TextInput, ScrollView } from "react-native-gesture-handler";
import { addFavorites } from "../../actions/index";
import Snackbar from "react-native-snackbar";
import NotifService from "./../../service/NotifService";
import { openDatabase } from "react-native-sqlite-storage";
let db = openDatabase({ name: "newdb", createFromLocation: "~quotes.db" });
let db = openDatabase({ name: "db", createFromLocation: "~quotes.db" });

class DisplayDaily extends Component {
constructor(props) {
super(props);
this.state = {quote: null}
this.notif = new NotifService(this.onRegister.bind(this), this.onNotif.bind(this));
this.state = { oldQuote: null, newQuote: null };
this.notif = new NotifService(
this.onRegister.bind(this),
this.onNotif.bind(this)
);
}

componentDidMount(){
// load 1 random quote to pass to the function that push a notification
componentWillMount() {
this.notif.cancelAll();
// display daily quote
db.transaction(tx => {
tx.executeSql("SELECT * FROM quotes ORDER BY RANDOM() LIMIT 1", [], (tx, results) => {
tx.executeSql("SELECT * FROM daily", [], (tx, results) => {
var len = results.rows.length;
console.log("getting data");
if (len > 0) {
this.setState({quote: results.rows.item(0).quote});
console.log(results.rows.item(0).quote);
this.setState({
oldQuote: {
id: results.rows.item(0).id,
quote: results.rows.item(0).quote
}
});
}
});
tx.executeSql("DELETE FROM daily", [], (tx, results) => {});
});

// pick new quote
db.transaction(tx => {
tx.executeSql(
"SELECT * FROM quotes ORDER BY RANDOM() LIMIT 1",
[],
(tx, results) => {
var len = results.rows.length;
if (len > 0) {
console.log(
"new quote is " +
results.rows.item(0).quote +
" ID " +
results.rows.item(0).id
);

this.setState({
newQuote: {
id: results.rows.item(0).id,
quote: results.rows.item(0).quote
}
});
this.notif.scheduleNotif(this.state.newQuote);
}
}
);
});
// store new quote in the database
db.transaction(tx => {
tx.executeSql(
"INSERT into daily values(?,?)",
[this.state.newQuote.id, this.state.newQuote.quote],
(tx, results) => {}
);
});
}
render() {
const quote =
this.state.oldQuote === null ? (
<Text>""</Text>
) : (
<Text
onPress={() => {
var x = false;
this.props.favorites.some(element => {
if (element.id === this.state.oldQuote.id) {
x = true;
}
});
if (!x) {
this.props.addFavorites(this.state.oldQuote);
} else {
Snackbar.show({
title: "Quote already added to favorites",
duration: Snackbar.LENGTH_SHORT
});
}
}}
style={{ padding: 15 }}
>
{this.state.oldQuote.quote}
</Text>
);
return (
<View>
<Text>Your daily quote:</Text>
<View style={styles.spacer}></View>
<View style={styles.spacer}></View>
<ScrollView>
<Text>Your daily quote:</Text>
{quote}
<View style={styles.spacer} />
<View style={styles.spacer} />

<TouchableOpacity style={styles.button} onPress={() => { this.notif.localNotif(this.state.quote) }}><Text>Local Notification (now)</Text></TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={() => { this.notif.scheduleNotif() }}><Text>Schedule Notification in 30s</Text></TouchableOpacity>
{/* <TouchableOpacity style={styles.button} onPress={() => { this.notif.localNotif(this.state.newQuote) }}><Text>Local Notification (now)</Text></TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={() => { this.notif.scheduleNotif(this.state.newQuote) }}><Text>Schedule Notification in 30s</Text></TouchableOpacity> */}
{/* <TouchableOpacity style={styles.button} onPress={() => { this.notif.cancelNotif() }}><Text>Cancel last notification (if any)</Text></TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={() => { this.notif.cancelAll() }}><Text>Cancel all notifications</Text></TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={() => { this.notif.checkPermission(this.handlePerm.bind(this)) }}><Text>Check Permission</Text></TouchableOpacity> */}
</View>
)
</ScrollView>
);
}


onRegister(token) {
Alert.alert("Registered !", JSON.stringify(token));
console.log(token);
}

onNotif(notif) {
console.log(notif.message);
this.setState({quote: notif.message})
this.setState({ oldQuote: notif.message });
// Alert.alert(notif.title, notif.message);
}

handlePerm(perms) {
Alert.alert("Permissions", JSON.stringify(perms));
}
}

function mapDispatchToProps(dispatch, state) {
return {
addFavorites: function(quote, initial) {
dispatch(addFavorites(quote));
// on opening, will triger the err since it is already in the database
// we pass in initial which is true
// next time we add, we dont pass anything, !undefined will be true
if (!initial) {
db.transaction(tx => {
tx.executeSql(
"INSERT into favorites values(?,?)",
[quote.id, quote.quote],
(tx, results) => {},
(tx, err) => {
if (tx.code == 0) {
Snackbar.show({
title: "Quote already added to favorites",
duration: Snackbar.LENGTH_SHORT
});
}
}
);
});
}
}
};
}

const mapStateToProps = state => {
return {
favorites: state.favorites
};
};

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F5FCFF"
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
textAlign: "center",
margin: 10
},
button: {
borderWidth: 1,
Expand All @@ -77,7 +186,7 @@ const styles = StyleSheet.create({
padding: 5,
width: "70%",
backgroundColor: "#DDDDDD",
borderRadius: 5,
borderRadius: 5
},
textField: {
borderWidth: 1,
Expand All @@ -87,15 +196,16 @@ const styles = StyleSheet.create({
width: "70%"
},
spacer: {
height: 10,
height: 10
},
title: {
fontWeight: "bold",
fontSize: 20,
textAlign: "center",
textAlign: "center"
}
});

export default DisplayDaily;


export default connect(
mapStateToProps,
mapDispatchToProps
)(DisplayDaily);
3 changes: 1 addition & 2 deletions src/containers/display-quotes/display-quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { connect } from "react-redux";
import { addFavorites } from "../../actions/index";
import { openDatabase } from "react-native-sqlite-storage";
import { ScrollView, FlatList } from "react-native-gesture-handler";
let db = openDatabase({ name: "newdb", createFromLocation: "~quotes.db" });
let db = openDatabase({ name: "db", createFromLocation: "~quotes.db" });
import Snackbar from "react-native-snackbar";

class DisplayQuotes extends Component {
Expand Down Expand Up @@ -143,7 +143,6 @@ function mapDispatchToProps(dispatch, state) {
"INSERT into favorites values(?,?)",
[quote.id, quote.quote],
(tx, results) => {
console.log("OK");
},
(tx, err) => {
if (tx.code == 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/containers/favorites/favorites.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getFavorites } from "../../actions/index";
import { TextInput, FlatList } from "react-native-gesture-handler";
import { removeFavorites } from "../../actions/index";
import { openDatabase } from "react-native-sqlite-storage";
let db = openDatabase({ name: "newdb", createFromLocation: "~quotes.db" });
let db = openDatabase({ name: "db", createFromLocation: "~quotes.db" });

class Favorites extends Component {
render() {
Expand Down
10 changes: 5 additions & 5 deletions src/service/NotifService.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ export default class NotifService {
});
}

scheduleNotif() {
scheduleNotif(quote) {
this.lastId++;
var date = new Date();
PushNotification.localNotificationSchedule({
date: new Date(Date.now() + (30 * 1000)), // in 30 secs

date: date.setDate(date.getDate() + 1),
/* Android Only Properties */
id: ''+this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
ticker: "My Notification Ticker", // (optional)
Expand All @@ -98,8 +98,8 @@ export default class NotifService {
userInfo: null, // (optional) default: null (object containing additional notification data)

/* iOS and Android properties */
title: "Scheduled Notification", // (optional)
message: "My Notification Message", // (required)
title: "Daily Quote", // (optional)
message: quote.quote, // (required)
playSound: true, // (optional) default: true
soundName: 'default', // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
});
Expand Down

0 comments on commit 997268c

Please sign in to comment.