void SetObserveChanges(IItemsViewSource itemsSource, bool enable) { if (itemsSource is IObservableItemsViewSource observableSource) { observableSource.ObserveChanges = enable; } }
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)); }
public void NotifyItemRangeRemoved(IItemsViewSource source, int startIndex, int count) { if (IsValidAdapter()) { _adapter.NotifyItemRangeRemoved(startIndex, count); } }
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(); } }
public ItemsViewController(ItemsView itemsView, ItemsViewLayout layout) : base(layout) { _itemsView = itemsView; _itemsSource = ItemsSourceFactory.Create(_itemsView.ItemsSource, CollectionView); UpdateLayout(layout); }
public void NotifyItemRangeChanged(IItemsViewSource source, int start, int end) { if (IsValidAdapter()) { _adapter.NotifyItemRangeChanged(start, end); } }
public void NotifyItemChanged(IItemsViewSource source, int startIndex) { if (IsValidAdapter()) { _adapter.NotifyItemChanged(startIndex); } }
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"); }
public void NotifyItemMoved(IItemsViewSource source, int fromPosition, int toPosition) { if (IsValidAdapter()) { _adapter.NotifyItemMoved(fromPosition, toPosition); } }
void UnsubscribeCollectionItemsSourceChanged(IItemsViewSource oldItemsSource) { if (oldItemsSource is ObservableItemsSource oldObservableItemsSource) { oldObservableItemsSource.CollectionItemsSourceChanged -= CollectionItemsSourceChanged; } }
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)); }
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)); }
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 }
public void NotifyItemRemoved(IItemsViewSource source, int startIndex) { if (IsValidAdapter()) { _adapter.NotifyItemRemoved(startIndex); var changedCount = _adapter.ItemCount - startIndex; _adapter.NotifyItemRangeChanged(startIndex, changedCount); } }
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); }
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); } }
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); } }
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); } }
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)); }
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); }
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;
void UpdateItemsSourceAndReload() { _itemsSource = ItemsSourceFactory.Create(_itemsView.ItemsSource, CollectionView); CollectionView.ReloadData(); CollectionView.CollectionViewLayout.InvalidateLayout(); }
public void NotifyItemRemoved(IItemsViewSource source, int startIndex) { RemoveCount += 1; }
public void NotifyItemRemoved(IItemsViewSource group, int localIndex) { localIndex = GetAbsolutePosition(group, localIndex); _notifier.NotifyItemRemoved(this, localIndex); }
public void NotifyItemRangeInserted(IItemsViewSource group, int localIndex, int count) { localIndex = GetAbsolutePosition(group, localIndex); _notifier.NotifyItemRangeInserted(this, localIndex, count); }
public void NotifyItemRangeChanged(IItemsViewSource group, int localStartIndex, int localEndIndex) { localStartIndex = GetAbsolutePosition(group, localStartIndex); localEndIndex = GetAbsolutePosition(group, localEndIndex); _notifier.NotifyItemRangeChanged(this, localStartIndex, localEndIndex); }
public void NotifyItemMoved(IItemsViewSource group, int localFromIndex, int localToIndex) { localFromIndex = GetAbsolutePosition(group, localFromIndex); localToIndex = GetAbsolutePosition(group, localToIndex); _notifier.NotifyItemMoved(this, localFromIndex, localToIndex); }
public UngroupedItemsSource(IItemsViewSource source) { _source = source; }