Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
54 changes: 37 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

# @rn-bridge/react-native-shortcuts

Android Shortcuts and iOS Quick Actions are features that allow users to quickly access specific app
functionalities directly from the home screen or app icon, enhancing user experience by providing
fast access to common tasks.
React native library for android shortcuts and iOS quick actions which allow users to quickly access specific app functionalities directly from the home screen or app icon, enhancing user experience by providing fast access to common tasks.

Fully compatible with TypeScript.
Fully compatible with TypeScript & Turbomodules.

## Example

Expand Down Expand Up @@ -39,6 +37,7 @@ yarn add @rn-bridge/react-native-shortcuts
## Setup

### iOS
#### If you are using `Objective-C`

Add the following code to your `AppDelegate.m`

Expand All @@ -52,6 +51,23 @@ Add the following code to your `AppDelegate.m`
completionHandler(YES);
}
```
#### If you are using `Swift`

Add the following line to your App's `Bridging-Header.h`

> [!IMPORTANT]
> A Bridging Header is required in iOS development when you want to use Objective-C code in a Swift project.
```objective-c
#import "RNShortcuts.h"
```

Add the following code to your `AppDelegate.swift`
```swift
override func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
RNShortcuts.handle(shortcutItem)
completionHandler(true)
}
```

### Android

Expand All @@ -72,8 +88,6 @@ No setup needed
| [`isShortcutSupported`](#isShortcutSupported) | Returns whether your device supports shortcuts(android), quick actions(ios). |
| [`getInitialShortcutId`](#getInitialShortcutId) | If the initial app launch was triggered by a shortcut, it will give the id of that shortcut, otherwise it will give null. |
| [`addOnShortcutUsedListener`](#addOnShortcutUsedListener) | If the app is in background and the app is launchced by a shortcut, it will give the id of that shortcut. |
| [`removeOnShortcutUsedListener`](#removeOnShortcutUsedListener) | Removes the listener. You app no longer receives events. |

## Usage

Import
Expand Down Expand Up @@ -235,17 +249,23 @@ If the app is in background and the app is launchced by a shortcut, it will give
shortcut.

```javascript
const callback = (id) => {
console.log('Shortcut Id:', id);
};

Shortcuts.addOnShortcutUsedListener(callback);
```

### removeOnShortcutUsedListener

```javascript
Shortcuts.removeOnShortcutUsedListener();
const listenerSubscription = React.useRef<null | EventSubscription>(null);

React.useEffect(() => {
const callback = (id: string) => {
console.log('Shortcut Id:', id);
if (id) {
Alert.alert('Shortcut Detected', `App opened with shortcut id: ${id}`);
}
};

listenerSubscription.current = Shortcuts.addOnShortcutUsedListener(callback);

return () => {
listenerSubscription.current?.remove();
listenerSubscription.current = null;
}
}, [])
```

## How To Run Example App ?
Expand Down
18 changes: 15 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,18 @@ static def supportsNamespace() {
android {
if (supportsNamespace()) {
namespace "com.rnbridge.shortcuts"
}

sourceSets {
main {
sourceSets {
main {
if (supportsNamespace()) {
manifest.srcFile "src/main/AndroidManifestNew.xml"
}
if (isNewArchitectureEnabled()) {
java.srcDirs += ['src/newarch']
} else {
java.srcDirs += ['src/oldarch']
}
}
}

Expand All @@ -60,7 +67,7 @@ android {
defaultConfig {
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")

buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
}

buildTypes {
Expand All @@ -72,6 +79,11 @@ android {
lintOptions {
disable "GradleCompatible"
}

buildFeatures {
buildConfig true
}

}

repositories {
Expand Down
12 changes: 6 additions & 6 deletions android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
# https://developer.android.com/r/tools/gradle-multi-project-decoupled-projects
# org.gradle.parallel=true
#Sun Aug 18 23:34:54 IST 2024
ReactNativeShortcuts_compileSdkVersion=33
ReactNativeShortcuts_kotlinVersion=1.9.24
ReactNativeShortcuts_minSdkVersion=21
ReactNativeShortcuts_ndkversion=26.1.10909125
ReactNativeShortcuts_targetSdkVersion=33
android.useAndroidX=true
ReactNativeShortcuts_compileSdkVersion=34
ReactNativeShortcuts_kotlinVersion=2.0.21
ReactNativeShortcuts_minSdkVersion=24
ReactNativeShortcuts_ndkversion=27.1.12297006
ReactNativeShortcuts_targetSdkVersion=34
android.useAndroidX=true
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-all.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
5 changes: 4 additions & 1 deletion android/gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
Expand Down Expand Up @@ -84,7 +86,8 @@ done
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down
2 changes: 2 additions & 0 deletions android/gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down
30 changes: 27 additions & 3 deletions android/src/main/java/com/rnbridge/shortcuts/RNShortcutsPackage.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
package com.rnbridge.shortcuts

import com.facebook.react.ReactPackage
import com.facebook.react.BaseReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.model.ReactModuleInfo
import com.facebook.react.module.model.ReactModuleInfoProvider
import com.facebook.react.uimanager.ViewManager


class RNShortcutsPackage : ReactPackage {
class RNShortcutsPackage : BaseReactPackage() {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return listOf(RNShortcutsModule(reactContext))
}

override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
return emptyList()
}

override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
return when (name) {
"RNShortcuts" -> RNShortcutsModule(reactContext)
else -> null
}
}

override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
return ReactModuleInfoProvider {
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
val isTurboModule: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
moduleInfos["RNShortcuts"] = ReactModuleInfo(
"RNShortcuts",
"RNShortcuts",
false,
false,
false,
isTurboModule
)
moduleInfos
}
}
}
Loading