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

Perf Improvements & Consistency #14

Merged
merged 5 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions VirtualListView/Apple/CvCell.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CoreGraphics;
using Foundation;
using Microsoft.Maui.Platform;
using Microsoft.VisualBasic;
using System.Diagnostics.CodeAnalysis;
using UIKit;

Expand All @@ -12,7 +13,7 @@ internal class CvCell : UICollectionViewCell

public WeakReference<NSIndexPath> IndexPath { get; set; }

public PositionInfo PositionInfo { get; set; }
public PositionInfo PositionInfo { get; private set; }

public WeakReference<Action<IView>> ReuseCallback { get; set; }

Expand Down Expand Up @@ -91,7 +92,10 @@ public override UICollectionViewLayoutAttributes PreferredLayoutAttributesFittin
}

public bool NeedsView
=> NativeView == null || !NativeView.TryGetTarget(out var _);
=> NativeView == null
|| VirtualView is null
|| !NativeView.TryGetTarget(out var _)
|| !VirtualView.TryGetTarget(out var _);

public WeakReference<IView> VirtualView { get; set; }

Expand All @@ -109,31 +113,33 @@ public override void PrepareForReuse()
}
}

public void SwapView(IView newView)
public void SetupView(IView view)
{
// Create a new platform native view if we don't have one yet
if (!(NativeView?.TryGetTarget(out var nativeView) ?? false))
if (!(NativeView?.TryGetTarget(out var _) ?? false))
{
nativeView = newView.ToPlatform(this.Handler.MauiContext);
var nativeView = view.ToPlatform(this.Handler.MauiContext);
nativeView.Frame = this.ContentView.Frame;
nativeView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth;
this.ContentView.AddSubview(nativeView);
NativeView = new WeakReference<UIView>(nativeView);

this.ContentView.AddSubview(nativeView);

NativeView = new WeakReference<UIView>(nativeView);
}

// Create a new virtual view if we don't have one yet
if (!(VirtualView?.TryGetTarget(out var virtualView) ?? false) || (virtualView?.Handler is null))
{
virtualView = newView;
VirtualView = new WeakReference<IView>(virtualView);
}
else
if (!(VirtualView?.TryGetTarget(out var virtualView) ?? false) || (virtualView?.Handler is null))
{
VirtualView = new WeakReference<IView>(view);
}
}

public void UpdatePosition(PositionInfo positionInfo)
{
PositionInfo = positionInfo;
if (VirtualView?.TryGetTarget(out var virtualView) ?? false)
{
var handler = virtualView.Handler;
virtualView.Handler = null;
newView.Handler = handler;
handler.SetVirtualView(newView);
VirtualView.SetTarget(newView);
}
if (virtualView is IPositionInfo viewPositionInfo)
viewPositionInfo.Update(positionInfo);
}
}
}
7 changes: 2 additions & 5 deletions VirtualListView/Apple/CvDataSource.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,13 @@ public override UICollectionViewCell GetCell(UICollectionView collectionView, NS
if (cell.NeedsView)
{
var view = Handler?.PositionalViewSelector?.ViewSelector?.CreateView(info, data);
cell.SwapView(view);
cell.SetupView(view);
}

cell.PositionInfo = info;
cell.UpdatePosition(info);

if (cell.VirtualView.TryGetTarget(out var cellVirtualView))
{
if (cellVirtualView is IPositionInfo viewPositionInfo)
viewPositionInfo.IsSelected = info.IsSelected;

Handler?.PositionalViewSelector?.ViewSelector?.RecycleView(info, data, cellVirtualView);

Handler.VirtualView.ViewSelector.ViewAttached(info, cellVirtualView);
Expand Down
10 changes: 7 additions & 3 deletions VirtualListView/Platforms/Android/RvAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi
_ => null
};

var view = itemHolder?.VirtualView ?? positionalViewSelector?.ViewSelector?.CreateView(info, data);
if (itemHolder.NeedsView)
{
var newView = positionalViewSelector?.ViewSelector?.CreateView(info, data);
itemHolder.SetupView(newView);
}

itemHolder.Update(info, view);
itemHolder.UpdatePosition(info);

positionalViewSelector?.ViewSelector?.RecycleView(info, data, itemHolder.ViewContainer.VirtualView);
positionalViewSelector?.ViewSelector?.RecycleView(info, data, itemHolder.ViewContainer.VirtualView);
}
}

Expand Down
28 changes: 19 additions & 9 deletions VirtualListView/Platforms/Android/RvItemHolder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Android.Views;
using AndroidX.RecyclerView.Widget;
using Microsoft.Maui.Controls;

