Skip to content

Commit

Permalink
Refactor event handling in BookmarksView and DataSource
Browse files Browse the repository at this point in the history
This fixes problem in Android from root level.
  • Loading branch information
prathameshnarkhede committed Sep 4, 2024
1 parent 9660568 commit c6d0ccc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 38 deletions.
35 changes: 13 additions & 22 deletions src/Toolkit/Toolkit.Maui/BookmarksView/BookmarksView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// * limitations under the License.
// ******************************************************************************/
using Esri.ArcGISRuntime.Mapping;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;

namespace Esri.ArcGISRuntime.Toolkit.Maui;
Expand Down Expand Up @@ -57,10 +56,6 @@ public BookmarksView()
{
ItemTemplate = DefaultDataTemplate;
ControlTemplate = DefaultControlTemplate;
#if ANDROID
// This Fixes a bug with Android adding item couple of times when new Bookmark is added to BookmarkCollection.
_dataSource.CollectionChanged += OnBookmarksCollectionChanged;
#endif
}

/// <summary>
Expand All @@ -69,6 +64,13 @@ public BookmarksView()
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();

var collectionView = (CollectionView)GetTemplateChild(_presentingViewName);
if (collectionView is CollectionView view)
{
view.SelectionChanged -= Internal_bookmarkSelected;
view.SelectionChanged += Internal_bookmarkSelected;
}
UpdatePresentingView();
}

Expand Down Expand Up @@ -146,8 +148,8 @@ private static void GeoViewChanged(BindableObject sender, object? oldValue, obje
/// </summary>
private static void ItemTemplateChanged(BindableObject sender, object? oldValue, object? newValue)
{
BookmarksView bookmarkView = (BookmarksView)sender;
bookmarkView.UpdatePresentingView();
BookmarksView bookmarksView = (BookmarksView)sender;
bookmarksView.UpdatePresentingView();
}

/// <summary>
Expand Down Expand Up @@ -184,22 +186,11 @@ private void Internal_bookmarkSelected(object? sender, SelectionChangedEventArgs

private void UpdatePresentingView()
{
var collection = (CollectionView)GetTemplateChild(_presentingViewName);
if (collection != null)
{
collection.SelectionChanged -= Internal_bookmarkSelected;
collection.SelectionChanged += Internal_bookmarkSelected;
collection.ItemTemplate = null;
collection.ItemTemplate = ItemTemplate;
collection.ItemsSource = _dataSource;
}
}

private void OnBookmarksCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action is NotifyCollectionChangedAction.Add)
var collectionView = (CollectionView)GetTemplateChild(_presentingViewName);
if (collectionView is CollectionView view)
{
UpdatePresentingView();
view.ItemTemplate = ItemTemplate;
view.ItemsSource = _dataSource;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ namespace Esri.ArcGISRuntime.Toolkit.UI.Controls
internal class BookmarksViewDataSource : IList<Bookmark>, INotifyCollectionChanged, INotifyPropertyChanged, IList
{
private GeoView? _geoView;
private WeakEventListener<BookmarksViewDataSource, INotifyCollectionChanged, object?, NotifyCollectionChangedEventArgs>? _geoViewBookmarksListener;
private WeakEventListener<BookmarksViewDataSource, ILoadable, object?, EventArgs>? _geoViewLoadListener;
private new WeakEventListener<BookmarksViewDataSource, INotifyCollectionChanged, object?, NotifyCollectionChangedEventArgs>? _overrideListListener;
private IList<Bookmark>? _overrideList;

private IList<Bookmark> ActiveBookmarkList
Expand Down Expand Up @@ -93,10 +96,13 @@ public void SetOverrideList(IEnumerable<Bookmark>? bookmarks)
// Subscribe to events if applicable
if (bookmarks is INotifyCollectionChanged iCollectionChanged)
{
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;
_overrideListListener?.Detach();
_overrideListListener = new WeakEventListener<BookmarksViewDataSource, INotifyCollectionChanged, object?, NotifyCollectionChangedEventArgs>(this, iCollectionChanged)
{
OnEventAction = static (instance, source, eventArgs) => instance.HandleOverrideListCollectionChanged(source, eventArgs),
OnDetachAction = static (instance, source, weakEventListener) => source.CollectionChanged -= weakEventListener.OnEvent
};
iCollectionChanged.CollectionChanged += _overrideListListener.OnEvent;
}
}

Expand Down Expand Up @@ -188,24 +194,29 @@ private void GeoView_PropertyChanged(object? sender, PropertyChangedEventArgs e)

private void GeoViewDocumentChanged(object? sender, object? e)
{
_geoViewLoadListener?.Detach();
if (_geoView is MapView mv && mv.Map is ILoadable mapLoadable)
{
// Listen for load completion
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;
_geoViewLoadListener = new WeakEventListener<BookmarksViewDataSource, ILoadable, object?, EventArgs>(this, mapLoadable)
{
OnEventAction = static (instance, source, eventArgs) => instance.Doc_Loaded(source, eventArgs),
OnDetachAction = static (instance, source, weakEventListener) => source.Loaded -= weakEventListener.OnEvent
};
mapLoadable.Loaded += _geoViewLoadListener.OnEvent;

// Ensure event is raised even if already loaded
_ = mv.Map.RetryLoadAsync();
}
else if (_geoView is SceneView sv && sv.Scene is ILoadable sceneLoadable)
{
// Listen for load completion
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;
_geoViewLoadListener = new WeakEventListener<BookmarksViewDataSource, ILoadable, object?, EventArgs>(this, sceneLoadable)
{
OnEventAction = static (instance, source, eventArgs) => instance.Doc_Loaded(source, eventArgs),
OnDetachAction = static (instance, source, weakEventListener) => source.Loaded -= weakEventListener.OnEvent
};
sceneLoadable.Loaded += _geoViewLoadListener.OnEvent;

// Ensure event is raised even if already loaded
_ = sv.Scene.RetryLoadAsync();
Expand Down Expand Up @@ -240,10 +251,13 @@ private void Doc_Loaded(object? sender, EventArgs e)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}

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;
_geoViewBookmarksListener?.Detach();
_geoViewBookmarksListener = new WeakEventListener<BookmarksViewDataSource, INotifyCollectionChanged, object?, NotifyCollectionChangedEventArgs>(this, bmCollection)
{
OnEventAction = static (instance, source, eventArgs) => instance.HandleGeoViewBookmarksCollectionChanged(source, eventArgs),
OnDetachAction = static (instance, source, weakEventListener) => source.CollectionChanged -= weakEventListener.OnEvent
};
bmCollection.CollectionChanged += _geoViewBookmarksListener.OnEvent;
}

private void HandleGeoViewBookmarksCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
Expand Down

0 comments on commit c6d0ccc

Please sign in to comment.