Skip to content

Commit

Permalink
feat: Distance is measured in mi for en (#153)
Browse files Browse the repository at this point in the history
* Distance is measured in mi for en.

* Moved haversine unit to translations
  • Loading branch information
Brad-Christie authored and amaury1093 committed Jul 31, 2019
1 parent e1305b6 commit 13c08b5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
7 changes: 5 additions & 2 deletions App/Screens/Details/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Distance } from './Distance';
import { Header } from './Header';
import { i18n } from '../../localization';
import { ApiContext, CurrentLocationContext } from '../../stores';
import { distanceToStation, getCorrectLatLng } from '../../util/station';
import { distanceToStation, getCorrectLatLng, DistanceUnit } from '../../util/station';

interface DetailsProps extends NavigationInjectedProps {}

Expand Down Expand Up @@ -61,6 +61,9 @@ export function Details (props: DetailsProps) {
// object` error. It's related to the MapView below.
const currentLocation = { ..._currentLocation! };

const haversineDistanceUnit = i18n.t('haversine_distance_unit') as DistanceUnit;
const distance = distanceToStation(currentLocation!, api!, haversineDistanceUnit);

const station = {
description: api!.shootISmoke.station || '',
title: api!.shootISmoke.station,
Expand Down Expand Up @@ -102,7 +105,7 @@ export function Details (props: DetailsProps) {
</MapView>
)}
</View>
<Distance distance={distanceToStation(currentLocation!, api!)} />
<Distance distance={distance} />
</View>
);
}
Expand Down
7 changes: 6 additions & 1 deletion App/Screens/Details/Distance/Distance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ interface DistanceProps {
}

