Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Convert everything to TypeScript #121

Merged
merged 43 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
4385d36
Start ts
amaury1093 Jul 1, 2019
2ba71ab
Convert all files to TS
amaury1093 Jul 1, 2019
c911747
Make search work
amaury1093 Jul 6, 2019
1a02aaf
Make png import work
amaury1093 Jul 6, 2019
4805930
Add retry
amaury1093 Jul 6, 2019
e41774b
Make api call work
amaury1093 Jul 7, 2019
957ebaa
Add long waiting on Loading
amaury1093 Jul 7, 2019
8f8abe1
Make screens work
amaury1093 Jul 7, 2019
156a810
Make cigarettes work
amaury1093 Jul 7, 2019
3fa011d
Make header work
amaury1093 Jul 7, 2019
7d1db03
Reverse geocoding
amaury1093 Jul 7, 2019
7e3b3b8
Make details work
amaury1093 Jul 7, 2019
e137e0b
Make search work
amaury1093 Jul 7, 2019
4bcc494
Fix reload app
amaury1093 Jul 7, 2019
8f992fe
Make tests pass
amaury1093 Jul 7, 2019
0fcf5e3
Run eslint
amaury1093 Jul 7, 2019
5edd500
AqiHistory
amaury1093 Jul 9, 2019
f05a927
Add History manager back
amaury1093 Jul 9, 2019
704bb62
Start getting background location
amaury1093 Jul 9, 2019
9784aa1
Remove circular dependency
amaury1093 Jul 10, 2019
28b334c
Fix isSaveNeeded
amaury1093 Jul 10, 2019
2e54f5e
Add getData for testing
amaury1093 Jul 10, 2019
8462eeb
Add clearTable
amaury1093 Jul 10, 2019
60874bd
Fix tests
amaury1093 Jul 10, 2019
50af1ec
Update README
amaury1093 Jul 10, 2019
2a03a7f
Remove vs code settings
amaury1093 Jul 10, 2019
6135652
Fix too many requests
amaury1093 Jul 12, 2019
9886b4f
Add task to save to AsyncStorage
amaury1093 Jul 13, 2019
9206856
Return correct background fetch response
amaury1093 Jul 13, 2019
cf203ab
Refactor a bit the components
amaury1093 Jul 13, 2019
b1eed90
Make scroll work nicely
amaury1093 Jul 13, 2019
5fd5ccb
Add shadow on buttons
amaury1093 Jul 13, 2019
40f8848
Make stuff work
amaury1093 Jul 13, 2019
a516044
Weekly monthly
amaury1093 Jul 13, 2019
86d52a0
Calculate cigarettes
amaury1093 Jul 13, 2019
c1fed0b
Small fixes
amaury1093 Jul 13, 2019
bce6ecb
Small fixes
amaury1093 Jul 13, 2019
b12ee31
Small tweaks with icons
amaury1093 Jul 14, 2019
eb862cb
Add more dev info
amaury1093 Jul 16, 2019
050761c
Reverse geocode in search
amaury1093 Jul 16, 2019
d7ba1fd
Small tweaks
amaury1093 Jul 16, 2019
8c029ed
Small tweaks
amaury1093 Jul 17, 2019
d44d40b
Update icons
amaury1093 Jul 18, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make api call work
  • Loading branch information
amaury1093 committed Jul 7, 2019
commit e41774b9366dc6347710fc4387351405336d049a
10 changes: 8 additions & 2 deletions App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import Sentry from 'sentry-expo';

import { Screens } from './Screens';
import { Background as LoadingBackground } from './Screens/Loading/Background';
import { ErrorContextProvider, LocationContextProvider } from './stores';
import {
ApiContextProvider,
ErrorContextProvider,
LocationContextProvider
} from './stores';

