Skip to content

Commit

Permalink
Start working on animations for insert/delete from list
Browse files Browse the repository at this point in the history
  • Loading branch information
Redth committed Dec 22, 2023
1 parent 0a25496 commit 9084fc6
Show file tree
Hide file tree
Showing 14 changed files with 375 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ private void vlv_SelectedItemsChanged(object sender, SelectedItemsChangedEventAr
vlv.ClearSelectedItems();

var item = Adapter.GetItem(toDelete.SectionIndex, toDelete.ItemIndex);

Items.Remove(item);
this.vlv.DeleteItems(new []{ new ItemPosition(toDelete.SectionIndex, toDelete.ItemIndex)});
}
}

Expand Down
8 changes: 7 additions & 1 deletion Sample/VirtualListViewSample/Section.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
namespace VirtualListViewSample;

public class Section : List<string>
public class Section : List<Item>
{
public string Title { get; set; }
}

public class Item
{
public string Text { get; set; }
public double Height { get; set; }
}
32 changes: 25 additions & 7 deletions Sample/VirtualListViewSample/SectionedAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace VirtualListViewSample;

public class SectionedAdapter : VirtualListViewAdapterBase<Section, string>
public class SectionedAdapter : VirtualListViewAdapterBase<Section, Item>
{
public SectionedAdapter(IList<Section> items) : base()
{
Expand All @@ -18,23 +18,41 @@ public override int GetNumberOfSections()
=> Items.Count;

public override int GetNumberOfItemsInSection(int sectionIndex)
=> Items[sectionIndex].Count;
{
var c = Items[sectionIndex].Count;
Console.WriteLine($"GetNumberOfItemsInSection: section={sectionIndex}, count={c}");
return c;
}

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

public void AddItem(string sectionTitle, string itemName)
public ItemPosition AddItem(string sectionTitle, string itemName, double itemHeight = 40f)
{
var section = Items.FirstOrDefault(s => s.Title == sectionTitle);

Section section = null;
var sectionIndex = -1;

for (var s = 0; s < Items.Count; s++)
{
if (Items[s].Title == sectionTitle)
{
section = Items[s];
sectionIndex = s;
break;
}
}

if (section is null)
{
section = new Section { Title = sectionTitle };
Items.Add(section);
sectionIndex = Items.Count - 1;
}

section.Add(itemName);
section.Add(new Item { Text = itemName, Height = itemHeight });
InvalidateData();

return new ItemPosition(sectionIndex, section.Count - 1);
}

public void RemoveItem(int sectionIndex, int itemIndex)
Expand Down
8 changes: 5 additions & 3 deletions Sample/VirtualListViewSample/SectionedAdapterPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns:local="clr-namespace:VirtualListViewSample"
xmlns:vlv="clr-namespace:Microsoft.Maui.Controls;assembly=VirtualListView"
Title="SectionedAdapterPage">
<Grid RowDefinitions="*,Auto" ColumnDefinitions="*,*,Auto" Padding="20">
<Grid RowDefinitions="*,Auto,Auto" ColumnDefinitions="*,*,Auto" Padding="20">
<vlv:VirtualListView
Grid.Row="0"
Grid.Column="0" Grid.ColumnSpan="3"
Expand All @@ -32,15 +32,17 @@
Margin="10,0,0,0"
Padding="4"
Background="LightBlue"
HeightRequest="{Binding Height}"
StrokeShape="{RoundRectangle CornerRadius=10}">
<Label Margin="10,6,10,6" Text="{Binding .}" />
<Label Margin="10,6,10,6" Text="{Binding Text}" />
</Border>
</DataTemplate>
</vlv:VirtualListView.ItemTemplate>
</vlv:VirtualListView>

<Entry x:Name="entrySection" Grid.Row="1" Grid.Column="0" Placeholder="Section" />
<Entry x:Name="entryItem" Grid.Row="1" Grid.Column="1" Placeholder="Item" />
<Button Grid.Row="1" Grid.Column="3" Text="Add" Clicked="Button_Clicked" />
<Entry x:Name="entryItemSize" Grid.Row="2" Grid.Column="0" Placeholder="Item Size" />
<Button Grid.Row="1" Grid.RowSpan="2" Grid.Column="3" Text="Add" Clicked="Button_Clicked" />
</Grid>
</ContentPage>
31 changes: 28 additions & 3 deletions Sample/VirtualListViewSample/SectionedAdapterPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,23 @@ private void Button_Clicked(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(entrySection.Text) && !string.IsNullOrEmpty(entryItem.Text))
{
Adapter.AddItem(entrySection.Text, entryItem.Text);
entryItem.Text = string.Empty;
#if IOS || MACCATALYST
if (vlv.Handler.PlatformView is UIKit.UICollectionView cv && vlv.Handler is VirtualListViewHandler vlvHandler)
{
var h = 40;
if (int.TryParse(entryItemSize.Text, out var p))
h = p;

var itemPosition = Adapter.AddItem(entrySection.Text, entryItem.Text, h);
Adapter.InvalidateData();

var indexPath = Foundation.NSIndexPath.FromItemSection(vlvHandler.PositionalViewSelector.GetPosition(itemPosition.SectionIndex, itemPosition.ItemIndex), 0);

cv.InsertItems(new []{ indexPath });

entryItem.Text = string.Empty;
}
#endif
}
}

Expand All @@ -42,7 +57,17 @@ private void vlv_SelectedItemsChanged(object sender, SelectedItemsChangedEventAr
{
var item = e.NewSelection.First();

Adapter.RemoveItem(item.SectionIndex, item.ItemIndex);
#if IOS || MACCATALYST
if (vlv.Handler.PlatformView is UIKit.UICollectionView cv && vlv.Handler is VirtualListViewHandler vlvHandler)
{
var indexPath = Foundation.NSIndexPath.FromItemSection(vlvHandler.PositionalViewSelector.GetPosition(item.SectionIndex, item.ItemIndex), 0);
Adapter.RemoveItem(item.SectionIndex, item.ItemIndex);
Adapter.InvalidateData();

cv.DeleteItems(new []{ indexPath });
}
#endif
//vlv.DeleteItems(new [] { new ItemPosition(item.SectionIndex, item.ItemIndex)});
vlv.ClearSelectedItems();
}

Expand Down
2 changes: 0 additions & 2 deletions VirtualListView/Adapters/IVirtualListViewAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ public interface IVirtualListViewAdapter

object GetItem(int sectionIndex, int itemIndex);

event EventHandler OnDataInvalidated;

void InvalidateData();
}
3 changes: 0 additions & 3 deletions VirtualListView/Adapters/VirtualListViewAdapterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ public abstract class VirtualListViewAdapterBase<TSection, TItem> : IVirtualList
{
public virtual int GetNumberOfSections() => 1;

public event EventHandler OnDataInvalidated;

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

public abstract TItem GetItem(int sectionIndex, int itemIndex);
Expand Down
5 changes: 4 additions & 1 deletion VirtualListView/Apple/CvDataSource.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ void TapCellHandler(CvCell cell)

public override nint GetItemsCount(UICollectionView collectionView, nint section)
{
return Handler?.PositionalViewSelector?.TotalCount ?? 0;
var c = Handler?.PositionalViewSelector?.TotalCount ?? 0;

Console.WriteLine($"GetItemsCount: section={section}, count={c}");
return c;
}
}
80 changes: 61 additions & 19 deletions VirtualListView/Apple/VirtualListViewHandler.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,6 @@ protected override void DisconnectHandler(UICollectionView nativeView)
internal CvCell? GetCell(NSIndexPath indexPath)
=> dataSource?.GetCell(PlatformView, indexPath) as CvCell;

public static void MapHeader(VirtualListViewHandler handler, IVirtualListView virtualListView)
=> handler?.InvalidateData();

public static void MapFooter(VirtualListViewHandler handler, IVirtualListView virtualListView)
=> handler?.InvalidateData();

public static void MapViewSelector(VirtualListViewHandler handler, IVirtualListView virtualListView)
=> handler?.InvalidateData();

public static void MapSelectionMode(VirtualListViewHandler handler, IVirtualListView virtualListView)
{
}

public static void MapInvalidateData(VirtualListViewHandler handler, IVirtualListView virtualListView, object? parameter)
=> handler?.InvalidateData();

void PlatformScrollToItem(ItemPosition itemPosition, bool animated)
{
var realIndex = PositionalViewSelector?.GetPosition(itemPosition.SectionIndex, itemPosition.ItemIndex) ?? -1;
Expand All @@ -134,6 +118,64 @@ void PlatformScrollToItem(ItemPosition itemPosition, bool animated)

PlatformView.ScrollToItem(indexPath, UICollectionViewScrollPosition.Top, animated);
}

public void PlatformDeleteItems(ItemPosition[] itemPositions)
{
var realIndexes = itemPositions.Select(p => PositionalViewSelector.GetPosition(p.SectionIndex, p.ItemIndex));

PlatformView.InvokeOnMainThread(() =>
{
try
{
var ristr = string.Join(", ", realIndexes);

Console.WriteLine("ReloadData");
PlatformView.ReloadData();

var nsindexes = realIndexes.Select(r => NSIndexPath.FromItemSection(new nint(r), new nint(0))).ToArray();

Console.WriteLine($"DeleteItems: {ristr}");
PlatformView.DeleteItems(nsindexes);
}
catch (Exception ex)
{
Console.WriteLine("ERROR: " + ex);
}
});
}

public void PlatformDeleteSection(int sectionIndex)
{
}

public void PlatformInsertItems(ItemPosition[] itemPositions)
{
var realIndexes = itemPositions.Select(p => PositionalViewSelector.GetPosition(p.SectionIndex, p.ItemIndex));

PlatformView.InvokeOnMainThread(() =>
{
try
{
var ristr = string.Join(", ", realIndexes);

Console.WriteLine("ReloadData");
PlatformView.ReloadData();

var nsindexes = realIndexes.Select(r => NSIndexPath.FromItemSection(new nint(r), new nint(0))).ToArray();

Console.WriteLine($"InsertItems: {ristr}");
PlatformView.InsertItems(nsindexes);
}
catch (Exception ex)
{
Console.WriteLine("ERROR: " + ex);
}
});
}

public void PlatformInsertSection(int sectionIndex)
{
}

void PlatformUpdateItemSelection(ItemPosition itemPosition, bool selected)
{
Expand Down Expand Up @@ -165,7 +207,7 @@ public static void MapOrientation(VirtualListViewHandler handler, IVirtualListVi
};
}

handler.InvalidateData();
handler.PlatformInvalidateData();
}

public static void MapRefreshAccentColor(VirtualListViewHandler handler, IVirtualListView virtualListView)
Expand Down Expand Up @@ -213,9 +255,9 @@ void UpdateEmptyView()
}
}

public void InvalidateData()
void PlatformInvalidateData()
{
this.PlatformView.InvokeOnMainThread(() => {
this.PlatformView.InvokeOnMainThread(() => {
layout?.InvalidateLayout();

UpdateEmptyViewVisibility();
Expand Down
Loading

0 comments on commit 9084fc6

Please sign in to comment.