11using System . Collections . Concurrent ;
2- using System . Runtime . CompilerServices ;
3- using Microsoft . Maui . Controls ;
42
53namespace Stellar . Maui . Views ;
64
7- public class ReactiveListView : ListView
5+ public class ReactiveListView : ActivatableListView
86{
9- private readonly ConcurrentDictionary < Cell , IDisposable > _cellActivators = new ConcurrentDictionary < Cell , IDisposable > ( ) ;
10-
11- private Action < CompositeDisposable , Cell , int > ? _cellActivatedAction ;
12-
137 public ReactiveListView ( Type cellType , ListViewCachingStrategy cachingStrategy = ListViewCachingStrategy . RecycleElement )
148 : this ( cachingStrategy )
159 {
@@ -26,68 +20,4 @@ public ReactiveListView(ListViewCachingStrategy cachingStrategy = ListViewCachin
2620 : base ( cachingStrategy )
2721 {
2822 }
29-
30- public IDisposable SetCellActivationAction ( Action < CompositeDisposable , Cell , int > cellActivatedAction )
31- {
32- _cellActivatedAction = cellActivatedAction ;
33-
34- return Disposable . Create (
35- ( ) =>
36- {
37- _cellActivatedAction = null ;
38-
39- foreach ( var cellItem in _cellActivators )
40- {
41- cellItem . Value ? . Dispose ( ) ;
42- }
43-
44- _cellActivators . Clear ( ) ;
45- } ) ;
46- }
47-
48- protected override void SetupContent ( Cell content , int index )
49- {
50- base . SetupContent ( content , index ) ;
51-
52- if ( _cellActivatedAction is not null && ! _cellActivators . ContainsKey ( content ) )
53- {
54- var disposable = new CompositeDisposable ( ) ;
55- _cellActivatedAction ( disposable , content , index ) ;
56- _cellActivators . AddOrUpdate ( content , disposable , ( _ , _ ) => disposable ) ;
57- }
58- }
59-
60- protected override void UnhookContent ( Cell content )
61- {
62- if ( _cellActivators . ContainsKey ( content ) && _cellActivators . TryRemove ( content , out var disposable ) )
63- {
64- disposable ? . Dispose ( ) ;
65- }
66-
67- base . UnhookContent ( content ) ;
68- }
69-
70- protected override void OnHandlerChanging ( HandlerChangingEventArgs args )
71- {
72- if ( args . NewHandler is null || _cellActivators . IsEmpty )
73- {
74- return ;
75- }
76-
77- foreach ( var key in _cellActivators . Keys . ToArray ( ) )
78- {
79- if ( _cellActivators . TryRemove ( key , out var disposable ) )
80- {
81- disposable ? . Dispose ( ) ;
82- }
83- }
84- }
85- }
86-
87- public static class ReactiveListViewExtensions
88- {
89- public static IDisposable WhenCellActivated ( this ReactiveListView reactiveList , Action < CompositeDisposable , Cell , int > whenCellActivated )
90- {
91- return reactiveList . SetCellActivationAction ( whenCellActivated ) ;
92- }
9323}
0 commit comments