Skip to content

Commit

Permalink
Refactor BookmarksView for simplicity and performance
Browse files Browse the repository at this point in the history
  • Loading branch information
prathameshnarkhede committed Sep 4, 2024
1 parent e71bfd7 commit 1f2c020
Showing 1 changed file with 11 additions and 31 deletions.
42 changes: 11 additions & 31 deletions src/Toolkit/Toolkit.Maui/BookmarksView/BookmarksView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Esri.ArcGISRuntime.Toolkit.Maui;
/// </summary>
public class BookmarksView : TemplatedView
{
private BookmarksViewDataSource _dataSource = new BookmarksViewDataSource();
private readonly BookmarksViewDataSource _dataSource = new();
private const string _presentingViewName = "PresentingView";

private static readonly DataTemplate DefaultDataTemplate;
Expand All @@ -33,18 +33,17 @@ public class BookmarksView : TemplatedView
[DynamicDependency(nameof(Bookmark.Name), "Esri.ArcGISRuntime.Mapping.Bookmark", "Esri.ArcGISRuntime")]
static BookmarksView()
{
DefaultDataTemplate = new DataTemplate(() =>
{
var defaultLabel = new Label();
defaultLabel.SetBinding(Label.TextProperty, nameof(Bookmark.Name));
return defaultLabel;
});
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"" />
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 @@ -70,8 +69,8 @@ protected override void OnApplyTemplate()
{
view.SelectionChanged -= Internal_bookmarkSelected;
view.SelectionChanged += Internal_bookmarkSelected;
view.ItemsSource = _dataSource;
}
UpdatePresentingView();
}

/// <summary>
Expand Down Expand Up @@ -123,7 +122,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 @@ -143,15 +142,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 bookmarksView = (BookmarksView)sender;
bookmarksView.UpdatePresentingView();
}

/// <summary>
/// Selects the bookmark and navigates to it in the associated <see cref="GeoView" />.
/// </summary>
Expand Down Expand Up @@ -184,16 +174,6 @@ private void Internal_bookmarkSelected(object? sender, SelectionChangedEventArgs
}
}

private void UpdatePresentingView()
{
var collectionView = (CollectionView)GetTemplateChild(_presentingViewName);
if (collectionView is CollectionView view)
{
view.ItemTemplate = ItemTemplate;
view.ItemsSource = _dataSource;
}
}

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

0 comments on commit 1f2c020

Please sign in to comment.