|
| 1 | +// @flow |
| 2 | + |
| 3 | +import React from 'react' |
| 4 | +import {StyleSheet} from 'react-native' |
| 5 | +import type {BusLineType} from './types' |
| 6 | +import MapView from 'react-native-maps' |
| 7 | +import moment from 'moment-timezone' |
| 8 | +import {NoticeView} from '../../components/notice' |
| 9 | +import type {TopLevelViewPropsType} from '../../types' |
| 10 | +import {getScheduleForNow} from './lib' |
| 11 | +import zip from 'lodash/zip' |
| 12 | +import uniqBy from 'lodash/uniqBy' |
| 13 | + |
| 14 | +import {data as defaultBusLines} from '../../../../docs/bus-times.json' |
| 15 | + |
| 16 | +const TIMEZONE = 'America/Winnipeg' |
| 17 | + |
| 18 | +const styles = StyleSheet.create({ |
| 19 | + map: {...StyleSheet.absoluteFillObject}, |
| 20 | +}) |
| 21 | + |
| 22 | +export class BusMapView extends React.PureComponent { |
| 23 | + static navigationOptions = ({navigation}) => ({ |
| 24 | + title: `${navigation.state.params.line} Map`, |
| 25 | + }) |
| 26 | + |
| 27 | + static defaultProps = { |
| 28 | + busLines: defaultBusLines, |
| 29 | + } |
| 30 | + |
| 31 | + state = { |
| 32 | + intervalId: 0, |
| 33 | + now: moment.tz(TIMEZONE), |
| 34 | + region: { |
| 35 | + latitude: 44.44946671480875, |
| 36 | + latitudeDelta: 0.06175530810822494, |
| 37 | + longitude: -93.17014753996669, |
| 38 | + longitudeDelta: 0.05493163793703104, |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + componentWillMount() { |
| 43 | + // This updates the screen every second, so that the "next bus" times are |
| 44 | + // updated without needing to leave and come back. |
| 45 | + this.setState(() => ({intervalId: setInterval(this.updateTime, 5000)})) |
| 46 | + } |
| 47 | + |
| 48 | + componentWillUnmount() { |
| 49 | + clearTimeout(this.state.intervalId) |
| 50 | + } |
| 51 | + |
| 52 | + props: TopLevelViewPropsType & { |
| 53 | + busLines: BusLineType[], |
| 54 | + navigation: { |
| 55 | + state: { |
| 56 | + params: { |
| 57 | + line: string, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + onRegionChange = (region: { |
| 64 | + latitude: number, |
| 65 | + latitudeDelta: number, |
| 66 | + longitude: number, |
| 67 | + longitudeDelta: number, |
| 68 | + }) => { |
| 69 | + this.setState(() => ({region})) |
| 70 | + } |
| 71 | + |
| 72 | + updateTime = () => { |
| 73 | + this.setState(() => ({now: moment.tz(TIMEZONE)})) |
| 74 | + } |
| 75 | + |
| 76 | + render() { |
| 77 | + let {now} = this.state |
| 78 | + // now = moment.tz('Fri 8:13pm', 'ddd h:mma', true, TIMEZONE) |
| 79 | + const busLines = this.props.busLines |
| 80 | + const lineToDisplay = this.props.navigation.state.params.line |
| 81 | + const activeBusLine = busLines.find(({line}) => line === lineToDisplay) |
| 82 | + |
| 83 | + if (!activeBusLine) { |
| 84 | + const lines = busLines.map(({line}) => line).join(', ') |
| 85 | + const notice = `The line "${lineToDisplay}" was not found among ${lines}` |
| 86 | + return <NoticeView text={notice} /> |
| 87 | + } |
| 88 | + |
| 89 | + const schedule = getScheduleForNow(activeBusLine.schedules, now) |
| 90 | + if (!schedule) { |
| 91 | + const notice = `No schedule was found for today, ${now.format('dddd')}` |
| 92 | + return <NoticeView text={notice} /> |
| 93 | + } |
| 94 | + |
| 95 | + const coords = schedule.coordinates || [] |
| 96 | + if (!coords.length) { |
| 97 | + const notice = `No coordinates have been provided for today's (${now.format( |
| 98 | + 'dddd', |
| 99 | + )}) schedule on the "${lineToDisplay}" line` |
| 100 | + return <NoticeView text={notice} /> |
| 101 | + } |
| 102 | + |
| 103 | + const markers = uniqBy( |
| 104 | + zip(coords, schedule.stops), |
| 105 | + ([[lat, lng]]) => `${lat},${lng}`, |
| 106 | + ) |
| 107 | + |
| 108 | + return ( |
| 109 | + <MapView |
| 110 | + region={this.state.region} |
| 111 | + style={styles.map} |
| 112 | + onRegionChange={this.onRegionChange} |
| 113 | + loadingEnabled={true} |
| 114 | + > |
| 115 | + {markers.map(([[latitude, longitude], title], i) => |
| 116 | + <MapView.Marker |
| 117 | + key={i} |
| 118 | + coordinate={{latitude, longitude}} |
| 119 | + title={title} |
| 120 | + // description={marker.description} |
| 121 | + // TODO: add "next arrival" time as the description |
| 122 | + />, |
| 123 | + )} |
| 124 | + </MapView> |
| 125 | + ) |
| 126 | + } |
| 127 | +} |
0 commit comments