Skip to content

Commit

Permalink
Rename adapter methods
Browse files Browse the repository at this point in the history
Less iOS like and more descriptive
  • Loading branch information
Redth committed Jul 26, 2023
1 parent 0f36e3e commit 0dc565a
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 44 deletions.
8 changes: 4 additions & 4 deletions Sample/VirtualListViewSample/SectionedAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ public SectionedAdapter(IList<Section> items) : base()

public readonly IList<Section> Items;

public override Section Section(int sectionIndex)
public override Section GetSection(int sectionIndex)
=> Items[sectionIndex];

public override int Sections
public override int GetNumberOfSections()
=> Items.Count;

public override int ItemsForSection(int sectionIndex)
public override int GetNumberOfItemsInSection(int sectionIndex)
=> Items[sectionIndex].Count;

public override string Item(int sectionIndex, int itemIndex)
public override string GetItem(int sectionIndex, int itemIndex)
=> Items[sectionIndex][itemIndex];

public void AddItem(string sectionTitle, string itemName)
Expand Down
8 changes: 4 additions & 4 deletions VirtualListView/Adapters/IVirtualListViewAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

public interface IVirtualListViewAdapter
{
int Sections { get; }
int GetNumberOfSections();

object Section(int sectionIndex);
object GetSection(int sectionIndex);

int ItemsForSection(int sectionIndex);
int GetNumberOfItemsInSection(int sectionIndex);

object Item(int sectionIndex, int itemIndex);
object GetItem(int sectionIndex, int itemIndex);

event EventHandler OnDataInvalidated;

Expand Down
6 changes: 3 additions & 3 deletions VirtualListView/Adapters/ObservableCollectionAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public ObservableCollectionAdapter(ObservableCollection<TItem> items)
bool disposedValue;

void CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
=> InvalidateData();
=> ((IVirtualListViewAdapter)this).InvalidateData();

public override TItem Item(int sectionIndex, int itemIndex)
public override TItem GetItem(int sectionIndex, int itemIndex)
=> Items[itemIndex];

public override int ItemsForSection(int sectionIndex)
public override int GetNumberOfItemsInSection(int sectionIndex)
=> Items.Count;

protected virtual void Dispose(bool disposing)
Expand Down
4 changes: 2 additions & 2 deletions VirtualListView/Adapters/VirtualListViewAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public VirtualListViewAdapter(IList<TItem> items)

readonly IList<TItem> items;

public override TItem Item(int sectionIndex, int itemIndex)
public override TItem GetItem(int sectionIndex, int itemIndex)
=> items[itemIndex];

public override int ItemsForSection(int sectionIndex)
public override int GetNumberOfItemsInSection(int sectionIndex)
=> items.Count;
}
23 changes: 14 additions & 9 deletions VirtualListView/Adapters/VirtualListViewAdapterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@

