Skip to content

Commit

Permalink
Simplify selected items
Browse files Browse the repository at this point in the history
This moves to use a simpler property for SelectedItems instead of just methods, and uses the propertymapper instead to manage things between core/controls
  • Loading branch information
Redth committed Jul 27, 2023
1 parent 7fcf92d commit 89fe250
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 210 deletions.
4 changes: 3 additions & 1 deletion Sample/VirtualListViewSample/MusicLibraryPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public MusicLibraryPage()
{
Dispatcher.Dispatch(() =>
{
vlv.SelectItems(new ItemPosition(0, 2), new ItemPosition(0, 4));
vlv.SelectItem(new ItemPosition(0, 2));
vlv.SelectItem(new ItemPosition(0, 4));

});
});
}
Expand Down
19 changes: 13 additions & 6 deletions Sample/VirtualListViewSample/ObservableCollectionPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ public ObservableCollectionPage()
}

vlv.Adapter = Adapter;

//vlv.SetBinding(VirtualListView.SelectedItemsProperty, new Binding("SelectedItems", BindingMode.TwoWay, source: this));
}

public ObservableCollectionAdapter<string> Adapter { get; set; }
public ObservableCollection<string> Items = new();

//public ItemPosition[] SelectedItems = Array.Empty<ItemPosition>();

protected override void OnAppearing()
{
base.OnAppearing();
Expand All @@ -47,13 +51,16 @@ private void Button_Clicked(object sender, EventArgs e)

private void vlv_SelectedItemsChanged(object sender, SelectedItemsChangedEventArgs e)
{
var item = e.NewSelection?.FirstOrDefault();

if (item != null)
if (e.NewSelection.Any())
{
Items.RemoveAt(item.Value.ItemIndex);
}
var toDelete = e.NewSelection.First();

(sender as IVirtualListView).ClearSelection();
var item = Adapter.GetItem(toDelete.SectionIndex, toDelete.ItemIndex);

vlv.ClearSelectedItems();

Items.Remove(item);
}

}
}
2 changes: 1 addition & 1 deletion Sample/VirtualListViewSample/SectionedAdapterPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ private void vlv_SelectedItemsChanged(object sender, SelectedItemsChangedEventAr
Adapter.RemoveItem(item.Value.SectionIndex, item.Value.ItemIndex);
}

(sender as IVirtualListView)?.ClearSelection();
(sender as VirtualListView).SelectedItems = null;
}
}
4 changes: 2 additions & 2 deletions VirtualListView/Apple/CvDataSource.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ void TapCellHandler(CvCell cell)
cell.PositionInfo.IsSelected = !cell.PositionInfo.IsSelected;

if (cell.PositionInfo.IsSelected)
Handler.VirtualView?.SelectItems(p);
Handler?.VirtualView?.SelectItem(p);
else
Handler.VirtualView?.DeselectItems(p);
Handler?.VirtualView?.DeselectItem(p);
}

public override nint GetItemsCount(UICollectionView collectionView, nint section)
Expand Down
4 changes: 2 additions & 2 deletions VirtualListView/Apple/CvDelegate.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ void HandleSelection(UICollectionView collectionView, NSIndexPath indexPath, boo
selectedCell.PositionInfo.ItemIndex);

if (selected)
Handler?.VirtualView?.SelectItems(itemPos);
Handler?.VirtualView?.SelectItem(itemPos);
else
Handler?.VirtualView?.DeselectItems(itemPos);
Handler?.VirtualView?.DeselectItem(itemPos);
}
}

Expand Down
45 changes: 14 additions & 31 deletions VirtualListView/Apple/VirtualListViewHandler.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected override void ConnectHandler(UICollectionView nativeView)

dataSource = new CvDataSource(this);
dataSource.IsSelectedHandler = (realSection, realIndex) =>
VirtualView?.IsItemSelected(realSection, realIndex) ?? false;
IsItemSelected(realSection, realIndex);