export function Distance (props: DistanceProps) {
const distanceUnit = i18n.t('distance_unit');

return (
<Banner elevated shadowPosition="top" style={styles.banner}>
<Text style={styles.distance}>
{i18n
.t('details_distance_label', { distanceToStation: props.distance })
.t('details_distance_label', {
distanceToStation: props.distance,
distanceUnit
})
.toUpperCase()}
</Text>
</Banner>
Expand Down
9 changes: 6 additions & 3 deletions App/Screens/Home/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import alert from '../../../../assets/images/alert.png';
import { CurrentLocation } from '../../../components';
import { i18n } from '../../../localization';
import { ApiContext, CurrentLocationContext } from '../../../stores';
import { distanceToStation, isStationTooFar } from '../../../util/station';
import { distanceToStation, isStationTooFar, DistanceUnit } from '../../../util/station';
import * as theme from '../../../util/theme';

interface HeaderProps {
Expand All @@ -41,7 +41,9 @@ export function Header (props: HeaderProps) {
const { currentLocation, isGps } = useContext(CurrentLocationContext);
const { onChangeLocationClick } = props;

const distance = distanceToStation(currentLocation!, api!);
const distanceUnit = i18n.t('distance_unit');
const haversineDistanceUnit = i18n.t('haversine_distance_unit') as DistanceUnit;
const distance = distanceToStation(currentLocation!, api!, haversineDistanceUnit);
const isTooFar = isStationTooFar(currentLocation!, api!);

return (
Expand All @@ -57,7 +59,8 @@ export function Header (props: HeaderProps) {
{isTooFar && <Image source={alert} style={styles.warning} />}
<Text style={theme.text}>
{i18n.t('home_header_air_quality_station_distance', {
distanceToStation: distance
distanceToStation: distance,
distanceUnit
})}{' '}
{!isGps && i18n.t('home_header_from_search')}
</Text>
Expand Down
6 changes: 4 additions & 2 deletions App/localization/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"home_station_too_far_message": "We couldn’t find a closer station to you.\nResults may be inaccurate at this distance.",
"home_share_title": "Did you know that you may be smoking up to 20 cigarettes per day, just for living in a big city?",
"home_share_message": "Shoot! I 'smoked' {{cigarettes}} cigarettes today by breathing urban air. And you? Find out here: https://shootismoke.github.io",
"home_header_air_quality_station_distance": "Air Quality Station: {{distanceToStation}}km away",
"home_header_air_quality_station_distance": "Air Quality Station: {{distanceToStation}}{{distanceUnit}} away",
"home_header_from_search": "from search",
"home_btn_why_is_station_so_far": "Why is the station so far?",
"home_btn_see_detailed_info": "See detailed info",
Expand Down Expand Up @@ -43,7 +43,9 @@
"details_header_latest_update_label": "Latest Update:",
"details_header_latest_update_ago": "ago",
"details_header_primary_pollutant_label": "Primary Pollutant:",
"details_distance_label": "AQI Station: {{distanceToStation}}km away",
"details_distance_label": "AQI Station: {{distanceToStation}}{{distanceUnit}} away",
"distance_unit": "mi",
"haversine_distance_unit": "mile",
"past_stations_loading": "Loading",
"past_stations_date_from_to": "{{startDate}} to {{endDate}}",
"past_stations_monitored_weekly": "Stations monitored from you during the week",
Expand Down
6 changes: 4 additions & 2 deletions App/localization/languages/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"home_station_too_far_message": "No encontramos una estación cerca\\nLos resultados pueden ser inexactos a esta distancia.",
"home_share_title": "¿Sabías que por vivir en una gran ciudad puedes estar fumando hasta 20 cigarrillos al día?",
"home_share_message": "Rayos! 'hoy fumé'' {{cigarettes}} cigarrillos respirando aire urbano. ¿Y tú? Encuentralo aquí: https://shootismoke.github.io",
"home_header_air_quality_station_distance": "Estación de calidad del aire: a {{distanceToStation}}km",
"home_header_air_quality_station_distance": "Estación de calidad del aire: a {{distanceToStation}}{{distanceUnit}}",
"home_btn_why_is_station_so_far": "¿Estación retirada?",
"home_btn_see_detailed_info": "Ver detalles",
"home_btn_more_details": "Más detalles",
Expand All @@ -34,7 +34,9 @@
"details_your_position_marker": "Tu ubicación",
"details_header_latest_update_label": "Última actualización:",
"details_header_primary_pollutant_label": "Contaminante primario:",
"details_distance_label": "Estación AQI: a {{distanceToStation}}km",
"details_distance_label": "Estación AQI: a {{distanceToStation}}{{distanceUnit}}",
"distance_unit": "km",
"haversine_distance_unit": "km",
"about_how_do_you_calculate_the_number_of_cigarettes_title": "Cálculo de cigarrillos",
"about_how_do_you_calculate_the_number_of_cigarettes_message_1": "Esta aplicación se inspiró en los hallazgos de Berkeley Earth sobre la",
"about_how_do_you_calculate_the_number_of_cigarettes_link_1": "Equivalencia entre la contaminación del aire y el tabaquismo.",
Expand Down
8 changes: 6 additions & 2 deletions App/util/station.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,24 @@ import { LatLng } from '../stores/fetchGpsPosition';
// Above this distance (km), we consider the station too far from the user
export const MAX_DISTANCE_TO_STATION = 10;

export type DistanceUnit = 'km' | 'mile';

/**
* Get distance from current location to station.
*
* @param currentLocation - The current location of the user.
* @param api - The api object returned by remote data.
* @param unit - The unit of measure returned.
*/
export function distanceToStation (currentLocation: LatLng, api: Api) {
export function distanceToStation (currentLocation: LatLng, api: Api, unit: DistanceUnit = 'km') {
return Math.round(
haversine(
currentLocation,
getCorrectLatLng(currentLocation, {
latitude: api.city.geo[0],
longitude: api.city.geo[1]
})
}),
{ unit }
)
);
}
Expand Down

0 comments on commit 13c08b5

Please sign in to comment.