public abstract class VirtualListViewAdapterBase<TSection, TItem> : IVirtualListViewAdapter
{
public virtual int Sections { get; } = 1;
public virtual int GetNumberOfSections() => 1;

public event EventHandler OnDataInvalidated;

public virtual void InvalidateData()
=> OnDataInvalidated?.Invoke(this, EventArgs.Empty);
{
OnDataInvalidated?.Invoke(this, EventArgs.Empty);
}

public abstract TItem Item(int sectionIndex, int itemIndex);
public abstract TItem GetItem(int sectionIndex, int itemIndex);

public abstract int ItemsForSection(int sectionIndex);
public abstract int GetNumberOfItemsInSection(int sectionIndex);

public virtual TSection Section(int sectionIndex)
public virtual TSection GetSection(int sectionIndex)
=> default;

object IVirtualListViewAdapter.Item(int sectionIndex, int itemIndex)
=> Item(sectionIndex, itemIndex);
object IVirtualListViewAdapter.GetItem(int sectionIndex, int itemIndex)
=> GetItem(sectionIndex, itemIndex);

object IVirtualListViewAdapter.Section(int sectionIndex)
=> Section(sectionIndex);
object IVirtualListViewAdapter.GetSection(int sectionIndex)
=> GetSection(sectionIndex);

void IVirtualListViewAdapter.InvalidateData()
=> InvalidateData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public static void MapDeselectItems(VirtualListViewHandler handler, IVirtualList

static void UpdateSelected(VirtualListViewHandler handler, ItemPosition[] itemPositions, bool selected)
{

foreach (var itemPosition in itemPositions)
{
var realIndex = handler.PositionalViewSelector.GetIndexPath(itemPosition.SectionIndex, itemPosition.ItemIndex);
Expand Down Expand Up @@ -215,9 +214,6 @@ void UpdateEmptyView()
public void InvalidateData()
{
this.PlatformView.InvokeOnMainThread(() => {
dataSource?.Reset(collectionView);
PositionalViewSelector?.Reset();

layout?.InvalidateLayout();

UpdateEmptyViewVisibility();
Expand Down
12 changes: 11 additions & 1 deletion VirtualListView/LRUCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ internal class LRUCache<TKey, TValue>
{
OrderedDictionary items = new OrderedDictionary();

public int Capacity { get; set; } = 1000;
public int Capacity { get; set; }

public LRUCache()
{
Capacity = 1000;
}

public LRUCache(int capacity)
{
Capacity = capacity;
}

public void AddReplace(TKey key, TValue value)
{
Expand Down
13 changes: 6 additions & 7 deletions VirtualListView/Platforms/Android/RvAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi
{
var data = info.Kind switch {
PositionKind.Item =>
positionalViewSelector?.Adapter?.Item(info.SectionIndex, info.ItemIndex),
positionalViewSelector?.Adapter?.GetItem(info.SectionIndex, info.ItemIndex),
PositionKind.SectionHeader =>
positionalViewSelector?.Adapter?.Section(info.SectionIndex),
positionalViewSelector?.Adapter?.GetSection(info.SectionIndex),
PositionKind.SectionFooter =>
positionalViewSelector?.Adapter?.Section(info.SectionIndex),
positionalViewSelector?.Adapter?.GetSection(info.SectionIndex),
_ => null
};

Expand All @@ -89,11 +89,11 @@ public override int GetItemViewType(int position)

var data = info.Kind switch {
PositionKind.Item =>
positionalViewSelector?.Adapter?.Item(info.SectionIndex, info.ItemIndex),
positionalViewSelector?.Adapter?.GetItem(info.SectionIndex, info.ItemIndex),
PositionKind.SectionHeader =>
positionalViewSelector?.Adapter?.Section(info.SectionIndex),
positionalViewSelector?.Adapter?.GetSection(info.SectionIndex),
PositionKind.SectionFooter =>
positionalViewSelector?.Adapter?.Section(info.SectionIndex),
positionalViewSelector?.Adapter?.GetSection(info.SectionIndex),
_ => null
};

Expand Down Expand Up @@ -149,7 +149,6 @@ public void Reset()
lock (lockObj)
{
cachedReuseIds.Clear();
positionalViewSelector.Reset();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ protected override void DisconnectHandler(FrameLayout nativeView)

public void InvalidateData()
{
PositionalViewSelector.Reset();
UpdateEmptyViewVisibility();
adapter?.Reset();
adapter?.NotifyDataSetChanged();
Expand Down
6 changes: 3 additions & 3 deletions VirtualListView/VirtualListViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ internal static class VirtualListViewExtensions
public static object DataFor(this IVirtualListViewAdapter vlva, PositionKind kind, int sectionIndex, int itemIndex)
=> kind switch
{
PositionKind.Item => vlva.Item(sectionIndex, itemIndex),
PositionKind.SectionHeader => vlva.Section(sectionIndex),
PositionKind.SectionFooter => vlva.Section(sectionIndex),
PositionKind.Item => vlva.GetItem(sectionIndex, itemIndex),
PositionKind.SectionHeader => vlva.GetSection(sectionIndex),
PositionKind.SectionFooter => vlva.GetSection(sectionIndex),
_ => default
};
}
4 changes: 2 additions & 2 deletions VirtualListView/VirtualListViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public VirtualListViewHandler(PropertyMapper mapper = null, CommandMapper comman

internal PositionalViewSelector PositionalViewSelector { get; private set; }

bool ShouldShowEmptyView => (PositionalViewSelector?.Adapter?.Sections ?? 0) <= 1
&& (PositionalViewSelector?.Adapter?.ItemsForSection(0) ?? 0) <= 0;
bool ShouldShowEmptyView => (PositionalViewSelector?.Adapter?.GetNumberOfSections() ?? 0) <= 1
&& (PositionalViewSelector?.Adapter?.GetNumberOfItemsInSection(0) ?? 0) <= 0;


public static void MapAdapter(VirtualListViewHandler handler, IVirtualListView virtualListView)
Expand Down
8 changes: 4 additions & 4 deletions VirtualListView/VirtualListViewSectionedAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public VirtualListViewSectionedAdapter(IList<TSection> sections)

readonly IList<TSection> sections;

public override int Sections
public override int GetNumberOfSections()
=> sections.Count;

public override TSection Section(int sectionIndex)
public override TSection GetSection(int sectionIndex)
=> sections[sectionIndex];

public override TItem Item(int sectionIndex, int itemIndex)
public override TItem GetItem(int sectionIndex, int itemIndex)
=> sections[sectionIndex][itemIndex];

public override int ItemsForSection(int sectionIndex)
public override int GetNumberOfItemsInSection(int sectionIndex)
=> sections[sectionIndex].Count;
}

0 comments on commit 0dc565a

Please sign in to comment.