Skip to content

Commit

Permalink
Send Stickers: Plug the sticker picker widget with the room datasourc…
Browse files Browse the repository at this point in the history
…e to send a sticker

#1860
  • Loading branch information
manuroe committed May 7, 2018
1 parent b7b0884 commit 121aeb9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
7 changes: 4 additions & 3 deletions Riot/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -2200,7 +2200,7 @@ - (void)initMatrixSessions
sdkOptions.backgroundModeHandler = [[MXUIKitBackgroundModeHandler alloc] init];

// Get modular widget events in rooms histories
[[MXKAppSettings standardAppSettings] addSupportedEventTypes:@[kWidgetEventTypeString]];
[[MXKAppSettings standardAppSettings] addSupportedEventTypes:@[kWidgetMatrixEventTypeString, kWidgetModularEventTypeString]];

// Disable long press on event in bubble cells
[MXKRoomBubbleTableViewCell disableLongPressGestureOnEvent:YES];
Expand Down Expand Up @@ -2259,9 +2259,10 @@ - (void)initMatrixSessions
// Each room member will be considered as a potential contact.
[MXKContactManager sharedManager].contactManagerMXRoomSource = MXKContactManagerMXRoomSourceAll;

// Send read receipts for modular widgets events too
// Send read receipts for widgets events too
NSMutableArray<MXEventTypeString> *acknowledgableEventTypes = [NSMutableArray arrayWithArray:mxSession.acknowledgableEventTypes];
[acknowledgableEventTypes addObject:kWidgetEventTypeString];
[acknowledgableEventTypes addObject:kWidgetMatrixEventTypeString];
[acknowledgableEventTypes addObject:kWidgetModularEventTypeString];
mxSession.acknowledgableEventTypes = acknowledgableEventTypes;
}
else if (mxSession.state == MXSessionStateStoreDataReady)
Expand Down
8 changes: 6 additions & 2 deletions Riot/ViewController/Widgets/WidgetPickerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ - (void)showInViewController:(MXKViewController *)mxkViewController
// Display the widget
[widget widgetUrl:^(NSString * _Nonnull widgetUrl) {

WidgetViewController *widgetVC = [[WidgetViewController alloc] initWithUrl:widgetUrl forWidget:widget];
[mxkViewController.navigationController pushViewController:widgetVC animated:YES];
WidgetViewController *widgetVC = [[WidgetViewController alloc] initWithUrl:widgetUrl forWidget:widget];

MXKRoomDataSourceManager *roomDataSourceManager = [MXKRoomDataSourceManager sharedManagerForMatrixSession:mxSession];
widgetVC.roomDataSource = [roomDataSourceManager roomDataSourceForRoom:roomId create:NO];

[mxkViewController.navigationController pushViewController:widgetVC animated:YES];

} failure:^(NSError * _Nonnull error) {

Expand Down
7 changes: 7 additions & 0 deletions Riot/ViewController/Widgets/WidgetViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import "WebViewViewController.h"

#import "WidgetManager.h"
#import "MatrixKit/MatrixKit.h"

/**
`WidgetViewController` displays widget within a webview.
Expand All @@ -26,6 +27,12 @@
*/
@interface WidgetViewController : WebViewViewController

/**
The room data source.
Required if the widget needs to post messages.
*/
@property (nonatomic) MXKRoomDataSource *roomDataSource;

/**
Init 'WidgetViewController' instance with a widget.
Expand Down
39 changes: 34 additions & 5 deletions Riot/ViewController/Widgets/WidgetViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#import "AppDelegate.h"

NSString *const kJavascriptSendResponseToModular = @"riotIOS.sendResponse('%@', %@);";
NSString *const kJavascriptSendResponseToPostMessageAPI = @"riotIOS.sendResponse('%@', %@);";

@interface WidgetViewController ()
{
Expand Down Expand Up @@ -186,13 +186,42 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

- (void)onPostMessageRequest:(NSString*)requestId data:(NSDictionary*)requestData
{
// TODO
NSString *action;
MXJSONModelSetString(action, requestData[@"action"]);

if ([@"m.sticker" isEqualToString:action])
{
// Extract the sticker event content and send it as is

// The key should be "data" according to https://docs.google.com/document/d/1uPF7XWY_dXTKVKV7jZQ2KmsI19wn9-kFRgQ1tFQP7wQ/edit?usp=sharing
// TODO: Fix it once spec is finalised
NSDictionary *widgetData;
NSDictionary *stickerContent;
MXJSONModelSetDictionary(widgetData, requestData[@"widgetData"]);
if (widgetData)
{
MXJSONModelSetDictionary(stickerContent, widgetData[@"content"]);
}

if (stickerContent)
{
// Let the data source manage the sending cycle
[_roomDataSource sendEventOfType:kMXEventTypeStringSticker content:stickerContent success:nil failure:nil];
}
else
{
NSLog(@"[WidgetVC] onPostMessageRequest: ERROR: Invalid content for m.sticker: %@", requestData);
}

// Consider we are done with the sticker picker widget
[self withdrawViewControllerAnimated:YES completion:nil];
}
}

- (void)sendBoolResponse:(BOOL)response toRequest:(NSString*)requestId
{
// Convert BOOL to "true" or "false"
NSString *js = [NSString stringWithFormat:kJavascriptSendResponseToModular,
NSString *js = [NSString stringWithFormat:kJavascriptSendResponseToPostMessageAPI,
requestId,
response ? @"true" : @"false"];

Expand All @@ -201,7 +230,7 @@ - (void)sendBoolResponse:(BOOL)response toRequest:(NSString*)requestId

- (void)sendIntegerResponse:(NSUInteger)response toRequest:(NSString*)requestId
{
NSString *js = [NSString stringWithFormat:kJavascriptSendResponseToModular,
NSString *js = [NSString stringWithFormat:kJavascriptSendResponseToPostMessageAPI,
requestId,
@(response)];

Expand All @@ -227,7 +256,7 @@ - (void)sendNSObjectResponse:(NSObject*)response toRequest:(NSString*)requestId
jsString = @"null";
}

NSString *js = [NSString stringWithFormat:kJavascriptSendResponseToModular,
NSString *js = [NSString stringWithFormat:kJavascriptSendResponseToPostMessageAPI,
requestId,
jsString];

Expand Down

0 comments on commit 121aeb9

Please sign in to comment.