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

Modify Android and iOS adapters to handle null items sources #4536

Merged
merged 2 commits into from Nov 29, 2018
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Next

Modify Android and iOS adapters to handle null items sources; fixes #…

  • Loading branch information...
hartez committed Nov 26, 2018
commit 74795dcbf9bf71de333428a150106a1fcde3d692
@@ -16,6 +16,8 @@ public EmptyViewGallery()
Children =
{
descriptionLabel,
GalleryBuilder.NavButton("EmptyView (null ItemsSource)", () =>
new EmptyViewNullGallery(), Navigation),
GalleryBuilder.NavButton("EmptyView (String)", () =>
new EmptyViewStringGallery(), Navigation),
GalleryBuilder.NavButton("EmptyView (View)", () =>
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.EmptyViewGalleries.EmptyViewNullGallery">
<ContentPage.Content>
<CollectionView x:Name="CollectionView">
<CollectionView.ItemsLayout>
<GridItemsLayout Span="3" Orientation="Vertical"></GridItemsLayout>
</CollectionView.ItemsLayout>
<CollectionView.EmptyView>
Nothing to display.
</CollectionView.EmptyView>
</CollectionView>
</ContentPage.Content>
</ContentPage>
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Xamarin.Forms.Controls.GalleryPages.CollectionViewGalleries.EmptyViewGalleries
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class EmptyViewNullGallery : ContentPage
{
public EmptyViewNullGallery()
{
InitializeComponent();
}
}
}
@@ -48,7 +48,7 @@
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Xam.Plugin.DeviceInfo" Version="3.0.2" />
</ItemGroup>

<Target Name="CreateControllGalleryConfig" BeforeTargets="Build">
<CreateItem Include="blank.config">
<Output TaskParameter="Include" ItemName="ConfigFile" />
@@ -0,0 +1,11 @@
using System;

namespace Xamarin.Forms.Platform.Android
{
internal class EmptySource : IItemsViewSource
{
public int Count => 0;

public object this[int index] => throw new IndexOutOfRangeException("IItemsViewSource is empty");

This comment has been minimized.

Copy link
@mattleibow

mattleibow Nov 29, 2018

Contributor
Suggested change
public object this[int index] => throw new IndexOutOfRangeException("IItemsViewSource is empty");
public object this[int index] => throw new IndexOutOfRangeException("EmptySource is empty.");
}
}
@@ -9,6 +9,11 @@ internal static class ItemsSourceFactory
{
public static IItemsViewSource Create(IEnumerable itemsSource, RecyclerView.Adapter adapter)
{
if (itemsSource == null)
{
return new EmptySource();

This comment has been minimized.

Copy link
@StephaneDelcroix

StephaneDelcroix Nov 27, 2018

Member

you probably could return a shared instance, and avoid instantiation

}

switch (itemsSource)
{
case IList _ when itemsSource is INotifyCollectionChanged:
@@ -75,6 +75,7 @@
<Compile Include="BorderBackgroundManager.cs" />
<Compile Include="CollectionView\CarouselViewRenderer.cs" />
<Compile Include="CollectionView\DataChangeObserver.cs" />
<Compile Include="CollectionView\EmptySource.cs" />
<Compile Include="CollectionView\EmptyViewAdapter.cs" />
<Compile Include="CollectionView\ItemsViewAdapter.cs" />
<Compile Include="CollectionView\EdgeSnapHelper.cs" />
@@ -143,8 +143,12 @@ string DetermineCellReusedId()

UICollectionViewCell GetPrototype()
{
if (_itemsSource.Count == 0)
{
return null;
}

// TODO hartez assuming this works, we'll need to evaluate using this nsindexpath (what about groups?)
// TODO hartez Also, what about situations where there is no data which matches the path?
var indexPath = NSIndexPath.Create(0, 0);
return GetCell(CollectionView, indexPath);
}
@@ -13,7 +13,6 @@ public CarouselViewRenderer()
}
}

// TODO hartez 2018/05/30 08:58:42 This follows the same basic scheme as RecyclerView.Adapter; you should be able to reuse the same wrapper class for the IEnumerable
//// TODO hartez 2018/05/30 09:05:38 Think about whether this Controller and/or the new Adapter should be internal or public
public class CollectionViewRenderer : ViewRenderer<CollectionView, UIView>
{
@@ -0,0 +1,11 @@
using System;

namespace Xamarin.Forms.Platform.iOS
{
internal class EmptySource : IItemsViewSource
{
public int Count => 0;

public object this[int index] => null;
}
}
@@ -9,6 +9,11 @@ internal static class ItemsSourceFactory
{
public static IItemsViewSource Create(IEnumerable itemsSource, UICollectionView collectionView)
{
if (itemsSource == null)
{
return new EmptySource();
}

switch (itemsSource)
{
case IList _ when itemsSource is INotifyCollectionChanged:
@@ -114,6 +114,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Properties\AssemblyInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\CellExtensions.cs" />
<Compile Include="CADisplayLinkTicker.cs" />
<Compile Include="CollectionView\EmptySource.cs" />
<Compile Include="CollectionView\IItemsViewSource.cs" />
<Compile Include="CollectionView\ItemsSourceFactory.cs" />
<Compile Include="CollectionView\ItemsViewCell.cs" />
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.