@@ -15,6 +15,8 @@ import {
1515 ListSectionHeader ,
1616 ListSeparator ,
1717} from '../components/list'
18+ import delay from 'delay'
19+ import { reportNetworkProblem } from '../../lib/report-network-problem'
1820import LoadingView from '../components/loading'
1921import type { WordType } from './types'
2022import type { TopLevelViewPropsType } from '../types'
@@ -25,9 +27,10 @@ import uniq from 'lodash/uniq'
2527import words from 'lodash/words'
2628import deburr from 'lodash/deburr'
2729import isString from 'lodash/isString'
28- import { data as terms } from '../../../docs/dictionary.json'
30+ import * as defaultData from '../../../docs/dictionary.json'
2931import { SearchBar } from '../components/searchbar'
3032
33+ const GITHUB_URL = 'https://stodevx.github.io/AAO-React-Native/dictionary.json'
3134const rowHeight = Platform . OS === 'ios' ? 76 : 89
3235const headerHeight = Platform . OS === 'ios' ? 33 : 41
3336
@@ -46,19 +49,59 @@ const styles = StyleSheet.create({
4649 } ,
4750} )
4851
49- export class DictionaryView extends React . Component {
52+ type Props = TopLevelViewPropsType
53+
54+ type State = {
55+ results : { [ key : string ] : Array < WordType > } ,
56+ }
57+
58+ export class DictionaryView extends React . PureComponent < void , Props , State > {
5059 static navigationOptions = {
5160 title : 'Campus Dictionary' ,
5261 headerBackTitle : 'Dictionary' ,
5362 }
5463
55- props : TopLevelViewPropsType
5664 searchBar : any
5765
58- state : {
59- results : { [ key : string ] : Array < WordType > } ,
60- } = {
61- results : terms ,
66+ state = {
67+ results : defaultData . data ,
68+ allTerms : defaultData . data ,
69+ loading : true ,
70+ refreshing : false ,
71+ }
72+
73+ componentWillMount ( ) {
74+ this . fetchData ( )
75+ }
76+
77+ refresh = async ( ) => {
78+ const start = Date . now ( )
79+ this . setState ( ( ) => ( { refreshing : true } ) )
80+
81+ await this . fetchData ( )
82+
83+ // wait 0.5 seconds – if we let it go at normal speed, it feels broken.
84+ const elapsed = Date . now ( ) - start
85+ if ( elapsed < 500 ) {
86+ await delay ( 500 - elapsed )
87+ }
88+
89+ this . setState ( ( ) => ( { refreshing : false } ) )
90+ }
91+
92+ fetchData = async ( ) => {
93+ this . setState ( ( ) => ( { loading : true } ) )
94+
95+ let { data : allTerms } = await fetchJson ( GITHUB_URL ) . catch ( err => {
96+ reportNetworkProblem ( err )
97+ return defaultData
98+ } )
99+
100+ if ( process . env . NODE_ENV === 'development' ) {
101+ allTerms = defaultData . data
102+ }
103+
104+ this . setState ( ( ) => ( { allTerms, loading : false } ) )
62105 }
63106
64107 onPressRow = ( data : WordType ) = > {
@@ -105,13 +148,13 @@ export class DictionaryView extends React.Component {
105148 _performSearch = ( text : string ) = > {
106149 // Android clear button returns an object
107150 if ( ! isString ( text ) ) {
108- this . setState ( { results : terms } )
151+ this . setState ( state => ( { results : state . allTerms } ) )
109152 return
110153 }
111154
112155 const query = text . toLowerCase ( )
113- this . setState ( ( ) => ( {
114- results : terms . filter ( term =>
156+ this . setState ( state => ( {
157+ results : state . allTerms . filter ( term =>
115158 this . termToArray ( term ) . some ( word => word . startsWith ( query ) ) ,
116159 ) ,
117160 } ) )
@@ -123,7 +166,7 @@ export class DictionaryView extends React.Component {
123166 performSearch = debounce ( this . _performSearch , 50 )
124167
125168 render ( ) {
126- if ( ! terms ) {
169+ if ( this . state . loading ) {
127170 return < LoadingView / >
128171 }
129172
0 commit comments