diff --git a/Riot/Utils/Widgets/Widget.h b/Riot/Utils/Widgets/Widget.h index 032967ece..c645e7e7a 100644 --- a/Riot/Utils/Widgets/Widget.h +++ b/Riot/Utils/Widgets/Widget.h @@ -59,11 +59,21 @@ */ @property (nonatomic, readonly, nonnull) MXEvent *widgetEvent; +/** + The Matrix session where the widget is. + */ +@property (nonatomic, readonly, nonnull) MXSession *mxSession; + /** Indicate if the widget is still active. */ @property (nonatomic, readonly) BOOL isActive; +/** + The room id of the widget. + */ +@property (nonatomic, readonly, nonnull) NSString *roomId; + /** Create a Widget instance from a widget event. diff --git a/Riot/Utils/Widgets/Widget.m b/Riot/Utils/Widgets/Widget.m index f26ab393f..f901aa816 100644 --- a/Riot/Utils/Widgets/Widget.m +++ b/Riot/Utils/Widgets/Widget.m @@ -33,6 +33,7 @@ { _widgetId = widgetEvent.stateKey; _widgetEvent = widgetEvent; + _mxSession = mxSession; MXJSONModelSetString(_type, widgetEvent.content[@"type"]); MXJSONModelSetString(_url, widgetEvent.content[@"url"]); @@ -55,4 +56,9 @@ return (_type != nil && _url != nil); } +- (NSString *)roomId +{ + return _widgetEvent.roomId; +} + @end diff --git a/Riot/Utils/Widgets/WidgetManager.m b/Riot/Utils/Widgets/WidgetManager.m index 09f1c65ea..7f96bfaa7 100644 --- a/Riot/Utils/Widgets/WidgetManager.m +++ b/Riot/Utils/Widgets/WidgetManager.m @@ -21,6 +21,8 @@ NSString *const kWidgetEventTypeString = @"im.vector.modular.widgets"; NSString *const kWidgetTypeJitsi = @"jitsi"; +NSString *const kMXKWidgetManagerDidUpdateWidgetNotification = @"kMXKWidgetManagerDidUpdateWidgetNotification"; + @interface WidgetManager () { // UserId -> Listener for matrix events for widgets. @@ -67,7 +69,7 @@ NSString *const kWidgetTypeJitsi = @"jitsi"; // We assume that returned events are ordered chronologically for (MXEvent *widgetEvent in widgetEvents.reverseObjectEnumerator) { - // (widgetEvent.stateKey = widget id) + // widgetEvent.stateKey = widget id if (!widgets[widgetEvent.stateKey]) { Widget *widget = [[Widget alloc] initWithWidgetEvent:widgetEvent inMatrixSession:room.mxSession]; @@ -97,8 +99,11 @@ NSString *const kWidgetTypeJitsi = @"jitsi"; if (direction == MXTimelineDirectionForwards) { - // @TODO - NSLog(@"event : %@", event); + Widget *widget = [[Widget alloc] initWithWidgetEvent:event inMatrixSession:mxSession]; + if (widget) + { + [[NSNotificationCenter defaultCenter] postNotificationName:kMXKWidgetManagerDidUpdateWidgetNotification object:widget]; + } } }]; diff --git a/Riot/ViewController/RoomViewController.m b/Riot/ViewController/RoomViewController.m index 5d678ca7e..08b4c4845 100644 --- a/Riot/ViewController/RoomViewController.m +++ b/Riot/ViewController/RoomViewController.m @@ -165,6 +165,9 @@ id kMXCallStateDidChangeObserver; id kMXCallManagerConferenceStartedObserver; id kMXCallManagerConferenceFinishedObserver; + + // Observers to manage widgets + id kMXKWidgetManagerDidUpdateWidgetObserver; // Observer kMXRoomSummaryDidChangeNotification to keep updated the missed discussion count id mxRoomSummaryDidChangeObserver; @@ -451,6 +454,7 @@ [self listenTypingNotifications]; [self listenCallNotifications]; + [self listenWidgetNotifications]; if (self.showExpandedHeader) { @@ -498,7 +502,8 @@ } [self removeCallNotificationsListeners]; - + [self removeWidgetNotificationsListeners]; + // Re-enable the read marker display, and disable its update. self.roomDataSource.showReadMarker = YES; self.updateRoomReadMarker = NO; @@ -1060,7 +1065,8 @@ } [self removeCallNotificationsListeners]; - + [self removeWidgetNotificationsListeners]; + if (previewHeader || (self.expandedHeaderContainer.isHidden == NO)) { // Here [destroy] is called before [viewWillDisappear:] @@ -3265,6 +3271,33 @@ }]; } +#pragma mark - Widget notifications management + +- (void)removeWidgetNotificationsListeners +{ + if (kMXKWidgetManagerDidUpdateWidgetObserver) + { + [[NSNotificationCenter defaultCenter] removeObserver:kMXKWidgetManagerDidUpdateWidgetObserver]; + kMXKWidgetManagerDidUpdateWidgetObserver = nil; + } +} + +- (void)listenWidgetNotifications +{ + kMXKWidgetManagerDidUpdateWidgetObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kMXKWidgetManagerDidUpdateWidgetNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) { + + Widget *widget = notif.object; + if (widget.mxSession == self.roomDataSource.mxSession + && [widget.roomId isEqualToString:customizedRoomDataSource.roomId]) + { + // Jitsi conference widget existence is shown in the bottom bar + // Update the bar + [self refreshActivitiesViewDisplay]; + [self refreshRoomInputToolbar]; + } + }]; +} + #pragma mark - Unreachable Network Handling - (void)refreshActivitiesViewDisplay