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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement miles/kilometer selector #252

Merged
merged 11 commits into from
Oct 6, 2019
Prev Previous commit
Next Next commit
fix: resolve review's comments
  • Loading branch information
matepapp committed Oct 6, 2019
commit f67dcdd1b4472d0dd0b2d7d2d96c48b12925f015
46 changes: 23 additions & 23 deletions App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,60 +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 * as Font from 'expo-font';
import Constants from 'expo-constants';
import React, { useEffect, useState } from 'react';
import { AppState } from 'react-native';
import * as Sentry from 'sentry-expo';
import * as Font from 'expo-font'
import Constants from 'expo-constants'
import React, { useEffect, useState } from 'react'
import { AppState } from 'react-native'
import * as Sentry from 'sentry-expo'

import { Screens } from './Screens';
import { Background as LoadingBackground } from './Screens/Loading/Background';
import { Screens } from './Screens'
import { Background as LoadingBackground } from './Screens/Loading/Background'
import {
ApiContextProvider,
DistanceUnitProvider,
ErrorContextProvider,
FrequencyContextProvider,
LocationContextProvider,
DistanceUnitProvider
} from './stores';
import { setupAmplitude, track } from './util/amplitude';
} from './stores'
import { setupAmplitude, track } from './util/amplitude'

// Add Sentry if available
if (Constants.manifest.extra.sentryPublicDsn) {
Sentry.init({
dsn: Constants.manifest.extra.sentryPublicDsn,
debug: true
});
debug: true,
})

if (Constants.manifest.revisionId) {
Sentry.setRelease(Constants.manifest.revisionId);
Sentry.setRelease(Constants.manifest.revisionId)
}
}

export function App () {
const [ready, setReady] = useState(false);
const [ready, setReady] = useState(false)
useEffect(() => {
Promise.all([
Font.loadAsync({
'gotham-black': require('../assets/fonts/Gotham-Black.ttf'),
'gotham-book': require('../assets/fonts/Gotham-Book.ttf')
'gotham-book': require('../assets/fonts/Gotham-Book.ttf'),
}),
// Add Amplitude if available
setupAmplitude()
setupAmplitude(),
])

.then(() => setReady(true))
.catch(console.error);
}, []);
.catch(console.error)
}, [])

useEffect(() => {
AppState.addEventListener('change', state => {
if (state === 'active') {
track('APP_REFOCUS');
track('APP_REFOCUS')
} else if (state === 'background') {
track('APP_EXIT');
track('APP_EXIT')
}
});
}, []);
})
}, [])

return ready ? (
<ErrorContextProvider>
Expand All @@ -83,5 +83,5 @@ export function App () {
</ErrorContextProvider>
) : (
<LoadingBackground />
);
)
}
4 changes: 2 additions & 2 deletions App/Screens/About/About.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ interface AboutProps

