Skip to content

Commit

Permalink
support block callback for watchOS2 sendMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
cheekiatng committed Sep 14, 2015
1 parent a4c81f7 commit 1d8aee1
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
37 changes: 36 additions & 1 deletion apidoc/Titanium/WatchSession/WatchSession.yml
Expand Up @@ -58,11 +58,18 @@ methods:
description: |
Sends a message to the installed watchapp on the apple watch in the foreground.
parameters:
- name: params
- name: message
summary: Message to send to apple watch.
type: Dictionary
- name: onReply
summary: Function to be called upon receiving a reply from the watch app.
description: |
This function is used to process the direct reply from the watch app after sending one.
type: Callback<MessageReply>
optional: true
platforms: [iphone]
osver: {ios: {min: "9.0"}}
since: 5.0.1
- name: updateApplicationContext
summary: |
Sends an app context update to the apple watch.
Expand Down Expand Up @@ -253,6 +260,34 @@ events:
summary: Error message if any.
type: String
platforms: [iphone]
# Callbacks
---
name: MessageReply
summary: Reply message received from watch app.
properties:
- name: message
summary: Reply message from watchapp.
description: Will be undefined if `success` is `false`.
type: Dictionary
- name: success
summary: Indicates if the operation succeeded.
description: Returns `true` if request succeeded, `false` otherwise.
type: Boolean
- name: error
summary: Error message, if any returned.
description: Will be undefined if `success` is `true`.
type: String
- name: code
summary: Error code. Returns 0 if `success` is `true`.
description: |
Error code will be 0 if `success` is `true`, nonzero otherwise. If the error
was generated by the operating system, that system's error value is used.
Otherwise, this value will be -1.
type: Number
since: 5.0.1
platforms: [iphone]
osver: {ios: {min: "9.0"}}

examples:
- title: Creating a Watch Session
example: |
Expand Down
63 changes: 60 additions & 3 deletions iphone/Classes/WatchSessionModule.m
Expand Up @@ -8,6 +8,51 @@

#import "WatchSessionModule.h"
#import "TiUtils.h"
#import "TiEvaluator.h"

@interface WatchMessageCallback : NSObject
{
id<TiEvaluator> context;
KrollCallback *callback;
}
-(id)initWithCallback:(KrollCallback*)callback context:(id<TiEvaluator>)context;
@end

@implementation WatchMessageCallback

-(id)initWithCallback:(KrollCallback*)callback_ context:(id<TiEvaluator>)context_
{
//Ignore analyzer warning here. Delegate will call autorelease onLoad or onError.
if (self = [super init])
{
callback = [callback_ retain];
context = [context_ retain];
}
return self;
}
-(void)dealloc
{
RELEASE_TO_NIL(callback);
RELEASE_TO_NIL(context);
[super dealloc];
}

-(void)replySuccess:(NSDictionary*)replyMessage
{
NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:
replyMessage,@"message",
NUMBOOL(YES),@"success",
NUMINT(0), @"code", nil];
[context fireEvent:callback withObject:event remove:NO thisObject:nil];
}

-(void)replyError:(NSError*)error
{
NSDictionary *event = [TiUtils dictionaryWithCode:[error code] message:[TiUtils messageFromError:error]];
[context fireEvent:callback withObject:event remove:NO thisObject:nil];
}

@end

@implementation WatchSessionModule
#pragma mark Titanium components
Expand Down Expand Up @@ -98,7 +143,7 @@ -(void)activateSession:(id)value
[self watchSession];
}

-(void)sendMessage:(id)value
-(void)sendMessage:(id)args
{
if ([WCSession isSupported] == NO) {
DebugLog(@"[ERROR] Target does not support watch connectivity");
Expand All @@ -108,8 +153,20 @@ -(void)sendMessage:(id)value
DebugLog(@"[ERROR] No watch paired");
return;
}
ENSURE_SINGLE_ARG(value, NSDictionary)
[[self watchSession] sendMessage:value replyHandler:nil errorHandler:nil];
ENSURE_SINGLE_ARG(args, NSDictionary)
NSDictionary *value = args;
KrollCallback *replyHandler = [value objectForKey:@"onReply"];
if (replyHandler == nil) {
[[self watchSession] sendMessage:[value objectForKey:@"message"] replyHandler:nil errorHandler:nil];
return;
}
[[self watchSession] sendMessage:[value objectForKey:@"message"] replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {
WatchMessageCallback *wmc = [[WatchMessageCallback alloc] initWithCallback:replyHandler context:[self executionContext]];
[wmc replySuccess:replyMessage];
} errorHandler:^(NSError * _Nonnull error) {
WatchMessageCallback *wmc = [[WatchMessageCallback alloc] initWithCallback:replyHandler context:[self executionContext]];
[wmc replyError:error];
}];
}
//sent to watch so that it can update its state when it wakes
-(void)updateApplicationContext:(id)value
Expand Down

0 comments on commit 1d8aee1

Please sign in to comment.