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 search work
  • Loading branch information
amaury1093 committed Jul 6, 2019
commit c91174762a23b03c0bf13657bfb01bb1c3a34284
55 changes: 20 additions & 35 deletions App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,38 @@
// 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 * as Font from 'expo-font';
import Constants from 'expo-constants';
import { Provider } from 'mobx-react';
import React, { useEffect, useState } from 'react';
import Sentry from 'sentry-expo';

import { RootStore } from './stores';
import { Background as LoadingBackground } from './Screens/Loading/Background';
import { Screens } from './Screens';

// Set up global MST stores
const stores = RootStore.create({
api: undefined,
error: undefined,
location: {}
});
import { Background as LoadingBackground } from './Screens/Loading/Background';
import { ErrorContextProvider, LocationContextProvider } from './stores';

// Add sentry if available
if (Constants.manifest.extra.sentryPublicDsn) {
Sentry.config(Constants.manifest.extra.sentryPublicDsn).install();
}

export class App extends PureComponent {
state = {
fontLoaded: false
};

async componentDidMount () {
// Using custom fonts with Expo
// https://docs.expo.io/versions/latest/guides/using-custom-fonts
await Font.loadAsync({
export function App() {
const [fontLoaded, setFontLoaded] = useState(false);
useEffect(() => {
Font.loadAsync({
'gotham-black': require('../assets/fonts/Gotham-Black.ttf'),
'gotham-book': require('../assets/fonts/Gotham-Book.ttf')
});

this.setState({ fontLoaded: true });
}

render () {
const { fontLoaded } = this.state;

return fontLoaded ? (
<Provider stores={stores}>
})
.then(() => setFontLoaded(true))
.catch(console.error);
}, []);

return fontLoaded ? (
<ErrorContextProvider>
<LocationContextProvider>
<Screens />
</Provider>
) : (
<LoadingBackground />
);
}
</LocationContextProvider>
</ErrorContextProvider>
) : (
<LoadingBackground />
);
}
35 changes: 25 additions & 10 deletions App/Screens/About/About.ts → App/Screens/About/About.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@

import React, { PureComponent } from 'react';
import Constants from 'expo-constants';
import { Linking, Platform, ScrollView, StyleSheet, Text, View } from 'react-native';
import {
Linking,
Platform,
ScrollView,
StyleSheet,
Text,
View
} from 'react-native';

import { Box } from './Box';
import { BackButton } from '../../components/BackButton';
Expand All @@ -39,22 +46,30 @@ export class About extends PureComponent {
handleOpenMarcelo = () =>
Linking.openURL('https://www.behance.net/marceloscoelho');

render () {
render() {
const { navigation } = this.props;
return (
<ScrollView style={theme.withPadding}>
<BackButton onClick={navigation.pop} style={styles.backButton} />

<View style={styles.section}>
<Text style={styles.h2}>
{i18n.t('about_how_do_you_calculate_the_number_of_cigarettes_title')}
{i18n.t(
'about_how_do_you_calculate_the_number_of_cigarettes_title'
)}
</Text>
<Text style={theme.text}>
{i18n.t('about_how_do_you_calculate_the_number_of_cigarettes_message_1')}{' '}
{i18n.t(
'about_how_do_you_calculate_the_number_of_cigarettes_message_1'
)}{' '}
<Text onPress={this.handleOpenArticle} style={theme.link}>
{i18n.t('about_how_do_you_calculate_the_number_of_cigarettes_link_1')}
{i18n.t(
'about_how_do_you_calculate_the_number_of_cigarettes_link_1'
)}
</Text>
{i18n.t('about_how_do_you_calculate_the_number_of_cigarettes_message_2')}
{i18n.t(
'about_how_do_you_calculate_the_number_of_cigarettes_message_2'
)}
<Text style={styles.micro}>&micro;</Text>
g/m&sup3;
{' \u207D'}
Expand All @@ -71,7 +86,9 @@ export class About extends PureComponent {
</View>

<View style={styles.section}>
<Text style={styles.h2}>{i18n.t('about_where_does_data_come_from_title')}</Text>
<Text style={styles.h2}>
{i18n.t('about_where_does_data_come_from_title')}
</Text>
<Text style={theme.text}>
{i18n.t('about_where_does_data_come_from_message_1')}{' '}
<Text onPress={this.handleOpenAqi} style={theme.link}>
Expand All @@ -91,9 +108,7 @@ export class About extends PureComponent {
</View>

<View style={styles.section}>
<Text style={styles.h2}>
{i18n.t('about_weird_results_title')}
</Text>
<Text style={styles.h2}>{i18n.t('about_weird_results_title')}</Text>
<Text style={theme.text}>
{i18n.t('about_weird_results_message_1')}{' '}
<Text onPress={this.handleOpenAqi} style={theme.link}>
Expand Down
53 changes: 27 additions & 26 deletions App/Screens/ErrorScreen/ErrorScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,44 @@
// 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 { inject, observer } from 'mobx-react';
import React, { Component } from 'react';
import React, { useContext } from 'react';
import { Image, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { NavigationInjectedProps } from 'react-navigation';

import error from '../../../assets/images/error.png';
import { i18n } from '../../localization';
import { ErrorContext } from '../../stores';
import * as theme from '../../utils/theme';

@inject('stores')
@observer
export class ErrorScreen extends Component {
goToSearch = () => this.props.navigation.navigate('Search');
interface ErrorScreenProps extends NavigationInjectedProps {}

render () {
const { error: errorText } = this.props.stores;
export function ErrorScreen(props: ErrorScreenProps) {
const { error: errorText } = useContext(ErrorContext);

return (
<View style={styles.container}>
<Image source={error} />
<View>
<Text style={styles.errorText}>
<Text style={styles.sorry}>{i18n.t('error_screen_common_sorry')}</Text>
{i18n.t('error_screen_error_cannot_load_cigarettes')}
return (
<View style={styles.container}>
<Image source={error} />
<View>
<Text style={styles.errorText}>
<Text style={styles.sorry}>
{i18n.t('error_screen_common_sorry')}
</Text>
</View>
<TouchableOpacity onPress={this.goToSearch}>
<View style={styles.chooseOther}>
<Text style={theme.bigButtonText}>{i18n.t('error_screen_choose_other_location').toUpperCase()}</Text>
</View>
</TouchableOpacity>
<Text style={theme.text}>
{i18n.t('error_screen_error_description')}
{i18n.t('error_screen_error_cannot_load_cigarettes')}
</Text>
<Text style={styles.errorMessage}>{i18n.t('error_screen_error_message', { errorText })}</Text>
</View>
);
}
<TouchableOpacity onPress={() => props.navigation.navigate('Search')}>
<View style={styles.chooseOther}>
<Text style={theme.bigButtonText}>
{i18n.t('error_screen_choose_other_location').toUpperCase()}
</Text>
</View>
</TouchableOpacity>
<Text style={theme.text}>{i18n.t('error_screen_error_description')}</Text>
<Text style={styles.errorMessage}>
{i18n.t('error_screen_error_message', { errorText })}
</Text>
</View>
);
}

const styles = StyleSheet.create({
Expand Down
66 changes: 33 additions & 33 deletions App/Screens/Loading/Loading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,39 +173,39 @@ export class Loading extends Component {
};
}

TaskManager.defineTask(TASK_STORE_AQI_HISTORY, async ({ data, error }) => {
if (error) {
console.log('<Loading> - TaskManager - defineTask - Error', error.message);
return;
}
if (data) {
const { locations } = data;
const { coords } = locations[0];

// We currently have 2 sources, aqicn, and windWaqi
// We put them in an array
const sources = [dataSources.aqicn, dataSources.windWaqi];

const api = await retry(
async (_, attempt) => {
// Attempt starts at 1
console.log(
`<Loading> - fetchData - Attempt #${attempt}: ${
sources[(attempt - 1) % 2].name
}`
);
const result = await sources[(attempt - 1) % 2](coords);
console.log('<Loading> - fetchData - Got result', result);

return result;
},
{ retries: 3 } // 2 attempts per source
);

if (await AqiHistoryDb.isSaveNeeded()) {
await AqiHistoryDb.saveData(api.city.name, api.rawPm25, coords);
}
}
// TaskManager.defineTask(TASK_STORE_AQI_HISTORY, async ({ data, error }) => {
// if (error) {
// console.log('<Loading> - TaskManager - defineTask - Error', error.message);
// return;
// }
// if (data) {
// const { locations } = data;
// const { coords } = locations[0];

// // We currently have 2 sources, aqicn, and windWaqi
// // We put them in an array
// const sources = [dataSources.aqicn, dataSources.windWaqi];

// const api = await retry(
// async (_, attempt) => {
// // Attempt starts at 1
// console.log(
// `<Loading> - fetchData - Attempt #${attempt}: ${
// sources[(attempt - 1) % 2].name
// }`
// );
// const result = await sources[(attempt - 1) % 2](coords);
// console.log('<Loading> - fetchData - Got result', result);

// return result;
// },
// { retries: 3 } // 2 attempts per source
// );

// if (await AqiHistoryDb.isSaveNeeded()) {
// await AqiHistoryDb.saveData(api.city.name, api.rawPm25, coords);
// }
// }
});

const styles = StyleSheet.create({
Expand Down
107 changes: 0 additions & 107 deletions App/Screens/Screens.ts

This file was deleted.

Loading