Skip to content

Commit

Permalink
Clean up events
Browse files Browse the repository at this point in the history
  • Loading branch information
Redth committed Jul 27, 2023
1 parent 186473b commit ad41c4e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected override void ConnectHandler(UICollectionView nativeView)

cvdelegate = new CvDelegate(this, collectionView);
cvdelegate.ScrollHandler = (x, y) =>
VirtualView?.Scrolled(new ScrolledEventArgs(x, y));
VirtualView?.Scrolled(x, y);

collectionView.DataSource = dataSource;
collectionView.Delegate = cvdelegate;
Expand Down
77 changes: 15 additions & 62 deletions VirtualListView/Controls/VirtualListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Maui.SelectionMode SelectionMode
public static readonly BindableProperty SelectionModeProperty =
BindableProperty.Create(nameof(SelectionMode), typeof(Maui.SelectionMode), typeof(VirtualListView), Maui.SelectionMode.None);

public event EventHandler<SelectedItemsChangedEventArgs> SelectedItemsChanged;
public event EventHandler<SelectedItemsChangedEventArgs> OnSelectedItemsChanged;

public event EventHandler<EventArgs> OnRefresh;

Expand Down Expand Up @@ -183,50 +183,19 @@ public View EmptyView
IView IVirtualListView.EmptyView => EmptyView;



//public View RefreshView
//{
// get => (View)GetValue(RefreshViewProperty);
// set => SetValue(RefreshViewProperty, value);
//}

//public static readonly BindableProperty RefreshViewProperty =
// BindableProperty.Create(nameof(EmptyView), typeof(View), typeof(VirtualListView), null,
// propertyChanged: (bobj, oldValue, newValue) =>
// {
// if (bobj is VirtualListView virtualListView)
// {
// if (oldValue is IView oldView)
// virtualListView.RemoveLogicalChild(oldView);

// if (newValue is IView newView)
// virtualListView.AddLogicalChild(newView);
// }
// });

//IView IVirtualListView.RefreshView => RefreshView;




public IVirtualListViewSelector ViewSelector => this;

public IView Header => GlobalHeader;
public IView Footer => GlobalFooter;



public event EventHandler DataInvalidated;


public event EventHandler<ScrolledEventArgs> OnScrolled;

void IVirtualListView.Scrolled(ScrolledEventArgs args)
public void Scrolled(double x, double y)
{
var args = new ScrolledEventArgs(x, y);

if (ScrolledCommand != null && ScrolledCommand.CanExecute(args))
{
ScrolledCommand.Execute(args);
}

OnScrolled?.Invoke(this, args);
}
Expand All @@ -242,11 +211,13 @@ public ICommand ScrolledCommand

public static readonly BindableProperty SelectedItemsProperty =
BindableProperty.Create(nameof(SelectedItems), typeof(IList<ItemPosition>), typeof(VirtualListView), Array.Empty<ItemPosition>(),
propertyChanged: (bobj, ov, nv) =>
propertyChanged: (bindableObj, oldValue, newValue) =>
{
if (bobj is VirtualListView vlv)
if (bindableObj is VirtualListView vlv
&& oldValue is IList<ItemPosition> oldSelection
&& newValue is IList<ItemPosition> newSelection)
{
Console.WriteLine($"SelectedItems: " + string.Join(", ", nv));
vlv.RaiseSelectedItemsChanged(oldSelection.ToArray(), newSelection.ToArray());
}
});

Expand All @@ -263,7 +234,7 @@ public void DeselectItem(ItemPosition itemPosition)
{
current.Remove(itemPosition);
SelectedItems = current.ToArray();

}
}

Expand All @@ -279,22 +250,16 @@ public void ClearSelectedItems()
SelectedItems = Array.Empty<ItemPosition>();
}

public void InvalidateData()
{
(Handler as VirtualListViewHandler)?.InvalidateData();

DataInvalidated?.Invoke(this, new EventArgs());
}

public bool SectionHasHeader(int sectionIndex)
=> SectionHeaderTemplateSelector != null || SectionHeaderTemplate != null;

public bool SectionHasFooter(int sectionIndex)
=> SectionFooterTemplateSelector != null || SectionFooterTemplate != null;

public IView CreateView(PositionInfo position, object data)
=> position.Kind switch {
PositionKind.Item =>
=> position.Kind switch
{
PositionKind.Item =>
ItemTemplateSelector?.SelectTemplate(data, position.SectionIndex, position.ItemIndex)?.CreateContent() as View
?? ItemTemplate?.CreateContent() as View,
PositionKind.SectionHeader =>
Expand Down Expand Up @@ -346,18 +311,6 @@ 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 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 DeselectItems(params ItemPosition[] paths)
// => (Handler as VirtualListViewHandler).DeselectItems(paths);

//public void ClearSelectedItems()
// => (Handler as VirtualListViewHandler).ClearSelectedItems();
void RaiseSelectedItemsChanged(ItemPosition[] previousSelection, ItemPosition[] newSelection)
=> this.OnSelectedItemsChanged?.Invoke(this, new SelectedItemsChangedEventArgs(previousSelection, newSelection));
}
18 changes: 7 additions & 11 deletions VirtualListView/IVirtualListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,26 @@ public interface IVirtualListView : IView

IView Footer { get; }

event EventHandler<SelectedItemsChangedEventArgs> SelectedItemsChanged;
event EventHandler<ScrolledEventArgs> OnScrolled;

void RaiseSelectedItemsChanged(ItemPosition[] previousSelection, ItemPosition[] newSelection);
void Scrolled(double x, double y);

event EventHandler DataInvalidated;
SelectionMode SelectionMode { get; }

IList<ItemPosition> SelectedItems { get; set; }

event EventHandler<SelectedItemsChangedEventArgs> OnSelectedItemsChanged;

Color RefreshAccentColor { get; }

void Refresh();

bool IsRefreshEnabled { get; }

void Scrolled(ScrolledEventArgs args);

SelectionMode SelectionMode { get; }

ListOrientation Orientation { get; }

IView EmptyView { get; }

IList<ItemPosition> SelectedItems { get; set; }

//bool IsItemSelected(int sectionIndex, int itemIndex);

void SelectItem(ItemPosition path);

void DeselectItem(ItemPosition path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected override void ConnectHandler(FrameLayout nativeView)
var x = Context.FromPixels(dx);
var y = Context.FromPixels(dy);

VirtualView?.Scrolled(new ScrolledEventArgs(x, y));
VirtualView?.Scrolled(x, y);
}));

recyclerView.SetLayoutManager(layoutManager);
Expand Down
2 changes: 0 additions & 2 deletions VirtualListView/VirtualListViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,5 @@ public static void MapSelectedItems(VirtualListViewHandler handler, IVirtualList

// Keep track of the new state for next time it changes
handler.previousSelections = newSelections.ToArray();

handler.VirtualView.RaiseSelectedItemsChanged(prev, newSelections.ToArray());
}
}

0 comments on commit ad41c4e

Please sign in to comment.