diff --git a/Riot/Utils/Widgets/WidgetManager.h b/Riot/Utils/Widgets/WidgetManager.h index a10898e29..655a88aba 100644 --- a/Riot/Utils/Widgets/WidgetManager.h +++ b/Riot/Utils/Widgets/WidgetManager.h @@ -93,6 +93,14 @@ WidgetManagerErrorCode; */ - (NSArray *)widgetsNotOfTypes:(NSArray*)notWidgetTypes inRoom:(MXRoom*)room; +/** + List all widgets of an account. + + @param mxSession the session of the user account. + @return a list of widgets. + */ +- (NSArray *)userWidgets:(MXSession*)mxSession; + /** Add a modular widget to a room. diff --git a/Riot/Utils/Widgets/WidgetManager.m b/Riot/Utils/Widgets/WidgetManager.m index d795e77bc..c004a36cf 100644 --- a/Riot/Utils/Widgets/WidgetManager.m +++ b/Riot/Utils/Widgets/WidgetManager.m @@ -172,6 +172,26 @@ NSString *const WidgetManagerErrorDomain = @"WidgetManagerErrorDomain"; return activeWidgets; } +- (NSArray *)userWidgets:(MXSession*)mxSession +{ + // Get all widgets in the user account data + NSMutableArray *userWidgets = [NSMutableArray array]; + for (NSDictionary *widgetEventContent in [mxSession.accountData accountDataForEventType:@"m.widgets"].allValues) + { + MXEvent *widgetEvent = [MXEvent modelFromJSON:widgetEventContent]; + if (widgetEvent) + { + Widget *widget = [[Widget alloc] initWithWidgetEvent:widgetEvent inMatrixSession:mxSession]; + if (widget) + { + [userWidgets addObject:widget]; + } + } + } + + return userWidgets; +} + - (MXHTTPOperation *)createWidget:(NSString*)widgetId withContent:(NSDictionary*)widgetContent inRoom:(MXRoom*)room diff --git a/Riot/ViewController/RoomViewController.m b/Riot/ViewController/RoomViewController.m index 8c1306325..71472f311 100644 --- a/Riot/ViewController/RoomViewController.m +++ b/Riot/ViewController/RoomViewController.m @@ -1257,9 +1257,10 @@ // If the setting is disabled, do not show the icon self.navigationItem.rightBarButtonItems = @[self.navigationItem.rightBarButtonItem]; } - else if (self.widgetsCount) + else if ([self widgetsCount:NO]) { // Show there are widgets by changing the "apps" icon color + // Show it in red only for room widgets, not user's widgets // TODO: Design must be reviewed UIImage *icon = self.navigationItem.rightBarButtonItems[1].image; icon = [MXKTools paintImage:icon withColor:kRiotColorPinkRed]; @@ -3063,7 +3064,7 @@ // Matrix Apps button else if (self.navigationItem.rightBarButtonItems.count == 2 && sender == self.navigationItem.rightBarButtonItems[1]) { - if (self.widgetsCount) + if ([self widgetsCount:YES]) { WidgetPickerViewController *widgetPicker = [[WidgetPickerViewController alloc] initForMXSession:self.roomDataSource.mxSession inRoom:self.roomDataSource.roomId]; @@ -3643,10 +3644,16 @@ [[AppDelegate theDelegate] showErrorAsAlert:error]; } -- (NSUInteger)widgetsCount +- (NSUInteger)widgetsCount:(BOOL)includeUserWidgets { - return [[WidgetManager sharedManager] widgetsNotOfTypes:@[kWidgetTypeJitsi] - inRoom:self.roomDataSource.room].count; + NSUInteger widgetsCount = [[WidgetManager sharedManager] widgetsNotOfTypes:@[kWidgetTypeJitsi] + inRoom:self.roomDataSource.room].count; + if (includeUserWidgets) + { + widgetsCount = [[WidgetManager sharedManager] userWidgets:self.roomDataSource.room.mxSession].count; + } + + return widgetsCount; } #pragma mark - Unreachable Network Handling diff --git a/Riot/ViewController/Widgets/IntegrationManagerViewController.m b/Riot/ViewController/Widgets/IntegrationManagerViewController.m index 35bd62e5e..e37dea2b9 100644 --- a/Riot/ViewController/Widgets/IntegrationManagerViewController.m +++ b/Riot/ViewController/Widgets/IntegrationManagerViewController.m @@ -551,20 +551,24 @@ NSString *const kJavascriptSendResponseToModular = @"riotIOS.sendResponse('%@', - (void)getWidgets:(NSDictionary*)eventData { MXRoom *room = [self roomCheckWithEvent:eventData]; + NSMutableArray *widgetStateEvents = [NSMutableArray array]; if (room) { NSArray *widgets = [[WidgetManager sharedManager] widgetsInRoom:room]; - - NSMutableArray *widgetStateEvents = [NSMutableArray arrayWithCapacity:widgets.count]; - for (Widget *widget in widgets) { [widgetStateEvents addObject:widget.widgetEvent.JSONDictionary]; } - - [self sendNSObjectResponse:widgetStateEvents toEvent:eventData]; } + + // Add user widgets (not linked to a specific room) + for (Widget *widget in [[WidgetManager sharedManager] userWidgets:mxSession]) + { + [widgetStateEvents addObject:widget.widgetEvent.JSONDictionary]; + } + + [self sendNSObjectResponse:widgetStateEvents toEvent:eventData]; } - (void)canSendEvent:(NSDictionary*)eventData diff --git a/Riot/ViewController/Widgets/WidgetPickerViewController.m b/Riot/ViewController/Widgets/WidgetPickerViewController.m index 59bf7c339..958f263ff 100644 --- a/Riot/ViewController/Widgets/WidgetPickerViewController.m +++ b/Riot/ViewController/Widgets/WidgetPickerViewController.m @@ -53,12 +53,19 @@ MXRoom *room = [mxSession roomWithRoomId:roomId]; - NSArray *widgets = [[WidgetManager sharedManager] widgetsNotOfTypes:@[kWidgetTypeJitsi] + NSArray *roomWidgets = [[WidgetManager sharedManager] widgetsNotOfTypes:@[kWidgetTypeJitsi] inRoom:room]; + + NSArray *userWidgets = [[WidgetManager sharedManager] userWidgets:room.mxSession]; + + NSMutableArray *widgets = [NSMutableArray array]; + [widgets addObjectsFromArray:roomWidgets]; + [widgets addObjectsFromArray:userWidgets]; + // List widgets for (Widget *widget in widgets) { - alertAction = [UIAlertAction actionWithTitle:widget.name + alertAction = [UIAlertAction actionWithTitle:widget.name ? widget.name : widget.type style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {