void SetObserveChanges(IItemsViewSource itemsSource, bool enable)
 {
     if (itemsSource is IObservableItemsViewSource observableSource)
     {
         observableSource.ObserveChanges = enable;
     }
 }
Esempio n. 2
0
        public async Task GetPositionConsistentOnUIThread()
        {
            var notifier = new MockCollectionChangedNotifier();
            var source   = new ObservableCollection <string>
            {
                "zero",
                "one",
                "two"
            };

            IItemsViewSource ois = ItemsSourceFactory.Create(source, Application.Current, notifier);

            int positionBeforeNotify = -1;

            // Add an item from a threadpool thread
            await Task.Run(() =>
            {
                source.Insert(0, "foo");

                // Post a check ahead of the queued update on the main thread
                _handler.PostAtFrontOfQueue(() => positionBeforeNotify = ois.GetPosition("zero"));
            });

            // Check the result on the main thread
            var onMainThreadGetItem = await Application.Current.Dispatcher.DispatchAsync(() => ois.GetPosition("zero"));

            Assert.That(positionBeforeNotify, Is.EqualTo(0));
            Assert.That(onMainThreadGetItem, Is.EqualTo(1));
            Assert.That(notifier.InsertCount, Is.EqualTo(1));
        }
Esempio n. 3
0
 public void NotifyItemRangeRemoved(IItemsViewSource source, int startIndex, int count)
 {
     if (IsValidAdapter())
     {
         _adapter.NotifyItemRangeRemoved(startIndex, count);
     }
 }
Esempio n. 4
0
        void EmptyCollectionViewReloadWorkaround()
        {
            var enumerator = _itemsView.ItemsSource.GetEnumerator();

            if (!enumerator.MoveNext())
            {
                // The source we're updating to is empty, so we can just update as normal; it won't crash
                UpdateItemsSourceAndReload();
            }
            else
            {
                // Grab the first item from the new ItemsSource and create a usable source for the UICollectionView
                // from that
                var firstItem = new List <object> {
                    enumerator.Current
                };
                _itemsSource = ItemsSourceFactory.Create(firstItem, CollectionView);

                // Insert that item into the UICollectionView
                // TODO ezhart When we implement grouping, this will need to be the index of the first actual item
                // Which might not be zero,zero if we have empty groups
                var indexesToInsert = new NSIndexPath[1] {
                    NSIndexPath.Create(0, 0)
                };

                UIView.PerformWithoutAnimation(() =>
                {
                    CollectionView.InsertItems(indexesToInsert);
                });

                // Okay, from now on we can just call ReloadData and things will work fine
                _safeForReload = true;
                UpdateItemsSource();
            }
        }
Esempio n. 5
0
        public ItemsViewController(ItemsView itemsView, ItemsViewLayout layout) : base(layout)
        {
            _itemsView   = itemsView;
            _itemsSource = ItemsSourceFactory.Create(_itemsView.ItemsSource, CollectionView);

            UpdateLayout(layout);
        }
Esempio n. 6
0
 public void NotifyItemRangeChanged(IItemsViewSource source, int start, int end)
 {
     if (IsValidAdapter())
     {
         _adapter.NotifyItemRangeChanged(start, end);
     }
 }
Esempio n. 7
0
 public void NotifyItemChanged(IItemsViewSource source, int startIndex)
 {
     if (IsValidAdapter())
     {
         _adapter.NotifyItemChanged(startIndex);
     }
 }
Esempio n. 8
0
        public async Task AddItemCountConsistentOnUIThread()
        {
            var notifier         = new MockCollectionChangedNotifier();
            var source           = new ObservableCollection <int>();
            IItemsViewSource ois = ItemsSourceFactory.Create(source, notifier);

            int countBeforeNotify = -1;

            // Add an item from a threadpool thread
            await Task.Run(() => {
                source.Add(1);

                // Post a check ahead of the queued update on the main thread
                _handler.PostAtFrontOfQueue(() => {
                    countBeforeNotify = ois.Count;
                });
            });

            // Check the result on the main thread
            var onMainThreadCount = await Device.InvokeOnMainThreadAsync(() => ois.Count);

            Assert.That(countBeforeNotify, Is.EqualTo(0), "Count should still be reporting no items before the notify resolves");
            Assert.That(onMainThreadCount, Is.EqualTo(1));
            Assert.That(notifier.InsertCount, Is.EqualTo(1), "Should have recorded exactly one Add");
        }
Esempio n. 9
0
 public void NotifyItemMoved(IItemsViewSource source, int fromPosition, int toPosition)
 {
     if (IsValidAdapter())
     {
         _adapter.NotifyItemMoved(fromPosition, toPosition);
     }
 }
