- Passthrough for Activator listeners

- Wildcards for custom replies
This commit is contained in:
ZaidElkurdi 2015-04-10 00:57:30 -07:00
parent a836b59617
commit c4f0c1f295
6 changed files with 53 additions and 26 deletions

View File

@ -23,7 +23,7 @@
BOOL passthrough = false; BOOL passthrough = false;
if (dict[@"passthrough"]) { if (dict[@"passthrough"]) {
enabled = [dict[@"passthrough"] boolValue]; passthrough = [dict[@"passthrough"] boolValue];
} }
self.name = name; self.name = name;

View File

@ -663,6 +663,7 @@
B86E670C1ACE7A77007C6014 /* Release */, B86E670C1ACE7A77007C6014 /* Release */,
); );
defaultConfigurationIsVisible = 0; defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
}; };
B86E670D1ACE7A77007C6014 /* Build configuration list for PBXNativeTarget "assistantappTests" */ = { B86E670D1ACE7A77007C6014 /* Build configuration list for PBXNativeTarget "assistantappTests" */ = {
isa = XCConfigurationList; isa = XCConfigurationList;
@ -671,6 +672,7 @@
B86E670F1ACE7A77007C6014 /* Release */, B86E670F1ACE7A77007C6014 /* Release */,
); );
defaultConfigurationIsVisible = 0; defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
}; };
B88C0A591ABF794E00D4D107 /* Build configuration list for PBXProject "AssistantPlusApp" */ = { B88C0A591ABF794E00D4D107 /* Build configuration list for PBXProject "AssistantPlusApp" */ = {
isa = XCConfigurationList; isa = XCConfigurationList;

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6751" systemVersion="14A329f" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES"> <document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6250" systemVersion="14C109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
<dependencies> <dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6736"/> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6244"/>
</dependencies> </dependencies>
<objects> <objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/> <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>

View File

@ -35,23 +35,29 @@
UIView *switchBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 90, self.view.frame.size.width, 50)]; UIView *switchBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 90, self.view.frame.size.width, 50)];
switchBackground.backgroundColor = [UIColor whiteColor]; switchBackground.backgroundColor = [UIColor whiteColor];
UILabel *enabledSwitchLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 80, 50)]; self.enabledSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(85, 9.5, 51, 31)];
enabledSwitchLabel.text = @"Enabled:";
UILabel *passthroughSwitchLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 0, 80, 50)];
passthroughSwitchLabel.text = @"Pass-Through:";
self.enabledSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 9.5, 51, 31)];
[self.enabledSwitch addTarget:self action:@selector(didToggleSwitch:) forControlEvents:UIControlEventValueChanged]; [self.enabledSwitch addTarget:self action:@selector(didToggleSwitch:) forControlEvents:UIControlEventValueChanged];
self.enabledSwitch.on = self.currListener.enabled; self.enabledSwitch.on = self.currListener.enabled;
self.passthroughSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 9.5, 51, 31)];
UILabel *enabledSwitchLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 80, 50)];
enabledSwitchLabel.text = @"Enabled:";
CGFloat viewWidth = self.view.frame.size.width;
self.passthroughSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(viewWidth-61, 9.5, 51, 31)];
[self.passthroughSwitch addTarget:self action:@selector(didToggleSwitch:) forControlEvents:UIControlEventValueChanged]; [self.passthroughSwitch addTarget:self action:@selector(didToggleSwitch:) forControlEvents:UIControlEventValueChanged];
self.passthroughSwitch.on = self.currListener.willPassthrough; self.passthroughSwitch.on = self.currListener.willPassthrough;
CGFloat passthroughSwitchXOrigin = self.passthroughSwitch.frame.origin.x;
UILabel *passthroughSwitchLabel = [[UILabel alloc] initWithFrame:CGRectMake(passthroughSwitchXOrigin-110, 0, 110, 50)];
passthroughSwitchLabel.text = @"Passthrough:";
[switchBackground addSubview:enabledSwitchLabel]; [switchBackground addSubview:enabledSwitchLabel];
[switchBackground addSubview:passthroughSwitchLabel]; [switchBackground addSubview:passthroughSwitchLabel];
[switchBackground addSubview:self.enabledSwitch]; [switchBackground addSubview:self.enabledSwitch];
[switchBackground addSubview:self.passthroughSwitch];
[self.view addSubview:switchBackground]; [self.view addSubview:switchBackground];
UIView *nameBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 170, self.view.frame.size.width, 50)]; UIView *nameBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 170, self.view.frame.size.width, 50)];
@ -77,18 +83,26 @@
[triggerBackground addSubview:triggerLabel]; [triggerBackground addSubview:triggerLabel];
[triggerBackground addSubview:self.triggerField]; [triggerBackground addSubview:self.triggerField];
[self.view addSubview:triggerBackground]; [self.view addSubview:triggerBackground];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(saveChangesIfNecessary)
name:UIApplicationWillResignActiveNotification
object:nil];
} }
- (void)viewWillDisappear:(BOOL)animated { - (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated]; [super viewWillDisappear:animated];
[self saveChangesIfNecessary];
}
- (void)saveChangesIfNecessary {
if (self.didChange) { if (self.didChange) {
self.currListener.trigger = self.triggerField.text; self.currListener.trigger = self.triggerField.text;
self.currListener.name = self.nameField.text; self.currListener.name = self.nameField.text;
[self.delegate listenerDidChange:self.currListener]; [self.delegate listenerDidChange:self.currListener];
} }
} }
#pragma mark - UI Delegates #pragma mark - UI Delegates
- (void)didToggleSwitch:(UISwitch*)theSwitch { - (void)didToggleSwitch:(UISwitch*)theSwitch {

View File

@ -10,26 +10,27 @@
#define kPreferencesPath "/var/mobile/Library/Preferences/com.assistantplus.app.plist" #define kPreferencesPath "/var/mobile/Library/Preferences/com.assistantplus.app.plist"
@implementation customReplyCommands { @implementation customReplyCommands {
NSDictionary *phrases; NSMutableArray *phrases;
} }
// @{ trigger : NSRegularExpression
// response : NSString }
- (void)createPhraseDictionary:(NSDictionary*)repliesDict { - (void)createPhraseDictionary:(NSDictionary*)repliesDict {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; phrases = [[NSMutableArray alloc] init];
NSArray *customReplies = repliesDict[@"customReplies"]; NSArray *customReplies = repliesDict[@"customReplies"];
if (customReplies) { if (customReplies) {
for (NSDictionary *currReply in customReplies) { for (NSDictionary *currReply in customReplies) {
NSString *currTrigger = currReply[@"trigger"]; NSString *currTrigger = currReply[@"trigger"];
NSString *currResponse = currReply[@"response"]; NSString *currResponse = currReply[@"response"];
if ([dict objectForKey:[currTrigger lowercaseString]]) { NSRegularExpression *regExpression = [NSRegularExpression regularExpressionWithPattern:currTrigger options:NSRegularExpressionCaseInsensitive error:nil];
NSMutableArray *mutableCmds = [[dict objectForKey:[currTrigger lowercaseString]] mutableCopy]; if (currTrigger && regExpression) {
[mutableCmds addObject:currResponse]; [phrases addObject:@{@"trigger" : regExpression,
[dict setObject:mutableCmds forKey:[currTrigger lowercaseString]]; @"response" : currResponse}];
} else {
[dict setObject:@[currResponse] forKey:[currTrigger lowercaseString]];
} }
} }
} }
phrases = dict;
} }
-(BOOL)handleSpeech:(NSString *)text withTokens:(NSSet *)tokens withSession:(id<APSiriSession>)session { -(BOOL)handleSpeech:(NSString *)text withTokens:(NSSet *)tokens withSession:(id<APSiriSession>)session {
@ -40,11 +41,21 @@
text = [[text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] lowercaseString]; text = [[text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] lowercaseString];
if ([phrases objectForKey:text]) { BOOL didHandle = NO;
NSArray *customReplies = [phrases objectForKey:text]; for (NSDictionary *currDict in phrases) {
for (NSString *currReply in customReplies) { NSRegularExpression *regExpression = currDict[@"trigger"];
[session sendTextSnippet:currReply temporary:NO scrollToTop:YES dialogPhase:@"Completion"]; if (regExpression) {
NSArray *arrayOfAllMatches = [regExpression matchesInString:text options:0 range:NSMakeRange(0, [text length])];
for (NSTextCheckingResult *match in arrayOfAllMatches) {
if (match.numberOfRanges > 0) {
[session sendTextSnippet:currDict[@"response"] temporary:NO scrollToTop:YES dialogPhase:@"Completion"];
didHandle = YES;
}
}
} }
}
if (didHandle) {
[session sendRequestCompleted]; [session sendRequestCompleted];
return YES; return YES;
} }