cvdelegate = new CvDelegate(this, collectionView);
cvdelegate.ScrollHandler = (x, y) =>
Expand Down Expand Up @@ -117,44 +117,27 @@ public static void MapSelectionMode(VirtualListViewHandler handler, IVirtualList
public static void MapInvalidateData(VirtualListViewHandler handler, IVirtualListView virtualListView, object? parameter)
=> handler?.InvalidateData();

public static void MapSelectItems(VirtualListViewHandler handler, IVirtualListView virtualListView, object parameter)
void PlatformUpdateItemSelection(ItemPosition itemPosition, bool selected)
{
if (parameter is ItemPosition[] items)
{
UpdateSelection(handler, items, true);
}
}
var realIndex = PositionalViewSelector?.GetPosition(itemPosition.SectionIndex, itemPosition.ItemIndex) ?? -1;

public static void MapDeselectItems(VirtualListViewHandler handler, IVirtualListView virtualListView, object parameter)
{
if (parameter is ItemPosition[] items)
{
UpdateSelection(handler, items, false);
}
}
if (realIndex < 0)
return;

static void UpdateSelection(VirtualListViewHandler handler, ItemPosition[] itemPositions, bool selected)
{
foreach (var itemPosition in itemPositions)
{
var realIndex = handler.PositionalViewSelector.GetPosition(itemPosition.SectionIndex, itemPosition.ItemIndex);
var cell = collectionView.CellForItem(NSIndexPath.FromItemSection(realIndex, 0));

var cell = handler.collectionView.CellForItem(NSIndexPath.FromItemSection(realIndex, 0));
if (cell is CvCell cvcell)
{
cvcell.PositionInfo.IsSelected = selected;

if (cell is CvCell cvcell)
if (cvcell.VirtualView is IPositionInfo positionInfo)
{
cvcell.PositionInfo.IsSelected = selected;

if (cvcell.VirtualView is IPositionInfo positionInfo)
{
handler.collectionView.InvokeOnMainThread(() =>
{
positionInfo.IsSelected = selected;
});
}
collectionView.InvokeOnMainThread(() =>
{
positionInfo.IsSelected = selected;
});
}
}

}

public static void MapOrientation(VirtualListViewHandler handler, IVirtualListView virtualListView)
Expand Down
63 changes: 50 additions & 13 deletions VirtualListView/Controls/VirtualListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,53 @@ void IVirtualListView.Scrolled(ScrolledEventArgs args)
OnScrolled?.Invoke(this, args);
}

public static readonly BindableProperty ScrolledCommandProperty =
BindableProperty.Create(nameof(ScrolledCommandProperty), typeof(ICommand), typeof(VirtualListView), default);

public ICommand ScrolledCommand
{
get => (ICommand)GetValue(ScrolledCommandProperty);
set => SetValue(ScrolledCommandProperty, value);
}

public IReadOnlyList<ItemPosition> SelectedItems => throw new NotImplementedException();
public static readonly BindableProperty SelectedItemsProperty =
BindableProperty.Create(nameof(SelectedItemsProperty), typeof(IList<ItemPosition>), typeof(VirtualListView), Array.Empty<ItemPosition>(),
propertyChanged: (bobj, ov, nv) =>
{
if (bobj is VirtualListView vlv)
{
Console.WriteLine($"SelectedItems: " + string.Join(", ", nv));
}
});

public static readonly BindableProperty ScrolledCommandProperty =
BindableProperty.Create(nameof(ScrolledCommandProperty), typeof(ICommand), typeof(VirtualListView), default);
public IList<ItemPosition> SelectedItems
{
get => (IList<ItemPosition>)GetValue(SelectedItemsProperty);
set => SetValue(SelectedItemsProperty, value ?? Array.Empty<ItemPosition>());
}

public void DeselectItem(ItemPosition itemPosition)
{
var current = SelectedItems.ToList();
if (current.Contains(itemPosition))
{
current.Remove(itemPosition);
SelectedItems = current.ToArray();

}
}

public void SelectItem(ItemPosition itemPosition)
{
var current = SelectedItems;
if (!current.Contains(itemPosition))
SelectedItems = current.Append(itemPosition).ToArray();
}

public void ClearSelectedItems()
{
SelectedItems = Array.Empty<ItemPosition>();
}

public void InvalidateData()
{
Expand Down Expand Up @@ -309,18 +346,18 @@ public void ViewDetached(PositionInfo position, IView view)
public void ViewAttached(PositionInfo position, IView view)
=> this.AddLogicalChild(view);

public bool IsItemSelected(int sectionIndex, int itemIndex)
=> (Handler as VirtualListViewHandler).IsItemSelected(sectionIndex, itemIndex);
//public bool IsItemSelected(int sectionIndex, int itemIndex)
// => (Handler as VirtualListViewHandler).IsItemSelected(sectionIndex, itemIndex);

public void OnSelectedItemsChanged(SelectedItemsChangedEventArgs args)
=> this.SelectedItemsChanged?.Invoke(this, args);
public void RaiseSelectedItemsChanged(ItemPosition[] previousSelection, ItemPosition[] newSelection)
=> this.SelectedItemsChanged?.Invoke(this, new SelectedItemsChangedEventArgs(previousSelection, newSelection));

public void SelectItems(params ItemPosition[] paths)
=> (Handler as VirtualListViewHandler).SelectItems(paths);
//public void SelectItems(params ItemPosition[] paths)
// => (Handler as VirtualListViewHandler).SelectItems(paths);

public void DeselectItems(params ItemPosition[] paths)
=> (Handler as VirtualListViewHandler).DeselectItems(paths);
//public void DeselectItems(params ItemPosition[] paths)
// => (Handler as VirtualListViewHandler).DeselectItems(paths);

public void ClearSelection()
=> (Handler as VirtualListViewHandler).ClearSelection();
//public void ClearSelectedItems()
// => (Handler as VirtualListViewHandler).ClearSelectedItems();
}
17 changes: 7 additions & 10 deletions VirtualListView/IVirtualListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public interface IVirtualListView : IView
IView Footer { get; }

event EventHandler<SelectedItemsChangedEventArgs> SelectedItemsChanged;
void OnSelectedItemsChanged(SelectedItemsChangedEventArgs eventArgs);

void RaiseSelectedItemsChanged(ItemPosition[] previousSelection, ItemPosition[] newSelection);

event EventHandler DataInvalidated;

Expand All @@ -27,23 +28,19 @@ public interface IVirtualListView : IView

SelectionMode SelectionMode { get; }

IReadOnlyList<ItemPosition> SelectedItems { get; }

ListOrientation Orientation { get; }

IView EmptyView { get; }

// IView RefreshView { get; }

bool IsItemSelected(int sectionIndex, int itemIndex);
IList<ItemPosition> SelectedItems { get; set; }

void SelectItems(params ItemPosition[] paths);
//bool IsItemSelected(int sectionIndex, int itemIndex);

void DeselectItems(params ItemPosition[] paths);
void SelectItem(ItemPosition path);

void ClearSelection();
void DeselectItem(ItemPosition path);

//void InvalidateData();
void ClearSelectedItems();
}

