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

[TIMOB-19708, TIMOB-19709, TIMOB-19710, TIMOB-19711, TIMOB-19712, TIMOB-19713, TIMOB-19714, TIMOB-19715] Fix and improve UIApplicationShortcuts #7290

Merged
merged 3 commits into from
Oct 13, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions apidoc/Titanium/App/iOS/iOS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ events:
- name: shortcutitemclick
summary: Fired when a user taps the Application Shortcut.
properties:
- name: type
- name: itemtype
summary: The unique identifier for the application shortcut.
type: String

Expand All @@ -1025,7 +1025,7 @@ events:
type: String

- name: userInfo
summary: The payload passed by the Application Shortcut.
summary: The payload passed by the application shortcut.
type: Dictionary
platforms: [iphone]
osver: {ios: {min: "9.0"}}
Expand Down
178 changes: 118 additions & 60 deletions apidoc/Titanium/UI/iOS/ApplicationShortcuts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ description: |
app-specific actions from the Home screen by pressing on the app icon. The pressing of an application shortcut
will then fire the `shortcutitemclick` Titanium.App.iOS event.

To use this feature make sure you have a 3D Touch compatible device running iOS 9 or later.
To use this feature make sure you have a 3D Touch compatible device running iOS 9 or later. You can check
this by using the <Titanium.UI.iOS.forceTouchSupported> property.
extends: Titanium.Proxy
platforms: [iphone]
osver: {ios: {min: "9.0"}}
Expand All @@ -30,112 +31,169 @@ methods:
- name: dynamicShortcutExists
summary: Returns true or false depending if the provided shortcut dictionary already exists.
parameters:
- name: params
summary: |
The parameters used when creating the dynamic shortcut. Must include a `type` property,
to determine if the shortcut exists.
type: Dictionary
- name: itemtype
summary: Checks if the dynamic application shortcut item identified by the `itemtype` exists.
type: String
returns:
type: Boolean

- name: addShortcutItem
summary: Creates a dynamic application shortcut.
- name: addDynamicShortcut
summary: Creates a new dynamic application shortcut item.
parameters:
- name: params
summary: The parameters used when creating a dynamic shortcut.
type: ShortcutParams

- name: removeDynamicShortcut
summary: Removes the dynamic application shortcut item identified by the `itemtype`.
parameters:
- name: itemtype
summary: |
The parameters used when creating the dynamic shortcut. Must include the `type` and `title` properties.
Properties are:
* type - the unique key for the application shortcut
* title - the title of the application shortcut
* subtitle - the subtitle displayed on the application shortcut
* icon - the icon to be displayed on the application shortcut, for example Titanium.UI.iOS.SHORTCUT_ICON_TYPE_COMPOSE
type: Dictionary

- name: removeShortcutItem
summary: Removes the provided application shortcut item.
Use the `itemtype` property to determine which shortcut should be removed.
type: String

- name: getDynamicShortcut
summary: Gets the dynamic application shortcut item identified by the `itemtype`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add that it is "read-only" - that changing the returned object will not have any effect.

parameters:
- name: params
- name: itemtype
summary: |
The parameters used when creating the dynamic shortcut. Must include a `type` property,
to determine which shortcut should be removed.
type: Dictionary
Use the `itemtype` property to determine which shortcut should be returned.
type: String

examples:
examples:
- title: Example app.js
example: |
The following code excerpt demonstrates