// Add sentry if available
if (Constants.manifest.extra.sentryPublicDsn) {
Expand All @@ -42,7 +46,9 @@ export function App() {
return fontLoaded ? (
<ErrorContextProvider>
<LocationContextProvider>
<Screens />
<ApiContextProvider>
<Screens />
</ApiContextProvider>
</LocationContextProvider>
</ErrorContextProvider>
) : (
Expand Down
5 changes: 1 addition & 4 deletions App/Screens/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// along with Sh**t! I Smoke. If not, see <http:https://www.gnu.org/licenses/>.

import React, { Component } from 'react';
import { inject, observer } from 'mobx-react';
import {
ScrollView,
Share,
Expand All @@ -33,9 +32,7 @@ import { SmokeVideo } from './SmokeVideo';
import swearWords from './swearWords';
import * as theme from '../../utils/theme';

@inject('stores')
@observer
export class Home extends Component {
export function
goToAbout = () => this.props.navigation.navigate('About');

goToDetails = () => this.props.navigation.navigate('Details');
Expand Down
19 changes: 9 additions & 10 deletions App/Screens/Loading/Background.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
// You should have received a copy of the GNU General Public License
// along with Sh**t! I Smoke. If not, see <http:https://www.gnu.org/licenses/>.

import React, { PureComponent } from 'react';
import { StyleSheet, View } from 'react-native';
import React from 'react';
import { StyleProp, StyleSheet, View, ViewStyle } from 'react-native';

import * as theme from '../../utils/theme';

export class Background extends PureComponent {
render () {
return (
<View style={[styles.container, this.props.style]}>
{this.props.children}
</View>
);
}
interface BackgroundProps {
children?: JSX.Element;
style?: StyleProp<ViewStyle>;
}

export function Background(props: BackgroundProps) {
return <View style={[styles.container, props.style]}>{props.children}</View>;
}

const styles = StyleSheet.create({
Expand Down
130 changes: 66 additions & 64 deletions App/Screens/Loading/Loading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,60 @@
// You should have received a copy of the GNU General Public License
// along with Sh**t! I Smoke. If not, see <http:https://www.gnu.org/licenses/>.

import React, { Component } from 'react';
import { inject, observer } from 'mobx-react';
import * as ExpoLocation from 'expo-location';
import * as Permissions from 'expo-permissions';
import * as TaskManager from 'expo-task-manager';
import * as Location from 'expo-location';
import retry from 'async-retry';
import React, { useContext, useEffect, useState } from 'react';
import { StyleSheet, Text } from 'react-native';
import { Background } from './Background';
import * as dataSources from '../../utils/dataSources';
import * as theme from '../../utils/theme';

import { i18n } from '../../localization';
import { AqiHistoryDb } from '../../managers';
import {
CurrentLocationContext,
GpsLocationContext,
Location
} from '../../stores';
import * as dataSources from '../../utils/dataSources';
import * as theme from '../../utils/theme';

const TASK_STORE_AQI_HISTORY = 'store-aqi-history';

@inject('stores')
@observer
export class Loading extends Component {
state = {
longWaiting: false // If api is taking a long time
};
interface LoadingProps {}

longWaitingTimeout = null; // The variable returned by setTimeout for longWaiting
// The variable returned by setTimeout for longWaiting
let longWaitingTimeout: number | null = null;

async componentDidMount () {
await this.fetchData();
await this._startRecordingAqiHistory();
}
function fetchApi() {}

componentWillUnmount () {
if (this.longWaitingTimeout) {
clearTimeout(this.longWaitingTimeout);
}
}
export function Loading() {
const currentLocation = useContext(CurrentLocationContext);
const gps = useContext(GpsLocationContext);

_startRecordingAqiHistory = async () => {
await Location.startLocationUpdatesAsync(TASK_STORE_AQI_HISTORY, {
accuracy: Location.Accuracy.BestForNavigation,
timeInterval: AqiHistoryDb.SAVE_DATA_INTERVAL,
distanceInterval: 0
});
};
const [longWaiting, setLongWaiting] = useState(false); // If api is taking a long time

useEffect(() => {}, []);

// async componentDidMount () {
// await this.fetchData();
// await this._startRecordingAqiHistory();
// }

// componentWillUnmount () {
// if (this.longWaitingTimeout) {
// clearTimeout(this.longWaitingTimeout);
// }
// }

// _startRecordingAqiHistory = async () => {
// await Location.startLocationUpdatesAsync(TASK_STORE_AQI_HISTORY, {
// accuracy: Location.Accuracy.BestForNavigation,
// timeInterval: AqiHistoryDb.SAVE_DATA_INTERVAL,
// distanceInterval: 0
// });
// };

_apiCall = async (currentPosition) => {
const _apiCall = async currentPosition => {
// We currently have 2 sources, aqicn, and windWaqi
// We put them in an array
const sources = [dataSources.aqicn, dataSources.windWaqi];
Expand All @@ -79,7 +89,7 @@ export class Loading extends Component {
);
};

async fetchData () {
async function fetchData() {
const { stores } = this.props;
const { location } = stores;

Expand Down Expand Up @@ -132,45 +142,38 @@ export class Loading extends Component {
}
}

render () {
return (
<Background style={theme.withPadding}>
<Text style={styles.text}>{this.renderText()}</Text>
</Background>
);
}
return (
<Background style={theme.withPadding}>
<Text style={styles.text}>{renderText(longWaiting, gps, api)}</Text>
</Background>
);
}

renderCough = index => (
function renderCough(index: number) {
return (
<Text key={index}>
{i18n.t('loading_title_cough')}
<Text style={styles.dots}>...</Text>
</Text>
);
}

renderText = () => {
const {
stores: {
api,
location: { gps }
}
} = this.props;
const { longWaiting } = this.state;
let coughs = 0; // Number of times to show "Cough..."
if (gps)++coughs;
if (longWaiting)++coughs;
if (api)++coughs;

return (
<Text>
{i18n.t('loading_title_loading')}
<Text style={styles.dots}>...</Text>
{Array.from({ length: coughs }, (_, index) => index + 1).map(
// Create array 1..N and rendering Cough...
this.renderCough
)}
</Text>
);
};
function renderText(longWaiting: boolean, gps: Location | undefined, api: any) {
let coughs = 0; // Number of times to show "Cough..."
if (gps) ++coughs;
if (longWaiting) ++coughs;
if (api) ++coughs;

return (
<Text>
{i18n.t('loading_title_loading')}
<Text style={styles.dots}>...</Text>
{Array.from({ length: coughs }, (_, index) => index + 1).map(
// Create array 1..N and rendering Cough...
renderCough
)}
</Text>
);
}

// TaskManager.defineTask(TASK_STORE_AQI_HISTORY, async ({ data, error }) => {
Expand Down Expand Up @@ -206,7 +209,6 @@ export class Loading extends Component {
// await AqiHistoryDb.saveData(api.city.name, api.rawPm25, coords);
// }
// }
});

const styles = StyleSheet.create({
dots: {
Expand Down
35 changes: 18 additions & 17 deletions App/Screens/Screens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
// import { Details } from './Details';
import { ErrorScreen } from './ErrorScreen';
// import { Home } from './Home';
// import { Loading } from './Loading';
import { Loading } from './Loading';
import { Search } from './Search';
import * as theme from '../utils/theme';

Expand All @@ -44,24 +44,24 @@ function stackNavigatorOptions(initialRouteName: string) {
} as StackNavigatorConfig;
}

// /**
// * The main stack navigator, for the app.
// */
/**
* The main stack navigator, for the app.
*/
// const RootStack = createAppContainer(
// createStackNavigator(
// {
// About: {
// screen: About
// },
// Details: {
// screen: Details
// },
// Home: {
// screen: Home
// },
// Search: {
// screen: Search
// }
// // About: {
// // screen: About
// // },
// // Details: {
// // screen: Details
// // },
// // Home: {
// // screen: Home
// // },
// // Search: {
// // screen: Search
// // }
// },
// stackNavigatorOptions('Home')
// )
Expand Down Expand Up @@ -114,7 +114,8 @@ const ErrorStack = createAppContainer(

const RootNavigator = createAppContainer(
createSwitchNavigator({
Error: ErrorStack
Error: ErrorStack,
Loading: Loading
})
);

Expand Down
39 changes: 19 additions & 20 deletions App/Screens/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
Location
} from '../../stores';
import { sideEffect } from '../../utils/fp';
import { noop } from '../../utils/noop';
import * as theme from '../../utils/theme';

// As per https://community.algolia.com/places/rest.html
Expand Down Expand Up @@ -78,11 +79,8 @@ const AxiosResponseT = t.type({
});

function fetchAlgolia(search: string, gps?: LatLng) {
// THe attempt number
let attempt = 0;

return retrying(
capDelay(2000, limitRetries(4)), // Do 4 times max, and set limit to 2s
capDelay(2000, limitRetries(algoliaUrls.length)), // Do 4 times max, and set limit to 2s
status =>
pipe(
status.previousDelay,
Expand All @@ -91,24 +89,21 @@ function fetchAlgolia(search: string, gps?: LatLng) {
() =>
pipe(
TE.rightIO(
sideEffect(() => {
++attempt;
}, undefined)
),
TE.chain(() =>
TE.rightIO(
C.log(
`<Search> - handleChangeSearch - Attempt #${attempt}: ${
algoliaUrls[attempt - (1 % algoliaUrls.length)]
}/1/places/query`
)
C.log(
`<Search> - fetchAlgolia - Attempt #${status.iterNumber}: ${
algoliaUrls[(status.iterNumber - 1) % algoliaUrls.length]
}/1/places/query`
)
),
TE.chain(() =>
TE.tryCatch(
() =>
axios.post(
`${algoliaUrls[attempt - 1]}/1/places/query`,
`${
algoliaUrls[
(status.iterNumber - 1) % algoliaUrls.length
]
}/1/places/query`,
{
aroundLatLng: gps
? `${gps.latitude},${gps.longitude}`
Expand Down Expand Up @@ -150,9 +145,7 @@ function fetchAlgolia(search: string, gps?: LatLng) {
TE.rightIO(
sideEffect(
C.log(
`<Search> - handleChangeSearch - Got ${
hits.length
} results`
`<Search> - fetchAlgolia - Got ${hits.length} results`
),
hits
)
Expand All @@ -161,7 +154,13 @@ function fetchAlgolia(search: string, gps?: LatLng) {
)
)
),
E.isLeft
e => {
E.fold(err => {
console.log(`<Search> - fetchAlgolia - Error ${err}`);
}, noop)(e);

return E.isLeft(e);
}
);
}

Expand Down
Loading