public enum ListOrientation
Expand Down
6 changes: 3 additions & 3 deletions VirtualListView/Platforms/Android/RvAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi
// The template selector doesn't infer selected properly
// so we need to ask the listview which tracks selections about the state
info.IsSelected = info.Kind == PositionKind.Item
&& (handler?.VirtualView?.IsItemSelected(info.SectionIndex, info.ItemIndex) ?? false);
&& (handler?.IsItemSelected(info.SectionIndex, info.ItemIndex) ?? false);

if (holder is RvItemHolder itemHolder)
{
Expand Down Expand Up @@ -134,9 +134,9 @@ public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int
positionInfo.IsSelected = rvh.PositionInfo.IsSelected;

if (rvh.PositionInfo.IsSelected)
handler.VirtualView?.SelectItems(p);
handler?.VirtualView?.SelectItem(p);
else
handler.VirtualView?.DeselectItems(p);
handler?.VirtualView?.DeselectItem(p);
});

viewHolder.ItemView.SetOnClickListener(clickListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,33 +94,18 @@ public static void MapSelectionMode(VirtualListViewHandler handler, IVirtualList
public static void MapInvalidateData(VirtualListViewHandler handler, IVirtualListView virtualListView, object? parameter)
=> handler.InvalidateData();

public static void MapSelectItems(VirtualListViewHandler handler, IVirtualListView virtualListView, object? parameter)
void PlatformUpdateItemSelection(ItemPosition itemPosition, bool selected)
{
if (parameter is ItemPosition[] itemPositions)
UpdateSelection(handler, itemPositions, true);
}
var position = PositionalViewSelector.GetPosition(itemPosition.SectionIndex, itemPosition.ItemIndex);

public static void MapDeselectItems(VirtualListViewHandler handler, IVirtualListView virtualListView, object? parameter)
{
if (parameter is ItemPosition[] itemPositions)
UpdateSelection(handler, itemPositions, false);
}
var vh = recyclerView.FindViewHolderForAdapterPosition(position);

static void UpdateSelection(VirtualListViewHandler handler, ItemPosition[] itemPositions, bool selected)
{
foreach (var itemPosition in itemPositions)
if (vh is RvItemHolder rvh)
{
var position = handler.PositionalViewSelector.GetPosition(itemPosition.SectionIndex, itemPosition.ItemIndex);

var vh = handler.recyclerView.FindViewHolderForAdapterPosition(position);

if (vh is RvItemHolder rvh)
{
rvh.PositionInfo.IsSelected = selected;
rvh.PositionInfo.IsSelected = selected;

if (rvh.ViewContainer?.VirtualView is IPositionInfo viewPositionInfo)
viewPositionInfo.IsSelected = selected;
}
if (rvh.ViewContainer?.VirtualView is IPositionInfo viewPositionInfo)
viewPositionInfo.IsSelected = selected;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,19 @@ public static void MapSelectItems(VirtualListViewHandler handler, IVirtualListVi
{
if (parameter is ItemPosition[] items && items != null && items.Length > 0)
{
UpdateSelection(handler, items, true);
PlatformUpdateSelection(handler, items, true);
}
}

public static void MapDeselectItems(VirtualListViewHandler handler, IVirtualListView virtualListView, object? parameter)
{
if (parameter is ItemPosition[] items && items != null && items.Length > 0)
{
UpdateSelection(handler, items, false);
PlatformUpdateSelection(handler, items, false);
}
}

static void UpdateSelection(VirtualListViewHandler handler, ItemPosition[] itemPositions, bool selected)
static void PlatformUpdateSelection(VirtualListViewHandler handler, ItemPosition[] itemPositions, bool selected)
{
foreach (var itemPosition in itemPositions)
{
Expand Down
Loading

0 comments on commit 89fe250

Please sign in to comment.