Example #1
0
 public BookmarksTableSource(BookmarksViewDataSource dataSource)
 {
     _bookmarks = dataSource;
     if (_bookmarks is INotifyCollectionChanged incc)
     {
         var listener = new Internal.WeakEventListener <INotifyCollectionChanged, object, NotifyCollectionChangedEventArgs>(incc);
         listener.OnEventAction  = (instance, source, eventArgs) => CollectionChanged?.Invoke(this, eventArgs);
         listener.OnDetachAction = (instance, weakEventListener) => instance.CollectionChanged -= weakEventListener.OnEvent;
         incc.CollectionChanged += listener.OnEvent;
     }
 }
        internal BookmarksAdapter(Context?context, BookmarksViewDataSource dataSource)
        {
            _context    = context;
            _dataSource = dataSource;
            _shadowList = dataSource.ToList();

            var listener = new Internal.WeakEventListener <INotifyCollectionChanged, object, NotifyCollectionChangedEventArgs>(dataSource)
            {
                OnEventAction = (instance, source, eventArgs) =>
                {
                    switch (eventArgs.Action)
                    {
                    case NotifyCollectionChangedAction.Add:
                        _shadowList.InsertRange(eventArgs.NewStartingIndex, eventArgs.NewItems.OfType <Bookmark>());
                        NotifyItemInserted(eventArgs.NewStartingIndex);
                        break;

                    case NotifyCollectionChangedAction.Remove:
                        _shadowList.RemoveRange(eventArgs.OldStartingIndex, eventArgs.OldItems.Count);
                        NotifyItemRemoved(eventArgs.OldStartingIndex);
                        break;

                    case NotifyCollectionChangedAction.Reset:
                        _shadowList = _dataSource.ToList();
                        NotifyDataSetChanged();
                        break;

                    case NotifyCollectionChangedAction.Move:
                        _shadowList = _dataSource.ToList();
                        NotifyDataSetChanged();
                        break;

                    case NotifyCollectionChangedAction.Replace:
                        _shadowList[eventArgs.OldStartingIndex] = (Bookmark)eventArgs.NewItems[0];
                        NotifyItemChanged(eventArgs.OldStartingIndex);
                        break;
                    }
                },
                OnDetachAction = (instance, weakEventListener) => instance.CollectionChanged -= weakEventListener.OnEvent,
            };

            dataSource.CollectionChanged += listener.OnEvent;
        }