Skip to content

Commit

Permalink
fix: Make reverse geocode and fetchApi work correctly (#136)
Browse files Browse the repository at this point in the history
* fix: Make reverse geocode and fetchApi work

* Show unknown city text
  • Loading branch information
amaury1093 authored Jul 22, 2019
1 parent 54a8473 commit 8050d31
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 40 deletions.
5 changes: 4 additions & 1 deletion App/Screens/PastStations/PastStations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ export function PastStations (props: PastStationsProps) {
function renderItem ({ item }: { item: AqiHistoryDbItem }) {
return (
<ListItem
description={[item.city, item.country].join(', ')}
description={
[item.city, item.country].filter(x => x).join(', ') ||
i18n.t('past_stations_unknown_city')
}
icon="pin"
title={item.station || UNKNOWN_STATION}
/>
Expand Down
44 changes: 7 additions & 37 deletions App/components/CurrentLocation/CurrentLocation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@
// You should have received a copy of the GNU General Public License
// along with Sh**t! I Smoke. If not, see <http://www.gnu.org/licenses/>.

import { pipe } from 'fp-ts/lib/pipeable';
import * as T from 'fp-ts/lib/Task';
import * as TE from 'fp-ts/lib/TaskEither';
import React, { useEffect, useState } from 'react';
import React from 'react';
import { StyleSheet, Text, TextProps } from 'react-native';

import { Api } from '../../stores/fetchApi';
import { fetchReverseGeocode, Location } from '../../stores/fetchGpsPosition';
import { Location } from '../../stores/fetchGpsPosition';
import { i18n } from '../../localization';
import { logFpError } from '../../util/fp';
import * as theme from '../../util/theme';

const UNKNOWN_STATION = i18n.t('current_location_unknown_station');
Expand All @@ -33,42 +29,16 @@ interface CurrentLocationProps extends TextProps {
currentLocation: Location;
}

// Text to show when fetching reverse geocoding
const LOADING_TEXT = `${i18n.t('current_location_fetching')}...`;

export function CurrentLocation (props: CurrentLocationProps) {
const { api, currentLocation, style, ...rest } = props;
const [locationName, setLocationName] = useState(
currentLocation.name || LOADING_TEXT
);

useEffect(() => {
if (currentLocation.name) {
return;
}

pipe(
fetchReverseGeocode(currentLocation),
TE.fold(
() => {
setLocationName(api.shootISmoke.station || UNKNOWN_STATION);

return T.of(undefined);
},
({ name }) => {
if (name) {
setLocationName(name);
}

return T.of(undefined);
}
)
)().catch(logFpError);
}, []);

return (
<Text style={[styles.title, style]} {...rest}>
{locationName.toUpperCase()}
{(
currentLocation.name ||
api.shootISmoke.station ||
UNKNOWN_STATION
).toUpperCase()}
</Text>
);
}
Expand Down
2 changes: 1 addition & 1 deletion App/localization/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"past_stations_monitored_weekly": "Stations monitored from you during the week",
"past_stations_monitored_monthly": "Stations monitored from you during the month",
"past_stations_unknown_station": "Unknown AQI station",
"past_stations_unknown_city": "Unknown city",
"about_how_do_you_calculate_the_number_of_cigarettes_title": "How do you calculate the number of cigarettes?",
"about_how_do_you_calculate_the_number_of_cigarettes_message_1": "This app was inspired by Berkeley Earth’s findings about the",
"about_how_do_you_calculate_the_number_of_cigarettes_link_1": "equivalence between air pollution and cigarette smoking",
Expand All @@ -71,6 +72,5 @@
"about_credits_source_code": "Source code",
"about_credits_available_github": "available on Github",
"about_language": "Language",
"current_location_fetching": "Fetching",
"current_location_unknown_station": "Unknown AQI station"
}
2 changes: 1 addition & 1 deletion App/stores/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function ApiContextProvider ({ children }: ApiContextProviderProps) {
saveApi(currentLocation, newApi),
TE.map(() => newApi)
)
: TE.right(undefined)
: TE.right(newApi)
),
TE.fold(
err => {
Expand Down
4 changes: 4 additions & 0 deletions App/stores/fetchGpsPosition/fetchGpsPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export function fetchReverseGeocode (currentLocation: LatLng) {
TE.tryCatch(async () => {
const reverse = await ExpoLocation.reverseGeocodeAsync(currentLocation);

if (!reverse.length) {
throw new Error('Reverse geocoding returned no results');
}

return reverse[0];
}, toError),
TE.map(
Expand Down

0 comments on commit 8050d31

Please sign in to comment.