Skip to content

Commit

Permalink
More Compositional
Browse files Browse the repository at this point in the history
  • Loading branch information
Redth committed Jun 10, 2024
1 parent c10986e commit 3457ce8
Show file tree
Hide file tree
Showing 21 changed files with 301 additions and 415 deletions.
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET MAUI",
"type": "maui",
"request": "launch",
"preLaunchTask": "maui: Build"
}
]
}
4 changes: 0 additions & 4 deletions Sample/VirtualListViewSample/ObservableCollectionPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
<Label HorizontalOptions="Center" VerticalOptions="Center" Text="EMPTY!!!" />
</Grid>
</vlv:VirtualListView.EmptyView>
<vlv:VirtualListView.GlobalHeader>
<Label Text="HEADER"/>

</vlv:VirtualListView.GlobalHeader>
<vlv:VirtualListView.ItemTemplate>
<DataTemplate>
<vlv:VirtualViewCell>
Expand Down
16 changes: 8 additions & 8 deletions Sample/VirtualListViewSample/ObservableCollectionPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ protected override void OnAppearing()
{
base.OnAppearing();

Task.Delay(1000).ContinueWith(t =>
{
this.Dispatcher.Dispatch(() =>
{
Items.Add("Item 11");
Items.Add("Item 12");
});
});
// Task.Delay(1000).ContinueWith(t =>
// {
// this.Dispatcher.Dispatch(() =>
// {
// Items.Add("Item 11");
// Items.Add("Item 12");
// });
// });
}
private void Button_Clicked(object sender, EventArgs e)
{
Expand Down
43 changes: 42 additions & 1 deletion VirtualListView/Adapters/IVirtualListViewAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,45 @@
namespace Microsoft.Maui.Adapters;

namespace Microsoft.Maui.Adapters;

internal class EmptyViewSelector : IVirtualListViewSelector
{
public IView CreateView(PositionInfo position, object data)
=> null;

public string GetReuseId(PositionInfo position, object data)
=> "ITEM";

public void RecycleView(PositionInfo position, object data, IView view)
{
}

public bool SectionHasFooter(int sectionIndex)
=> false;

public bool SectionHasHeader(int sectionIndex)
=> false;
}

internal class EmptyAdapter : IVirtualListViewAdapter
{
public int GetNumberOfSections()
=> 0;

public object GetSection(int sectionIndex)
=> null;

public int GetNumberOfItemsInSection(int sectionIndex)
=> 0;

public object GetItem(int sectionIndex, int itemIndex)
=> null;

public event EventHandler OnDataInvalidated;

Check warning on line 37 in VirtualListView/Adapters/IVirtualListViewAdapter.cs

View workflow job for this annotation

GitHub Actions / Build

The event 'EmptyAdapter.OnDataInvalidated' is never used

public void InvalidateData()
{
}
}

public interface IVirtualListViewAdapter
{
Expand Down
3 changes: 3 additions & 0 deletions VirtualListView/Adapters/ObservableCollectionAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ void CollectionChanged(object sender, System.Collections.Specialized.NotifyColle
public override TItem GetItem(int sectionIndex, int itemIndex)
=> Items[itemIndex];

public override int GetNumberOfSections()
=> 1;

public override int GetNumberOfItemsInSection(int sectionIndex)
=> Items.Count;

Expand Down
6 changes: 3 additions & 3 deletions VirtualListView/Apple/CvCell.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ public override UICollectionViewLayoutAttributes PreferredLayoutAttributesFittin

var preferredAttributes = base.PreferredLayoutAttributesFittingAttributes(layoutAttributes);

if (Handler?.VirtualView is not null)
if (VirtualView.TryGetTarget(out var virtualView))
{
if (Handler.VirtualView.Orientation == ListOrientation.Vertical)
{
var measure =
Handler.VirtualView.Measure(preferredAttributes.Size.Width, double.PositiveInfinity);
virtualView.Measure(preferredAttributes.Size.Width, double.PositiveInfinity);

preferredAttributes.Frame =
new CGRect(preferredAttributes.Frame.X, preferredAttributes.Frame.Y,
Expand All @@ -110,7 +110,7 @@ public override UICollectionViewLayoutAttributes PreferredLayoutAttributesFittin
else
{
var measure =
Handler.VirtualView.Measure(double.PositiveInfinity, preferredAttributes.Size.Height);
virtualView.Measure(double.PositiveInfinity, preferredAttributes.Size.Height);

preferredAttributes.Frame =
new CGRect(preferredAttributes.Frame.X, preferredAttributes.Frame.Y,
Expand Down
81 changes: 29 additions & 52 deletions VirtualListView/Apple/CvDataSource.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,18 @@ public CvDataSource(VirtualListViewHandler handler)
readonly ReusableIdManager sectionFooterIdManager = new ReusableIdManager("SectionFooter", new NSString("SectionFooter"));

public override nint NumberOfSections(UICollectionView collectionView)
=> 1;

=> Handler.Adapter.GetNumberOfSections();

public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
var info = PositionInfo.ForItem();

object? data = null;

var nativeReuseId = CvCell.ReuseIdUnknown;
var sectionIndex = indexPath.Section;
var itemIndex = indexPath.Item.ToInt32();
var info = Handler.PlatformController.GetInfo(sectionIndex, itemIndex);

object? data = Handler.Adapter.DataFor(PositionKind.Item, sectionIndex, itemIndex);
var reuseId = Handler.VirtualView.ViewSelector?.GetReuseId(info, data);
var nativeReuseId = itemIdManager.GetReuseId(collectionView, reuseId);

if (info is not null)
{
data = Handler?.Adapter?.DataFor(info.Kind, info.SectionIndex, info.ItemIndex);

nativeReuseId = info.Kind switch
{
var reuseId = Handler?.VirtualView?.ViewSelector?.GetReuseId(info, data);

nativeReuseId = info.Kind switch
{
PositionKind.Item => itemIdManager.GetReuseId(collectionView, reuseId),
PositionKind.SectionHeader => sectionHeaderIdManager.GetReuseId(collectionView, reuseId),
PositionKind.SectionFooter => sectionFooterIdManager.GetReuseId(collectionView, reuseId),
PositionKind.Header => globalIdManager.GetReuseId(collectionView, reuseId),
PositionKind.Footer => globalIdManager.GetReuseId(collectionView, reuseId),
_ => CvCell.ReuseIdUnknown,
};
}
}

var nativeCell = collectionView.DequeueReusableCell(nativeReuseId, indexPath);
if (nativeCell is not CvCell cell)
return (UICollectionViewCell)nativeCell;
Expand All @@ -62,50 +43,48 @@ public override UICollectionViewCell GetCell(UICollectionView collectionView, NS
cell.ReuseCallback = new WeakReference<Action<IView>>((rv) =>
{
if (info is not null && (cell.VirtualView?.TryGetTarget(out var cellView) ?? false))
Handler?.VirtualView?.ViewSelector?.ViewDetached(info, cellView);
Handler.VirtualView.ViewSelector?.ViewDetached(info, cellView);
});

if (info is not null)
{
if (info.SectionIndex < 0 || info.ItemIndex < 0)
info.IsSelected = false;
else
info.IsSelected = Handler?.IsItemSelected(info.SectionIndex, info.ItemIndex) ?? false;
info.IsSelected = Handler.IsItemSelected(info.SectionIndex, info.ItemIndex);

if (cell.NeedsView)
{
var view = Handler?.PositionalViewSelector?.ViewSelector?.CreateView(info, data);
var view = Handler.ViewSelector?.CreateView(info, data);
if (view is not null)
cell.SetupView(view);
}

if (cell.NeedsView)
{
var view = Handler.VirtualView.ViewSelector?.CreateView(info, data);
if (view is not null)
cell.SetupView(view);
}

if (cell.NeedsView && info is not null && data is not null)
{
var view = Handler?.VirtualView?.ViewSelector?.CreateView(info, data);
if (view is not null)
cell.SetupView(view);
}

if (info is not null)
{
cell.UpdatePosition(info);

if (cell.VirtualView?.TryGetTarget(out var cellVirtualView) ?? false)
if (cell.VirtualView.TryGetTarget(out var cellVirtualView))
{
Handler?.VirtualView?.ViewSelector?.RecycleView(info, data, cellVirtualView);
Handler.VirtualView.ViewSelector?.RecycleView(info, data, cellVirtualView);

Handler?.VirtualView?.ViewSelector?.ViewAttached(info, cellVirtualView);
Handler.VirtualView.ViewSelector?.ViewAttached(info, cellVirtualView);
}
}

return cell;
}

public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind,
NSIndexPath indexPath)
{
return base.GetViewForSupplementaryElement(collectionView, elementKind, indexPath);
}
// public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, NSString elementKind,
// NSIndexPath indexPath)
// {
// return base.GetViewForSupplementaryElement(collectionView, elementKind, indexPath);
// }

void TapCellHandler(CvCell cell)
{
Expand All @@ -114,13 +93,11 @@ void TapCellHandler(CvCell cell)
cell.PositionInfo.IsSelected = !cell.PositionInfo.IsSelected;

if (cell.PositionInfo.IsSelected)
Handler?.VirtualView?.SelectItem(p);
Handler.VirtualView.SelectItem(p);
else
Handler?.VirtualView?.DeselectItem(p);
Handler.VirtualView.DeselectItem(p);
}

public override nint GetItemsCount(UICollectionView collectionView, nint section)
{
return Handler?.PositionalViewSelector?.TotalCount ?? 0;
}
=> Handler.Adapter.GetNumberOfItemsInSection(section.ToInt32());
}
118 changes: 0 additions & 118 deletions VirtualListView/Apple/CvLayout.ios.maccatalyst.cs

This file was deleted.

Loading

0 comments on commit 3457ce8

Please sign in to comment.