Esempio n. 10
0
 void UnsubscribeCollectionItemsSourceChanged(IItemsViewSource oldItemsSource)
 {
     if (oldItemsSource is ObservableItemsSource oldObservableItemsSource)
     {
         oldObservableItemsSource.CollectionItemsSourceChanged -= CollectionItemsSourceChanged;
     }
 }
Esempio n. 11
0
        public async Task GetItemConsistentOnUIThread()
        {
            var notifier = new MockCollectionChangedNotifier();
            var source   = new ObservableCollection <string>
            {
                "zero",
                "one",
                "two"
            };

            IItemsViewSource ois = ItemsSourceFactory.Create(source, notifier);

            string itemAtPosition2BeforeNotify = string.Empty;

            // Add an item from a threadpool thread
            await Task.Run(() => {
                source.Insert(0, "foo");

                // Post a check ahead of the queued update on the main thread
                _handler.PostAtFrontOfQueue(() => itemAtPosition2BeforeNotify = (string)ois.GetItem(2));
            });

            // Check the result on the main thread
            var onMainThreadGetItem = await Device.InvokeOnMainThreadAsync(() => (string)ois.GetItem(2));

            Assert.That(itemAtPosition2BeforeNotify, Is.EqualTo("two"));
            Assert.That(onMainThreadGetItem, Is.EqualTo("one"));
            Assert.That(notifier.InsertCount, Is.EqualTo(1));
        }
Esempio n. 12
0
        public async Task RemoveitemCountConsistentOnUIThread()
        {
            var notifier = new MockCollectionChangedNotifier();
            var source   = new ObservableCollection <int> {
                1
            };
            IItemsViewSource ois = ItemsSourceFactory.Create(source, notifier);

            int countBeforeNotify = -1;

            // Remove an item from a threadpool thread
            await Task.Run(() =>
            {
                source.Remove(1);

                // Post a check ahead of the queued update on the main thread
                _handler.PostAtFrontOfQueue(() => countBeforeNotify = ois.Count);
            });

            // Check the result on the main thread
            var onMainThreadCount = await Device.InvokeOnMainThreadAsync(() => ois.Count);

            Assert.That(countBeforeNotify, Is.EqualTo(1));
            Assert.That(onMainThreadCount, Is.EqualTo(0));
            Assert.That(notifier.RemoveCount, Is.EqualTo(1));
        }
Esempio n. 13
0
 void SubscribeCollectionItemsSourceChanged(IItemsViewSource itemsSource)
 {
     if (itemsSource is ObservableItemsSource newItemsSource)
     {
         newItemsSource.CollectionItemsSourceChanged += CollectionItemsSourceChanged;
     }
 }
        public CollectionViewController(ItemsView itemsView, ItemsViewLayout layout) : base(layout)
        {
            _itemsView   = itemsView;
            _itemsSource = ItemsSourceFactory.Create(_itemsView.ItemsSource, CollectionView);
            _layout      = layout;

            _layout.GetPrototype = GetPrototype;
            _layout.UniformSize  = false;            // todo hartez Link this to ItemsView.ItemSizingStrategy hint
        }
Esempio n. 15
0
        public void NotifyItemRemoved(IItemsViewSource source, int startIndex)
        {
            if (IsValidAdapter())
            {
                _adapter.NotifyItemRemoved(startIndex);

                var changedCount = _adapter.ItemCount - startIndex;
                _adapter.NotifyItemRangeChanged(startIndex, changedCount);
            }
        }
Esempio n. 16
0
        public ItemsViewController(ItemsView itemsView, ItemsViewLayout layout) : base(layout)
        {
            _itemsView   = itemsView;
            _itemsSource = ItemsSourceFactory.Create(_itemsView.ItemsSource, CollectionView);

            // If we already have data, the UICollectionView will have items and we'll be safe to call
            // ReloadData if the ItemsSource changes in the future (see UpdateItemsSource for more).
            _safeForReload = _itemsSource?.Count > 0;

            UpdateLayout(layout);
        }
Esempio n. 17
0
        public void NotifyItemMoved(IItemsViewSource source, int fromPosition, int toPosition)
        {
            if (IsValidAdapter())
            {
                _adapter.NotifyItemMoved(fromPosition, toPosition);

                var minPosition  = System.Math.Min(fromPosition, toPosition);
                var changedCount = _adapter.ItemCount - minPosition;
                _adapter.NotifyItemRangeChanged(minPosition, changedCount);
            }
        }
