Skip to content

Add Windows support (RNW 0.64.0) #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 28 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
19ead02
upgrade dependencies
Apr 12, 2021
1645867
windows-init
Apr 12, 2021
7b4a85f
add windows implementation code
Apr 12, 2021
e550735
add gelocation to pch, reference correct module
Apr 12, 2021
d8e8684
add test methods for callbacks
Apr 13, 2021
8e66859
for Windows call with Promise
Apr 13, 2021
050db05
call requestAuthorization asyncily, rename watchPosition to startObse…
Apr 13, 2021
1195650
cleanup
Apr 14, 2021
14b1222
re-add accuracy
Apr 14, 2021
a22dc6e
re-add speed
Apr 14, 2021
f726c29
re-add heading
Apr 14, 2021
cb5cf2b
remove sample module
Apr 14, 2021
143bb16
Adding getStatus function
sandaluz-aptera Apr 16, 2021
9a769a8
Ignore .vs folder
sandaluz-aptera Apr 16, 2021
d1a2493
made getStatus async
sandaluz-aptera Apr 16, 2021
d0ef47e
made getStatus use a callback function
sandaluz-aptera Apr 16, 2021
bec7977
remove unneccessaries from pch to reduce size, fix DevOps quota fail
Apr 20, 2021
f8edca5
refactor geolocator initialization to set accuracy, clean pch, return…
Apr 28, 2021
7680876
return timestamp in milliseconds
Apr 28, 2021
db4c26c
convert Timestamp to time_t (whose epoch is 1970).
Apr 28, 2021
531c3d0
return the correct shape
May 5, 2021
4c61eef
add StatusChanged event handling
Jun 10, 2021
1b1e5f8
add statusDidChange event plumbing
Jun 11, 2021
7d99dae
limit status subscription to windows
Jun 11, 2021
c7b8b0d
update typings
Jun 11, 2021
36535e8
react to change in RN 0.65.0 requiring addListener/removeListeners fo…
Sep 17, 2021
2ed485f
fix spelling on removeListeners
Sep 17, 2021
dee5edd
add noexcept to fix build
Sep 17, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ buck-out/
*.keystore

# Build
lib/
lib/
/.vs
122 changes: 115 additions & 7 deletions js/implementation.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import {RNCGeolocation, GeolocationEventEmitter} from './nativeInterface';

import invariant from 'invariant';
import {logError, warning} from './utils';
import { Platform } from 'react-native';

let subscriptions = [];
let updatesEnabled = false;

let statusSubscriptions = [];
let statusUpdatesEnabled = false;

