public AccountsViewModel( IApplicationService applicationService = null, IAccountsService accountsService = null) { accountsService = accountsService ?? Locator.Current.GetService <IAccountsService>(); applicationService = applicationService ?? Locator.Current.GetService <IApplicationService>(); Title = "Accounts"; var activeAccount = applicationService.Account; var currentUsername = activeAccount?.Username; var accounts = new ReactiveList <Account>(); Items = accounts.CreateDerivedCollection(x => { var vm = new AccountItemViewModel(x); if (activeAccount?.Id == x.Id) { vm.GoToCommand.BindCommand(DismissCommand); vm.IsSelected = true; } else { vm.GoToCommand .Do(_ => applicationService.SetDefaultAccount(x)) .Subscribe(_ => MessageBus.Current.SendMessage(new LogoutMessage())); } vm.DeleteCommand.Subscribe(_ => { accountsService.Remove(x); accounts.Remove(x); }); return(vm); }); DismissCommand = ReactiveCommand.Create( () => { }, accounts.Changed.Select(x => accounts.Any(y => y.Username == currentUsername))); LoadCommand = ReactiveCommand.CreateFromTask(async _ => { var allAccounts = await accountsService.GetAccounts(); return(allAccounts.ToList()); }); LoadCommand.Subscribe(x => accounts.Reset(x)); }
public LoadingViewModel( IScreen hostScreen = null, IAppSettings appSettings = null ) : base(hostScreen) { _appSettings = appSettings ?? Locator.Current.GetService <IAppSettings>(); LoadCommand = ReactiveCommand.CreateFromTask(LoadStuff); RxExtensions.Signal() .InvokeCommand(LoadCommand); _isLoading = LoadCommand.IsExecuting .ToProperty(this, vm => vm.IsLoading); LoadCommand.Subscribe(); }
public InputViewModel() { _hub = MessageHub.Instance; //ExecuteCommand = ReactiveCommand.Create(OnExecute); ExecuteCommand = ReactiveCommand.Create(OnExecute, this.WhenAny( x => x.Code, hasCode => !string.IsNullOrWhiteSpace(hasCode.Value))); //.Subscribe(_ => OnExecute()); //OkButton = ReactiveCommand.Create( // this.WhenAny(x => x.Code, y => y.Query, // (x, y) => !string.IsNullOrWhiteSpace(x.Value) && !string.IsNullOrWhiteSpace(y.Value)) ); // this.WhenAny(x => x.Code, y=> y.Query, (x, y) => !string.IsNullOrWhiteSpace(x) && !string.IsNullOrWhiteSpace(y)); LoadCommand = ReactiveCommand.CreateFromObservable(OnLoad); LoadCommand.Subscribe(ReadFile); SaveCommand = ReactiveCommand.Create(OnSave, this.WhenAny( x => x.FileName, fileName => !string.IsNullOrWhiteSpace(fileName.Value))); }
public override void SubscribeModel() { model.DayTableModels.ObserveAddChanged() .Subscribe(x => { MainGroupListViewModel sameTitleViewModel = DayTableViewModels.FirstOrDefault(y => y.Title == x.Station); if (sameTitleViewModel != null) { sameTitleViewModel.Add(new DayTableViewModel(x)); } else { DayTableViewModels.Add(new MainGroupListViewModel(x.Station) { new DayTableViewModel(x) }); } }) .AddTo(Disposables); model.DayTableModels.ObserveResetChanged() .Subscribe(x => DayTableViewModels.Clear()) .AddTo(Disposables); model.ObserveProperty(x => x.IsLoading) .Subscribe(x => isLoading.Value = x) .AddTo(Disposables); Observable.FromEventPattern <ErrorEventArgs>(h => model.ErrorOccurred += h, h => model.ErrorOccurred -= h) .Select(x => x.EventArgs) .Subscribe(x => ShowAlertCommand.Execute(x.Message)) .AddTo(Disposables); LoadCommand.Subscribe(async _ => await model.LoadAsync()) .AddTo(Disposables); LoadIfEmptyCommand.Subscribe(async _ => await model.LoadIfEmptyAsync()) .AddTo(Disposables); ItemSelectedCommand.Subscribe(x => PushDayTableViewCommand.Execute(x.Clone())) .AddTo(Disposables); ItemAppearingCommand.Subscribe(x => x.SubscribeModel()) .AddTo(Disposables); ItemDisappearingCommand.Subscribe(x => x.UnSubscribeModel()) .AddTo(Disposables); }
public MainWindowViewModel() { Items = new ReactiveList <TodoItem> { ChangeTrackingEnabled = true }; Items.ItemsAdded.Subscribe(i => i.DeleteCommand.Subscribe(_ => Items.Remove(i))); var itemDoneChanged = Items.ItemChanged.Where(x => x.PropertyName == "Done").Select(_ => Unit.Default); var countChanged = Items.CountChanged.Select(_ => Unit.Default); itemDoneChanged.Merge(countChanged).Subscribe(x1 => ItemsLeftCount = Items.Count(i => !i.Done)); FilteredItems = Items.CreateDerivedCollection(x => x, (Func <TodoItem, bool>)FilterItem); var canCreate = this.ObservableForProperty(vm => vm.NewItemText).Select(s => !String.IsNullOrWhiteSpace(s.Value)); CreateCommand = ReactiveCommand.Create(canCreate); CreateCommand.SelectArgs <KeyEventArgs>() .Where(x => x.Key == Key.Enter) .Subscribe(CreateItemExecute); //Items.Add(new TodoItem("ASDASD")); ChangeThemeCommand = ReactiveCommand.Create(); ChangeThemeCommand.Subscribe(_ => { ThemeManager.ChangeAppStyle(Application.Current, ThemeManager.DetectAppStyle(Application.Current).Item2, ThemeManager.AppThemes.ElementAt(_ThemeIndex++ % ThemeManager.AppThemes.Count())); }); ChangeAccentCommand = ReactiveCommand.Create(); ChangeAccentCommand.Subscribe(_ => { ThemeManager.ChangeAppStyle(Application.Current, ThemeManager.Accents.ElementAt(_ThemeAccent++ % ThemeManager.Accents.Count()), ThemeManager.DetectAppStyle(Application.Current).Item1); }); LoadCommand = ReactiveCommand.CreateAsyncObservable(_ => Observable.Create <TodoItem>(obs => { if (!failedYet) { obs.OnError(new Exception("test")); failedYet = true; } else { failedYet = false; obs.OnNext(new TodoItem("a")); obs.OnNext(new TodoItem("b")); obs.OnNext(new TodoItem("c")); obs.OnCompleted(); } return(Disposable.Empty); })); var observable = LoadCommand.ThrownExceptions.Select(ex => new UserError(ex.Message, recoveryOptions: new[] { RecoveryCommand.Yes, RecoveryCommand.Cancel })); //_LastError = observable.ToProperty(this, x => x.LastError); observable.SelectMany(UserError.Throw).Subscribe(result => { //this.RaisePropertyChanged("HasError"); switch (result) { case RecoveryOptionResult.CancelOperation: return; case RecoveryOptionResult.RetryOperation: LoadCommand.Execute(null); break; } }); LoadCommand.Subscribe(Items.Add); }
private void LoadCommands() { LoadCommand = ReactiveCommand.CreateAsyncTask(async _ => { var imageDefinition = await _imageLoadTask.PickThumbnailImage(); return(imageDefinition); }); LoadCommand.Subscribe(LoadThumbnailImage); SnapCommand = ReactiveCommand.CreateAsyncTask(async _ => { var camera = new CameraCaptureUI(); var file = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo); return(await _imageLoadTask.GetImageDefinitionFromFile(file)); }); SnapCommand.Subscribe(LoadThumbnailImage); SaveCommand = ReactiveCommand.Create(this.WhenAny(x => x.PlayerDetails.IsDirty, x => x.PlayerDetails.Name, x => x.PlayerDetails.NickName, (isDirty, name, nickName) => isDirty.Value && !string.IsNullOrWhiteSpace(name.Value) && !string.IsNullOrWhiteSpace(nickName.Value))); SaveCommand.Subscribe(arg => SaveCommandHandler()); SaveCommand.CanExecuteObservable.Subscribe(HandleSaveCanExecuteChangedHandler); DeleteCommand = ReactiveCommand.Create(this.WhenAny(x => x.PlayerDetails.Id, id => id.Value != Guid.Empty)); DeleteCommand.Subscribe(async arg => await DeleteCommandHandler()); DeleteCommand.CanExecuteObservable.Subscribe(HandleDeleteCanExecuteChangedHandler); CreateNewCommand = ReactiveCommand.Create(this.WhenAnyValue(x => x.PlayerDetails.IsDirty) .Select(x => !x).Skip(1)); CreateNewCommand.Subscribe(arg => CreateCommandHandler()); CreateNewCommand.CanExecuteObservable.Subscribe(HandleNewCanExecuteChangedHandler); ShowStatsCommand = ReactiveCommand.Create(); ShowStatsCommand.Subscribe(arg => { var newId = arg is Guid ? (Guid?)arg : null; if (newId.HasValue) { ClickedPlayerId = newId.Value; } }); ShowStatsCommand.CanExecuteObservable.Subscribe(HandleShowStatsCommandChangedHandler); PlayerSummaryCommand = ReactiveCommand.Create(); PlayerSummaryCommand.Subscribe(ShowSummary); PlayerDetailCommand = ReactiveCommand.Create(); PlayerDetailCommand.Subscribe(ShowDetail); }