Skip to content

Commit

Permalink
Refactor BookmarksView to centralize CollectionView updates
Browse files Browse the repository at this point in the history
- This Fixes a bug with Android adding item couple of times when new Bookmark is added.
- Centralize the update logic for the CollectionView in the BookmarksView class by introducing a new `UpdateListView` method.
  • Loading branch information
prathameshnarkhede committed Sep 3, 2024
1 parent 475793d commit 4d65c75
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions src/Toolkit/Toolkit.Maui/BookmarksView/BookmarksView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// * limitations under the License.
// ******************************************************************************/
using Esri.ArcGISRuntime.Mapping;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;

namespace Esri.ArcGISRuntime.Toolkit.Maui;
Expand All @@ -24,7 +25,6 @@ namespace Esri.ArcGISRuntime.Toolkit.Maui;
/// </summary>
public class BookmarksView : TemplatedView
{
private CollectionView? _presentingView;
private BookmarksViewDataSource _dataSource = new BookmarksViewDataSource();

private static readonly DataTemplate DefaultDataTemplate;
Expand Down Expand Up @@ -55,8 +55,11 @@ static BookmarksView()
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 @@ -65,20 +68,7 @@ public BookmarksView()
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();

if (_presentingView != null)
{
_presentingView.SelectionChanged -= Internal_bookmarkSelected;
}

_presentingView = GetTemplateChild("PresentingView") as CollectionView;

if (_presentingView != null)
{
_presentingView.SelectionChanged += Internal_bookmarkSelected;
_presentingView.ItemTemplate = ItemTemplate;
_presentingView.ItemsSource = _dataSource;
}
UpdateListView();
}

/// <summary>
Expand Down Expand Up @@ -156,19 +146,7 @@ private static void GeoViewChanged(BindableObject sender, object? oldValue, obje
private static void ItemTemplateChanged(BindableObject sender, object? oldValue, object? newValue)
{
BookmarksView bookmarkView = (BookmarksView)sender;

if (bookmarkView._presentingView != null)
{
bookmarkView._presentingView.ItemTemplate = newValue as DataTemplate;

#if WINDOWS
// This workaround addresses an issue with MAUI WinUI.
// Without refreshing the items source of the BookmarksView ListView the change is not reflected in the UI.
var existingItems = bookmarkView._presentingView.ItemsSource;
bookmarkView._presentingView.ItemsSource = null;
bookmarkView._presentingView.ItemsSource = existingItems;
#endif
}
bookmarkView.UpdateListView();
}

/// <summary>
Expand Down Expand Up @@ -203,6 +181,27 @@ private void Internal_bookmarkSelected(object? sender, SelectionChangedEventArgs
}
}

private void UpdateListView()
{
var collection = (CollectionView)GetTemplateChild("PresentingView");
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)
{
UpdateListView();
}
}

/// <summary>
/// Raised whenever a bookmark is selected.
/// </summary>
Expand Down

0 comments on commit 4d65c75

Please sign in to comment.