Jitsi widget: Dedup widgets

This commit is contained in:
manuroe 2017-08-09 16:48:05 +02:00
parent a4e85e844d
commit f76ed0cd82

View file

@ -56,21 +56,39 @@ NSString *const kWidgetTypeJitsi = @"jitsi";
- (NSArray<Widget *> *)widgetsInRoom:(MXRoom *)room
{
NSMutableArray<Widget *> *widgets = [NSMutableArray array];
// Widget id -> widget
NSMutableDictionary <NSString*, Widget *> *widgets = [NSMutableDictionary dictionary];
// Get all im.vector.modular.widgets state events in the room
NSArray<MXEvent*> *widgetEvents = [room.state stateEventsWithType:kWidgetEventTypeString];
for (MXEvent *widgetEvent in widgetEvents)
// There can be several im.vector.modular.widgets state events for a same widget but
// only the last one must be considered.
// We assume that returned events are ordered chronologically
for (MXEvent *widgetEvent in widgetEvents.reverseObjectEnumerator)
{
// (widgetEvent.stateKey = widget id)
if (!widgets[widgetEvent.stateKey])
{
Widget *widget = [[Widget alloc] initWithWidgetEvent:widgetEvent inMatrixSession:room.mxSession];
if (widget)
{
widgets[widget.widgetId] = widget;
}
}
}
// Return active widgets only
NSMutableArray<Widget *> *activeWidgets = [NSMutableArray array];
for (Widget *widget in widgets.allValues)
{
if (widget.isActive)
{
[widgets addObject:widget];
[activeWidgets addObject:widget];
}
}
return widgets;
return activeWidgets;
}
- (void)addMatrixSession:(MXSession *)mxSession