var win = Titanium.UI.createWindow({
title:'Example', backgroundColor:'#fff', layout:"vertical"
Ti.App.addEventListener("resume", function(){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the resume and resumed here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For debug, will remove that lines.

Ti.API.info("Resume Event Fired");
});

Ti.App.addEventListener("resumed", function(){
Ti.API.info("Resumed Event Fired");
});

Ti.App.iOS.addEventListener("shortcutitemclick", function(e){
Ti.API.info("shortcutitemclick Event Fired");
Ti.API.info("event payload:" + JSON.stringify(e));
});

var win = Titanium.UI.createWindow({
title:'Test', backgroundColor:'#fff', layout:"vertical"
});

var btn1 = Ti.UI.createButton({
top: 50, height:45, title:"Add Contact Us Application Shortcut"
top: 50, height:45, title:"Add Contact Us Application Shortcut"
});
win.add(btn1);

btn1.addEventListener("click",function(){
var appShortcuts = Ti.UI.iOS.createApplicationShortcuts();
appShortcuts.addShortcutItem({
type:"contact_us",
title:"Contact Us",
subtitle:"Tap to reach us",
icon: Ti.UI.iOS.SHORTCUT_ICON_TYPE_ADD,
userInfo:{
infoKey:"contact_us"
}
});
var appShortcuts = Ti.UI.iOS.createApplicationShortcuts();
appShortcuts.addDynamicShortcut({
itemtype:"contact_us",
title:"Contact Us",
subtitle:"Tap to reach us",
icon: Ti.UI.iOS.SHORTCUT_ICON_TYPE_ADD,
userInfo:{
infoKey:"contact_us"
}
});
});

var btn2 = Ti.UI.createButton({
top: 10, height:45, title:"Remove Contact Us Application Shortcut"
top: 10, height:45, title:"Remove Contact Us Application Shortcut"
});
win.add(btn2);

btn2.addEventListener("click",function(){
var appShortcuts = Ti.UI.iOS.createApplicationShortcuts();
appShortcuts.removeShortcutItem({
type:"contact_us"
});
var appShortcuts = Ti.UI.iOS.createApplicationShortcuts();
appShortcuts.removeDynamicShortcut("contact_us");
});

var btn3 = Ti.UI.createButton({
top: 10, height:45, title:"Count Dynamic App Shortcuts"
top: 10, height:45, title:"Count Dynamic App Shortcuts"
});
win.add(btn3);

btn3.addEventListener("click",function(){
var appShortcuts = Ti.UI.iOS.createApplicationShortcuts();
var shortcutItems = appShortcuts.listDynamicShortcuts();
Ti.API.info("Dynamic App Shortcut as JSON:" + JSON.stringify(shortcutItems));
var appShortcuts = Ti.UI.iOS.createApplicationShortcuts();
var shortcuts = appShortcuts.listDynamicShortcuts();
Ti.API.info("Dynamic App Shortcut count:" + shortcuts.length);
Ti.API.info("Dynamic App Shortcut as JSON:" + JSON.stringify(shortcuts));
});

var btn4 = Ti.UI.createButton({
top: 10, height:45, title:"Count Static App Shortcuts"
top: 10, height:45, title:"Count Static App Shortcuts"
});
win.add(btn4);

btn4.addEventListener("click",function(){
var appShortcuts = Ti.UI.iOS.createApplicationShortcuts();
var shortcutItems = appShortcuts.listStaticShortcuts();
Ti.API.info("Static App Shortcut as JSON:" + JSON.stringify(shortcutItems));
var appShortcuts = Ti.UI.iOS.createApplicationShortcuts();
var shortcuts = appShortcuts.listStaticShortcuts();
Ti.API.info("Static App Shortcut count:" + shortcuts.length);
Ti.API.info("Static App Shortcut as JSON:" + JSON.stringify(shortcuts));
});

var btn5 = Ti.UI.createButton({
top: 10, height:45, title:"Dynamic Shortcut Exists?"
top: 10, height:45, title:"Dynamic Shortcut Exists?"
});
win.add(btn5);

btn5.addEventListener("click",function(){
var appShortcuts = Ti.UI.iOS.createApplicationShortcuts();
var exists = appShortcuts.dynamicShortcutExists({
type:"contact_us"
});
var msg = (exists) ? "Icon exists" : "Sorry isn't there";
alert(msg);
var appShortcuts = Ti.UI.iOS.createApplicationShortcuts();
var exists = appShortcuts.dynamicShortcutExists("contact_us");
var msg = (exists) ? "Icon exists" : "Sorry isn't there";
alert(msg);
});

var btn6 = Ti.UI.createButton({
top: 10, height:45, title:"Remove All Dynamic Shortcuts"
top: 10, height:45, title:"Remove All Dynamic Shortcuts"
});
win.add(btn6);

btn6.addEventListener("click",function(){
var appShortcuts = Ti.UI.iOS.createApplicationShortcuts();
appShortcuts.removeAllDynamicShortcuts();
});
var appShortcuts = Ti.UI.iOS.createApplicationShortcuts();
appShortcuts.removeAllDynamicShortcuts();
});