type GeoConfiguration = {
skipPermissionRequests: boolean,
authorizationLevel: 'always' | 'whenInUse' | 'auto',
Expand Down Expand Up @@ -53,8 +57,12 @@ const Geolocation = {
*
* See https://facebook.github.io/react-native/docs/geolocation.html#requestauthorization
*/
requestAuthorization: function() {
RNCGeolocation.requestAuthorization();
requestAuthorization: async function() {
if (Platform.OS === 'windows') {
await RNCGeolocation.requestAuthorization();
} else {
RNCGeolocation.requestAuthorization();
}
},

/*
Expand All @@ -73,11 +81,20 @@ const Geolocation = {
);

// Permission checks/requests are done on the native side
RNCGeolocation.getCurrentPosition(
geo_options || {},
geo_success,
geo_error || logError,
);
if (Platform.OS === 'windows') {
RNCGeolocation.getCurrentPosition(geo_options)
.then((position) => {
geo_success(position);
}).catch((error) => {
(geo_error || logError)(error);
});
} else {
RNCGeolocation.getCurrentPosition(
geo_options || {},
geo_success,
geo_error || logError,
);
}
},

/*
Expand All @@ -104,6 +121,33 @@ const Geolocation = {
return watchID;
},

/*
* Invokes the success callback whenever the location changes.
*
* See https://facebook.github.io/react-native/docs/geolocation.html#watchposition
*/
watchStatus: function(
success: Function,
error?: Function,
options?: GeoOptions,
): number {
if (Platform.OS === 'windows') {
if (!statusUpdatesEnabled) {
RNCGeolocation.startObservingStatus(options || {});
statusUpdatesEnabled = true;
}
const watchID = statusSubscriptions.length;
statusSubscriptions.push([
GeolocationEventEmitter.addListener('statusDidChange', success),
error
? GeolocationEventEmitter.addListener('geolocationError', error)
: null,
]);
return watchID;
}
return 0;
},

/*
* Unsubscribes the watcher with the given watchID.
*
Expand Down Expand Up @@ -133,6 +177,37 @@ const Geolocation = {
}
},

/*
* Unsubscribes the watcher with the given watchID.
*
* See https://facebook.github.io/react-native/docs/geolocation.html#clearwatch
*/
clearStatusWatch: function(watchID: number) {
if (Platform.OS === 'windows') {
const sub = statusSubscriptions[watchID];
if (!sub) {
// Silently exit when the watchID is invalid or already cleared
// This is consistent with timers
return;
}

sub[0].remove();
// array element refinements not yet enabled in Flow
const sub1 = sub[1];
sub1 && sub1.remove();
statusSubscriptions[watchID] = undefined;
let noWatchers = true;
for (let ii = 0; ii < statusSubscriptions.length; ii++) {
if (statusSubscriptions[ii]) {
noWatchers = false; // still valid subscriptions
}
}
if (noWatchers) {
Geolocation.stopObservingStatus();
}
}
},

/*
* Stops observing for device location changes and removes all registered listeners.
*
Expand All @@ -155,6 +230,39 @@ const Geolocation = {
subscriptions = [];
}
},

/*
* Stops observing for device location changes and removes all registered listeners.
*
* See https://facebook.github.io/react-native/docs/geolocation.html#stopobserving
*/
stopObservingStatus: function() {
if (Platform.OS === 'windows') {
if (statusUpdatesEnabled) {
RNCGeolocation.stopObservingStatus();
statusUpdatesEnabled = false;
for (let ii = 0; ii < statusSubscriptions.length; ii++) {
const sub = statusSubscriptions[ii];
if (sub) {
warning(false, 'Called stopObservingStatus with existing subscriptions.');
sub[0].remove();
// array element refinements not yet enabled in Flow
const sub1 = sub[1];
sub1 && sub1.remove();
}
}
statusSubscriptions = [];
}
}
},

getStatus: function(callback: Function) {
if (Platform.OS === 'windows') {
RNCGeolocation.getStatus(callback);
} else {
callback('unknown');
}
},
};

module.exports = Geolocation;
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"build:e2e:ios:release": "detox build -c ios.sim.release",
"test:e2e:ios:debug": "detox test -c ios.sim.debug",
"test:e2e:ios:release": "detox test -c ios.sim.release",
"prepare": "bob build"
"prepare": "bob build",
"windows": "react-native run-windows"
},
"keywords": [
"react-native",
Expand Down Expand Up @@ -65,8 +66,9 @@
"jest": "^24.9.0",
"metro-react-native-babel-preset": "0.56.0",
"prettier": "^1.18.2",
"react": "16.8.3",
"react-native": "0.59.10",
"react": "17.0.1",
"react-native": "0.64.0",
"react-native-windows": "^0.64.3",
"react-test-renderer": "16.8.3",
"semantic-release": "^15.13.21"
},
Expand Down
9 changes: 9 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ export interface GeolocationStatic {

stopObserving(): void;

watchStatus(
success: (status: string) => void,
error?: (error: GeolocationError) => void,
options?: GeolocationOptions): number;

clearStatusWatch(watchID: number): void;

stopObservingStatus(): void;

requestAuthorization(): void;

setRNConfiguration(config: GeolocationConfiguration): void;
Expand Down
92 changes: 92 additions & 0 deletions windows/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
*AppPackages*
*BundleArtifacts*

#OS junk files
[Tt]humbs.db
*.DS_Store

#Visual Studio files
*.[Oo]bj
*.user
*.aps
*.pch
*.vspscc
*.vssscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.[Cc]ache
*.ilk
*.log
*.lib
*.sbr
*.sdf
*.opensdf
*.opendb
*.unsuccessfulbuild
ipch/
[Oo]bj/
[Bb]in
[Dd]ebug*/
[Rr]elease*/
Ankh.NoLoad

# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb

#MonoDevelop
*.pidb
*.userprefs

#Tooling
_ReSharper*/
*.resharper
[Tt]est[Rr]esult*
*.sass-cache

#Project files
[Bb]uild/

#Subversion files
.svn

# Office Temp Files
~$*

# vim Temp Files
*~

#NuGet
packages/
*.nupkg

#ncrunch
*ncrunch*
*crunch*.local.xml

# visual studio database projects
*.dbmdl

#Test files
*.testsettings

#Other files
*.DotSettings
.vs/
*project.lock.json

#Files generated by the VS build
**/Generated Files/**

Loading