protected virtual void LoadViewModel() { var iLoadableViewModel = ViewModel as ILoadableViewModel; if (iLoadableViewModel == null) { return; } var refreshControl = new UIRefreshControl(); OnActivation(d => { d(refreshControl.GetChangedObservable().Subscribe(_ => { if (iLoadableViewModel.LoadCommand.CanExecute(null)) { iLoadableViewModel.LoadCommand.ExecuteAsync() .Subscribe(__ => refreshControl.EndRefreshing()); } })); d(iLoadableViewModel.LoadCommand.IsExecuting .Where(x => x && !refreshControl.Refreshing).Subscribe(_ => { nint rows = 0; if (TableView.Source != null) { for (var i = 0; i < TableView.Source.NumberOfSections(TableView); i++) { rows += TableView.Source.RowsInSection(TableView, i); } } if (rows == 0) { _loadingActivityView.Value.StartAnimating(); TableView.TableFooterView = _loadingActivityView.Value; RefreshControl.Do(x => x.EndRefreshing()); RefreshControl = null; } })); d(iLoadableViewModel.LoadCommand.IsExecuting .Where(x => !x).Subscribe(_ => { _loadingActivityView.Value.StopAnimating(); if (TableView.TableFooterView != null) { TableView.TableFooterView = null; TableView.ReloadData(); } if (RefreshControl == null) { RefreshControl = refreshControl; } })); }); }
protected virtual void LoadViewModel() { var iLoadableViewModel = ViewModel as ILoadableViewModel; var iPaginatableViewModel = ViewModel as IPaginatableViewModel; if (iLoadableViewModel == null) { return; } var refreshControl = new UIRefreshControl(); refreshControl.ValueChanged += async(sender, e) => { if (iLoadableViewModel.LoadCommand.CanExecute(null)) { await iLoadableViewModel.LoadCommand.ExecuteAsync(); refreshControl.EndRefreshing(); } }; iLoadableViewModel.LoadCommand.IsExecuting .Where(x => x && !refreshControl.Refreshing).Subscribe(_ => { nint rows = 0; if (TableView.Source != null) { for (var i = 0; i < TableView.Source.NumberOfSections(TableView); i++) { rows += TableView.Source.RowsInSection(TableView, i); } } if (rows == 0) { _loadingActivityView.Value.StartAnimating(); TableView.TableFooterView = _loadingActivityView.Value; RefreshControl.Do(x => x.EndRefreshing()); RefreshControl = null; } }); iLoadableViewModel.LoadCommand.IsExecuting .Where(x => !x).Subscribe(_ => { _loadingActivityView.Value.StopAnimating(); if (TableView.TableFooterView != null) { TableView.TableFooterView = null; TableView.ReloadData(); } if (RefreshControl == null) { RefreshControl = refreshControl; } }); this.WhenActivated(d => { var iSourceInformsEnd = TableView.Source as IInformsEnd; if (iPaginatableViewModel != null && iSourceInformsEnd != null) { d(iSourceInformsEnd.RequestMore.Select(__ => iPaginatableViewModel.LoadMoreCommand).IsNotNull().Subscribe(async x => { _loadingActivityView.Value.StartAnimating(); TableView.TableFooterView = _loadingActivityView.Value; await x.ExecuteAsync(); TableView.TableFooterView = null; _loadingActivityView.Value.StopAnimating(); })); } var iSourceInformsEmpty = TableView.Source as IInformsEmpty; if (iSourceInformsEmpty != null) { d(iSourceInformsEmpty.IsEmpty.CombineLatest(iLoadableViewModel.LoadCommand.IsExecuting, (x, y) => x && !y) .Where(_ => EmptyView != null) .Subscribe(x => { if (x) { if (EmptyView.Value.Superview == null) { EmptyView.Value.Alpha = 0f; EmptyView.Value.Frame = new CGRect(0, 0, TableView.Bounds.Width, TableView.Bounds.Height * 2f); TableView.AddSubview(EmptyView.Value); UIView.Animate(0.2f, 0f, UIViewAnimationOptions.AllowUserInteraction | UIViewAnimationOptions.CurveEaseIn, () => EmptyView.Value.Alpha = 1.0f, null); } } else if (EmptyView.IsValueCreated) { EmptyView.Value.RemoveFromSuperview(); } })); } }); }