var btn7 = Ti.UI.createButton({
top: 10, height:45, title:"Get shortcut by itemtype \"contact_us\""
});
win.add(btn7);

btn7.addEventListener("click",function(){
var appShortcuts = Ti.UI.iOS.createApplicationShortcuts();
var shortcut = appShortcuts.getDynamicShortcut("contact_us");
alert(shortcut);
});

win.open();

---
name: ShortcutParams
summary: Dictionary of options for <Titanium.UI.iOS.addDynamicShortcut>.
description: |
The parameters used when creating the dynamic shortcut. Must include the `itemtype` and `title` properties.
properties:
- name: type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be itemtype

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

summary: The unique key for the application shortcut.
type: String
optional: false

- name: title
summary: The title of the application shortcut
type: String
optional: false

- name: subtitle
summary: The subtitle displayed on the application shortcut
type: String
optional: true

- name: icon
summary: |
The icon to be displayed on the application shortcut, for example <Titanium.UI.iOS.SHORTCUT_ICON_TYPE_COMPOSE>.
Note: You can also use a local image specified by the image path.
type: Number
constants: Titanium.UI.iOS.SHORTCUT_ICON_TYPE_*
optional: true
10 changes: 6 additions & 4 deletions iphone/Classes/TiApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,12 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
}

#if IS_XCODE_7
UIApplicationShortcutItem *shortcut = [launchOptions objectForKey:UIApplicationLaunchOptionsShortcutItemKey];

if(shortcut !=nil) {
launchedShortcutItem = shortcut;
if ([TiUtils isIOS9OrGreater] == YES) {
UIApplicationShortcutItem *shortcut = [launchOptions objectForKey:UIApplicationLaunchOptionsShortcutItemKey];

if (shortcut != nil) {
launchedShortcutItem = shortcut;
}
}
#endif

Expand Down
9 changes: 8 additions & 1 deletion iphone/Classes/TiAppiOSProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,14 @@ -(void)_listenerRemoved:(NSString*)type count:(int)count

-(void)didReceiveApplicationShortcutNotification:(NSNotification*)info
{
[self fireEvent:@"shortcutitemclick" withObject:[info userInfo]];
NSDictionary *event = @{
@"title" : [[info userInfo] valueForKey:@"title"],
@"subtitle" : [[info userInfo] valueForKey:@"subtitle"],
@"itemtype" : [[info userInfo] valueForKey:@"type"],
@"userInfo" : [[info userInfo] objectForKey:@"userInfo"],
};

[self fireEvent:@"shortcutitemclick" withObject:event];
}

-(id)createSearchableIndex:(id)unused
Expand Down
2 changes: 1 addition & 1 deletion iphone/Classes/TiBase.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void TiLogMessage(NSString* str, ...) {
NSString * const kTiUserNotificationSettingsNotification = @"TiUserNotificationSettingsNotification";
NSString * const kTiWatchKitExtensionRequest = @"TiWatchKitExtensionRequest";
NSString * const kTiContinueActivity = @"TiContinueActivity";
NSString * const kTiApplicationShortcut = @"kTiContinueActivity";
NSString * const kTiApplicationShortcut = @"TiApplicationShortcut";

#ifndef TI_USE_AUTOLAYOUT
NSString* const kTiBehaviorSize = @"SIZE";
Expand Down