1616using System . Reactive . Subjects ;
1717using System . Threading ;
1818using System . Linq ;
19+ using System . Collections . Specialized ;
1920
2021namespace GitHub . Collections
2122{
@@ -53,51 +54,7 @@ public static ObservableCollection<T> CreateListenerCollection<T>(this ITracking
5354 }
5455
5556 var col = new ObservableCollection < T > ( stickieItemsOnTop . Concat ( tcol ) ) ;
56- tcol . CollectionChanged += ( s , e ) =>
57- {
58- var offset = 0 ;
59- if ( stickieItemsOnTop != null )
60- {
61- foreach ( var item in stickieItemsOnTop )
62- {
63- if ( col . Contains ( item ) )
64- offset ++ ;
65- }
66- }
67-
68- if ( e . Action == System . Collections . Specialized . NotifyCollectionChangedAction . Move )
69- {
70- for ( int i = 0 , oldIdx = e . OldStartingIndex , newIdx = e . NewStartingIndex ;
71- i < e . OldItems . Count ; i ++ , oldIdx ++ , newIdx ++ )
72- {
73- col . Move ( oldIdx + offset , newIdx + offset ) ;
74- }
75- }
76- else if ( e . Action == System . Collections . Specialized . NotifyCollectionChangedAction . Add )
77- {
78- foreach ( T item in e . NewItems )
79- col . Add ( item ) ;
80- }
81- else if ( e . Action == System . Collections . Specialized . NotifyCollectionChangedAction . Remove )
82- {
83- foreach ( T item in e . OldItems )
84- col . Remove ( item ) ;
85- }
86- else if ( e . Action == System . Collections . Specialized . NotifyCollectionChangedAction . Replace )
87- {
88- for ( int i = 0 , idx = e . OldStartingIndex ; i < e . OldItems . Count ; i ++ , idx ++ )
89- col [ idx + offset ] = ( T ) e . NewItems [ i ] ;
90- }
91- else if ( e . Action == System . Collections . Specialized . NotifyCollectionChangedAction . Reset )
92- {
93- col . Clear ( ) ;
94- if ( stickieItemsOnTop != null )
95- {
96- foreach ( var item in stickieItemsOnTop )
97- col . Add ( item ) ;
98- }
99- }
100- } ;
57+ tcol . CollectionChanged += ( _ , e ) => UpdateStickieItems ( col , e , stickieItemsOnTop ) ;
10158 return col ;
10259 }
10360
@@ -121,25 +78,91 @@ public static ObservableCollection<T> CreateListenerCollection<T>(this ITracking
12178 Debug . Assert ( stickieItemOnTop != null , "stickieItemOnTop may not be null in CreateListenerCollection" ) ;
12279 Debug . Assert ( selection != null , "selection may not be null in CreateListenerCollection" ) ;
12380
124- var result = tcol . CreateListenerCollection ( new [ ] { stickieItemOnTop } ) ;
125- var addedStickieItem = true ;
81+ var stickieItems = new [ ] { stickieItemOnTop } ;
82+ var result = new ObservableCollection < T > ( tcol ) ;
83+ var addedStickieItem = false ;
84+ var hasSelection = false ;
85+
86+ tcol . CollectionChanged += ( _ , e ) =>
87+ {
88+ UpdateStickieItems ( result , e , hasSelection ? stickieItems : null ) ;
89+ } ;
12690
12791 selection . Subscribe ( x =>
12892 {
129- if ( ( x == null || object . Equals ( x , stickieItemOnTop ) ) && addedStickieItem )
93+ if ( x == null || object . Equals ( x , stickieItemOnTop ) )
13094 {
131- result . Remove ( stickieItemOnTop ) ;
132- addedStickieItem = false ;
95+ if ( addedStickieItem )
96+ {
97+ result . Remove ( stickieItemOnTop ) ;
98+ addedStickieItem = false ;
99+ }
100+
101+ hasSelection = false ;
133102 }
134- else if ( x != null && ! addedStickieItem )
103+ else
135104 {
136- result . Insert ( 0 , stickieItemOnTop ) ;
137- addedStickieItem = true ;
105+ if ( ! addedStickieItem )
106+ {
107+ result . Insert ( 0 , stickieItemOnTop ) ;
108+ addedStickieItem = true ;
109+ }
110+
111+ hasSelection = true ;
138112 }
139113 } ) ;
140114
141115 return result ;
142116 }
117+
118+ static void UpdateStickieItems < T > (
119+ ObservableCollection < T > col ,
120+ NotifyCollectionChangedEventArgs e ,
121+ IList < T > stickieItemsOnTop )
122+ {
123+ var offset = 0 ;
124+ if ( stickieItemsOnTop != null )
125+ {
126+ foreach ( var item in stickieItemsOnTop )
127+ {
128+ if ( col . Contains ( item ) )
129+ offset ++ ;
130+ }
131+ }
132+
133+ if ( e . Action == System . Collections . Specialized . NotifyCollectionChangedAction . Move )
134+ {
135+ for ( int i = 0 , oldIdx = e . OldStartingIndex , newIdx = e . NewStartingIndex ;
136+ i < e . OldItems . Count ; i ++ , oldIdx ++ , newIdx ++ )
137+ {
138+ col . Move ( oldIdx + offset , newIdx + offset ) ;
139+ }
140+ }
141+ else if ( e . Action == System . Collections . Specialized . NotifyCollectionChangedAction . Add )
142+ {
143+ foreach ( T item in e . NewItems )
144+ col . Add ( item ) ;
145+ }
146+ else if ( e . Action == System . Collections . Specialized . NotifyCollectionChangedAction . Remove )
147+ {
148+ foreach ( T item in e . OldItems )
149+ col . Remove ( item ) ;
150+ }
151+ else if ( e . Action == System . Collections . Specialized . NotifyCollectionChangedAction . Replace )
152+ {
153+ for ( int i = 0 , idx = e . OldStartingIndex ; i < e . OldItems . Count ; i ++ , idx ++ )
154+ col [ idx + offset ] = ( T ) e . NewItems [ i ] ;
155+ }
156+ else if ( e . Action == System . Collections . Specialized . NotifyCollectionChangedAction . Reset )
157+ {
158+ col . Clear ( ) ;
159+ if ( stickieItemsOnTop != null )
160+ {
161+ foreach ( var item in stickieItemsOnTop )
162+ col . Add ( item ) ;
163+ }
164+ }
165+ }
143166 }
144167
145168 /// <summary>
0 commit comments