Skip to content

Commit

Permalink
Separate android & ios/web implementation for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Agontuk committed Aug 24, 2019
1 parent 47ed7b6 commit 798e6f6
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 109 deletions.
103 changes: 103 additions & 0 deletions js/Geolocation.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { NativeEventEmitter, NativeModules } from 'react-native';

const { RNFusedLocation } = NativeModules;
const LocationEventEmitter = new NativeEventEmitter(RNFusedLocation);

const noop = () => {};
let subscriptions = [];
let updatesEnabled = false;

const Geolocation = {
setRNConfiguration: (config) => {}, // eslint-disable-line no-unused-vars

requestAuthorization: () => {},

getCurrentPosition: async (success, error = noop, options = {}) => {
if (!success) {
// eslint-disable-next-line no-console
console.error('Must provide a success callback');
}

// Right now, we're assuming user already granted location permission.
RNFusedLocation.getCurrentPosition(options, success, error);
},

watchPosition: (success, error = null, options = {}) => {
if (!success) {
// eslint-disable-next-line no-console
console.error('Must provide a success callback');
}

if (!updatesEnabled) {
RNFusedLocation.startObserving(options);
updatesEnabled = true;
}

const watchID = subscriptions.length;

subscriptions.push([
LocationEventEmitter.addListener('geolocationDidChange', success),
error ? LocationEventEmitter.addListener('geolocationError', error) : null
]);

return watchID;
},

clearWatch: (watchID) => {
const sub = subscriptions[watchID];

if (!sub) {
// Silently exit when the watchID is invalid or already cleared
// This is consistent with timers
return;
}

sub[0].remove();

const sub1 = sub[1];

if (sub1) {
sub1.remove();
}

subscriptions[watchID] = undefined;

let noWatchers = true;

for (let ii = 0; ii < subscriptions.length; ii += 1) {
if (subscriptions[ii]) {
noWatchers = false; // still valid subscriptions
}
}

if (noWatchers) {
Geolocation.stopObserving();
}
},

stopObserving: () => {
if (updatesEnabled) {
RNFusedLocation.stopObserving();
updatesEnabled = false;

for (let ii = 0; ii < subscriptions.length; ii += 1) {
const sub = subscriptions[ii];
if (sub) {
// eslint-disable-next-line no-console
console.warn('Called stopObserving with existing subscriptions.');
sub[0].remove();

const sub1 = sub[1];

if (sub1) {
sub1.remove();
}
}
}

subscriptions = [];
}
}
};

export default Geolocation;
3 changes: 3 additions & 0 deletions js/Geolocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Geolocation from '@react-native-community/geolocation';

export default Geolocation;
110 changes: 1 addition & 109 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';

// eslint-disable-next-line import/no-mutable-exports
let Geolocation = null;

const noop = () => {};
let subscriptions = [];
let updatesEnabled = false;
import Geolocation from './Geolocation';

export const PositionError = Object.freeze({
PERMISSION_DENIED: 1,
Expand All @@ -16,105 +9,4 @@ export const PositionError = Object.freeze({
INTERNAL_ERROR: -1
});

if (Platform.OS === 'ios') {
// eslint-disable-next-line global-require
Geolocation = require('@react-native-community/geolocation');
} else if (Platform.OS === 'android') {
const { RNFusedLocation } = NativeModules;
const LocationEventEmitter = new NativeEventEmitter(RNFusedLocation);

Geolocation = {
setRNConfiguration: (config) => {}, // eslint-disable-line no-unused-vars

requestAuthorization: () => {},

getCurrentPosition: async (success, error = noop, options = {}) => {
if (!success) {
// eslint-disable-next-line no-console
console.error('Must provide a success callback');
}

// Right now, we're assuming user already granted location permission.
RNFusedLocation.getCurrentPosition(options, success, error);
},

watchPosition: (success, error = null, options = {}) => {
if (!success) {
// eslint-disable-next-line no-console
console.error('Must provide a success callback');
}

if (!updatesEnabled) {
RNFusedLocation.startObserving(options);
updatesEnabled = true;
}

const watchID = subscriptions.length;

subscriptions.push([
LocationEventEmitter.addListener('geolocationDidChange', success),
error ? LocationEventEmitter.addListener('geolocationError', error) : null
]);

return watchID;
},

clearWatch: (watchID) => {
const sub = subscriptions[watchID];

if (!sub) {
// Silently exit when the watchID is invalid or already cleared
// This is consistent with timers
return;
}

sub[0].remove();

const sub1 = sub[1];

if (sub1) {
sub1.remove();
}

subscriptions[watchID] = undefined;

let noWatchers = true;

for (let ii = 0; ii < subscriptions.length; ii += 1) {
if (subscriptions[ii]) {
noWatchers = false; // still valid subscriptions
}
}

if (noWatchers) {
Geolocation.stopObserving();
}
},

stopObserving: () => {
if (updatesEnabled) {
RNFusedLocation.stopObserving();
updatesEnabled = false;

for (let ii = 0; ii < subscriptions.length; ii += 1) {
const sub = subscriptions[ii];
if (sub) {
// eslint-disable-next-line no-console
console.warn('Called stopObserving with existing subscriptions.');
sub[0].remove();

const sub1 = sub[1];

if (sub1) {
sub1.remove();
}
}
}

subscriptions = [];
}
}
};
}

export default Geolocation;

0 comments on commit 798e6f6

Please sign in to comment.