bartwesselink / cordova-plugin-siri-shortcuts

Adds support for Siri Shortcuts to Cordova, requires XCode 10 and iOS >= 12.0
30 stars 26 forks source link

Is it possible to clear the currently activated shortcut? #5

Closed drewrygh closed 6 years ago

drewrygh commented 6 years ago

Say we have an app with 2 states (persistentIdentifiers: app-home, and app-settings), and there are shortcuts registered for both of these states that brings you to them. If the app is launched into the app-settings state through a Siri shortcut, getActivatedShortcut() returns app-settings while the user navigates around the app, until the app is killed and re-opened.

However, this means that if the user minimizes/resumes in the app-home state (while app-settings is still the activated shortcut), there doesn't seem to be a way to know if the user resumed the app through the app-settings shortcut, or if they manually resumed the app. In other words, we don't know which state to load when the app loads, because getActivatedShortcut() returns {..., persistentIdentifier: app-settings} in both scenarios.

I think one way around this would be to add a clear() method that would clear the currently activated shortcut. This could be called after the code handling shortcuts via getActivatedShortcut() executes, so that if the user resumes the app, it's possible to determine whether it was resumed manually, or through a Siri shortcut.

Hopefully this makes sense. @bartwesselink this seems like it could be a fairly common use case, do you have any thoughts on it?

zabuTNT commented 6 years ago

Hi @drewrygh, to do what you ask, you need to clean up userActivity. I'm not a swift expert but I resolved this setting userActivity to nil.

in function getActivatedShortcut in SiriShortcuts.swift add appDelegate.userActivity = nil

after

pluginResult = CDVPluginResult(
                           status: CDVCommandStatus_OK,
                           messageAs: returnData as [AnyHashable: Any]
                        )

and now if you manually resume the app getActivatedShortcut() is not called.

bartwesselink commented 6 years ago

I think this is a fair point. I'm thinking of adding a clear parameter to the getActivatedShortcut() JavaScript method. If the default value would be true, than that'd be a BC-break. I'm not sure what the best default value for this would be.

I will have a look at this this weekend, I have very little time this week.

drewrygh commented 6 years ago

Hey @zabuTNT - thanks for the tip, this works for now.

drewrygh commented 6 years ago

@bartwesselink - sounds good! Thanks for all of the work on this plugin.

bartwesselink commented 6 years ago

@drewrygh I added an options parameter to getActivatedShortcut({ clear: true }). This opposes a BC-break, but like I said, the plug-in is still in beta. The default value of clear is also true for now.

Can you confirm this working in 0.0.9?

drewrygh commented 6 years ago

Yes, it looks good to me. Thanks!