Skip to content

Commit

Permalink
Make iOS more consistent with android
Browse files Browse the repository at this point in the history
  • Loading branch information
Redth committed Nov 28, 2023
1 parent 6f2b93d commit 2c8fe23
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
34 changes: 18 additions & 16 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 @@ -109,31 +110,32 @@ 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))
{
nativeView = newView.ToPlatform(this.Handler.MauiContext);
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);
}

// 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 = view;
VirtualView = new WeakReference<IView>(virtualView);
}
}

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

0 comments on commit 2c8fe23

Please sign in to comment.