From ee54cf68949a04490b49ec33bb885c1543200b4a Mon Sep 17 00:00:00 2001 From: Paul Makles <paulmakles@gmail.com> Date: Sun, 25 Nov 2018 20:56:07 +0000 Subject: [PATCH] Add 'add to my stops' --- App.js | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/App.js b/App.js index a2f21c4..d782b91 100644 --- a/App.js +++ b/App.js @@ -1,5 +1,5 @@ import React, { Component } from 'react'; -import { TextInput, ScrollView, StyleSheet, Text, View } from 'react-native'; +import { ToastAndroid, Button, AsyncStorage, TextInput, ScrollView, StyleSheet, Text, View } from 'react-native'; import moment, { relativeTimeThreshold, updateLocale } from 'moment'; import Tabs from 'react-native-tabs'; @@ -227,13 +227,31 @@ class TabAbout extends Component { class TabMyStops extends Component { constructor(props) { super(props); + this.state = { + stops: [] + }; + } + + componentDidMount() { + this.isCancelled = false; + AsyncStorage.getItem('myStops').then(x => { + if (x == null) return; + !this.isCancelled && this.setState({ + stops: JSON.parse(x) + }); + }).catch(() => {}); + } + + componentWillUnmount() { + this.isCancelled = true; } render() { return ( <ScrollView style={styles.list}> - <BusArrivals name="Romford Station" id="490001243V" /> - <BusArrivals name="Hainault Station" id="490000095B" /> + {this.state.stops.map(i => { + return (<BusArrivals name={i.name} id={i.id} key={i.id} />); + })} </ScrollView> ); } @@ -253,6 +271,28 @@ class SearchEntry extends Component { return ( <View style={[estyles.container, {backgroundColor: this.state.i % 2 ? '#fafafa' : '#fefefe'}]}> <Text style={estyles.destination}>{this.state.name}</Text> + <Button + onPress={() => { + let d = {id: this.state.id, name: this.state.name}; + AsyncStorage.getItem('myStops').then(x => { + if (x == null) return AsyncStorage.setItem('myStops', JSON.stringify([d])).then(() => { + ToastAndroid.show('Added to your stops!', ToastAndroid.SHORT); + }); + let arr = JSON.parse(x); + if (arr.indexOf(d) > -1) { + ToastAndroid.show('Already added!', ToastAndroid.SHORT); + } else { + arr.push(d); + AsyncStorage.setItem('myStops', JSON.stringify(arr)).then(() => { + ToastAndroid.show('Added to your stops!', ToastAndroid.SHORT); + }); + } + }).catch(() => { + ToastAndroid.show('Encountered an error!', ToastAndroid.SHORT); + }); + }} + title="Add" + /> </View> ); } -- GitLab