Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking change: Changed from ListView to CollectionView in BookmarksView which resolves ItemTemplate issue in iOS #598

Merged
merged 10 commits into from
Sep 5, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
x:Class="Toolkit.SampleApp.Maui.Samples.BookmarksViewSample">
<ContentPage.Resources>
<DataTemplate x:Key="ItemTemplateOne">
<TextCell Text="{Binding Name}" TextColor="Red" />
<Label Text="{Binding Name}" TextColor="Red" />
</DataTemplate>
<DataTemplate x:Key="ItemTemplateTwo">
<ViewCell>
<Label BackgroundColor="Red" TextColor="White" Text="{Binding Name}" />
</ViewCell>
<Label BackgroundColor="Red" TextColor="White" Text="{Binding Name}" />
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved
</DataTemplate>
</ContentPage.Resources>
<ContentPage.Content>
Expand Down
73 changes: 31 additions & 42 deletions src/Toolkit/Toolkit.Maui/BookmarksView/BookmarksView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace Esri.ArcGISRuntime.Toolkit.Maui;
/// </summary>
public class BookmarksView : TemplatedView
{
private ListView? _presentingView;
private BookmarksViewDataSource _dataSource = new BookmarksViewDataSource();
private const string _presentingViewName = "PresentingView";

private static readonly DataTemplate DefaultDataTemplate;
private static readonly ControlTemplate DefaultControlTemplate;
Expand All @@ -35,18 +35,17 @@ static BookmarksView()
{
DefaultDataTemplate = new DataTemplate(() =>
{
var defaultCell = new TextCell();
defaultCell.SetBinding(TextCell.TextProperty, nameof(Bookmark.Name));
return defaultCell;
var defaultLabel = new Label();
defaultLabel.SetBinding(Label.TextProperty, nameof(Bookmark.Name));
return defaultLabel;
});

string template = @"<ControlTemplate xmlns=""http://schemas.microsoft.com/dotnet/2021/maui"" xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" xmlns:esriTK=""clr-namespace:Esri.ArcGISRuntime.Toolkit.Maui"">
<ListView x:Name=""PresentingView"" HorizontalOptions=""FillAndExpand"" VerticalOptions=""FillAndExpand"">
<x:Arguments>
<ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>
</x:Arguments>
</ListView>
</ControlTemplate>";
string template =
$@"<ControlTemplate xmlns=""http://schemas.microsoft.com/dotnet/2021/maui""
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
xmlns:esriTK=""clr-namespace:Esri.ArcGISRuntime.Toolkit.Maui"">
<CollectionView x:Name=""{_presentingViewName}"" SelectionMode=""Single"" />
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved
</ControlTemplate>";
DefaultControlTemplate = Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml(new ControlTemplate(), template);
}

Expand All @@ -56,7 +55,6 @@ static BookmarksView()
public BookmarksView()
{
ItemTemplate = DefaultDataTemplate;

ControlTemplate = DefaultControlTemplate;
}

Expand All @@ -67,19 +65,13 @@ protected override void OnApplyTemplate()
{
base.OnApplyTemplate();

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

_presentingView = GetTemplateChild("PresentingView") as ListView;
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved

if (_presentingView != null)
var collectionView = (CollectionView)GetTemplateChild(_presentingViewName);
if (collectionView is CollectionView view)
{
_presentingView.ItemSelected += Internal_bookmarkSelected;
_presentingView.ItemTemplate = ItemTemplate;
_presentingView.ItemsSource = _dataSource;
view.SelectionChanged -= Internal_bookmarkSelected;
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved
view.SelectionChanged += Internal_bookmarkSelected;
}
UpdatePresentingView();
}

/// <summary>
Expand Down Expand Up @@ -156,20 +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;

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
}
BookmarksView bookmarksView = (BookmarksView)sender;
bookmarksView.UpdatePresentingView();
}

/// <summary>
Expand All @@ -192,16 +172,25 @@ private void SelectAndNavigateToBookmark(Bookmark bookmark)
/// <summary>
/// Handles selection on the underlying list view.
/// </summary>
private void Internal_bookmarkSelected(object? sender, SelectedItemChangedEventArgs e)
private void Internal_bookmarkSelected(object? sender, SelectionChangedEventArgs e)
{
if (e.SelectedItem is Bookmark bm)
if (sender is CollectionView cv)
{
SelectAndNavigateToBookmark(bm);
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved
if (cv.SelectedItem is Bookmark item)
{
SelectAndNavigateToBookmark(item);
}
cv.ClearValue(CollectionView.SelectedItemProperty);
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved
}
}

if (e.SelectedItem != null && sender is ListView lv)
private void UpdatePresentingView()
{
var collectionView = (CollectionView)GetTemplateChild(_presentingViewName);
if (collectionView is CollectionView view)
{
lv.SelectedItem = null;
view.ItemTemplate = ItemTemplate;
view.ItemsSource = _dataSource;
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved
}
}

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 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
Loading