namespace Microsoft.Maui;

Expand All @@ -22,18 +23,27 @@ public RvItemHolder(IMauiContext mauiContext, ListOrientation orientation)
public IView VirtualView
=> ViewContainer?.VirtualView;

public void Update(PositionInfo positionInfo, IView newView)
{
PositionInfo = positionInfo;
public bool NeedsView
=> ViewContainer?.NeedsView ?? true;

if (newView is IPositionInfo viewWithPositionInfo)
viewWithPositionInfo.Update(PositionInfo);

SwapView(newView);
public void SetupView(IView view)
{
ViewContainer.SetupView(view);
}

void SwapView(IView view)
public void UpdatePosition(PositionInfo positionInfo)
{
ViewContainer.SwapView(view);
PositionInfo = positionInfo;
ViewContainer.UpdatePosition(positionInfo);
}

//public void Update(PositionInfo positionInfo, IView newView)
//{
// PositionInfo = positionInfo;

// if (newView is IPositionInfo viewWithPositionInfo)
// viewWithPositionInfo.Update(PositionInfo);

// ViewContainer.SwapView(newView);
// }
}
27 changes: 15 additions & 12 deletions VirtualListView/Platforms/Android/RvViewContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ public RvViewContainer(IMauiContext context)

public AView NativeView { get; private set; }

public void SwapView(IView newView)
public bool NeedsView => VirtualView is null || VirtualView.Handler is null || NativeView is null;

public void UpdatePosition(IPositionInfo positionInfo)
{
if (VirtualView is IPositionInfo viewWithPositionInfo)
viewWithPositionInfo.Update(positionInfo);
}

public void SetupView(IView view)
{
if (VirtualView == null || VirtualView.Handler == null || NativeView == null)
if (NativeView is null)
NativeView = view.ToPlatform(MauiContext);

if (VirtualView is null)
{
NativeView = newView.ToPlatform(MauiContext);
VirtualView = newView;
VirtualView = view;
AddView(NativeView);
}
else
{
var handler = VirtualView.Handler;
newView.Handler = handler;
handler.SetVirtualView(newView);
VirtualView = newView;
}
}
}
}
36 changes: 15 additions & 21 deletions VirtualListView/Platforms/Windows/IrElementContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,27 @@ internal bool IsRecycled
}
}

public bool NeedsView
=> VirtualView is null || VirtualView.Handler is null;

public IView VirtualView { get; private set; }

public void Update(PositionInfo positionInfo, IView newView)
public void SetupView(IView view)
{
PositionInfo = positionInfo;

if (newView is IPositionInfo viewWithPositionInfo)
viewWithPositionInfo.Update(PositionInfo);

SwapView(newView);
if (VirtualView is null || VirtualView.Handler is null)
{
Content = view.ToPlatform(MauiContext);
VirtualView = view;
}
}

void SwapView(IView newView)
public void UpdatePosition(PositionInfo positionInfo)
{
if (VirtualView == null || VirtualView.Handler == null || Content == null)
{
Content = newView.ToPlatform(MauiContext);
VirtualView = newView;
}
else
{
var handler = VirtualView.Handler;
newView.Handler = handler;
handler.SetVirtualView(newView);
VirtualView = newView;
}
}
PositionInfo = positionInfo;

if (VirtualView is IPositionInfo viewWithPositionInfo)
viewWithPositionInfo.Update(PositionInfo);
}

protected override void OnTapped(TappedRoutedEventArgs e)
{
Expand Down
15 changes: 8 additions & 7 deletions VirtualListView/Platforms/Windows/IrElementFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,17 @@ public UIElement GetElement(UI.Xaml.ElementFactoryGetArgs args)
&& (Handler?.IsItemSelected(info.SectionIndex, info.ItemIndex) ?? false);


var view = container.VirtualView ?? PositionalViewSelector.ViewSelector?.CreateView(info, data);

container.Update(info, view);


if (container.NeedsView)
{
var virtualView = PositionalViewSelector.ViewSelector?.CreateView(info, data);
container.SetupView(virtualView);
}

container.UpdatePosition(info);
container.IsRecycled = false;
PositionalViewSelector.ViewSelector?.RecycleView(info, data, view);

PositionalViewSelector.ViewSelector?.ViewAttached(info, view);
PositionalViewSelector.ViewSelector?.RecycleView(info, data, container.VirtualView);
PositionalViewSelector.ViewSelector?.ViewAttached(info, container.VirtualView);

return container;
}
Expand Down
Loading