Skip to content

Commit

Permalink
Revise weak event listener (#462)
Browse files Browse the repository at this point in the history
* Revise weak event listener

Updates the weak event listener internal API to be clearer to consumers. Also updates usage of weak event listener.

Resolves #365

* Fix error in layercontentdatasource

* Remove unnecessary #pragma from WeakEventListener
  • Loading branch information
nCastle1 authored Oct 25, 2022
1 parent bebc62f commit 405a18f
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 176 deletions.
22 changes: 11 additions & 11 deletions src/Toolkit/Toolkit.Maui/SymbolDisplay/SymbolDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ namespace Esri.ArcGISRuntime.Toolkit.Maui;
/// </summary>
public partial class SymbolDisplay : TemplatedView
{
private WeakEventListener<System.ComponentModel.INotifyPropertyChanged, object?, System.ComponentModel.PropertyChangedEventArgs>? _inpcListener;
private WeakEventListener<Window, object, DisplayDensityChangedEventArgs>? _displayDensityChangedListener;
private WeakEventListener<SymbolDisplay, System.ComponentModel.INotifyPropertyChanged, object?, System.ComponentModel.PropertyChangedEventArgs>? _inpcListener;
private WeakEventListener<SymbolDisplay, Window, object, DisplayDensityChangedEventArgs>? _displayDensityChangedListener;
private Task? _currentUpdateTask;
private bool _isRefreshRequired;
private static readonly ControlTemplate DefaultControlTemplate;
Expand Down Expand Up @@ -64,16 +64,16 @@ private void SymbolDisplay_PropertyChanged(object? sender, System.ComponentModel

_displayDensityChangedListener?.Detach();

_displayDensityChangedListener = new WeakEventListener<Window, object, DisplayDensityChangedEventArgs>(Window)
_displayDensityChangedListener = new WeakEventListener<SymbolDisplay, Window, object, DisplayDensityChangedEventArgs>(this, Window)
{
OnEventAction = (instance, source, eventArgs) =>
OnEventAction = static (instance, source, eventArgs) =>
{
if (_lastScaleFactor != GetScaleFactor())
if (instance._lastScaleFactor != instance.GetScaleFactor())
{
Refresh();
instance.Refresh();
}
},
OnDetachAction = (instance, weakEventListener) => instance.DisplayDensityChanged -= weakEventListener.OnEvent,
OnDetachAction = static (instance, source, weakEventListener) => source.DisplayDensityChanged -= weakEventListener.OnEvent,
};
Window.DisplayDensityChanged += _displayDensityChangedListener.OnEvent;
}
Expand Down Expand Up @@ -109,13 +109,13 @@ private void OnSymbolChanged(Symbology.Symbol? oldValue, Symbology.Symbol? newVa

if (newValue != null)
{
_inpcListener = new WeakEventListener<System.ComponentModel.INotifyPropertyChanged, object?, System.ComponentModel.PropertyChangedEventArgs>(newValue)
_inpcListener = new WeakEventListener<SymbolDisplay, System.ComponentModel.INotifyPropertyChanged, object?, System.ComponentModel.PropertyChangedEventArgs>(this, newValue)
{
OnEventAction = (instance, source, eventArgs) =>
OnEventAction = static (instance, source, eventArgs) =>
{
Refresh();
instance.Refresh();
},
OnDetachAction = (instance, weakEventListener) => instance.PropertyChanged -= weakEventListener.OnEvent,
OnDetachAction = static (instance, source, weakEventListener) => source.PropertyChanged -= weakEventListener.OnEvent,
};
newValue.PropertyChanged += _inpcListener.OnEvent;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Toolkit/Toolkit.UI.Controls/SymbolDisplay/SymbolDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Esri.ArcGISRuntime.Toolkit.UI.Controls
/// </summary>
public partial class SymbolDisplay : Control
{
private Internal.WeakEventListener<System.ComponentModel.INotifyPropertyChanged, object?, System.ComponentModel.PropertyChangedEventArgs>? _inpcListener;
private Internal.WeakEventListener<SymbolDisplay, System.ComponentModel.INotifyPropertyChanged, object?, System.ComponentModel.PropertyChangedEventArgs>? _inpcListener;
private Task? _currentUpdateTask;
private bool _isRefreshRequired;

Expand Down Expand Up @@ -60,13 +60,13 @@ private void OnSymbolChanged(Symbology.Symbol? oldValue, Symbology.Symbol? newVa

if (newValue != null)
{
_inpcListener = new Internal.WeakEventListener<System.ComponentModel.INotifyPropertyChanged, object?, System.ComponentModel.PropertyChangedEventArgs>(newValue)
_inpcListener = new Internal.WeakEventListener<SymbolDisplay, System.ComponentModel.INotifyPropertyChanged, object?, System.ComponentModel.PropertyChangedEventArgs>(this, newValue)
{
OnEventAction = (instance, source, eventArgs) =>
OnEventAction = static (instance, source, eventArgs) =>
{
Refresh();
instance.Refresh();
},
OnDetachAction = (instance, weakEventListener) => instance.PropertyChanged -= weakEventListener.OnEvent,
OnDetachAction = static (instance, source, weakEventListener) => source.PropertyChanged -= weakEventListener.OnEvent,
};
newValue.PropertyChanged += _inpcListener.OnEvent;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Toolkit/Toolkit.WPF/UI/Controls/TableOfContents/TocItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ internal TocItem(object content, bool showLegend, int depth, TocItem? parent)

if (content is INotifyPropertyChanged inpc)
{
var listener = new Internal.WeakEventListener<INotifyPropertyChanged, object?, PropertyChangedEventArgs>(inpc)
var listener = new Internal.WeakEventListener<TocItem, INotifyPropertyChanged, object?, PropertyChangedEventArgs>(this, inpc)
{
OnEventAction = (instance, source, eventArgs) => ContentPropertyChanged(eventArgs.PropertyName),
OnDetachAction = (instance, weakEventListener) => instance.PropertyChanged -= weakEventListener.OnEvent,
OnEventAction = static (instance, source, eventArgs) => instance.ContentPropertyChanged(eventArgs.PropertyName),
OnDetachAction = static (instance, source, weakEventListener) => source.PropertyChanged -= weakEventListener.OnEvent,
};
inpc.PropertyChanged += listener.OnEvent;
}
Expand Down Expand Up @@ -166,10 +166,10 @@ private void SetChildren()
RefreshChildren();
if (ilc.SublayerContents is INotifyCollectionChanged incc)
{
var listener = new Internal.WeakEventListener<INotifyCollectionChanged, object?, NotifyCollectionChangedEventArgs>(incc)
var listener = new Internal.WeakEventListener<TocItem, INotifyCollectionChanged, object?, NotifyCollectionChangedEventArgs>(this, incc)
{
OnEventAction = (instance, source, eventArgs) => RefreshChildren(),
OnDetachAction = (instance, weakEventListener) => instance.CollectionChanged -= weakEventListener.OnEvent,
OnEventAction = static (instance, source, eventArgs) => instance.RefreshChildren(),
OnDetachAction = static (instance, source, weakEventListener) => source.CollectionChanged -= weakEventListener.OnEvent,
};
incc.CollectionChanged += listener.OnEvent;
}
Expand Down
32 changes: 16 additions & 16 deletions src/Toolkit/Toolkit/LayerContentDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ protected virtual void OnGeoViewPropertyChanged(GeoView geoView, string? propert
{
}

private WeakEventListener<INotifyPropertyChanged, object?, PropertyChangedEventArgs>? _documentListener;
private WeakEventListener<LayerContentDataSource<T>, INotifyPropertyChanged, object?, PropertyChangedEventArgs>? _documentListener;

private void SubscribeToDocument(INotifyPropertyChanged? document)
private void SubscribeToDocument(GeoModel? document)
{
if (_documentListener != null)
{
Expand All @@ -223,10 +223,10 @@ private void SubscribeToDocument(INotifyPropertyChanged? document)

if (document != null)
{
_documentListener = new WeakEventListener<INotifyPropertyChanged, object?, PropertyChangedEventArgs>(document)
_documentListener = new WeakEventListener<LayerContentDataSource<T>, INotifyPropertyChanged, object?, PropertyChangedEventArgs>(this, document)
{
OnEventAction = (instance, source, eventArgs) => DocumentPropertyChanged(instance, eventArgs.PropertyName),
OnDetachAction = (instance, weakEventListener) => instance.PropertyChanged -= weakEventListener.OnEvent,
OnEventAction = static (instance, source, eventArgs) => instance.DocumentPropertyChanged(instance, eventArgs.PropertyName),
OnDetachAction = static (instance, source, weakEventListener) => source.PropertyChanged -= weakEventListener.OnEvent,
};
document.PropertyChanged += _documentListener.OnEvent;
}
Expand All @@ -251,7 +251,7 @@ protected virtual void OnDocumentPropertyChanged(object? sender, string? propert
{
}

private WeakEventListener<INotifyPropertyChanged, object?, PropertyChangedEventArgs>? _basemapListener;
private WeakEventListener<LayerContentDataSource<T>, INotifyPropertyChanged, object?, PropertyChangedEventArgs>? _basemapListener;

private void SubscribeToBasemap(Basemap? basemap)
{
Expand All @@ -263,10 +263,10 @@ private void SubscribeToBasemap(Basemap? basemap)

if (basemap != null)
{
_basemapListener = new WeakEventListener<INotifyPropertyChanged, object?, PropertyChangedEventArgs>(basemap)
_basemapListener = new WeakEventListener<LayerContentDataSource<T>, INotifyPropertyChanged, object?, PropertyChangedEventArgs>(this, basemap)
{
OnEventAction = (instance, source, eventArgs) => BasemapPropertyChanged(instance, eventArgs.PropertyName),
OnDetachAction = (instance, weakEventListener) => instance.PropertyChanged -= weakEventListener.OnEvent,
OnEventAction = static (instance, source, eventArgs) => instance.BasemapPropertyChanged(instance, eventArgs.PropertyName),
OnDetachAction = static (instance, source, weakEventListener) => source.PropertyChanged -= weakEventListener.OnEvent,
};
basemap.PropertyChanged += _basemapListener.OnEvent;
}
Expand Down Expand Up @@ -338,7 +338,7 @@ private void TrackOperationalLayers()
{
foreach (var action in _operationalLayerTrackingDetachActions)
{
action();
action.Invoke();
}

_operationalLayerTrackingDetachActions = null;
Expand Down Expand Up @@ -366,10 +366,10 @@ private IEnumerable<Action> TrackLayerContentsRecursive(IEnumerable<ILayerConten
{
if (layer is INotifyPropertyChanged inpc)
{
var listener = new WeakEventListener<INotifyPropertyChanged, object?, PropertyChangedEventArgs>(inpc)
var listener = new WeakEventListener<LayerContentDataSource<T>, INotifyPropertyChanged, object?, PropertyChangedEventArgs>(this, inpc)
{
OnEventAction = (instance, source, eventArgs) => Layer_PropertyChanged((ILayerContent)instance, eventArgs.PropertyName),
OnDetachAction = (instance, weakEventListener) => instance.PropertyChanged -= weakEventListener.OnEvent,
OnEventAction = static (instance, source, eventArgs) => instance.Layer_PropertyChanged((ILayerContent)source, eventArgs.PropertyName),
OnDetachAction = static (instance, source, weakEventListener) => source.PropertyChanged -= weakEventListener.OnEvent,
};
inpc.PropertyChanged += listener.OnEvent;
yield return listener.Detach;
Expand All @@ -383,10 +383,10 @@ private IEnumerable<Action> TrackLayerContentsRecursive(IEnumerable<ILayerConten

if (layers is INotifyCollectionChanged incc)
{
var listener = new WeakEventListener<INotifyCollectionChanged, object?, NotifyCollectionChangedEventArgs>(incc)
var listener = new WeakEventListener<LayerContentDataSource<T>, INotifyCollectionChanged, object?, NotifyCollectionChangedEventArgs>(this, incc)
{
OnEventAction = (instance, source, eventArgs) => Layers_CollectionChanged(source, eventArgs),
OnDetachAction = (instance, weakEventListener) => instance.CollectionChanged -= weakEventListener.OnEvent,
OnEventAction = static (instance, source, eventArgs) => instance.Layers_CollectionChanged(source, eventArgs),
OnDetachAction = static (instance, source, weakEventListener) => source.CollectionChanged -= weakEventListener.OnEvent,
};
incc.CollectionChanged += listener.OnEvent;
yield return listener.Detach;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ public void SetOverrideList(IEnumerable<Bookmark>? bookmarks)
// Subscribe to events if applicable
if (bookmarks is INotifyCollectionChanged iCollectionChanged)
{
var listener = new WeakEventListener<INotifyCollectionChanged, object?, NotifyCollectionChangedEventArgs>(iCollectionChanged);
listener.OnEventAction = (instance, source, eventArgs) => HandleOverrideListCollectionChanged(source, eventArgs);
listener.OnDetachAction = (instance, weakEventListener) => instance.CollectionChanged -= weakEventListener.OnEvent;
var listener = new WeakEventListener<BookmarksViewDataSource, INotifyCollectionChanged, object?, NotifyCollectionChangedEventArgs>(this, iCollectionChanged);
listener.OnEventAction = static (instance, source, eventArgs) => instance.HandleOverrideListCollectionChanged(source, eventArgs);
listener.OnDetachAction = static (instance, source, weakEventListener) => source.CollectionChanged -= weakEventListener.OnEvent;
iCollectionChanged.CollectionChanged += listener.OnEvent;
}
}
Expand Down Expand Up @@ -191,9 +191,9 @@ private void GeoViewDocumentChanged(object? sender, object? e)
if (_geoView is MapView mv && mv.Map is ILoadable mapLoadable)
{
// Listen for load completion
var listener = new WeakEventListener<ILoadable, object?, EventArgs>(mapLoadable);
listener.OnEventAction = (instance, source, eventArgs) => Doc_Loaded(source, eventArgs);
listener.OnDetachAction = (instance, weakEventListener) => instance.Loaded -= weakEventListener.OnEvent;
var listener = new WeakEventListener<BookmarksViewDataSource, ILoadable, object?, EventArgs>(this, mapLoadable);
listener.OnEventAction = static (instance, source, eventArgs) => instance.Doc_Loaded(source, eventArgs);
listener.OnDetachAction = static (instance, source, weakEventListener) => source.Loaded -= weakEventListener.OnEvent;
mapLoadable.Loaded += listener.OnEvent;

// Ensure event is raised even if already loaded
Expand All @@ -202,9 +202,9 @@ private void GeoViewDocumentChanged(object? sender, object? e)
else if (_geoView is SceneView sv && sv.Scene is ILoadable sceneLoadable)
{
// Listen for load completion
var listener = new WeakEventListener<ILoadable, object?, EventArgs>(sceneLoadable);
listener.OnEventAction = (instance, source, eventArgs) => Doc_Loaded(source, eventArgs);
listener.OnDetachAction = (instance, weakEventListener) => instance.Loaded -= weakEventListener.OnEvent;
var listener = new WeakEventListener<BookmarksViewDataSource, ILoadable, object?, EventArgs>(this, sceneLoadable);
listener.OnEventAction = static (instance, source, eventArgs) => instance.Doc_Loaded(source, eventArgs);
listener.OnDetachAction = static (instance, source, weakEventListener) => source.Loaded -= weakEventListener.OnEvent;
sceneLoadable.Loaded += listener.OnEvent;

// Ensure event is raised even if already loaded
Expand Down Expand Up @@ -240,9 +240,9 @@ private void Doc_Loaded(object? sender, EventArgs e)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}

var listener = new WeakEventListener<INotifyCollectionChanged, object?, NotifyCollectionChangedEventArgs>(bmCollection);
listener.OnEventAction = (instance, source, eventArgs) => HandleGeoViewBookmarksCollectionChanged(source, eventArgs);
listener.OnDetachAction = (instance, weakEventListener) => instance.CollectionChanged -= weakEventListener.OnEvent;
var listener = new WeakEventListener<BookmarksViewDataSource, INotifyCollectionChanged, object?, NotifyCollectionChangedEventArgs>(this, bmCollection);
listener.OnEventAction = static (instance, source, eventArgs) => instance.HandleGeoViewBookmarksCollectionChanged(source, eventArgs);
listener.OnDetachAction = static (instance, source, weakEventListener) => source.CollectionChanged -= weakEventListener.OnEvent;
bmCollection.CollectionChanged += listener.OnEvent;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ internal class OverviewMapController
private Symbol? _areaSymbol;
private Symbol? _pointSymbol;
private GeoView? _connectedView;
private readonly WeakEventListener<GeoView, object?, EventArgs>? _viewpointListener;
private readonly WeakEventListener<GeoView, object?, EventArgs>? _navigationListener;
private readonly WeakEventListener<OverviewMapController, GeoView, object?, EventArgs>? _viewpointListener;
private readonly WeakEventListener<OverviewMapController, GeoView, object?, EventArgs>? _navigationListener;
private readonly GraphicsOverlay _extentOverlay;
private readonly Graphic _extentGraphic;
private readonly MapView _insetView;
Expand All @@ -66,18 +66,18 @@ public OverviewMapController(MapView overview)
_extentOverlay.Graphics.Add(_extentGraphic);

// _overview.ViewpointChanged += OnViewpointChanged
_viewpointListener = new WeakEventListener<GeoView, object?, EventArgs>(_insetView)
_viewpointListener = new WeakEventListener<OverviewMapController, GeoView, object?, EventArgs>(this, _insetView)
{
OnEventAction = (instance, source, eventArgs) => OnInsetViewpointChanged(source, eventArgs),
OnDetachAction = (instance, weakEventListener) => instance.ViewpointChanged -= weakEventListener.OnEvent,
OnEventAction = static (instance, source, eventArgs) => instance.OnInsetViewpointChanged(source, eventArgs),
OnDetachAction = static (instance, source, weakEventListener) => source.ViewpointChanged -= weakEventListener.OnEvent,
};
_insetView.ViewpointChanged += _viewpointListener.OnEvent;

// _overview.NavigationCompleted += OnNavigationCompleted;
_navigationListener = new WeakEventListener<GeoView, object?, EventArgs>(_insetView)
_navigationListener = new WeakEventListener<OverviewMapController, GeoView, object?, EventArgs>(this, _insetView)
{
OnEventAction = (instance, source, eventArgs) => OnInsetNavigationCompleted(source, eventArgs),
OnDetachAction = (instance, weakEventListener) => instance.NavigationCompleted -= weakEventListener.OnEvent,
OnEventAction = static (instance, source, eventArgs) => instance.OnInsetNavigationCompleted(source, eventArgs),
OnDetachAction = static (instance, source, weakEventListener) => source.NavigationCompleted -= weakEventListener.OnEvent,
};
_insetView.NavigationCompleted += _navigationListener.OnEvent;
}
Expand Down
Loading

0 comments on commit 405a18f

Please sign in to comment.