Skip to content

Fix "Max number of dynamics shortcuts exceed" error & add a few features #4

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 4 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ If your app targets Android 7.1 (API level 25) or higher, you can define shortcu

#### Android

1. Open up `android/app/src/main/java/[...]/MainActivity.java`
1. Open up `android/app/src/main/java/[...]/MainApplication.java`
- Add `import com.rnas.RNAppShortcutsPackage;` to the imports at the top of the file
- Add `new RNAppShortcutsPackage()` to the list returned by the `getPackages()` method
2. Append the following lines to `android/settings.gradle`:
Expand Down Expand Up @@ -65,7 +65,8 @@ RNAppShortcuts.addShortcut({
shortLabel: 'sample',
longLabel: 'sample label',
iconFolderName: 'drawable',
iconName: 'icon'
iconName: 'icon',
order: 0
})
```

Expand All @@ -76,7 +77,8 @@ RNAppShortcuts.updateShortcut({
shortLabel: 'updated',
longLabel: 'updated label',
iconFolderName: 'drawable',
iconName: 'icon'
iconName: 'icon',
order: 1
})
```

Expand All @@ -89,6 +91,7 @@ RNAppShortcuts.updateShortcut({
| longLabel | String | Long label for the shortcut. |
| iconFolderName | String | Folder name of the shortcut's icon. For example, if the icon is in `./android/app/src/res/drawable` folder of your prject, you should use `'drawable'` here.|
| iconName | String | Name of the icon, without extension name. |
| order | Number | Position of the shortcut. The lower the number is (0 being the minimum value), the higher the icon will positionned. |

### Remove App ShortCut

Expand Down
17 changes: 15 additions & 2 deletions android/src/main/java/com/rnas/RNAppShortcutsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.support.annotation.Nullable;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.facebook.react.bridge.ReactApplicationContext;
Expand All @@ -30,6 +31,7 @@ public class RNAppShortcutsModule extends ReactContextBaseJavaModule {
private final String ICON_FOLDER_KEY = "iconFolderName";
private final String ICON_NAME_KEY = "iconName";
private final String ACTIVITY_NAME_KEY = "activityName";
private final String ORDER_KEY = "order";

public RNAppShortcutsModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand Down Expand Up @@ -87,7 +89,16 @@ public void addShortcut(ReadableMap shortcutDetails) {
ShortcutInfo shortcut = initShortcut(shortcutDetails);

ShortcutManager shortcutManager = getShortCutManager();
shortcutManager.addDynamicShortcuts(Arrays.asList(shortcut));
List<ShortcutInfo> shortcuts = shortcutManager.getDynamicShortcuts();

if (shortcuts.size() >= shortcutManager.getMaxShortcutCountPerActivity() &&
shortcuts.size() > 0) {
shortcuts.remove(shortcuts.size() - 1);
shortcuts.add(0, shortcut);
shortcutManager.setDynamicShortcuts(shortcuts);
} else {
shortcutManager.addDynamicShortcuts(Collections.singletonList(shortcut));
}
}

@ReactMethod
Expand Down Expand Up @@ -130,9 +141,11 @@ private ShortcutInfo initShortcut(ReadableMap shortcutDetail) {
ShortcutInfo shortcut = new ShortcutInfo.Builder(currentActivity, shortcutDetail.getString(ID_KEY))
.setShortLabel(shortcutDetail.getString(SHORT_LABEL_KEY))
.setLongLabel(shortcutDetail.getString(LONG_LABEL_KEY))
.setRank(shortcutDetail.getInt(ORDER_KEY))
.setIcon(Icon.createWithResource(currentActivity.getApplicationContext(), iconId))
.setIntent(intent)
.build();

return shortcut;
}

Expand All @@ -159,4 +172,4 @@ private ShortcutManager getShortCutManager() {
return shortcutManager;
}

}
}