Skip to content

Commit

Permalink
Add setter to IsRefreshing binding
Browse files Browse the repository at this point in the history
  • Loading branch information
brminnick committed Sep 21, 2024
1 parent bbacfd5 commit 72408d1
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions src/HackerNews/Pages/NewsPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,55 @@ class NewsPage : BaseContentPage<NewsViewModel>
readonly IDispatcher _dispatcher;

public NewsPage(IBrowser browser,
IDispatcher dispatcher,
NewsViewModel newsViewModel) : base(newsViewModel, "Top Stories")
IDispatcher dispatcher,
NewsViewModel newsViewModel) : base(newsViewModel, "Top Stories")
{
_browser = browser;
_dispatcher = dispatcher;

BindingContext.PullToRefreshFailed += HandlePullToRefreshFailed;

Content = new RefreshView
{
RefreshColor = Colors.Black,

Content = new CollectionView
{
BackgroundColor = Color.FromArgb("F6F6EF"),
SelectionMode = SelectionMode.Single,
ItemTemplate = new StoryDataTemplate(),

}.Bind(CollectionView.ItemsSourceProperty, static (NewsViewModel vm) => vm.TopStoryCollection)
.Invoke(collectionView => collectionView.SelectionChanged += HandleSelectionChanged)

}.Bind(RefreshView.IsRefreshingProperty, static (NewsViewModel vm) => vm.IsListRefreshing)
.Bind(RefreshView.CommandProperty, static (NewsViewModel vm) => vm.RefreshCommand);
RefreshColor = Colors.Black,

Content = new CollectionView
{
BackgroundColor = Color.FromArgb("F6F6EF"),
SelectionMode = SelectionMode.Single,
ItemTemplate = new StoryDataTemplate(),

}.Bind(CollectionView.ItemsSourceProperty,
getter: static (NewsViewModel vm) => vm.TopStoryCollection)
.Invoke(collectionView => collectionView.SelectionChanged += HandleSelectionChanged)

}.Bind(RefreshView.IsRefreshingProperty,
getter: static (NewsViewModel vm) => vm.IsListRefreshing,
setter: static (vm, isRefreshing) => vm.IsListRefreshing = isRefreshing)
.Bind(RefreshView.CommandProperty,
getter: static (NewsViewModel vm) => vm.RefreshCommand,
mode: BindingMode.OneTime);
}

protected override void OnAppearing()
{
base.OnAppearing();

if (Content is RefreshView refreshView
&& refreshView.Content is CollectionView collectionView
if (Content is RefreshView { Content: CollectionView collectionView } refreshView
&& IsNullOrEmpty(collectionView.ItemsSource))
{
refreshView.IsRefreshing = true;
}

static bool IsNullOrEmpty(in IEnumerable? enumerable) => !enumerable?.GetEnumerator().MoveNext() ?? true;
static bool IsNullOrEmpty(in IEnumerable? enumerable)
{
if (enumerable is null)
return false;

var enumerator = enumerable.GetEnumerator() ?? throw new InvalidOperationException("Enumerator not found");
using var disposable = (IDisposable)enumerator;
return !enumerator.MoveNext();
}
}

async void HandleSelectionChanged(object? sender, SelectionChangedEventArgs e)
Expand Down

0 comments on commit 72408d1

Please sign in to comment.