From 4abec864f4726f0ab5258f0d541c005152db32b7 Mon Sep 17 00:00:00 2001 From: redth Date: Tue, 15 Aug 2023 13:28:59 -0400 Subject: [PATCH] Refresh now passes completion action So that the choice of when to end showing the refresh indicator can be managed by the consumer. --- Sample/VirtualListViewSample/MainViewModel.cs | 4 +++- .../Apple/VirtualListViewHandler.ios.maccatalyst.cs | 4 +--- VirtualListView/Controls/VirtualListView.cs | 8 ++++---- VirtualListView/IVirtualListView.cs | 2 +- .../Android/VirtualListViewHandler.android.cs | 3 +-- VirtualListView/RefreshEventArgs.cs | 12 ++++++++++++ VirtualListView/SelectedItemsChangedEventArgs.cs | 2 +- 7 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 VirtualListView/RefreshEventArgs.cs diff --git a/Sample/VirtualListViewSample/MainViewModel.cs b/Sample/VirtualListViewSample/MainViewModel.cs index 653fc72..ad879f0 100644 --- a/Sample/VirtualListViewSample/MainViewModel.cs +++ b/Sample/VirtualListViewSample/MainViewModel.cs @@ -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] diff --git a/VirtualListView/Apple/VirtualListViewHandler.ios.maccatalyst.cs b/VirtualListView/Apple/VirtualListViewHandler.ios.maccatalyst.cs index b7d4b6e..5062a17 100644 --- a/VirtualListView/Apple/VirtualListViewHandler.ios.maccatalyst.cs +++ b/VirtualListView/Apple/VirtualListViewHandler.ios.maccatalyst.cs @@ -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); diff --git a/VirtualListView/Controls/VirtualListView.cs b/VirtualListView/Controls/VirtualListView.cs index e1be2bb..d4640ae 100644 --- a/VirtualListView/Controls/VirtualListView.cs +++ b/VirtualListView/Controls/VirtualListView.cs @@ -111,16 +111,16 @@ public Maui.SelectionMode SelectionMode public event EventHandler OnSelectedItemsChanged; - public event EventHandler OnRefresh; + public event EventHandler 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 diff --git a/VirtualListView/IVirtualListView.cs b/VirtualListView/IVirtualListView.cs index 6dbc2e8..83b54d9 100644 --- a/VirtualListView/IVirtualListView.cs +++ b/VirtualListView/IVirtualListView.cs @@ -26,7 +26,7 @@ public interface IVirtualListView : IView Color RefreshAccentColor { get; } - void Refresh(); + void Refresh(Action completionCallback); bool IsRefreshEnabled { get; } diff --git a/VirtualListView/Platforms/Android/VirtualListViewHandler.android.cs b/VirtualListView/Platforms/Android/VirtualListViewHandler.android.cs index 8d30f5f..295a76c 100644 --- a/VirtualListView/Platforms/Android/VirtualListViewHandler.android.cs +++ b/VirtualListView/Platforms/Android/VirtualListViewHandler.android.cs @@ -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); diff --git a/VirtualListView/RefreshEventArgs.cs b/VirtualListView/RefreshEventArgs.cs new file mode 100644 index 0000000..02b0f62 --- /dev/null +++ b/VirtualListView/RefreshEventArgs.cs @@ -0,0 +1,12 @@ +namespace Microsoft.Maui; + +public class RefreshEventArgs : EventArgs +{ + public RefreshEventArgs(Action completion) + : base() + { + Complete = completion; + } + + public readonly Action Complete; +} \ No newline at end of file diff --git a/VirtualListView/SelectedItemsChangedEventArgs.cs b/VirtualListView/SelectedItemsChangedEventArgs.cs index c076aeb..1a88f25 100644 --- a/VirtualListView/SelectedItemsChangedEventArgs.cs +++ b/VirtualListView/SelectedItemsChangedEventArgs.cs @@ -14,4 +14,4 @@ public SelectedItemsChangedEventArgs( public IReadOnlyList PreviousSelection { get; } public IReadOnlyList NewSelection { get; } -} \ No newline at end of file +}