Skip to content

Commit

Permalink
BugFix: Fixes BasemapGallery View Style in MAUI Windows (#596)
Browse files Browse the repository at this point in the history
* MAUI Windows :
Enabled Grid View for BasemapGallery in MAUI Windows.
Changed BasemaGallery's CollectionView ItemLayout to fixed GridItemsLayout and manipulated span and spacing values with BasemapGallery View Style.

* Refactor ItemsLayout update logic in BasemapGallery.
Includes a specific workaround for iOS to avoid a `NullReferenceException` when changing the span of `GridItemsLayout`.
  • Loading branch information
prathameshnarkhede authored Aug 23, 2024
1 parent 95d2781 commit 8f5c2c5
Showing 1 changed file with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static BasemapGallery()
Border border = new Border { Padding = 4, StrokeThickness = 1, StrokeShape = new Rectangle() };
Grid outerScrimContainer = new Grid();
outerScrimContainer.RowDefinitions.Add(new RowDefinition { Height = new GridLength(86) });
outerScrimContainer.RowDefinitions.Add(new RowDefinition { Height = new GridLength(40)});
outerScrimContainer.RowDefinitions.Add(new RowDefinition { Height = new GridLength(40) });
outerScrimContainer.RowSpacing = 4;

Image thumbnail = new Image { WidthRequest = 64, HeightRequest = 64, Aspect = Aspect.AspectFill, BackgroundColor = Colors.Transparent, HorizontalOptions = LayoutOptions.Center };
Expand All @@ -61,9 +61,9 @@ static BasemapGallery()
Grid scrimGrid = new Grid();
scrimGrid.SetAppThemeColor(BackgroundColorProperty, Colors.White, Colors.Black);
outerScrimContainer.Children.Add(scrimGrid);
Grid.SetRowSpan(scrimGrid, 2);

Grid.SetRowSpan(scrimGrid, 2);


thumbnail.SetBinding(Image.SourceProperty, nameof(BasemapGalleryItem.ThumbnailData), converter: ImageSourceConverter);
nameLabel.SetBinding(Label.TextProperty, nameof(BasemapGalleryItem.Name));
scrimGrid.SetBinding(OpacityProperty, nameof(BasemapGalleryItem.IsValid), mode: BindingMode.OneWay, converter: OpacityConverter);
Expand Down Expand Up @@ -194,7 +194,8 @@ protected override void OnApplyTemplate()
_loadingScrim = GetTemplateChild("PART_LoadingScrim") as View;
if (ListView != null)
{
ListView.BindingContext = _controller;
ListView.BindingContext = _controller;
ListView.ItemsLayout = new GridItemsLayout(ItemsLayoutOrientation.Vertical);
}
}

Expand Down Expand Up @@ -280,14 +281,8 @@ private void HandleTemplateChange(double currentWidth)
case BasemapGalleryViewStyle.Automatic:
styleAfterUpdate = currentWidth >= ViewStyleWidthThreshold ? BasemapGalleryViewStyle.Grid : BasemapGalleryViewStyle.List;
break;
}

// This check may be removable once UWP collectionview supports dynamic item sizing: https://gist.github.com/hartez/7d0edd4182dbc7de65cebc6c67f72e14
if (DeviceInfo.Platform == DevicePlatform.WinUI)
{
styleAfterUpdate = BasemapGalleryViewStyle.List;
}

}

if (styleAfterUpdate == BasemapGalleryViewStyle.Grid)
{
gridSpanAfterUpdate = System.Math.Max((int)(currentWidth / 128), 1);
Expand All @@ -298,13 +293,13 @@ private void HandleTemplateChange(double currentWidth)
if (styleAfterUpdate == BasemapGalleryViewStyle.List)
{
ListView.ItemTemplate = ListItemTemplate;
ListView.ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Vertical) { ItemSpacing = 0 };
UpdateItemsLayout(1, 0, 0);
ListView.Margin = new Thickness(0);
}
else
{
ListView.ItemTemplate = GridItemTemplate;
ListView.ItemsLayout = new GridItemsLayout(gridSpanAfterUpdate, ItemsLayoutOrientation.Vertical) { HorizontalItemSpacing = 4, VerticalItemSpacing = 4 };
ListView.ItemTemplate = DefaultGridDataTemplate;
UpdateItemsLayout(gridSpanAfterUpdate, 4, 4);
ListView.Margin = new Thickness(4, 4, 0, 0);
}

Expand All @@ -313,6 +308,31 @@ private void HandleTemplateChange(double currentWidth)
}
}

private void UpdateItemsLayout(int span, double verticalSpacing, double horizontalSpacing)
{
if (ListView is not null)
{
#if __IOS__
// This is a workaround for a bug in the current version of the iOS renderer for CollectionView
// where CollectionView throws `NullReferneceException` on changing span of GridItemsLayout.
// It won't be needed once we move MAUI version up to 8.0.10+.
// Will have to consider if toolkit can require a newer version (currently it's fixed to API).
ListView.ItemsLayout = new GridItemsLayout(span, ItemsLayoutOrientation.Vertical)
{
VerticalItemSpacing = verticalSpacing,
HorizontalItemSpacing = horizontalSpacing,
};
#else
if (ListView.ItemsLayout is GridItemsLayout layout)
{
layout.Span = span;
layout.VerticalItemSpacing = verticalSpacing;
layout.HorizontalItemSpacing = horizontalSpacing;
}
#endif
}
}

/// <inheritdoc />
protected override void OnSizeAllocated(double width, double height)
{
Expand Down

0 comments on commit 8f5c2c5

Please sign in to comment.