Skip to content

Commit

Permalink
Refresh now passes completion action
Browse files Browse the repository at this point in the history
So that the choice of when to end showing the refresh indicator can be managed by the consumer.
  • Loading branch information
Redth committed Aug 15, 2023
1 parent 976b760 commit 4abec86
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
4 changes: 3 additions & 1 deletion Sample/VirtualListViewSample/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ public MainViewModel()
MusicDataAdapter adapter;

[RelayCommand]
async Task Refresh()
async Task Refresh(Action completion)
{
await Task.Delay(3000);
System.Diagnostics.Debug.WriteLine("Refresh Complete");
completion?.Invoke();
}

[RelayCommand]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ protected override UICollectionView CreatePlatformView()
refreshControl.AddTarget(new EventHandler((s, a) =>
{
refreshControl.BeginRefreshing();
VirtualView?.Refresh();
refreshControl.EndRefreshing();

VirtualView?.Refresh(() => refreshControl.EndRefreshing());
}), UIControlEvent.ValueChanged);

collectionView.AddSubview(refreshControl);
Expand Down
8 changes: 4 additions & 4 deletions VirtualListView/Controls/VirtualListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ public Maui.SelectionMode SelectionMode

public event EventHandler<SelectedItemsChangedEventArgs> OnSelectedItemsChanged;

public event EventHandler<EventArgs> OnRefresh;
public event EventHandler<RefreshEventArgs> OnRefresh;

void IVirtualListView.Refresh()
void IVirtualListView.Refresh(Action completionCallback)
{
if (RefreshCommand != null && RefreshCommand.CanExecute(null))
{
RefreshCommand.Execute(null);
RefreshCommand.Execute(completionCallback);
}

OnRefresh?.Invoke(this, EventArgs.Empty);
OnRefresh?.Invoke(this, new RefreshEventArgs(completionCallback));
}

public ICommand RefreshCommand
Expand Down
2 changes: 1 addition & 1 deletion VirtualListView/IVirtualListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface IVirtualListView : IView

Color RefreshAccentColor { get; }

void Refresh();
void Refresh(Action completionCallback);

bool IsRefreshEnabled { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ protected override void ConnectHandler(FrameLayout nativeView)
{
swipeRefreshLayout.SetOnRefreshListener(new SrlRefreshListener(() =>
{
VirtualView?.Refresh();
swipeRefreshLayout.Refreshing = false;
VirtualView?.Refresh(() => swipeRefreshLayout.Refreshing = false);
}));

layoutManager = new LinearLayoutManager(Context);
Expand Down
12 changes: 12 additions & 0 deletions VirtualListView/RefreshEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Microsoft.Maui;

public class RefreshEventArgs : EventArgs
{
public RefreshEventArgs(Action completion)
: base()
{
Complete = completion;
}

public readonly Action Complete;
}
2 changes: 1 addition & 1 deletion VirtualListView/SelectedItemsChangedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ public SelectedItemsChangedEventArgs(
public IReadOnlyList<ItemPosition> PreviousSelection { get; }

public IReadOnlyList<ItemPosition> NewSelection { get; }
}
}

0 comments on commit 4abec86

Please sign in to comment.