Skip to content

Commit

Permalink
geofence setup
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshaR1642 committed Aug 16, 2024
1 parent e84513b commit 770eab1
Show file tree
Hide file tree
Showing 38 changed files with 22,652 additions and 143 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ android/keystores/debug.keystore
!.yarn/sdks
!.yarn/versions

example/.yarn/*
!example/.yarn/patches
!example/.yarn/plugins
!example/.yarn/releases
!example/.yarn/sdks
!example/.yarn/versions

# Expo
.expo/

Expand Down
1 change: 0 additions & 1 deletion .watchmanconfig

This file was deleted.

10 changes: 0 additions & 10 deletions .yarnrc.yml

This file was deleted.

18 changes: 13 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,36 @@ The [example app](/example/) demonstrates usage of the library. You need to run

It is configured to use the local version of the library, so any changes you make to the library's source code will be reflected in the example app. Changes to the library's JavaScript code will be reflected in the example app without a rebuild, but native code changes will require a rebuild of the example app.

If you want to use Android Studio or XCode to edit the native code, you can open the `example/android` or `example/ios` directories respectively in those editors. To edit the Objective-C or Swift files, open `example/ios/ReactNativeGeofencingExample.xcworkspace` in XCode and find the source files at `Pods > Development Pods > @rn-bridge/react-native-geofencing`.
If you want to use Android Studio or XCode to edit the native code, you can open the `example/android` or `example/ios` directories respectively in those editors. To edit the Objective-C or Swift files, open `example/ios/ReactNativeGeofencingExample.xcworkspace` in XCode and find the source files at `Pods > Development Pods > react-native-geofencing`.

To edit the Java or Kotlin files, open `example/android` in Android studio and find the source files at `rn-bridge-react-native-geofencing` under `Android`.
To edit the Java or Kotlin files, open `example/android` in Android studio and find the source files at `rnbridge/geofencing` under `Android`.

You can use various commands from the root directory to work with the project.

To start the packager:

First
```sh
yarn example start
cd example
yarn install
cd ios
pod install
```

```sh
cd example && yarn start
```

To run the example app on Android:

```sh
yarn example android
cd example && yarn android
```

To run the example app on iOS:

```sh
yarn example ios
cd example && yarn example ios
```

Make sure your code passes TypeScript and ESLint. Run the following to verify:
Expand Down
161 changes: 150 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,172 @@
# @rn-bridge/react-native-geofencing

Native modules to determine if a location is within defined geographical boundaries
> [!NOTE]
> Native module to determine if a location is within defined geographical boundaries. Geofencing combines awareness of the user's current location with awareness of the user's proximity to locations that may be of interest. To mark a location of interest, you specify its latitude and longitude. To adjust the proximity for the location, you add a radius. The latitude, longitude, and radius define a geofence, creating a circular area, or fence, around the location of interest.
Fully compatible with TypeScript.

## Supported platforms

| Platform | Support |
|---|---|
| iOS ||
| Android ||
| Web ||
| Windows ||
| macOS ||

## Installation

```sh
npm install @rn-bridge/react-native-geofencing
```
or

## Usage
```sh
yarn add @rn-bridge/react-native-geofencing
```

## Configuration and Permissions

```js
import { multiply } from '@rn-bridge/react-native-geofencing';
### iOS

To enable geofence in your app, you need to add the `NSLocationWhenInUseUsageDescription` and `NSLocationAlwaysAndWhenInUseUsageDescription` keys to your `Info.plist` file. If your app supports iOS 10 or earlier, you must also include the `NSLocationAlwaysUsageDescription` key. Without these keys, geofence requests will fail.

To enable geofence while the app runs in the background, add the `NSLocationAlwaysUsageDescription` key to `Info.plist`.

It’s important to ensure you’ve enabled the necessary background modes. Here’s how to do so:

- Go to your Xcode project settings.
- Select your target.
- Go to the `Signing & Capabilities` tab.
- Enable `Background Modes` capability.
- Check `Location updates`

<img width="743" alt="Screenshot 2024-08-15 at 7 27 40 PM" src="https://github.com/user-attachments/assets/87d0215e-e1c9-45b0-9488-4f0e9509a21b">

// ...
<br></br>
Your `Info.plist` should look like this, Explain why the background location is required!!

const result = await multiply(3, 7);
```java
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app need your location to provide best feature based on location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app need your location to provide best feature based on location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app need your location to provide best feature based on location in background</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>
```

### Android

## Contributing
The first step in requesting geofence monitoring is to request the necessary permissions. To use geofencing, your app must request the following:

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
- `ACCESS_FINE_LOCATION`
- `ACCESS_BACKGROUND_LOCATION` if your app targets Android 10 (API level 29) or higher

## License
Your `AndroidManifest.xml` should look liks this

MIT
```java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

</manifest>
```

## Summary

### Methods

* [`addGeofence`](#addGeofence)
* [`removeGeofence`](#removeGeofence)
* [`getRegisteredGeofences`](#getRegisteredGeofences)

### Listeners

* [`onEnter`](#onEnter)
* [`onExit`](#onExit)
* [`removeOnEnterListener`](#removeOnEnterListener)
* [`removeOnExitListener`](#removeOnExitListener)

---

Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
## Usage

### addGeofence
```js
import { addGeofence } from '@rn-bridge/react-native-geofencing';

const response = await addGeofence({
id: 'a9957259-8036-4dcb-974c-34eae9b44bdb',
latitude: 16.153062,
longitude: 100.281585,
radius: 500
})
console.log(response) // { success: true, id, type: "add" }
```
Supported options:
* `id` (String) - Set the ID of the geofence. This is a string to identify this geofence.
* `latitude` (Double) - The latitude and longitude are used to define the precise geographical location of the geofence’s center. This ensures accurate detection when a device enters or exits the geofenced area.
* `longitude` (Double) - The latitude and longitude are used to define the precise geographical location of the geofence’s center. This ensures accurate detection when a device enters or exits the geofenced area.
* `radius` (Integer) - The radius defines the boundary of the geofence around the central point (latitude and longitude). It allows for flexibility in creating different-sized geofences

### removeGeofence
```javascript
import { removeGeofence } from '@rn-bridge/react-native-geofencing';

const response = await removeGeofence(id)
console.log(response) // { success: true, id, type: "remove" }
```
Supported options:
* `id` (String) - The ID uniquely identifies a geofence, enabling precise and efficient removal by referencing the specific geofence without ambiguity.

### getRegisteredGeofences
```javascript
import { getRegisteredGeofences } from '@rn-bridge/react-native-geofencing';

const response = await getRegisteredGeofences()
console.log(response) // [id, ...]
```

> [!IMPORTANT]
> ## Listening for location transitions
### onEnter
```javascript
import { Geofence, Events } from '@rn-bridge/react-native-geofencing';

Geofence.onEnter((ids: string[]) => {
console.log(Events.Enter, ids);
});
```

### onExit
```javascript
import { Geofence, Events } from '@rn-bridge/react-native-geofencing';

Geofence.onExit((ids: string[]) => {
console.log(Events.Exit, ids);
});
```

### removeOnEnterListener
```javascript
import { Geofence } from '@rn-bridge/react-native-geofencing';

Geofence.removeOnEnterListener()
```

### removeOnExitListener
```javascript
import { Geofence } from '@rn-bridge/react-native-geofencing';

Geofence.removeOnExitListener()
```

14 changes: 9 additions & 5 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.android.Version

buildscript {
// Buildscript is evaluated before everything else so we can't use getExtOrDefault
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["ReactNativeGeofencing_kotlinVersion"]
Expand All @@ -8,7 +10,7 @@ buildscript {
}

dependencies {
classpath "com.android.tools.build:gradle:7.2.1"
classpath "com.android.tools.build:gradle:8.3.2"
// noinspection DifferentKotlinGradleVersion
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
Expand Down Expand Up @@ -38,8 +40,8 @@ def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["ReactNativeGeofencing_" + name]).toInteger()
}

def supportsNamespace() {
def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
static def supportsNamespace() {
def parsed = Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
def major = parsed[0].toInteger()
def minor = parsed[1].toInteger()

Expand All @@ -49,7 +51,7 @@ def supportsNamespace() {

android {
if (supportsNamespace()) {
namespace "com.rnbridge.reactnativegeofencing"
namespace "com.rnbridge.geofencing"

sourceSets {
main {
Expand Down Expand Up @@ -93,7 +95,9 @@ dependencies {
// For < 0.71, this will be from the local maven repo
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation "com.facebook.react:react-android:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'com.google.code.gson:gson:2.11.0'
implementation 'com.google.android.gms:play-services-location:21.3.0'
}

22 changes: 18 additions & 4 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
ReactNativeGeofencing_kotlinVersion=1.7.0
## For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
#
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx1024m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
#
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. For more details, visit
# https://developer.android.com/r/tools/gradle-multi-project-decoupled-projects
# org.gradle.parallel=true
#Fri Aug 16 11:45:03 IST 2024
ReactNativeGeofencing_compileSdkVersion=33
ReactNativeGeofencing_kotlinVersion=1.9.24
ReactNativeGeofencing_minSdkVersion=21
ReactNativeGeofencing_targetSdkVersion=31
ReactNativeGeofencing_compileSdkVersion=31
ReactNativeGeofencing_ndkversion=21.4.7075529
ReactNativeGeofencing_ndkversion=26.1.10909125
ReactNativeGeofencing_targetSdkVersion=33
android.useAndroidX=true
Binary file added android/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 770eab1

Please sign in to comment.