@ -1,31 +1,25 @@
import { Alert , PermissionsAndroid , Platform , ToastAndroid } from 'react-native' ;
import { Alert , PermissionsAndroid , Platform , ToastAndroid } from 'react-native' ;
import Geolocation from 'react-native-geolocation-service' ;
import Geolocation from 'react-native-geolocation-service' ;
import { strings } from './i18n' ;
import { strings } from './i18n' ;
import moment from 'moment' ;
import moment from 'moment' ;
import { DATE _FORMAT } from './general' ;
import { DATE _FORMAT } from './general' ;
export const hasLocationPermission = async ( ) => {
export const hasLocationPermission = async ( ) => {
if ( Platform . OS === 'ios' ||
if ( Platform . OS === 'ios' || ( Platform . OS === 'android' && Platform . Version < 23 ) ) {
( Platform . OS === 'android' && Platform . Version < 23 ) ) {
return true ;
return true ;
}
}
const hasPermission = await PermissionsAndroid . check (
const hasPermission = await PermissionsAndroid . check (
PermissionsAndroid . PERMISSIONS . ACCESS _FINE _LOCATION
PermissionsAndroid . PERMISSIONS . ACCESS _FINE _LOCATION
// PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION
) ;
) ;
if ( hasPermission ) return true ;
if ( hasPermission ) return true ;
const status = await PermissionsAndroid . request (
const status = await PermissionsAndroid . request (
PermissionsAndroid . PERMISSIONS . ACCESS _FINE _LOCATION ,
PermissionsAndroid . PERMISSIONS . ACCESS _FINE _LOCATION ,
// PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION
{
{
title : "NawakaraConnect Location Permission" ,
title : "NeOps Location Permission" ,
message : "NawakaraConnect wants to enable location permission. Let NawakaraConnect collects the device's location to get current position of the device." ,
message : "NeOps wants to enable location permission. Let NeOps collect the device's location to get the current position of the device." ,
// buttonNeutral: 'Ask Me Later',
buttonNegative : 'Deny' ,
buttonNegative : 'Deny' ,
buttonPositive : 'Accept' ,
buttonPositive : 'Accept' ,
}
}
@ -43,12 +37,15 @@ export const hasLocationPermission = async () => {
}
}
export const getCoords = async ( callback ) => {
export const getCoords = async ( callback ) => {
if ( typeof callback !== 'function' ) {
throw new TypeError ( 'Callback must be a function' ) ;
}
const hasLocation = await hasLocationPermission ( ) ;
const hasLocation = await hasLocationPermission ( ) ;
if ( ! hasLocation ) {
if ( ! hasLocation ) {
callback ( null )
callback ( { lat : null , lon : null } ) ;
return
return ;
}
}
Geolocation . getCurrentPosition (
Geolocation . getCurrentPosition (
@ -57,21 +54,19 @@ export const getCoords = async (callback) => {
Alert . alert (
Alert . alert (
strings ( 'takePicture.fakeGPSDetected' ) ,
strings ( 'takePicture.fakeGPSDetected' ) ,
strings ( 'takePicture.fakeGPSFailedMessage' ) ,
strings ( 'takePicture.fakeGPSFailedMessage' ) ,
[
[ { text : "OK" } ]
{ text : "OK" }
]
) ;
) ;
return false ;
callback ( { lat : null , lon : null } ) ;
return ;
}
}
let lat = position . coords . latitude ;
let lat = position . coords . latitude ;
let lon = position . coords . longitude ;
let lon = position . coords . longitude ;
let timestamp = moment ( position . timestamp ) . format ( DATE _FORMAT ) ;
let timestamp = moment ( position . timestamp ) . format ( 'D MMMM YYYY HH:mm' ) ;
callback ( { lat , lon , timestamp } )
callback ( { lat , lon , timestamp } ) ;
} ,
} ,
( error ) => {
( error ) => {
// See error code charts below.
console . log ( '[Home] error Geolocation.getCurrentPosition' , error . code , error . message ) ;
console . log ( '[Home] error Geolocation.getCurrentPosition' , error . code , error . message ) ;
// callback(null)
callback ( { lat : null , lon : null } ) ;
} ,
} ,
{ enableHighAccuracy : true , timeout : 15000 , maximumAge : 10000 }
{ enableHighAccuracy : true , timeout : 15000 , maximumAge : 10000 }
) ;
) ;