Skip to content
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

feat(app-shortcuts): support icon and description #376

Merged
merged 3 commits into from
Jan 2, 2025
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
5 changes: 5 additions & 0 deletions .changeset/famous-pumas-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capawesome/capacitor-app-shortcuts': minor
---

feat(app-shortcuts): add support for icon and description
17 changes: 9 additions & 8 deletions packages/app-shortcuts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,19 @@ Remove all listeners for this plugin.

#### Shortcut

| Prop | Type | Description | Since |
| ----------------- | ------------------- | ------------------------------------------- | ----- |
| **`description`** | <code>string</code> | The description. Only available on Android. | 6.0.0 |
| **`id`** | <code>string</code> | The unique identifier. | 6.0.0 |
| **`title`** | <code>string</code> | The display name. | 6.0.0 |
| Prop | Type | Description | Since |
| ----------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| **`description`** | <code>string</code> | The description. On **Android**, the launcher shows this instead of the short title when it has enough space. **Attention**: On **iOS**, the icon and the description must be used together. | 6.0.0 |
| **`id`** | <code>string</code> | The unique identifier. | 6.0.0 |
| **`title`** | <code>string</code> | The display name. | 6.0.0 |
| **`icon`** | <code>number</code> | The icon to display. On **Android**, the icon is the constant value of the `R.drawable` enum. On **iOS**, the icon is the constant value of the `UIApplicationShortcutIcon.IconType` enum. **Attention**: On **iOS**, the icon and the description must be used together. | 6.1.0 |


#### SetOptions

| Prop | Type | Description | Since |
| --------------- | ----------------------- | ------------------------- | ----- |
| **`shortcuts`** | <code>Shortcut[]</code> | Th list of app shortcuts. | 6.0.0 |
| Prop | Type | Description | Since |
| --------------- | ----------------------- | -------------------------- | ----- |
| **`shortcuts`** | <code>Shortcut[]</code> | The list of app shortcuts. | 6.0.0 |


#### PluginListenerHandle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
public class AppShortcutsHelper {

@NonNull
public static HashMap<String, String> createHashMapFromJSONObject(@NonNull JSONObject object) throws JSONException {
HashMap<String, String> map = new HashMap<>();
public static HashMap<String, Object> createHashMapFromJSONObject(@NonNull JSONObject object) throws JSONException {
HashMap<String, Object> map = new HashMap<>();
Iterator<String> keys = object.keys();
while (keys.hasNext()) {
String key = keys.next();
map.put(key, (String) object.get(key));
map.put(key, object.get(key));
}
return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.core.content.pm.ShortcutInfoCompat;
import androidx.core.graphics.drawable.IconCompat;
import com.getcapacitor.Bridge;
import com.getcapacitor.JSArray;
import com.getcapacitor.PluginCall;
Expand Down Expand Up @@ -35,26 +36,31 @@ private List<ShortcutInfoCompat> createShortcutInfoCompatList(JSArray shortcuts,
ArrayList<ShortcutInfoCompat> shortcutInfoCompatList = new ArrayList<>();
List<JSONObject> shortcutsList = shortcuts.toList();
for (JSONObject shortcut : shortcutsList) {
HashMap<String, String> shortcutMap = AppShortcutsHelper.createHashMapFromJSONObject(shortcut);
String id = shortcutMap.get("id");
HashMap<String, Object> shortcutMap = AppShortcutsHelper.createHashMapFromJSONObject(shortcut);
Object id = shortcutMap.get("id");
if (id == null) {
throw new Exception(AppShortcutsPlugin.ERROR_ID_MISSING);
}
String title = shortcutMap.get("title");
Object title = shortcutMap.get("title");
if (title == null) {
throw new Exception(AppShortcutsPlugin.ERROR_TITLE_MISSING);
}
String description = shortcutMap.get("description");
String description = (String) shortcutMap.get("description");
Object icon = shortcutMap.get("icon");

ShortcutInfoCompat.Builder shortcutInfoCompat = new ShortcutInfoCompat.Builder(context, id);
shortcutInfoCompat.setShortLabel(title);
ShortcutInfoCompat.Builder shortcutInfoCompat = new ShortcutInfoCompat.Builder(context, (String) id);
shortcutInfoCompat.setShortLabel((String) title);
if (description != null) {
shortcutInfoCompat.setLongLabel(description);
}
shortcutInfoCompat.setIntent(
new Intent(Intent.ACTION_VIEW, bridge.getIntentUri(), bridge.getContext(), bridge.getActivity().getClass())
.putExtra(AppShortcutsPlugin.INTENT_EXTRA_ITEM_NAME, id)
.putExtra(AppShortcutsPlugin.INTENT_EXTRA_ITEM_NAME, (String) id)
);
if (icon != null) {
shortcutInfoCompat.setIcon(IconCompat.createWithResource(context, (int) icon));
}

shortcutInfoCompatList.add(shortcutInfoCompat.build());
}
return shortcutInfoCompatList;
Expand Down
2 changes: 2 additions & 0 deletions packages/app-shortcuts/example/src/js/script.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AppShortcuts } from '@capawesome/capacitor-app-shortcuts';
import { Capacitor } from '@capacitor/core';

document.addEventListener('DOMContentLoaded', () => {
const initialize = async () => {
Expand All @@ -23,6 +24,7 @@ document.addEventListener('DOMContentLoaded', () => {
description: 'Let us know how we can improve',
id: 'feedback',
title: 'Feedback',
icon: Capacitor.getPlatform() === 'ios' ? 6 : 17301547,
},
],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,18 @@ import Capacitor
guard let title = shortcut["title"] as? String, !title.isEmpty else {
throw CustomError.titleMissing
}
return UIApplicationShortcutItem(type: type, localizedTitle: title)
let description = shortcut["description"] as? String
let icon = shortcut["icon"] as? Int

if description != nil && icon != nil {
if let iconType = UIApplicationShortcutIcon.IconType(rawValue: icon!) {
return UIApplicationShortcutItem(type: type, localizedTitle: title, localizedSubtitle: description, icon: UIApplicationShortcutIcon(type: iconType))
} else {
return UIApplicationShortcutItem(type: type, localizedTitle: title)
}
} else {
return UIApplicationShortcutItem(type: type, localizedTitle: title)
}
}
}
}
22 changes: 20 additions & 2 deletions packages/app-shortcuts/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface GetResult {
*/
export interface SetOptions {
/**
* Th list of app shortcuts.
* The list of app shortcuts.
*
* @since 6.0.0
*/
Expand All @@ -75,7 +75,9 @@ export interface Shortcut {
/**
* The description.
*
* Only available on Android.
* On **Android**, the launcher shows this instead of the short title when it has enough space.
*
* **Attention**: On **iOS**, the icon and the description must be used together.
*
* @since 6.0.0
*/
Expand All @@ -92,6 +94,22 @@ export interface Shortcut {
* @since 6.0.0
*/
title: string;
/**
* The icon to display.
*
* On **Android**, the icon is the constant value of the `R.drawable` enum.
*
* On **iOS**, the icon is the constant value of the `UIApplicationShortcutIcon.IconType` enum.
*
* **Attention**: On **iOS**, the icon and the description must be used together.
*
* @since 6.1.0
* @example 17301547
* @example 6
* @see https://developer.android.com/reference/android/R.drawable
* @see https://developer.apple.com/documentation/uikit/uiapplicationshortcuticon/icontype
*/
icon?: number;
}

/**
Expand Down
Loading