Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Enable ScrollTo tests for Android, UWP; implement ScrollTo with group…
Browse files Browse the repository at this point in the history
… index for Android (#13007)

* Remove inaccurate category

* Enable ScrollTo tests for Android/UWP; Implement grouped ScrollTo for Android

* Implement missing ScrollTo grouped item by index

* Exempt 3788 test from UWP

* Ignore test on UWP

Co-authored-by: Rui Marinho <me@ruimarinho.net>
  • Loading branch information
hartez and rmarinho committed Jul 12, 2021
1 parent 056e30f commit ec1520e
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

namespace Xamarin.Forms.Controls
{
#if UITEST
[Category(UITestCategories.CarouselView)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 11113, "[Bug][iOS] Path: TranslateTransform has no effect on iOS", PlatformAffected.iOS)]
public partial class Issue11113 : TestContentPage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Xamarin.Forms.Controls.Issues
#if UITEST
[NUnit.Framework.Category(Core.UITests.UITestCategories.Github5000)]
[NUnit.Framework.Category(UITestCategories.ListView)]
[NUnit.Framework.Category(UITestCategories.UwpIgnore)] // Can't accurately check item contents in ListViews yet
#endif
public class Issue3788 : TestContentPage
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

namespace Xamarin.Forms.Controls.Issues
{

#if __WINDOWS__
[NUnit.Framework.Category(UITestCategories.UwpIgnore)] // Can't accurately check this test
#endif
#if UITEST
[Category(UITestCategories.CollectionView)]
#endif
Expand All @@ -27,7 +31,7 @@ protected override void Init()
#endif
}

#if UITEST && __IOS__ // Grouping for Android hasn't been merged yet
#if UITEST
[Test]
public void CanScrollToGroupAndItemIndex()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.ComponentModel;
using System.ComponentModel;
using Android.Content;

namespace Xamarin.Forms.Platform.Android
Expand Down Expand Up @@ -28,5 +27,20 @@ protected override TAdapter CreateAdapter()
{
return (TAdapter)new GroupableItemsViewAdapter<TItemsView, TItemsViewSource>(ItemsView);
}

protected override int DetermineTargetPosition(ScrollToRequestEventArgs args)
{
if (!ItemsView.IsGrouped || args.Mode == ScrollToMode.Element)
{
return base.DetermineTargetPosition(args);
}

if (ItemsViewAdapter.ItemsSource is IGroupedItemsPosition groupedItemsPosition)
{
return groupedItemsPosition.GetPosition(args.GroupIndex, args.Index);
}

return 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public interface IGroupableItemsViewSource : IItemsViewSource
bool IsGroupHeader(int position);
bool IsGroupFooter(int position);
}

public interface IGroupedItemsPosition
{
int GetPosition(int groupIndex, int itemIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,6 @@ protected virtual int DetermineTargetPosition(ScrollToRequestEventArgs args)
{
if (args.Mode == ScrollToMode.Position)
{
// TODO hartez 2018/08/28 15:40:03 Need to handle group indices here as well
return args.Index;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Xamarin.Forms.Platform.Android
{
internal class ObservableGroupedSource : IGroupableItemsViewSource, ICollectionChangedNotifier
internal class ObservableGroupedSource : IGroupableItemsViewSource, ICollectionChangedNotifier, IGroupedItemsPosition
{
readonly ICollectionChangedNotifier _notifier;
readonly IList _groupSource;
Expand Down Expand Up @@ -436,5 +436,38 @@ int CountItemsInGroups(int groupStartIndex, int groupCount)
}
return itemCount;
}

public int GetPosition(int groupIndex, int itemIndex)
{
// Ignore invalid indexes
if (groupIndex >= _groups.Count)
{
return 0;
}

if (itemIndex >= _groups[groupIndex].Count)
{
return 0;
}

int position = 0;

// Account for all the positions in the earlier groups;
// these counts will already include headers/footers, if present
for (int g = 0; g < groupIndex; g++)
{
position += _groups[g].Count;
}

position += itemIndex;

if (_hasGroupHeaders)
{
// Does this last group have a header? We'll need to account for that
position += 1;
}

return position;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Collections;
using System.ComponentModel;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

Expand Down Expand Up @@ -45,5 +46,39 @@ protected override void UpdateItemTemplate()

ListViewBase.GroupStyleSelector = new GroupHeaderStyleSelector();
}

protected override object FindBoundItem(ScrollToRequestEventArgs args)
{
if (!ItemsView.IsGrouped || args.Mode == ScrollToMode.Element)
{
return base.FindBoundItem(args);
}

var groups = CollectionViewSource.Source as GroupedItemTemplateCollection;

if (groups == null || args.GroupIndex >= groups.Count)
{
return null;
}

if (!(groups[args.GroupIndex].Items is IEnumerable group))
{
return null;
}

var index = args.Index;

foreach (var item in group)
{
if (index == 0)
{
return item;
}

index -= 1;
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ async void ScrollToRequested(object sender, ScrollToRequestEventArgs args)
await ScrollTo(args);
}

object FindBoundItem(ScrollToRequestEventArgs args)
protected virtual object FindBoundItem(ScrollToRequestEventArgs args)
{
if (args.Mode == ScrollToMode.Position)
{
Expand Down

0 comments on commit ec1520e

Please sign in to comment.