export function About (props: AboutProps) {
const { navigation } = props;
const { distanceUnit, setDistanceUnit, localizedDistanceUnit } = useDistanceUnit();
const { distanceUnit, localizedDistanceUnit, setDistanceUnit } = useDistanceUnit();

trackScreen('ABOUT');

Expand Down Expand Up @@ -147,8 +147,8 @@ export function About (props: AboutProps) {
<View style={styles.distanceSwitchWrapper}>
<Switch
onValueChange={toggleDistanceSwitch}
value={distanceUnit === 'km'}
trackColor={{ true: theme.primaryColor, false: theme.iconBackgroundColor }}
value={distanceUnit === 'km'}
/>
<Text style={styles.distanceText}>{localizedDistanceUnit('long')}</Text>
</View>
Expand Down
2 changes: 1 addition & 1 deletion App/Screens/Details/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import { Header } from './Header';
import { i18n } from '../../localization';
import { ApiContext, CurrentLocationContext } from '../../stores';
import { trackScreen } from '../../util/amplitude';
import { distanceToStation, getCorrectLatLng } from '../../util/station';
import { useDistanceUnit } from '../../stores/distanceUnit';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alphabetical

import { distanceToStation, getCorrectLatLng } from '../../util/station';

interface DetailsProps extends NavigationInjectedProps {}

Expand Down
55 changes: 27 additions & 28 deletions App/Screens/Home/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,31 @@
// 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, { useContext } from 'react';
import { GestureResponderEvent, Image, StyleSheet, Text, View } from 'react-native';
import { scale } from 'react-native-size-matters';
import React, { useContext } from 'react'
import { GestureResponderEvent, Image, StyleSheet, Text, View } from 'react-native'
import { scale } from 'react-native-size-matters'

import alert from '../../../../assets/images/alert.png';
import { ChangeLocation, CurrentLocation } from '../../../components';
import { i18n } from '../../../localization';
import { ApiContext, CurrentLocationContext } from '../../../stores';
import { distanceToStation, isStationTooFar } from '../../../util/station';
import { useDistanceUnit } from '../../../stores/distanceUnit';
import * as theme from '../../../util/theme';
import alert from '../../../../assets/images/alert.png'
import { ChangeLocation, CurrentLocation } from '../../../components'
import { i18n } from '../../../localization'
import { ApiContext, CurrentLocationContext } from '../../../stores'
import { distanceToStation, isStationTooFar } from '../../../util/station'
import { useDistanceUnit } from '../../../stores/distanceUnit'
import * as theme from '../../../util/theme'

interface HeaderProps {
onChangeLocationClick: (event: GestureResponderEvent) => void;
onChangeLocationClick: (event: GestureResponderEvent) => void
}

export function Header (props: HeaderProps) {
const { api } = useContext(ApiContext)!;
const { currentLocation, isGps } = useContext(CurrentLocationContext);
const { localizedDistanceUnit, distanceUnit } = useDistanceUnit();
const { onChangeLocationClick } = props;
const { api } = useContext(ApiContext)!
const { currentLocation, isGps } = useContext(CurrentLocationContext)
const { distanceUnit, localizedDistanceUnit } = useDistanceUnit()
const { onChangeLocationClick } = props

const shortDistanceUnit = localizedDistanceUnit('short');
const distance = distanceToStation(currentLocation!, api!, distanceUnit);
console.log('distance', distance + distanceUnit);
const isTooFar = isStationTooFar(currentLocation!, api!);
const shortDistanceUnit = localizedDistanceUnit('short')
const distance = distanceToStation(currentLocation!, api!, distanceUnit)
const isTooFar = isStationTooFar(currentLocation!, api!)

return (
<View style={styles.container}>
Expand All @@ -50,7 +49,7 @@ export function Header (props: HeaderProps) {
<Text style={styles.distanceText}>
{i18n.t('home_header_air_quality_station_distance', {
distanceToStation: distance,
distanceUnit: shortDistanceUnit
distanceUnit: shortDistanceUnit,
})}{' '}
{!isGps && i18n.t('home_header_from_search')}
</Text>
Expand All @@ -59,31 +58,31 @@ export function Header (props: HeaderProps) {

<ChangeLocation onPress={onChangeLocationClick} />
</View>
);
)
}

const styles = StyleSheet.create({
container: {
...theme.withPadding,
alignItems: 'center',
flexDirection: 'row',
paddingTop: theme.spacing.normal
paddingTop: theme.spacing.normal,
},
currentLocation: {
flex: 1,
marginRight: theme.spacing.mini
marginRight: theme.spacing.mini,
},
distance: {
alignItems: 'center',
flexDirection: 'row',
marginTop: theme.spacing.mini
marginTop: theme.spacing.mini,
},
distanceText: {
...theme.text,
flex: 1
flex: 1,
},
warning: {
marginRight: theme.spacing.mini,
marginTop: scale(-2) // FIXME We shouldn't need that, with `alignItems: 'center'` on .distance
}
});
marginTop: scale(-2), // FIXME We shouldn't need that, with `alignItems: 'center'` on .distance
},
})
6 changes: 3 additions & 3 deletions App/stores/distanceUnit.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { createContext, ReactNode, useContext, useEffect, useState } from 'react';
import { AsyncStorage } from 'react-native';
import React, { useState, useEffect, createContext, FC, useContext } from 'react';

import { i18n } from '../localization';

Expand All @@ -20,8 +20,8 @@ const Context = createContext<ContextType>({
setDistanceUnit: () => {}
});

export const DistanceUnitProvider: FC = ({ children }) => {
const [distanceUnit, setDistanceUnit] = useState<DistanceUnit>('km');
export function DistanceUnitProvider ({ children }: { children: ReactNode }) {
const [distanceUnit, setDistanceUnit] = useState<DistanceUnit>(i18n.locale === 'en-US' ? 'mile' : 'km');

const getDistanceUnit = async (): Promise<void> => {
const unit = await AsyncStorage.getItem(STORAGE_KEY);
Expand Down