|
1 | 1 | // @flow |
2 | 2 | import React from 'react' |
3 | | -import {View, StyleSheet, Platform, Text} from 'react-native' |
| 3 | +import {View, Text, StyleSheet, Platform} from 'react-native' |
4 | 4 | import type {BusLineType, FancyBusTimeListType} from './types' |
5 | 5 | import {getScheduleForNow, getSetOfStopsForNow} from './lib' |
6 | 6 | import get from 'lodash/get' |
7 | 7 | import zip from 'lodash/zip' |
8 | 8 | import head from 'lodash/head' |
9 | 9 | import last from 'lodash/last' |
| 10 | +import Icon from 'react-native-vector-icons/Ionicons' |
10 | 11 | import moment from 'moment-timezone' |
11 | 12 | import * as c from '../../components/colors' |
12 | 13 | import {Separator} from '../../components/separator' |
13 | 14 | import {BusStopRow} from './bus-stop-row' |
14 | | -import {ListRow, ListSectionHeader, Title} from '../../components/list' |
| 15 | +import {ListRow, ListSectionHeader, Title, Detail} from '../../components/list' |
| 16 | +import {Row, Column} from '../../components/layout' |
15 | 17 |
|
16 | 18 | const TIME_FORMAT = 'h:mma' |
17 | 19 | const TIMEZONE = 'America/Winnipeg' |
@@ -49,75 +51,114 @@ function makeSubtitle({now, moments, isLastBus}) { |
49 | 51 | return lineDetail |
50 | 52 | } |
51 | 53 |
|
52 | | -export function BusLine({line, now}: {line: BusLineType, now: moment}) { |
53 | | - // grab the colors (with fallbacks) via _.get |
54 | | - const barColor = get(barColors, line.line, c.black) |
55 | | - const currentStopColor = get(stopColors, line.line, c.gray) |
56 | | - const androidColor = Platform.OS === 'android' ? {color: barColor} : null |
| 54 | +const parseTime = (now: moment) => time => { |
| 55 | + // either pass `false` through or return a parsed time |
| 56 | + if (time === false) { |
| 57 | + return false |
| 58 | + } |
| 59 | + |
| 60 | + return ( |
| 61 | + moment |
| 62 | + // interpret in Central time |
| 63 | + .tz(time, TIME_FORMAT, true, TIMEZONE) |
| 64 | + // and set the date to today |
| 65 | + .dayOfYear(now.dayOfYear()) |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +export class BusLine extends React.PureComponent { |
| 70 | + props: {line: BusLineType, now: moment, openMap: () => any} |
| 71 | + |
| 72 | + render() { |
| 73 | + const {line, now} = this.props |
| 74 | + |
| 75 | + // grab the colors (with fallbacks) via _.get |
| 76 | + const barColor = get(barColors, line.line, c.black) |
| 77 | + const currentStopColor = get(stopColors, line.line, c.gray) |
| 78 | + const androidColor = Platform.OS === 'android' ? {color: barColor} : null |
| 79 | + |
| 80 | + const schedule = getScheduleForNow(line.schedules, now) |
| 81 | + if (!schedule) { |
| 82 | + return ( |
| 83 | + <View> |
| 84 | + <ListSectionHeader title={line.line} titleStyle={androidColor} /> |
| 85 | + <ListRow> |
| 86 | + <Title><Text>This line is not running today.</Text></Title> |
| 87 | + </ListRow> |
| 88 | + </View> |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + const scheduledMoments: FancyBusTimeListType[] = schedule.times.map( |
| 93 | + timeset => timeset.map(parseTime(now)), |
| 94 | + ) |
| 95 | + |
| 96 | + const moments: FancyBusTimeListType = getSetOfStopsForNow( |
| 97 | + scheduledMoments, |
| 98 | + now, |
| 99 | + ) |
| 100 | + const pairs: [[string, moment]] = zip(schedule.stops, moments) |
| 101 | + |
| 102 | + const timesIndex = scheduledMoments.indexOf(moments) |
| 103 | + const isLastBus = timesIndex === scheduledMoments.length - 1 |
| 104 | + const subtitle = makeSubtitle({now, moments, isLastBus}) |
57 | 105 |
|
58 | | - const schedule = getScheduleForNow(line.schedules, now) |
59 | | - if (!schedule) { |
60 | 106 | return ( |
61 | 107 | <View> |
62 | | - <ListSectionHeader title={line.line} titleStyle={androidColor} /> |
63 | | - <ListRow> |
64 | | - <Title><Text>This line is not running today.</Text></Title> |
65 | | - </ListRow> |
66 | | - </View> |
67 | | - ) |
68 | | - } |
| 108 | + <ListSectionHeader |
| 109 | + title={line.line} |
| 110 | + subtitle={subtitle} |
| 111 | + titleStyle={androidColor} |
| 112 | + /> |
69 | 113 |
|
70 | | - const scheduledMoments: FancyBusTimeListType[] = schedule.times.map( |
71 | | - timeset => { |
72 | | - return timeset.map( |
73 | | - time => |
74 | | - time === false |
75 | | - ? // either pass `false` through or return a parsed time |
76 | | - false |
77 | | - : moment |
78 | | - // interpret in Central time |
79 | | - .tz(time, TIME_FORMAT, true, TIMEZONE) |
80 | | - // and set the date to today |
81 | | - .dayOfYear(now.dayOfYear()), |
82 | | - ) |
83 | | - }, |
84 | | - ) |
| 114 | + {pairs.map(([placeTitle, moment], i, list) => |
| 115 | + <View key={i}> |
| 116 | + <BusStopRow |
| 117 | + // get the arrival time for this stop from each bus loop after |
| 118 | + // the current time (as given by `now`) |
| 119 | + times={scheduledMoments.slice(timesIndex).map(set => set[i])} |
| 120 | + place={placeTitle} |
| 121 | + now={now} |
| 122 | + time={moment} |
| 123 | + barColor={barColor} |
| 124 | + currentStopColor={currentStopColor} |
| 125 | + isFirstRow={i === 0} |
| 126 | + isLastRow={i === list.length - 1} |
| 127 | + /> |
| 128 | + <Separator style={styles.separator} /> |
| 129 | + </View>, |
| 130 | + )} |
85 | 131 |
|
86 | | - const moments: FancyBusTimeListType = getSetOfStopsForNow( |
87 | | - scheduledMoments, |
88 | | - now, |
89 | | - ) |
90 | | - const pairs: [[string, moment]] = zip(schedule.stops, moments) |
| 132 | + <ListRow |
| 133 | + onPress={this.props.openMap} |
| 134 | + fullWidth={true} |
| 135 | + spacing={{left: 45}} |
| 136 | + > |
| 137 | + <Row alignItems="center"> |
| 138 | + <Column alignItems="center" width={45} paddingRight={5}> |
| 139 | + <Icon |
| 140 | + name={ |
| 141 | + Platform.OS === 'ios' |
| 142 | + ? 'ios-navigate-outline' |
| 143 | + : 'md-navigate' |
| 144 | + } |
| 145 | + size={24} |
| 146 | + style={{color: c.iosDisabledText}} |
| 147 | + /> |
| 148 | + </Column> |
91 | 149 |
|
92 | | - const timesIndex = scheduledMoments.indexOf(moments) |
93 | | - const isLastBus = timesIndex === scheduledMoments.length - 1 |
94 | | - const subtitle = makeSubtitle({now, moments, isLastBus}) |
| 150 | + <Column> |
| 151 | + <Title> |
| 152 | + <Text>Open Map</Text> |
| 153 | + </Title> |
95 | 154 |
|
96 | | - return ( |
97 | | - <View> |
98 | | - <ListSectionHeader |
99 | | - title={line.line} |
100 | | - subtitle={subtitle} |
101 | | - titleStyle={androidColor} |
102 | | - /> |
103 | | - |
104 | | - {pairs.map(([placeTitle, moment], i, list) => |
105 | | - <View key={i}> |
106 | | - <BusStopRow |
107 | | - // get the arrival time for this stop from each bus loop after |
108 | | - // the current time (as given by `now`) |
109 | | - times={scheduledMoments.slice(timesIndex).map(set => set[i])} |
110 | | - place={placeTitle} |
111 | | - now={now} |
112 | | - time={moment} |
113 | | - barColor={barColor} |
114 | | - currentStopColor={currentStopColor} |
115 | | - isFirstRow={i === 0} |
116 | | - isLastRow={i === list.length - 1} |
117 | | - /> |
118 | | - {i < list.length - 1 ? <Separator style={styles.separator} /> : null} |
119 | | - </View>, |
120 | | - )} |
121 | | - </View> |
122 | | - ) |
| 155 | + <Detail> |
| 156 | + <Text>See the planned bus route on a map!</Text> |
| 157 | + </Detail> |
| 158 | + </Column> |
| 159 | + </Row> |
| 160 | + </ListRow> |
| 161 | + </View> |
| 162 | + ) |
| 163 | + } |
123 | 164 | } |
0 commit comments