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
86 changes: 42 additions & 44 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,8 +25,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 +36,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,8 +56,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 @@ -66,20 +69,7 @@ public BookmarksView()
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)
{
_presentingView.ItemSelected += Internal_bookmarkSelected;
_presentingView.ItemTemplate = ItemTemplate;
_presentingView.ItemsSource = _dataSource;
}
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved
UpdateListView();
}

/// <summary>
Expand Down Expand Up @@ -157,19 +147,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 All @@ -192,16 +170,36 @@ 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 (sender is CollectionView cv)
{
if (cv.SelectedItem is Bookmark item)
{
SelectAndNavigateToBookmark(item);
}
cv.ClearValue(CollectionView.SelectedItemProperty);
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved
}
}

private void UpdateListView()
{
if (e.SelectedItem is Bookmark bm)
var collection = (CollectionView)GetTemplateChild(_presentingViewName);
if (collection != null)
{
SelectAndNavigateToBookmark(bm);
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved
collection.SelectionChanged -= Internal_bookmarkSelected;
collection.SelectionChanged += Internal_bookmarkSelected;
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved
collection.ItemTemplate = null;
collection.ItemTemplate = ItemTemplate;
collection.ItemsSource = _dataSource;
}
}

if (e.SelectedItem != null && sender is ListView lv)
private void OnBookmarksCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action is NotifyCollectionChangedAction.Add)
{
lv.SelectedItem = null;
UpdateListView();
}
}
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
Loading