Skip to content

Commit 18afb2b

Browse files
committed
split BusView into "loading" and "refreshing" states
1 parent 0f92b89 commit 18afb2b

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

source/views/transportation/bus/bus-view.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ export class BusView extends React.PureComponent {
2424
state = {
2525
busLines: defaultData.data,
2626
intervalId: 0,
27-
loading: false,
27+
loading: true,
28+
refreshing: false,
2829
now: moment.tz(TIMEZONE),
2930
// now: moment.tz('Fri 8:13pm', 'ddd h:mma', true, TIMEZONE),
3031
}
3132

3233
componentWillMount() {
33-
this.fetchData()
34+
this.fetchData().then(() => {
35+
this.setState(() => ({loading: false}))
36+
})
37+
3438
// This updates the screen every second, so that the "next bus" times
3539
// are updated without needing to leave and come back.
3640
this.setState(() => ({intervalId: setInterval(this.updateTime, 1000)}))
@@ -50,11 +54,7 @@ export class BusView extends React.PureComponent {
5054
busLines = defaultData.data
5155
}
5256

53-
this.setState(() => ({
54-
busLines,
55-
now: moment.tz(TIMEZONE),
56-
loading: false,
57-
}))
57+
this.setState(() => ({busLines, now: moment.tz(TIMEZONE)}))
5858
}
5959

6060
updateTime = () => {
@@ -63,13 +63,16 @@ export class BusView extends React.PureComponent {
6363

6464
refreshTime = async () => {
6565
const start = Date.now()
66-
this.setState(() => ({loading: true}))
66+
this.setState(() => ({refreshing: true}))
67+
6768
this.updateTime()
69+
6870
const elapsed = Date.now() - start
6971
if (elapsed < 500) {
7072
await delay(500 - elapsed)
7173
}
72-
this.setState(() => ({loading: false}))
74+
75+
this.setState(() => ({refreshing: false}))
7376
}
7477

7578
openMap = () => {
@@ -96,15 +99,15 @@ export class BusView extends React.PureComponent {
9699
)
97100
}
98101

102+
const refreshControl = (
103+
<RefreshControl
104+
onRefresh={this.refreshTime}
105+
refreshing={this.state.refreshing}
106+
/>
107+
)
108+
99109
return (
100-
<ScrollView
101-
refreshControl={
102-
<RefreshControl
103-
onRefresh={this.refreshTime}
104-
refreshing={this.state.loading}
105-
/>
106-
}
107-
>
110+
<ScrollView refreshControl={refreshControl}>
108111
<BusLine line={activeBusLine} now={now} openMap={this.openMap} />
109112
</ScrollView>
110113
)

0 commit comments

Comments
 (0)