Widgets: Add "Remove for everyone" to the contextual menu

This commit is contained in:
manuroe
2019-11-20 17:38:12 +01:00
parent e7fcaa197a
commit ea6437c521
4 changed files with 55 additions and 1 deletions
@@ -86,6 +86,44 @@ NSString *const kJavascriptSendResponseToPostMessageAPI = @"riotIOS.sendResponse
self.URL = self.widgetUrl;
}
- (BOOL)hasUserEnoughPowerToManageCurrentWidget
{
BOOL hasUserEnoughPower = NO;
MXSession *session = widget.mxSession;
MXRoom *room = [session roomWithRoomId:self.widget.roomId];
MXRoomState *roomState = room.dangerousSyncState;
if (roomState)
{
// Check user's power in the room
MXRoomPowerLevels *powerLevels = roomState.powerLevels;
NSInteger oneSelfPowerLevel = [powerLevels powerLevelOfUserWithUserID:session.myUser.userId];
// The user must be able to send state events to manage widgets
if (oneSelfPowerLevel >= powerLevels.stateDefault)
{
hasUserEnoughPower = YES;
}
}
return hasUserEnoughPower;
}
- (void)removeCurrentWidget
{
WidgetManager *widgetManager = [WidgetManager sharedManager];
MXRoom *room = [self.widget.mxSession roomWithRoomId:self.widget.roomId];
NSString *widgetId = self.widget.widgetId;
if (room && widgetId)
{
[widgetManager closeWidget:widgetId inRoom:room success:^{
} failure:^(NSError *error) {
NSLog(@"[WidgetVC] removeCurrentWidget failed. Error: %@", error);
}];
}
}
- (void)showErrorAsAlert:(NSError*)error
{
NSString *title = [error.userInfo valueForKey:NSLocalizedFailureReasonErrorKey];
@@ -246,6 +284,17 @@ NSString *const kJavascriptSendResponseToPostMessageAPI = @"riotIOS.sendResponse
}]];
}
if ([self hasUserEnoughPowerToManageCurrentWidget])
{
[menu addAction:[UIAlertAction actionWithTitle:NSLocalizedStringFromTable(@"widget_menu_remove", @"Vector", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[self removeCurrentWidget];
[self withdrawViewControllerAnimated:YES completion:nil];
}]];
}
[menu addAction:[UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"cancel"]
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {