Skip to content

Commit

Permalink
Remove proxy class, use weakref
Browse files Browse the repository at this point in the history
  • Loading branch information
Redth committed Nov 28, 2023
1 parent 515ae01 commit 3fb3bd8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
21 changes: 5 additions & 16 deletions VirtualListView/Apple/CvCell.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public CvCell(CGRect frame) : base(frame)
this.ContentView.AddGestureRecognizer(new UITapGestureRecognizer(() => InvokeTap()));
}

public TapHandlerProxy TapHandler { get; set; }
public WeakReference<Action<CvCell>> TapHandler { get; set; }

WeakReference<UIKeyCommand[]> keyCommands;

Expand Down Expand Up @@ -55,7 +55,10 @@ public void KeyCommandSelect()
void InvokeTap()
{
if (PositionInfo.Kind == PositionKind.Item)
TapHandler?.Invoke(this);
{
if (TapHandler?.TryGetTarget(out var handler) ?? false)
handler?.Invoke(this);
}
}

public void UpdateSelected(bool selected)
Expand Down Expand Up @@ -133,18 +136,4 @@ public void SwapView(IView newView)
VirtualView.SetTarget(newView);
}
}

public class TapHandlerProxy
{
public TapHandlerProxy(Action<CvCell> tapHandler)
{
TapHandler = tapHandler;
}

public Action<CvCell> TapHandler { get; set; }

public void Invoke(CvCell cell)
=> TapHandler?.Invoke(cell);

}
}
2 changes: 1 addition & 1 deletion VirtualListView/Apple/CvDataSource.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override UICollectionViewCell GetCell(UICollectionView collectionView, NS
};

var cell = collectionView.DequeueReusableCell(nativeReuseId, indexPath) as CvCell;
cell.TapHandler = new CvCell.TapHandlerProxy(TapCellHandler);
cell.TapHandler = new WeakReference<Action<CvCell>>(TapCellHandler);
cell.Handler = Handler;
cell.IndexPath = new WeakReference<NSIndexPath>(indexPath);

Expand Down

0 comments on commit 3fb3bd8

Please sign in to comment.