Skip to content

Commit

Permalink
Merged in CLT-711_geolocalisation_android (pull request beefe#59)
Browse files Browse the repository at this point in the history
CLT-711 geolocalisation android

* - The problem was from High_Accuracy in Android. Removed the 3rd parameter in getCurrentPosition. Now working, even if the user location type is high_accuracy in his android parameters.
    - Also added alert messages to the user: Because if he allow the permission, but his location stay disabled, the application provide no feedback to the user.

* - add little library who can handle (with playservices) the high_precision issue for Android
    facebook/react-native#7495
    - added some prompt message to the user to let him know the problem in case of failure
    - added requestPermissionLocation (e.g if the permission was removed after install)
    - Removed some not used imports in ChatSaga

* removed Alerts

* Documented error 5 and added github link in comment

Approved-by: Aaron Lee <aaron@onefatgiraffe.com>
  • Loading branch information
Rodolphe Fauquez authored and apparition47 committed Jan 29, 2018
1 parent aa53ef1 commit 320003e
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 33 deletions.
49 changes: 34 additions & 15 deletions App/Sagas/ChatSagas.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { put, take, call, select } from 'redux-saga/effects'
import {
put,
call,
select
} from 'redux-saga/effects'
import ChatActions from '../Redux/ChatRedux'
import { NavigationActions } from 'react-navigation'
import {PlanType} from "../Models/PlanType"
import Validator from "validator"
import isSameDay from 'date-fns/is_same_day'
import {ClintalMessage} from '../Models/ClintalMessage'
import {MessageCell} from '../Models/MessageCell'
import { Question, AnswerType } from '../Models/Question'
import { ChatUiType } from '../Models/ChatUiType';
import RegistrationActions from "../Redux/RegisterRedux";
import Geo from "../Services/Geolocation";
import {
Question,
AnswerType
} from '../Models/Question'
import { ChatUiType } from '../Models/ChatUiType'
import RegistrationActions from '../Redux/RegisterRedux'
import Geo from '../Services/Geolocation'
import { PermissionsAndroid } from 'react-native'

const pageSize = 20

Expand Down Expand Up @@ -180,6 +183,16 @@ export function * getBotQuestion (api, action) {
}
}

const requestLocationPermission = () => {
try {
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
)
} catch (err) {
console.warn(err)
}
}

export function * postBotReplyFlow (api, action) {
const {questionId, choice} = action

Expand All @@ -203,15 +216,21 @@ export function * postBotReplyFlow (api, action) {
// 現在地周辺 auto-detect current location
try {
const {latitude, longitude} = yield Geo.getCurrentPosition()
} catch(err) {
} catch (err) {
let errorMessage = ''

if (err.code === 1) { // PositionError.PERMISSION_DENIED
yield put(ChatActions.sendBotAnswerFailure("GPSの取得が許可されていません。設定をご確認ください。"))
errorMessage = "GPSの取得が許可されていません。設定をご確認ください。"
requestLocationPermission()
yield put(ChatActions.sendBotAnswerFailure(errorMessage))
} else if (err.code === 2) { // PositionError.POSITION_UNAVAILABLE
yield put(ChatActions.sendBotAnswerFailure("GPSの取得に失敗しました。設定をご確認ください。"))
errorMessage = "GPSの取得に失敗しました。設定をご確認ください。"
yield put(ChatActions.sendBotAnswerFailure(errorMessage))
} else { // PositionError.TIMEOUT
yield put(ChatActions.sendBotAnswerFailure("GPSの取得にタイムアウトしました。設定をご確認ください。"))
errorMessage = "GPSの取得にタイムアウトしました。設定をご確認ください。"
yield put(ChatActions.sendBotAnswerFailure(errorMessage))
}
return;
return
}
}
response = yield call(api.postBotAnswerLocation, questionId, choice.id, latitude, longitude)
Expand Down
77 changes: 60 additions & 17 deletions App/Services/Geolocation.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,64 @@
const getCurrentPosition = () => {
import {Platform} from 'react-native'
import Geolocation from 'react-native-geolocation-service'

let secondTryAgainGetPosition = false

const getCurrentPosition = (enableHighAccuracy = true) => {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
resolve({latitude, longitude});
},
(error) => {
reject(error)
},
{
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 1000
}
);
});
/* The default navigator.geolocation.getCurrentPosition have some issues (TIMEOUT) with enableHighAccuracy: true in Android,
and removing the 3rd parameter or setting enableHighAccuracy to false do not resolve the problem, so
this custom library can handle it:
The google system will show a modal and give the possibility to activate the location service directly from the modal
so we don't have to ask to the user to do it manually
See more: https://github.com/Agontuk/react-native-geolocation-service */
if (Platform.OS === 'android') {
Geolocation.getCurrentPosition(
(position) => {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
resolve({latitude, longitude});
},
(error) => {
// See docs: 5 === SETTINGS_NOT_SATISFIED
// "Location service is not enabled or location mode is not appropriate for the current request"
if (error.code === 5) {
reject(error)
} else {
if (!secondTryAgainGetPosition) {
reject(error)
secondTryAgainGetPosition = true
getCurrentPosition(false)
} else {
secondTryAgainGetPosition = false
reject(error)
}
}
},
{
enableHighAccuracy: enableHighAccuracy,
timeout: 20000,
maximumAge: 1000
}
)
} else {
//default system for ios
navigator.geolocation.getCurrentPosition(
(position) => {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
resolve({latitude, longitude});
},
(error) => {
reject(error)
},
{
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 1000
}
)
}
})
}

export default {
Expand Down
5 changes: 5 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ dependencies {
compile 'com.google.android.gms:play-services-base:11.4.2'
compile 'com.google.firebase:firebase-core:11.4.2'
compile 'com.google.firebase:firebase-messaging:11.4.2'

compile(project(':react-native-geolocation-service')) {
exclude group: 'com.google.android.gms', module: 'play-services-location'
}
compile 'com.google.android.gms:play-services-location:11.4.2'
}

// Run this once to be able to run the application with BUCK
Expand Down
4 changes: 3 additions & 1 deletion android/app/src/main/java/com/clintal/MainApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import com.agontuk.RNFusedLocation.RNFusedLocationPackage;

import java.util.Arrays;
import java.util.List;
Expand All @@ -33,7 +34,8 @@ protected List<ReactPackage> getPackages() {
new MainReactPackage(),
new RNFirebasePackage(),
new RNFirebaseMessagingPackage(),
new VectorIconsPackage()
new VectorIconsPackage(),
new RNFusedLocationPackage()
);
}
};
Expand Down
2 changes: 2 additions & 0 deletions android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ include ':react-native-firebase'
project(':react-native-firebase').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-firebase/android')
include ':react-native-vector-icons'
project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android')
include ':react-native-geolocation-service'
project(':react-native-geolocation-service').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-geolocation-service/android')

include ':app'
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"react-native-datepicker": "^1.6.0",
"react-native-drawer": "^2.5.0",
"react-native-firebase": "^3.1.1",
"react-native-geolocation-service": "^1.0.0",
"react-native-gifted-chat": "^0.3.0",
"react-native-hyperlink": "^0.0.11",
"react-native-simple-radio-button": "^2.7.0",
Expand Down

0 comments on commit 320003e

Please sign in to comment.