Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public static class DiffableDataSourceExtensions
{
public static NSDiffableDataSourceSnapshot<IdentifierType<TSection>, IdentifierType<TItem>>
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;
}
}
public class EasyUITableViewDiffableDataSource<TSection, TSectionIdentifier, TItem, TItemIdentifier>
: UITableViewDiffableDataSource<IdentifierType<TSection>, IdentifierType<TItem>>, IUITableViewDiffableDataSource<TSection>
{
Func<TSection, TSectionIdentifier> _getSectionIdentifier;
Func<TSection, IEnumerable<TItem>> _getSectionItems;
Func<TItem, TItemIdentifier> _getItemIdentifier;
Func<UITableView, int, TSection, string> _titleForHeader;
Func<UITableView, int, TSection, string> _titleForFooter;
NSDiffableDataSourceSnapshot<IdentifierType<TSection>, IdentifierType<TItem>> _snapshot;
public EasyUITableViewDiffableDataSource(
UITableView tableView,
Func<TSection, TSectionIdentifier> getSectionIdentifier,
Func<TSection, IEnumerable<TItem>> getSectionItems,
Func<TItem, TItemIdentifier> getItemIdentifier,
UITableViewDiffableDataSourceCellProvider cellProvider,
Func<UITableView, int, TSection, string> titleForHeader = null,
Func<UITableView, int, TSection, string> titleForFooter = null
) : base(tableView, cellProvider)
{
_getSectionIdentifier = getSectionIdentifier;
_getSectionItems = getSectionItems;
_getItemIdentifier = getItemIdentifier;
_titleForHeader = titleForHeader;
_titleForFooter = titleForFooter;
}
public void ApplySnapshot(IEnumerable<TSection> sections, bool animatingDifferences)
{
_snapshot = sections.ToDiffableDataSourceSnapshot(
_getSectionIdentifier, _getSectionItems, _getItemIdentifier);
ApplySnapshot(_snapshot, animatingDifferences);
}
public override string TitleForHeader(UITableView tableView, nint section)
=> _titleForHeader?.Invoke(tableView, (int)section, _snapshot.SectionIdentifiers[section].Item);
public override string TitleForFooter(UITableView tableView, nint section)
=> _titleForFooter?.Invoke(tableView, (int)section, _snapshot.SectionIdentifiers[section].Item);
}
public static class EasyUITableViewDiffableDataSource
{
public static IUITableViewDiffableDataSource<TSection> Create<TSection, TSectionIdentifier, TItem, TItemIdentifier>(
UITableView tableView,
List<TSection> items,
Func<TSection, TSectionIdentifier> getSectionIdentifier,
Func<TSection, IEnumerable<TItem>> getSectionItems,
Func<TItem, TItemIdentifier> getItemIdentifier,
Func<UITableView, NSIndexPath, TItem, UITableViewCell> getCell,
Func<UITableView, int, TSection, string> titleForHeader = null,
Func<UITableView, int, TSection, string> titleForFooter = null)
=> new EasyUITableViewDiffableDataSource<TSection, TSectionIdentifier, TItem, TItemIdentifier>
(
tableView, getSectionIdentifier, getSectionItems, getItemIdentifier,
(tv, indexPath, item) => getCell(tv, indexPath, (item as IdentifierType<TItem>).Item),
titleForHeader, titleForFooter
);
}
public class FuncIdentifierType<T,U> : IdentifierType<T>
{
private readonly Func<T, U> _identifierFunc;
public FuncIdentifierType(T item, Func<T,U> identifierFunc) : base(item)
{
_identifierFunc = identifierFunc;
}
// invoke the provided func and hash the result
public override nuint GetNativeHash()
=> (nuint)_identifierFunc(Item).GetHashCode();
public override bool IsEqual(NSObject anObject)
=> GetNativeHash() == anObject.GetNativeHash();
}
public interface IIdentifier
{
string Identifier { get; }
}
public class IIdentifierType<T> : IdentifierType<T>
where T: IIdentifier
{
public IIdentifierType(T item) : base(item) {}
// read the identifier and take its hash
public override nuint GetNativeHash()
=> (System.nuint)Item.Identifier.GetHashCode();
public override bool IsEqual(NSObject anObject)
=> GetNativeHash() == anObject.GetNativeHash();
}
public interface IUITableViewDiffableDataSource<TSection>
{
void ApplySnapshot(IEnumerable<TSection> sections, bool animatingDifferences);
}
public static NSDiffableDataSourceSnapshot<IdentifierType<TSection>, IdentifierType<TItem>>
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;
}
public class IdentifierType<T> : NSObject
{
public readonly T Item;
public IdentifierType(T item)
{
Item = item;
}
// pass-through the .NET hash
public override nuint GetNativeHash()
=> (nuint) Item.GetHashCode();
public override bool IsEqual(NSObject anObject)
=> GetNativeHash() == anObject.GetNativeHash();
}
var source =
EasyUITableViewDiffableDataSource.Create(
TableView,
items: Games, // used for type inference, can be null at the time of the call
getSectionIdentifier: game => game.GameName, // return key for section
getSectionItems: game => game.Characters, // return items for section
getItemIdentifier: character => character.CharacterName, // return key for item
getCell: (tv, indexPath, character) => // standard GetCell, but includes typed model parameter (character)
{
// yeah yeah cell reuse
var cell = new UITableViewCell(UITableViewCellStyle.Default, "cell");
cell.TextLabel.Text = character.CharacterName;
return cell;
},
titleForHeader: (tv, sectionIndex, section) => section.GameName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.