private void ConfigureDataSource() { var dataSource = new UICollectionViewDiffableDataSource <NS <Section>, NS <int> >( collectionView: _collectionView, cellProvider: (collectionView, indexPath, identifier) => { var cell = (DummyCell)collectionView.DequeueReusableCell(DummyCell.Key, indexPath); cell.Configure(((NS <int>)identifier).Value.ToString()); return(cell); }); // initial data const int ItemsPerSection = 6; var snapshot = new NSDiffableDataSourceSnapshot <NS <Section>, NS <int> >(); EnumExtensions.Apply <Section>(section => { var ns = new NS <Section>(section); snapshot.AppendSections(new[] { ns }); var itemOffset = ((int)ns.Value) * ItemsPerSection; var itemUpperbound = itemOffset + ItemsPerSection; var items = Enumerable.Range(itemOffset, itemUpperbound).Select(x => new NS <int>(x)).ToArray(); snapshot.AppendItems(items); }); dataSource.ApplySnapshot(snapshot, animatingDifferences: false); }
NSDiffableDataSourceSnapshot <Section, MountainsController.Mountain> InitialSnapshot() { var mountainsController = new MountainsController(); var limit = 8; var mountains = mountainsController.FilterMountains(limit: limit); var bucketList = mountains.Take(limit / 2).ToArray(); var visited = mountains.Skip(limit / 2).Take(limit / 2).ToArray(); var snapshot = new NSDiffableDataSourceSnapshot <Section, MountainsController.Mountain> (); snapshot.AppendSections(new [] { Section.Visited }); snapshot.AppendItems(visited); snapshot.AppendSections(new [] { Section.BucketList }); snapshot.AppendItems(bucketList); return(snapshot); }
public void ObjectUsageTest() { var diff = new NSDiffableDataSourceSnapshot <NSNumber, NSUuid> (); diff.AppendSections(new NSNumber [] { NSNumber.FromNInt(1) }); diff.AppendItems(new NSUuid [] { new NSUuid() }, NSNumber.FromNInt(1)); Assert.That(diff.NumberOfSections, Is.GreaterThan((nint)0), "Sections"); Assert.That(diff.GetNumberOfItems(NSNumber.FromNInt(1)), Is.GreaterThan((nint)0), "Items"); }
void ConfigureDataSource() { dataSource = new UICollectionViewDiffableDataSource <NSNumber, NSNumber> (collectionView, CellProviderHandler) { SupplementaryViewProvider = SupplementaryViewProviderHandler }; // initial data var snapshot = new NSDiffableDataSourceSnapshot <NSNumber, NSNumber> (); var idOffset = 0; var itemsPerSection = 18; foreach (var section in SectionKind.AllSections) { snapshot.AppendSections(new [] { NSNumber.FromInt32(section.EnumValue) }); var items = Enumerable.Range(idOffset, itemsPerSection).Select(i => NSNumber.FromInt32(i)).ToArray(); snapshot.AppendItems(items); idOffset += itemsPerSection; } dataSource.ApplySnapshot(snapshot, false); UICollectionViewCell CellProviderHandler(UICollectionView collectionView, NSIndexPath indexPath, NSObject obj) { // Get a cell of the desired kind. var cell = collectionView.DequeueReusableCell(TextCell.Key, indexPath) as TextCell; // Populate the cell with our item description. cell.Label.Text = $"{indexPath.Section}, {indexPath.Row}"; cell.ContentView.BackgroundColor = UIColorExtensions.CornflowerBlue; cell.ContentView.Layer.BorderColor = UIColor.Black.CGColor; cell.ContentView.Layer.BorderWidth = 1; cell.ContentView.Layer.CornerRadius = 8; cell.Label.TextAlignment = UITextAlignment.Center; cell.Label.Font = UIFont.GetPreferredFontForTextStyle(UIFontTextStyle.Title1); // Return the cell. return(cell); } UICollectionReusableView SupplementaryViewProviderHandler(UICollectionView collectionView, string kind, NSIndexPath indexPath) { var sectionKind = SectionKind.GetSectionKind(indexPath.Section); // Get a supplementary view of the desired kind. var header = collectionView.DequeueReusableSupplementaryView(new NSString(kind), TitleSupplementaryView.Key, indexPath) as TitleSupplementaryView; // Populate the view with our section's description. header.Label.Text = $".{sectionKind}"; // Return the view. return(header); } }
void ConfigureDataSource() { dataSource = new UICollectionViewDiffableDataSource <NSNumber, NSNumber> (collectionView, CellProviderHandler) { SupplementaryViewProvider = SupplementaryViewProviderHandler }; // initial data var itemsPerSection = 5; var sections = Enumerable.Range(0, 5).Select(i => NSNumber.FromInt32(i)).ToArray(); var snapshot = new NSDiffableDataSourceSnapshot <NSNumber, NSNumber> (); var itemOffset = 0; foreach (var section in sections) { snapshot.AppendSections(new [] { section }); var items = Enumerable.Range(itemOffset, itemsPerSection).Select(i => NSNumber.FromInt32(i)).ToArray(); snapshot.AppendItems(items); itemOffset += itemsPerSection; } dataSource.ApplySnapshot(snapshot, false); UICollectionViewCell CellProviderHandler(UICollectionView collectionView, NSIndexPath indexPath, NSObject obj) { // Get a cell of the desired kind. var cell = collectionView.DequeueReusableCell(ListCell.Key, indexPath) as ListCell; // Populate the cell with our item description. cell.Label.Text = $"{indexPath.Section}, {indexPath.Row}"; // Return the cell. return(cell); } UICollectionReusableView SupplementaryViewProviderHandler(UICollectionView collectionView, string kind, NSIndexPath indexPath) { // Get a supplementary view of the desired kind. var supplementaryView = collectionView.DequeueReusableSupplementaryView(new NSString(kind), TitleSupplementaryView.Key, indexPath) as TitleSupplementaryView; // Populate the view with our section's description. var viewKind = kind == sectionHeaderElementKind ? "Header" : "Footer"; supplementaryView.Label.Text = $"{viewKind} for section {indexPath.Section}"; supplementaryView.BackgroundColor = UIColor.LightGray; supplementaryView.Layer.BorderColor = UIColor.Black.CGColor; supplementaryView.Layer.BorderWidth = 1; // Return the view. return(supplementaryView); } }
NSDiffableDataSourceSnapshot <InsertionSortArray, InsertionSortArray.SortNode> RandomizedSnapshot(CGRect bounds) { var snapshot = new NSDiffableDataSourceSnapshot <InsertionSortArray, InsertionSortArray.SortNode> (); var rowCount = GetRows(bounds); var columnCount = GetColumns(bounds); for (int i = 0; i < rowCount; i++) { var section = new InsertionSortArray(columnCount); snapshot.AppendSections(new [] { section }); snapshot.AppendItems(section.Nodes); } return(snapshot); }
ToDiffableDataSourceSnapshot <TSection, TItem>(this Dictionary <TSection, List <TItem> > dict) { var snapshot = new NSDiffableDataSourceSnapshot <IdentifierType <TSection>, IdentifierType <TItem> >(); foreach (var kvp in dict) { var section = IdentifierType.For(kvp.Key); var items = kvp.Value.Select(IdentifierType.For).ToArray(); snapshot.AppendSections(new[] { section }); snapshot.AppendItems(items); } return(snapshot); }
void ConfigureDataSource() { dataSource = new UICollectionViewDiffableDataSource <ConferenceVideoController.VideoCollection, ConferenceVideoController.Video> (collectionView, CellProviderHandler) { SupplementaryViewProvider = SupplementaryViewProviderHandler }; currentSnapshot = new NSDiffableDataSourceSnapshot <ConferenceVideoController.VideoCollection, ConferenceVideoController.Video> (); foreach (var videoCollection in videosController.Collections) { currentSnapshot.AppendSections(new [] { videoCollection }); currentSnapshot.AppendItems(videoCollection.Videos); } dataSource.ApplySnapshot(currentSnapshot, false); UICollectionViewCell CellProviderHandler(UICollectionView collectionView, NSIndexPath indexPath, NSObject obj) { var video = obj as ConferenceVideoController.Video; // Get a cell of the desired kind. var cell = collectionView.DequeueReusableCell(ConferenceVideoCell.Key, indexPath) as ConferenceVideoCell; // Populate the cell with our item description. cell.TitleLabel.Text = video.Title; cell.CategoryLabel.Text = video.Category; // Return the cell. return(cell); } UICollectionReusableView SupplementaryViewProviderHandler(UICollectionView collectionView, string kind, NSIndexPath indexPath) { // Get a supplementary view of the desired kind. var titleSupplementary = collectionView.DequeueReusableSupplementaryView(new NSString(kind), TitleSupplementaryView.Key, indexPath) as TitleSupplementaryView; // Populate the view with our section's description. var videoCategory = currentSnapshot.SectionIdentifiers [indexPath.Section]; titleSupplementary.Label.Text = videoCategory.Title; // Return the view. return(titleSupplementary); } }
ToDiffableDataSourceSnapshot <TSection, TSectionIdentifier, TItem, TItemIdentifier>( this IEnumerable <TSection> sections, Func <TSection, TSectionIdentifier> getSectionIdentifier, Func <TSection, IEnumerable <TItem> > getItems, Func <TItem, TItemIdentifier> getItemIdentifier) { var snapshot = new NSDiffableDataSourceSnapshot <IdentifierType <TSection>, IdentifierType <TItem> >(); foreach (var section in sections) { var items = getItems(section); var sectionIdentifier = IdentifierType.For(section, getSectionIdentifier); var itemIdentifiers = items.Select(item => IdentifierType.For(item, getItemIdentifier)).ToArray(); snapshot.AppendSections(new[] { sectionIdentifier }); snapshot.AppendItems(itemIdentifiers); } return(snapshot); }
private void ConfigureDataSource() { var dataSource = new UICollectionViewDiffableDataSource <NS <Section>, NS <int> >( collectionView: _collectionView, cellProvider: (collectionView, indexPath, identifier) => { var cell = (DummyCell)collectionView.DequeueReusableCell(DummyCell.Key, indexPath); cell.Configure(((NS <int>)identifier).Value.ToString()); return(cell); }); // initial data var snapshot = new NSDiffableDataSourceSnapshot <NS <Section>, NS <int> >(); snapshot.AppendSections(new[] { new NS <Section>(Section.Main) }); snapshot.AppendItems(Enumerable.Range(0, 100).Select(x => new NS <int>(x)).ToArray()); dataSource.ApplySnapshot(snapshot, animatingDifferences: false); }
ToDiffableDataSourceSnapshot <TObject, TSection, TItem>( this TObject obj, Func <TObject, IEnumerable <TSection> > getSections, Func <TSection, IEnumerable <TItem> > getItems) { var snapshot = new NSDiffableDataSourceSnapshot <IdentifierType <TSection>, IdentifierType <TItem> >(); foreach (var section in getSections(obj)) { var items = getItems(section); var sectionIdentifier = IdentifierType.For(section); var itemIdentifiers = items.Select(IdentifierType.For).ToArray(); snapshot.AppendSections(new[] { sectionIdentifier }); snapshot.AppendItems(itemIdentifiers); } return(snapshot); }
void ConfigureDataSource() { dataSource = new UICollectionViewDiffableDataSource <NSNumber, NSNumber> (collectionView, CellProviderHandler); // initial data var itemsPerSection = 5; var sections = Enumerable.Range(0, 5).Select(i => NSNumber.FromInt32(i)).ToArray(); currentSnapshot = new NSDiffableDataSourceSnapshot <NSNumber, NSNumber> (); var itemOffset = 0; foreach (var section in sections) { currentSnapshot.AppendSections(new [] { section }); var items = Enumerable.Range(itemOffset, itemsPerSection).Select(i => NSNumber.FromInt32(i)).ToArray(); currentSnapshot.AppendItems(items); itemOffset += itemsPerSection; } dataSource.ApplySnapshot(currentSnapshot, false); UICollectionViewCell CellProviderHandler(UICollectionView collectionView, NSIndexPath indexPath, NSObject obj) { var sectionId = currentSnapshot.SectionIdentifiers [indexPath.Section]; var numberOfItemsInSection = currentSnapshot.GetNumberOfItems(sectionId); var isLastCell = indexPath.Item + 1 == numberOfItemsInSection; // Get a cell of the desired kind. var cell = collectionView.DequeueReusableCell(ListCell.Key, indexPath) as ListCell; // Populate the cell with our item description. cell.Label.Text = $"{indexPath.Section}, {indexPath.Row}"; cell.SeparatorView.Hidden = isLastCell; // Return the cell. return(cell); } }