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
82 changes: 28 additions & 54 deletions src/Toolkit/Toolkit.Maui/BookmarksView/BookmarksView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,28 @@ namespace Esri.ArcGISRuntime.Toolkit.Maui;
/// </summary>
public class BookmarksView : TemplatedView
{
private ListView? _presentingView;
private BookmarksViewDataSource _dataSource = new BookmarksViewDataSource();
private readonly BookmarksViewDataSource _dataSource = new();
private const string _presentingViewName = "PresentingView";
CollectionView? _presentingView;
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved

private static readonly DataTemplate DefaultDataTemplate;
private static readonly ControlTemplate DefaultControlTemplate;

[DynamicDependency(nameof(Bookmark.Name), "Esri.ArcGISRuntime.Mapping.Bookmark", "Esri.ArcGISRuntime")]
static BookmarksView()
{
DefaultDataTemplate = new DataTemplate(() =>
{
var defaultCell = new TextCell();
defaultCell.SetBinding(TextCell.TextProperty, nameof(Bookmark.Name));
return defaultCell;
});

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 dataTemplate =
@"<DataTemplate xmlns=""http://schemas.microsoft.com/dotnet/2021/maui"">
<Label Text=""{Binding Name}"" />
</DataTemplate>";
DefaultDataTemplate = Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml(new DataTemplate(), dataTemplate);

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"" ItemTemplate=""{{TemplateBinding ItemTemplate}}"" />
</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,17 +65,15 @@ protected override void OnApplyTemplate()
{
base.OnApplyTemplate();

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

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

if (_presentingView != null)
if (GetTemplateChild(_presentingViewName) is CollectionView view)
{
_presentingView.ItemSelected += Internal_bookmarkSelected;
_presentingView.ItemTemplate = ItemTemplate;
_presentingView = view;
_presentingView.SelectionChanged += Internal_bookmarkSelected;
_presentingView.ItemsSource = _dataSource;
}
}
Expand Down Expand Up @@ -131,7 +127,7 @@ public GeoView? GeoView
/// Identifies the <see cref="ItemTemplate" /> bindable property.
/// </summary>
public static readonly BindableProperty ItemTemplateProperty =
BindableProperty.Create(nameof(ItemTemplate), typeof(DataTemplate), typeof(BookmarksView), DefaultDataTemplate, BindingMode.OneWay, null, propertyChanged: ItemTemplateChanged);
BindableProperty.Create(nameof(ItemTemplate), typeof(DataTemplate), typeof(BookmarksView), DefaultDataTemplate, BindingMode.OneWay, null);

/// <summary>
/// Handles property changes for the <see cref="BookmarksOverride" /> bindable property.
Expand All @@ -151,27 +147,6 @@ private static void GeoViewChanged(BindableObject sender, object? oldValue, obje
bookmarkView._dataSource.SetGeoView(newValue as GeoView);
}

/// <summary>
/// Handles property changes for the <see cref="ItemTemplate" /> bindable property.
/// </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
}
}

/// <summary>
/// Selects the bookmark and navigates to it in the associated <see cref="GeoView" />.
/// </summary>
Expand All @@ -192,16 +167,15 @@ 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)
{
SelectAndNavigateToBookmark(bm);
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved
}

if (e.SelectedItem != null && sender is ListView lv)
if (sender is CollectionView cv)
{
lv.SelectedItem = null;
if (cv.SelectedItem is Bookmark item)
{
SelectAndNavigateToBookmark(item);
}
cv.ClearValue(CollectionView.SelectedItemProperty);
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