Skip to content

Commit 72408d1

Browse files
Add setter to IsRefreshing binding
1 parent bbacfd5 commit 72408d1

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

src/HackerNews/Pages/NewsPage.cs

+30-18
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,55 @@ class NewsPage : BaseContentPage<NewsViewModel>
99
readonly IDispatcher _dispatcher;
1010

1111
public NewsPage(IBrowser browser,
12-
IDispatcher dispatcher,
13-
NewsViewModel newsViewModel) : base(newsViewModel, "Top Stories")
12+
IDispatcher dispatcher,
13+
NewsViewModel newsViewModel) : base(newsViewModel, "Top Stories")
1414
{
1515
_browser = browser;
1616
_dispatcher = dispatcher;
1717

1818
BindingContext.PullToRefreshFailed += HandlePullToRefreshFailed;
1919

2020
Content = new RefreshView
21-
{
22-
RefreshColor = Colors.Black,
23-
24-
Content = new CollectionView
2521
{
26-
BackgroundColor = Color.FromArgb("F6F6EF"),
27-
SelectionMode = SelectionMode.Single,
28-
ItemTemplate = new StoryDataTemplate(),
29-
30-
}.Bind(CollectionView.ItemsSourceProperty, static (NewsViewModel vm) => vm.TopStoryCollection)
31-
.Invoke(collectionView => collectionView.SelectionChanged += HandleSelectionChanged)
32-
33-
}.Bind(RefreshView.IsRefreshingProperty, static (NewsViewModel vm) => vm.IsListRefreshing)
34-
.Bind(RefreshView.CommandProperty, static (NewsViewModel vm) => vm.RefreshCommand);
22+
RefreshColor = Colors.Black,
23+
24+
Content = new CollectionView
25+
{
26+
BackgroundColor = Color.FromArgb("F6F6EF"),
27+
SelectionMode = SelectionMode.Single,
28+
ItemTemplate = new StoryDataTemplate(),
29+
30+
}.Bind(CollectionView.ItemsSourceProperty,
31+
getter: static (NewsViewModel vm) => vm.TopStoryCollection)
32+
.Invoke(collectionView => collectionView.SelectionChanged += HandleSelectionChanged)
33+
34+
}.Bind(RefreshView.IsRefreshingProperty,
35+
getter: static (NewsViewModel vm) => vm.IsListRefreshing,
36+
setter: static (vm, isRefreshing) => vm.IsListRefreshing = isRefreshing)
37+
.Bind(RefreshView.CommandProperty,
38+
getter: static (NewsViewModel vm) => vm.RefreshCommand,
39+
mode: BindingMode.OneTime);
3540
}
3641

3742
protected override void OnAppearing()
3843
{
3944
base.OnAppearing();
4045

41-
if (Content is RefreshView refreshView
42-
&& refreshView.Content is CollectionView collectionView
46+
if (Content is RefreshView { Content: CollectionView collectionView } refreshView
4347
&& IsNullOrEmpty(collectionView.ItemsSource))
4448
{
4549
refreshView.IsRefreshing = true;
4650
}
4751

48-
static bool IsNullOrEmpty(in IEnumerable? enumerable) => !enumerable?.GetEnumerator().MoveNext() ?? true;
52+
static bool IsNullOrEmpty(in IEnumerable? enumerable)
53+
{
54+
if (enumerable is null)
55+
return false;
56+
57+
var enumerator = enumerable.GetEnumerator() ?? throw new InvalidOperationException("Enumerator not found");
58+
using var disposable = (IDisposable)enumerator;
59+
return !enumerator.MoveNext();
60+
}
4961
}
5062

5163
async void HandleSelectionChanged(object? sender, SelectionChangedEventArgs e)

0 commit comments

Comments
 (0)