Skip to content

Commit 530b5e8

Browse files
committed
Add ActivatableListView with cell activation support
- Introduced a new class for enhanced list view functionality - Added support for activating cells with custom actions - Implemented disposal management for activated cells - Created an extension method for easier usage of cell activation
1 parent d12ed7a commit 530b5e8

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Collections.Concurrent;
2+
3+
namespace Stellar.Maui;
4+
5+
public class ActivatableListView : ListView
6+
{
7+
private readonly ConcurrentDictionary<Cell, IDisposable> _cellActivators = new();
8+
9+
private Action<CompositeDisposable, Cell, int> _cellActivatedAction;
10+
11+
public IDisposable SetCellActivationAction(Action<CompositeDisposable, Cell, int> cellActivatedAction)
12+
{
13+
_cellActivatedAction = cellActivatedAction;
14+
15+
return Disposable.Create(
16+
this,
17+
x =>
18+
{
19+
x._cellActivatedAction = null;
20+
21+
foreach (var cellItem in x._cellActivators)
22+
{
23+
cellItem.Value?.Dispose();
24+
}
25+
26+
x._cellActivators.Clear();
27+
});
28+
}
29+
30+
protected override void SetupContent(Cell content, int index)
31+
{
32+
base.SetupContent(content, index);
33+
34+
if (_cellActivatedAction == null || _cellActivators.ContainsKey(content))
35+
{
36+
return;
37+
}
38+
39+
var disposable = new CompositeDisposable();
40+
_cellActivatedAction(disposable, content, index);
41+
_cellActivators.AddOrUpdate(content, disposable, (k, v) => disposable);
42+
}
43+
44+
protected override void UnhookContent(Cell content)
45+
{
46+
if (_cellActivators.TryRemove(content, out var disposable))
47+
{
48+
disposable?.Dispose();
49+
}
50+
51+
base.UnhookContent(content);
52+
}
53+
}
54+
55+
public static class ActivatableListViewExtensions
56+
{
57+
public static IDisposable WhenCellActivated(this ActivatableListView reactiveList, Action<CompositeDisposable, Cell, int> whenCellActivated)
58+
{
59+
return reactiveList.SetCellActivationAction(whenCellActivated);
60+
}
61+
}

0 commit comments

Comments
 (0)