public SelectItemViewModel() { Search = new ReactiveProperty <string>(); Items = new ReactiveList <Item>(); SelectedItem = new ReactiveProperty <Item>(); Search.WhenAnyValue(x => x.Value) .Throttle(TimeSpan.FromSeconds(1)) .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(search => { Items.Clear(); // It's not possible to add an item more than once so remove existing shop items from the search var items = ResourceService.Instance.Items.Where(item => ShopService.Instance.Items.All(x => x.ItemNumber != item.ItemNumber)); if (!string.IsNullOrWhiteSpace(search)) { var split = search.Split(' '); items = items.Where(item => split.All(word => item.Name.Contains(word, StringComparison.OrdinalIgnoreCase))); } Items.AddRange(items); }); var canSelect = SelectedItem.WhenAnyValue(x => x.Value).Select(x => x != null); Select = ReactiveCommand.Create(SelectImpl, canSelect); Cancel = ReactiveCommand.Create(CancelImpl); }
public SelectEffectViewModel() { Search = new ReactiveProperty <string>(); Effects = new ReactiveList <Effect>(); SelectedEffect = new ReactiveProperty <Effect>(); Search.WhenAnyValue(x => x.Value) .Throttle(TimeSpan.FromSeconds(1)) .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(search => { Effects.Clear(); if (string.IsNullOrWhiteSpace(search)) { Effects.AddRange(ResourceService.Instance.Effects); return; } var split = search.Split(' '); var effects = ResourceService.Instance.Effects .Where(effect => split.All(word => effect.Name.Contains(word, StringComparison.OrdinalIgnoreCase))) .ToArray(); Effects.AddRange(effects); }); var canSelect = SelectedEffect.WhenAnyValue(x => x.Value).Select(x => x != null); Select = ReactiveCommand.Create(SelectImpl, canSelect); Cancel = ReactiveCommand.Create(CancelImpl); }
public ConnectViewModel() { ConnectText = new ReactiveProperty <string>("Connect"); Host = new ReactiveProperty <string>("127.0.0.1:3306").SetValidateNotifyError(x => ConnectViewModelValidation.Host.Validate(x).ErrorMessages.FirstOrDefault()); Username = new ReactiveProperty <string>("root").SetValidateNotifyError(x => ConnectViewModelValidation.Username.Validate(x).ErrorMessages.FirstOrDefault()); Password = new ReactiveProperty <string>("").SetValidateNotifyError(x => ConnectViewModelValidation.Password.Validate(x).ErrorMessages.FirstOrDefault()); Database = new ReactiveProperty <string>("").SetValidateNotifyError(x => ConnectViewModelValidation.Database.Validate(x).ErrorMessages.FirstOrDefault()); ResourcePath = new ReactiveProperty <string>("") .SetValidateNotifyError(x => ConnectViewModelValidation.ResourcePath.Validate(x).ErrorMessages.FirstOrDefault()); var canConnect = this.WhenAnyValue(x => x.Host.Value, x => x.Username.Value, x => x.Password.Value, x => x.Database.Value, x => x.ResourcePath.Value) .Select(_ => ConnectViewModelValidation.MySql.Validate(this).Succeeded); Connect = ReactiveCommand.CreateFromTask(ConnectImpl, canConnect); Exit = ReactiveCommand.Create(ExitImpl); SelectResourceFile = ReactiveCommand.CreateFromTask(SelectResourceFileImpl); }
public SettingsViewModel(IUserInterface userInterface, IUserDialogs userDialogs, IDotaClientDistanceLoader distanceLoader, ISettingsManager <Settings> settingsManager) { _userDialogs = userDialogs; Settings = settingsManager.LoadAsync().ToObservable().ToReactiveProperty(); SaveSettingsCommand = ReactiveCommand.CreateFromTask <Settings>(settingsManager.SaveAsync); ToggleDarkModeCommand = ReactiveCommand.Create <bool>(userInterface.DarkMode); LoadDistanceCommand = ReactiveCommand.CreateFromTask <Settings>(distanceLoader.LoadAsync); BrowseDota2FolderCommand = ReactiveCommand.CreateFromTask(OpenFolderDialog); BrowseDota2FolderCommand.Where(value => !string.IsNullOrWhiteSpace(value)) .SubscribeOnUIDispatcher() .Subscribe(value => Settings.Value.Dota2FolderPath = value); this.WhenAnyValue(vm => vm.Settings.Value.DarkMode) .InvokeCommand(ToggleDarkModeCommand); this.WhenAnyValue(vm => vm.Settings.Value.Dota2FolderPath) .Where(value => !string.IsNullOrWhiteSpace(value)) .Select(_ => Settings.Value) .ObserveOnUIDispatcher() .InvokeCommand(LoadDistanceCommand); }
private ReactiveCommand GetSelectSourceFileCommand() { var command = ReactiveCommand.Create(SelectSourceFile, null, DispatcherScheduler.Current); _disposables.Add(command); return(command); }
private ReactiveCommand GetStartCalculationCommand() { var canExecuteObservable = CanStartCalculationExecuteObservable(); var command = ReactiveCommand.Create(StartCalculation, canExecuteObservable, DispatcherScheduler.Current); _disposables.Add(command); return(command); }
protected JobViewModelBase(T job, IScheduler scheduler) { Job = job; _scheduler = scheduler; _preChangeName = job.Name; Save = ReactiveCommand.Create(ExecuteSave, this.WhenAny(x => x.ValidationError, x => string.IsNullOrEmpty(x.Value))); }
public DownloadItemViewModel(Job job) { Job = job; Job.WhenAnyValue(j => j.Status).StartWith(job.Status).Subscribe(OnStatusUpdated); // update progress every 300ms Job.WhenAnyValue(j => j.TransferPercent) .Sample(TimeSpan.FromMilliseconds(300)) .Where(x => !job.IsFinished) .Subscribe(progress => { // on main thread System.Windows.Application.Current.Dispatcher.Invoke(() => { DownloadPercent = progress; DownloadPercentFormatted = $"{Math.Round(DownloadPercent)}%"; }); }); // update download speed every 1.5 seconds var lastUpdatedProgress = DateTime.Now; long bytesReceived = 0; Job.WhenAnyValue(j => j.TransferPercent) .Sample(TimeSpan.FromMilliseconds(1500)) .Where(x => !job.IsFinished) .Subscribe(progress => { // on main thread System.Windows.Application.Current.Dispatcher.Invoke(() => { var timespan = DateTime.Now - lastUpdatedProgress; var bytespan = Job.TransferredBytes - bytesReceived; if (timespan.TotalMilliseconds > 0) { var downloadSpeed = 1000 * bytespan / timespan.TotalMilliseconds; DownloadSpeedFormatted = $"{downloadSpeed.Bytes().ToString("#.0")}/s"; } bytesReceived = Job.TransferredBytes; lastUpdatedProgress = DateTime.Now; }); }); // update initial size only once Job.WhenAnyValue(j => j.TransferSize).Select(size => size.Bytes().ToString("#.0")).ToProperty(this, vm => vm.DownloadSizeFormatted, out _downloadSizeFormatted); // abort job on command CancelJob = ReactiveCommand.Create(() => { Job.Cancel(); }); // retry job RetryJob = ReactiveCommand.Create(() => { JobManager.RetryJob(Job); }); // delete job DeleteJob = ReactiveCommand.Create(() => { JobManager.DeleteJob(Job); }); // setup icon SetupFileIcon(); }
public EqualizerViewModel(Equalizer equalizer) { this.Equalizer = equalizer; this.SetToDefaultCommand = ReactiveCommand.Create( () => this.Equalizer.SetToDefault(), this.WhenAnyValue(x => x.Equalizer.IsEnabled)); this.CloseEqualizerCommand = ReactiveCommand.Create( () => this.Equalizer.SaveEqualizerSettings()); }
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 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); }
protected BaseViewModel() { CloseCommand = ReactiveCommand.Create <CancelEventArgs>((cancelEventArg) => { Dispose(); }); DbContext = Locator.Current.GetService <IMyDbContext>(); }
/// <summary> /// ToCommand is a convenience method for returning a new /// ReactiveCommand based on an existing Observable chain. /// </summary> /// <param name="scheduler">The scheduler to publish events on - default /// is RxApp.MainThreadScheduler.</param> /// <returns>A new ReactiveCommand whose CanExecute Observable is the /// current object.</returns> public static ReactiveCommand <object> ToCommand(this IObservable <bool> This, IScheduler scheduler = null) { return(ReactiveCommand.Create(This, scheduler)); }
public ToFCalibratorViewModel() { CalibVisible = Visibility.Hidden; CalculateCalibrationCommand = ReactiveCommand.Create(() => CalculateCalibration()); PerformCalibrationCommand = ReactiveCommand.Create(() => PerformCalibration()); }