public ItemsViewModel() { AddItem = ReactiveCommand.CreateFromTask(AddItemImpl); Items = new ReactiveList <ShopItem>(); CurrentPage = new ReactiveProperty <int>(1); PageCount = new ReactiveProperty <int>(ShopService.Instance.Items.Count); Search = new ReactiveProperty <string>(""); ShopService.Instance.Items.Changed.Subscribe(_ => { PageCount.Value = GetFilteredItems().Count() / ItemsPerPage; UpdateItems(); }); _pageString = this.WhenAnyValue(x => x.CurrentPage.Value, x => x.PageCount.Value) .Select(x => $"{x.Item1} / {x.Item2}") .ToProperty(this, x => x.PageString); this.WhenAnyValue(x => x.PageCount.Value) .Subscribe(x => { if (CurrentPage.Value > x) { CurrentPage.Value = x; } UpdateItems(); }); this.WhenAnyValue(x => x.Search.Value) .Throttle(TimeSpan.FromSeconds(2)) .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(_ => { PageCount.Value = GetFilteredItems().Count() / ItemsPerPage; UpdateItems(); }); var canNextPage = this.WhenAnyValue(x => x.CurrentPage.Value, x => x.PageCount.Value) .Select(x => x.Item1 < x.Item2); var canPrevPage = this.WhenAnyValue(x => x.CurrentPage.Value) .Select(x => x > 1); NextPage = ReactiveCommand.CreateFromTask(NextPageImpl, canNextPage); PrevPage = ReactiveCommand.CreateFromTask(PrevPageImpl, canPrevPage); }
private static ReactiveCommand <object, Unit> SendCommandEx( IDispatcher bus, Func <object, Command> commandFunc, IObservable <bool> canExecute = null, IScheduler scheduler = null, string userErrorMsg = null, TimeSpan?responseTimeout = null, TimeSpan?ackTimeout = null) { if (scheduler == null) { scheduler = RxApp.MainThreadScheduler; } Func <object, Task> task = async x => await Task.Run(() => { var c = commandFunc(x); if (c != null) { bus.Send(c, userErrorMsg, responseTimeout, ackTimeout); } }); var cmd = canExecute == null? ReactiveCommand.CreateFromTask(task, outputScheduler : scheduler) : ReactiveCommand.CreateFromTask(task, canExecute, scheduler); cmd.ThrownExceptions .SelectMany(ex => Interactions.Errors.Handle(new UserError(userErrorMsg ?? ex.Message, ex))) .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(result => { // This will return the recovery option returned from the registered user error handler, // e.g. a simple message box in the view code behind /* n.b. this forces evaluation/execution of the select many */ }); return(cmd); }
private static ReactiveCommand FireCommandEx( IDispatcher bus, Func <Object, Command> commandFunc, IObservable <bool> canExecute = null, IScheduler scheduler = null, string userErrorMsg = null, TimeSpan?responseTimeout = null, TimeSpan?ackTimeout = null) { if (scheduler == null) { scheduler = RxApp.MainThreadScheduler; } Func <object, Task> task = async _ => await Task.Run(() => { var c = commandFunc(_); if (c != null) { bus.Send(c, userErrorMsg, responseTimeout, ackTimeout); } }); var cmd = ReactiveCommand.CreateFromTask(task, canExecute, scheduler); cmd.ThrownExceptions #pragma warning disable CS0618 // Type or member is obsolete .SelectMany(ex => UserError.Throw(userErrorMsg ?? ex.Message, ex)) #pragma warning restore CS0618 // Type or member is obsolete .ObserveOn(MainThreadScheduler).Subscribe(result => { //This will return the recovery option returned from the registered user error handler //right now this is a simple message box in the view code behind /* n.b. this forces evaluation/execution of the select many */ }); return(cmd); }
private static ReactiveCommand FromAction( Action <object> action, IObservable <bool> canExecute = null, IScheduler scheduler = null, string userErrorMsg = null) { if (scheduler == null) { scheduler = RxApp.MainThreadScheduler; } Func <object, Task> task = async _ => await Task.Run(() => action(_)); var cmd = ReactiveCommand.CreateFromTask(task, canExecute, scheduler); cmd.ThrownExceptions .ObserveOn(MainThreadScheduler).SelectMany(ex => UserError.Throw(userErrorMsg ?? ex.Message, ex)) .Subscribe(result => { //This will return the recovery option returned from the registered user error handler //right now this is a simple message box in the view code behind /* n.b. this forces evaluation/execution of the select many */ }); return(cmd); }
public ViewModel2( SynchronizationContext context, IObservable <string> name, IObservable <string> password, IObservable <int> progress) { name.ToProperty(this, x => x.UserName, out _userName); password.ToProperty(this, x => x.Password, out _password); progress.ToProperty(this, x => x.Progress, out _progress); CanUserLogin = this.WhenAnyValue( x => x.UserName, x => x.Password, (user, pass) => !string.IsNullOrWhiteSpace(user) && !string.IsNullOrWhiteSpace(pass) && user.Length >= 2 && pass.Length >= 3) .DistinctUntilChanged(); CmdLogin = ReactiveCommand.Create(() => CanUserLogin, CanUserLogin); //.InvokeCommand(CmdProcessLoginAsync); CmdProcessLoginAsync = ReactiveCommand.CreateFromTask(() => { return(Task.Run(() => { Progress = 0; while (Progress <= 100) { Progress += 10; Thread.Sleep(100); } })); }, CanUserLogin); //CmdCancel = ReactiveCommand.CreateCombined(); }
public MainViewViewModel(IUserDialogs userDialogs, IDotaClientDistancePatcher distancePatcher, SettingsViewModel settingsViewModel, IScreen hostScreen) { SettingsViewModel = settingsViewModel; HostScreen = hostScreen; var pathCanExecute = this.WhenAnyValue( model => model.SettingsViewModel.Settings.Value.X32Client.Distance.Value, model => model.SettingsViewModel.Settings.Value.X64Client.Distance.Value, (x32, x64) => new[] { x32, x64 }) .Select(items => items.All(value => value >= 1000 && value <= 9999)); PatchCommand = ReactiveCommand.CreateFromTask <Settings>(distancePatcher.PatchAsync, pathCanExecute); PatchCommand .SubscribeOnUIDispatcher() .Subscribe(_ => { userDialogs.Alert("Done!"); }); PatchCommand.ThrownExceptions .SubscribeOnUIDispatcher() .Subscribe(exception => userDialogs.Alert("Error occurred!")); }
public PullRequestDetailViewModel( IPullRequestService pullRequestsService, IPullRequestSessionManager sessionManager, IModelServiceFactory modelServiceFactory, IUsageTracker usageTracker, ITeamExplorerContext teamExplorerContext, IPullRequestFilesViewModel files, ISyncSubmodulesCommand syncSubmodulesCommand, IViewViewModelFactory viewViewModelFactory, IGitService gitService, IOpenIssueishDocumentCommand openDocumentCommand, [Import(AllowDefault = true)] JoinableTaskContext joinableTaskContext) { Guard.ArgumentNotNull(pullRequestsService, nameof(pullRequestsService)); Guard.ArgumentNotNull(sessionManager, nameof(sessionManager)); Guard.ArgumentNotNull(modelServiceFactory, nameof(modelServiceFactory)); Guard.ArgumentNotNull(usageTracker, nameof(usageTracker)); Guard.ArgumentNotNull(teamExplorerContext, nameof(teamExplorerContext)); Guard.ArgumentNotNull(syncSubmodulesCommand, nameof(syncSubmodulesCommand)); Guard.ArgumentNotNull(viewViewModelFactory, nameof(viewViewModelFactory)); Guard.ArgumentNotNull(gitService, nameof(gitService)); Guard.ArgumentNotNull(openDocumentCommand, nameof(openDocumentCommand)); this.pullRequestsService = pullRequestsService; this.sessionManager = sessionManager; this.modelServiceFactory = modelServiceFactory; this.usageTracker = usageTracker; this.teamExplorerContext = teamExplorerContext; this.syncSubmodulesCommand = syncSubmodulesCommand; this.viewViewModelFactory = viewViewModelFactory; this.gitService = gitService; this.openDocumentCommand = openDocumentCommand; JoinableTaskContext = joinableTaskContext ?? ThreadHelper.JoinableTaskContext; Files = files; Checkout = ReactiveCommand.CreateFromObservable( DoCheckout, this.WhenAnyValue(x => x.CheckoutState) .Cast <CheckoutCommandState>() .Select(x => x != null && x.IsEnabled)); Checkout.IsExecuting.Subscribe(x => isInCheckout = x); SubscribeOperationError(Checkout); Pull = ReactiveCommand.CreateFromObservable( DoPull, this.WhenAnyValue(x => x.UpdateState) .Cast <UpdateCommandState>() .Select(x => x != null && x.PullEnabled)); SubscribeOperationError(Pull); Push = ReactiveCommand.CreateFromObservable( DoPush, this.WhenAnyValue(x => x.UpdateState) .Cast <UpdateCommandState>() .Select(x => x != null && x.PushEnabled)); SubscribeOperationError(Push); SyncSubmodules = ReactiveCommand.CreateFromTask( DoSyncSubmodules, this.WhenAnyValue(x => x.UpdateState) .Cast <UpdateCommandState>() .Select(x => x != null && x.SyncSubmodulesEnabled)); SyncSubmodules.Subscribe(_ => Refresh().ToObservable()); SubscribeOperationError(SyncSubmodules); OpenConversation = ReactiveCommand.Create(DoOpenConversation); OpenOnGitHub = ReactiveCommand.Create(DoOpenDetailsUrl); ShowReview = ReactiveCommand.Create <IPullRequestReviewSummaryViewModel>(DoShowReview); ShowAnnotations = ReactiveCommand.Create <IPullRequestCheckViewModel>(DoShowAnnotations); }