Skip to content

Commit

Permalink
#64 add native exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrKedra committed Aug 1, 2020
1 parent 317e74a commit ba8be3a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
2 changes: 2 additions & 0 deletions android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

-keep public class com.horcrux.svg.** {*;}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"react-native": "0.63.0",
"react-native-android-location-enabler": "^1.2.1",
"react-native-base64": "^0.0.2",
"react-native-exception-handler": "^2.10.8",
"react-native-geolocation-service": "^4.0.1",
"react-native-gesture-handler": "^1.6.1",
"react-native-google-places-autocomplete": "^1.4.1",
Expand Down
35 changes: 20 additions & 15 deletions src/main/AppLauncher.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import {connect} from 'react-redux';
import {View, PermissionsAndroid, Appearance} from 'react-native';
import {View, PermissionsAndroid, Appearance, ToastAndroid} from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
import NetInfo from "@react-native-community/netinfo";

Expand All @@ -13,11 +13,20 @@ import LoadingComponent from "./components/LoadingComponent";
import NoInternetConnectionComponent from "./components/NoInternetConnectionComponent";
import RNAndroidLocationEnabler from "react-native-android-location-enabler";
import {getDarkTheme, getLightTheme} from "./theme/Theme";
import { setJSExceptionHandler, setNativeExceptionHandler } from 'react-native-exception-handler';

const ACTIVE_LOCATION_STORAGE = '@active_location';
const HOME_LOCATION_STORAGE = '@home_location';
const THEME_STORAGE = '@theme';

setJSExceptionHandler((error, isFatal) => {
alert(error);
});

setNativeExceptionHandler((error) => {
alert(error);
});

class AppLauncher extends React.Component {

state = {
Expand Down Expand Up @@ -63,10 +72,10 @@ class AppLauncher extends React.Component {
if(isStorage === null){
this.props.navigation.replace('WelcomeScreen');
} else {
this.normalAppLaunch();
await this.normalAppLaunch();
}
} catch(e) {
console.log(e);
ToastAndroid.show('Problem with app launching', ToastAndroid.SHORT);
}
};

Expand Down Expand Up @@ -97,22 +106,21 @@ class AppLauncher extends React.Component {
}

async normalAppLaunch(){
if(await this.dataIsFresh()){
if (await this.dataIsFresh()) {
await this.showForecastFromStorage();
} else {
if(await this.isInternetConnection()){
if (await this.isInternetConnection()) {
this.tryToLoadDataFromInternet();
}
}
}

async dataIsFresh() {
try {
const lastUpdate = await AsyncStorage.getItem('@forecast_update_date');
return (new Date() - new Date(JSON.parse(lastUpdate))) < 3600000;
} catch (e) {
const lastUpdate = await AsyncStorage.getItem('@forecast_update_date');
if(!lastUpdate)
return false;
}
const lastUpdateDate = new Date(JSON.parse(lastUpdate));
return (new Date() - lastUpdateDate) < 3600000;
}

async isInternetConnection() {
Expand All @@ -128,18 +136,15 @@ class AppLauncher extends React.Component {
Geolocation.getCurrentPosition(
(position) => this.loadForecastWithGivenPosition(position),
(error) => {
console.log(error);
ToastAndroid.show('couldnt get location', ToastAndroid.SHORT);
this.loadForecastUsingLocationInStorage();
},
{enableHighAccuracy: false, timeout: 15000, maximumAge: 10000}
);
})
} else {
this.loadForecastUsingLocationInStorage();
});
}
} catch (err) {
this.loadForecastUsingLocationInStorage();
console.log(err);
}
}

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5918,6 +5918,11 @@ react-native-base64@^0.0.2:
resolved "https://registry.yarnpkg.com/react-native-base64/-/react-native-base64-0.0.2.tgz#c28463c2c6779ac3ec5fdd12979ebc8c5f9b410a"
integrity sha512-Fu/J1a2y0X22EJDWqJR2oEa1fpP4gTFjYxk8ElJdt1Yak3HOXmFJ7EohLVHU2DaQkgmKfw8qb7u/48gpzveRbg==

react-native-exception-handler@^2.10.8:
version "2.10.8"
resolved "https://registry.yarnpkg.com/react-native-exception-handler/-/react-native-exception-handler-2.10.8.tgz#1a7f846592d888d23adaf797e31802585f1d9ff0"
integrity sha512-ZN+jwpADRkCUNdad/50k0mZdMoICGrGdtaxgvRU+pNcWRRBAXJhuo4+jY0eQaoVpx1ghycGE6tBu9ka8gL2NOQ==

react-native-geolocation-service@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/react-native-geolocation-service/-/react-native-geolocation-service-4.0.2.tgz#b107bf2bd6e794d072a3be9c0325a27a41028568"
Expand Down

0 comments on commit ba8be3a

Please sign in to comment.