Esempio n. 18
0
        internal ItemsViewAdapter(ItemsView itemsView, Func <View, Context, ItemContentView> createItemContentView = null)
        {
            CollectionView.VerifyCollectionViewFlagEnabled(nameof(ItemsViewAdapter));

            ItemsView = itemsView;
            _createItemContentView = createItemContentView;
            ItemsSource            = ItemsSourceFactory.Create(itemsView.ItemsSource, this);

            if (_createItemContentView == null)
            {
                _createItemContentView = (view, context) => new ItemContentView(context);
            }
        }
Esempio n. 19
0
        internal ItemsViewAdapter(ItemsView itemsView, Func <IVisualElementRenderer, Context, AView> createView = null)
        {
            CollectionView.VerifyCollectionViewFlagEnabled(nameof(ItemsViewAdapter));

            ItemsView    = itemsView;
            _createView  = createView;
            _itemsSource = ItemsSourceFactory.Create(itemsView.ItemsSource, this);

            if (_createView == null)
            {
                _createView = (renderer, context) => new ItemContentView(renderer, context);
            }
        }
Esempio n. 20
0
        int GetAbsolutePosition(IItemsViewSource group, int indexInGroup)
        {
            var groupIndex = _groups.IndexOf(group);

            var runningIndex = 0;

            for (int n = 0; n < groupIndex; n++)
            {
                runningIndex += _groups[n].Count;
            }

            return(AdjustPositionForHeader(runningIndex + indexInGroup));
        }
Esempio n. 21
0
        public static bool IsIndexPathValid(this IItemsViewSource source, NSIndexPath indexPath)
        {
            if (indexPath.Section >= source.GroupCount)
            {
                return(false);
            }

            if (indexPath.Item >= source.ItemCountInGroup(indexPath.Section))
            {
                return(false);
            }

            return(true);
        }
Esempio n. 22
0
        internal ItemsViewAdapter(ItemsView itemsView, Func <View, Context, ItemContentView> createItemContentView = null)
        {
            Xamarin.Forms.CollectionView.VerifyCollectionViewFlagEnabled(nameof(ItemsViewAdapter));

            ItemsView = itemsView ?? throw new ArgumentNullException(nameof(itemsView));

            UpdateUsingItemTemplate();
            UpdateHeaderOffset();
            UpdateHasFooter();

            ItemsView.PropertyChanged += ItemsViewPropertyChanged;

            _createItemContentView = createItemContentView;
            ItemsSource            = ItemsSourceFactory.Create(itemsView.ItemsSource, this);

            if (_createItemContentView == null)
            {
                _createItemContentView = (view, context) => new ItemContentView(context);
            }
        }
 public void SetItemsSource(IItemsViewSource itemsSource) => _itemsSource = itemsSource;
Esempio n. 24
0
 void UpdateItemsSourceAndReload()
 {
     _itemsSource = ItemsSourceFactory.Create(_itemsView.ItemsSource, CollectionView);
     CollectionView.ReloadData();
     CollectionView.CollectionViewLayout.InvalidateLayout();
 }
Esempio n. 25
0
 public void NotifyItemRemoved(IItemsViewSource source, int startIndex)
 {
     RemoveCount += 1;
 }
Esempio n. 26
0
 public void NotifyItemRemoved(IItemsViewSource group, int localIndex)
 {
     localIndex = GetAbsolutePosition(group, localIndex);
     _notifier.NotifyItemRemoved(this, localIndex);
 }
Esempio n. 27
0
 public void NotifyItemRangeInserted(IItemsViewSource group, int localIndex, int count)
 {
     localIndex = GetAbsolutePosition(group, localIndex);
     _notifier.NotifyItemRangeInserted(this, localIndex, count);
 }
Esempio n. 28
0
 public void NotifyItemRangeChanged(IItemsViewSource group, int localStartIndex, int localEndIndex)
 {
     localStartIndex = GetAbsolutePosition(group, localStartIndex);
     localEndIndex   = GetAbsolutePosition(group, localEndIndex);
     _notifier.NotifyItemRangeChanged(this, localStartIndex, localEndIndex);
 }
Esempio n. 29
0
 public void NotifyItemMoved(IItemsViewSource group, int localFromIndex, int localToIndex)
 {
     localFromIndex = GetAbsolutePosition(group, localFromIndex);
     localToIndex   = GetAbsolutePosition(group, localToIndex);
     _notifier.NotifyItemMoved(this, localFromIndex, localToIndex);
 }
Esempio n. 30
0
 public UngroupedItemsSource(IItemsViewSource source)
 {
     _source = source;
 }