public SelectedToolViewModel(UIView toolboxView, AvailableToolsContainerViewController container, MeetingViewModel meeting, IReactiveList<IToolViewModel> selectedTools, NavigationPaneViewModel navigationPane) { SelectedTools = selectedTools; IsEmpty = SelectedTools.Count == 0; SelectedTools.Changed.ObserveOn(RxApp.MainThreadScheduler) .Subscribe(p => { IsEmpty = SelectedTools.Count == 0; }); var showAvailableToolsCommand = new ReactiveCommand(); showAvailableToolsCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(_ => { UIView.BeginAnimations(null); UIView.SetAnimationDuration(0.25); toolboxView.Alpha = 1.0f; UIView.CommitAnimations(); }); container.CloseToolboxRequested += (s, e) => { UIView.BeginAnimations(null); UIView.SetAnimationDuration(0.25); toolboxView.Alpha = 0.0f; UIView.CommitAnimations(); }; ShowAvailableToolsCommand = showAvailableToolsCommand; navigationPane.ShowToolLibraryAction = () => ShowAvailableToolsCommand.Execute(null); var setActiveToolsCommand = new ReactiveCommand(); setActiveToolsCommand.ObserveOn(RxApp.MainThreadScheduler) .Subscribe(tool => meeting.ActiveTool = (IToolViewModel)tool); SetActiveToolCommand = setActiveToolsCommand; }
public DispatchViewModel(IScreen screen, ISession session) { HostScreen = screen; GoBack = HostScreen.Router.NavigateBack; Techs = new ReactiveList<Employee>(); Tickets = new ReactiveList<Ticket>(); var getFreshTechs = new ReactiveCommand(); getFreshTechs.ObserveOn(RxApp.MainThreadScheduler).Subscribe(_ => { Techs.Clear(); session.FetchResults<Employee>() .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(x => Techs.Add(x)); }); var getFreshTickets = new ReactiveCommand(); getFreshTickets.ObserveOn(RxApp.MainThreadScheduler).Subscribe(_ => { Tickets.Clear(); session.FetchResults<Ticket>() .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(x => Tickets.Add(x)); }); Refresh = new ReactiveCommand(session.IsWorking.Select(x => !x)); Refresh.Subscribe(_ => { getFreshTechs.Execute(default(object)); getFreshTickets.Execute(default(object)); }); Assign = new ReactiveCommand(Observable.CombineLatest( this.WhenAny( x => x.SelectedEmployee, y => y.SelectedTicket, (x, y) => x.Value != null && y.Value != null), Refresh.CanExecuteObservable, (x, y) => x && y)); Assign.Subscribe(_ => { using (session.ScopedChanges()) { var eventTaker = session.Take<TicketEvent>(); eventTaker.Add(new TicketEvent { Employee = SelectedEmployee, Ticket = SelectedTicket, TicketStatus = TicketStatus.Assigned, Time = DateTime.Now }); } }); _error = session.ThrownExceptions .Select(x => x.Message) .ObserveOn(RxApp.MainThreadScheduler) .ToProperty(this, x => x.Error); Refresh.Execute(default(object)); }
private void SetupNewGameCommand() { NewGameCommand = ReactiveCommand.Create <Unit, MinefieldCellCollection>( unit => CreateMinefieldCellCollection(), this.WhenAny(vm => vm.Status, model => model.Value != GameStatus.Begin)); NewGameCommand .ObserveOn(MainThreadScheduler) .Subscribe(UpdateMinefield); NewGameCommand.ThrownExceptions.Subscribe(exception => MessageBox.Show(exception.Message)); }
public AnnotationToolViewModel(AgentAnnotationViewModel agentAnnotations) { AgentAnnotations = agentAnnotations; _isEditingSub = AgentAnnotations.WhenAnyValue(p => p.IsEditing, p => p).Where(p => !p).Subscribe(_ => ClearAllActive()); _activeToolSub = this.WhenAnyValue(p => p.RectActive, p => p.EllipseActive, p => p.FreeDrawActive, p => p.LineActive, (a, b, c, d) => { return(!a && !b && !c && !d); }).Where(p => p).Subscribe(_ => { Type = null; }); _undoCommand = new ReactiveCommand( agentAnnotations.WhenAny(p => p.ContainsAnnotationsForCurrentPage, p => p.Value).Select(p => p)); _undoCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => { ClearAllActive(); agentAnnotations.UndoAnnotation(); }); _rectCommand = new ReactiveCommand(); _rectCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => { ClearAllActive(); RectActive = true; Type = AnnotationType.Rectangle; }); _ellipseCommand = new ReactiveCommand(); _ellipseCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => { ClearAllActive(); EllipseActive = true; Type = AnnotationType.Ellipse; }); _lineCommand = new ReactiveCommand(); _lineCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => { ClearAllActive(); LineActive = true; Type = AnnotationType.Line; }); _freeDrawCommand = new ReactiveCommand(); _freeDrawCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => { ClearAllActive(); FreeDrawActive = true; Type = AnnotationType.AdHoc; }); }
public NavigationPaneViewModel(INavigationService navigation) { IsToolLibraryButtonHidden = true; IsUpcomingMeetingsButtonHidden = true; var invokeToolLibraryCommand = new ReactiveCommand(this.WhenAnyValue(p => p.IsToolLibraryButtonHidden, p => !p)); invokeToolLibraryCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => { if (ShowToolLibraryAction != null && !IsToolLibraryButtonHidden) { HideNavPaneCommand.Execute(null); ShowToolLibraryAction(); } }); InvokeToolLibraryCommand = invokeToolLibraryCommand; var invokeUpcomingMeetingsCommand = new ReactiveCommand(this.WhenAnyValue(p => p.IsUpcomingMeetingsButtonHidden, p => !p)); invokeUpcomingMeetingsCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => { if (!IsUpcomingMeetingsButtonHidden) { HideNavPaneCommand.Execute(null); navigation.BackCommand.Execute(null); } }); InvokeUpcomingMeetingsCommand = invokeUpcomingMeetingsCommand; var showSettingsCommand = new ReactiveCommand(); //showSettingsCommand.RegisterAsync(async p => //{ //}); ShowSettingsCommand = showSettingsCommand; var showPrivacyPolicyCommand = new ReactiveCommand(); //showPrivacyPolicyCommand.RegisterAsync(async p => //{ //}); ShowPrivacyPolicyCommand = showPrivacyPolicyCommand; var hideNavPaneCommand = new ReactiveCommand(); hideNavPaneCommand.ObserveOn(RxApp.MainThreadScheduler) .Subscribe(p => { if (HideNavPaneAction != null) HideNavPaneAction(); }); HideNavPaneCommand = hideNavPaneCommand; var logoutCommand = new ReactiveCommand(); logoutCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => navigation.PopToRoot()); LogoutCommand = logoutCommand; }
public CollectionsViewModel() { AddToReactiveCollectionCommand = new ReactiveCommand() .AddTo(Disposables); ReactiveCollection = AddToReactiveCollectionCommand .Select(_ => Guid.NewGuid()) .ToReactiveCollection() .AddTo(Disposables); ClearReactiveCollectionCommand = new ReactiveCommand() .AddTo(Disposables); ClearReactiveCollectionCommand.ObserveOn(TaskPoolScheduler.Default) .Subscribe(_ => { ReactiveCollection.ClearOnScheduler(); }); GuidViewModels = _model.Guids .ToReadOnlyReactiveCollection(x => new GuidViewModel(x)) .AddTo(Disposables); AddGuidToModelCommand = new ReactiveCommand() .AddTo(Disposables); AddGuidToModelCommand.ObserveOn(TaskPoolScheduler.Default) .Subscribe(_ => _model.Guids.Add(Guid.NewGuid())) .AddTo(Disposables); ClearModelGuidsCommand = new ReactiveCommand() .AddTo(Disposables); ClearModelGuidsCommand.ObserveOn(TaskPoolScheduler.Default) .Subscribe(_ => _model.Guids.Clear()) .AddTo(Disposables); FilterSourceItems = new ObservableCollection <FilterSourceItem>(); FilteredItems = FilterSourceItems .ToFilteredReadOnlyObservableCollection(x => x.Count >= 7) .AddTo(Disposables); AddToFilterSourceItemsCommand = new ReactiveCommand() .WithSubscribe(() => FilterSourceItems.Add(new FilterSourceItem())) .AddTo(Disposables); ClearFromFilterSourceItemsCommand = FilterSourceItems.ObserveProperty(x => x.Count) .Select(x => x != 0) .ToReactiveCommand() .WithSubscribe(() => { foreach (var d in FilterSourceItems) { d.Dispose(); } FilterSourceItems.Clear(); }) .AddTo(Disposables); }
public DemoViewModel([CanBeNull] IEnumerable<IElevationProvider> providers = null) { _availableElevationProviders = providers ?? Locator.Current.GetService<IEnumerable<IElevationProvider>>(); if (_availableElevationProviders == null) throw new ArgumentNullException("providers"); _writeableBitmap = new WriteableBitmap(pixelWidthInt, pixelHeightInt, 96, 96, PixelFormats.Rgb24, null); var epAvailable = this.WhenAnyValue(t => t.SelectedElevationProvider) .Select(sep => sep != null); _retrieveElevationsCommand = ReactiveCommand.CreateAsyncTask(epAvailable, _ => refreshElevationsAsync()); _retrieveElevationsCommand.ObserveOn(RxApp.MainThreadScheduler) .Subscribe(s => ElevationValueStats = s); _retrieveElevationsCommand.ThrownExceptions.Subscribe(e => UserError.Throw("Error during retrieval of elevation data", e)); _selectedElevationProvider = _availableElevationProviders.FirstOrDefault(); _progress = _progresSubject.ToProperty(this, t => t.Progress, scheduler: RxApp.MainThreadScheduler); }
public PhotoViewModel(IRouter router = null, IPhotosService service = null) : base(router) { Service = service ?? Locator.Current.GetService <IPhotosService>(); var canLoadData = this.WhenAnyValue(vm => vm.Photo) .Select(p => p != null); LoadPhotoData = ReactiveCommand.CreateAsyncTask(canLoadData, async o => await LoadPhotoDataImpl()); photo = OnActivated.Select(p => p.Photo) .ToProperty(this, vm => vm.Photo); photoData = LoadPhotoData .ObserveOn(RxApp.MainThreadScheduler) .ToProperty(this, vm => vm.PhotoData); isLoading = LoadPhotoData.IsExecuting .ObserveOn(RxApp.MainThreadScheduler) .ToProperty(this, vm => vm.IsLoading); this.WhenAnyValue(vm => vm.Photo) .InvokeCommand(this, vm => vm.LoadPhotoData); }
/// <summary> /// Set us up with the complete list of calendars. /// </summary> /// <param name="parent"></param> public CategoryAllPageViewModel(IScreen parent) { HostScreen = parent; // The list of categories. UpdateCategoryList = ReactiveCommand.Create(); ListOfCalendars = new ReactiveList <CategoryConfigInfo>(); UpdateCategoryList .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(_ => { ListOfCalendars.Clear(); ListOfCalendars.AddRange(CategoryDB.LoadCategories()); }); UpdateCategoryList.Execute(null); // And setup the category VM ShowCategoryDetails = ReactiveCommand.Create(); var asCategoryInfo = ShowCategoryDetails .Cast <CategoryConfigInfo>(); asCategoryInfo .Select(ci => { var index = ListOfCalendars.IndexOf(ci); var civm = new CategoryConfigViewModel(ci); civm.UpdateToCI .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(newCI => ListOfCalendars[index] = newCI); return(civm); }) .ToProperty(this, x => x.ConfigViewModel, out _categoryConfig, null); asCategoryInfo .Select(ci => new CategoryURIViewModel(ci.MeetingList)) .ToProperty(this, x => x.CategoryFullListVM, out _catgoryFullListVM, null); // Track what we are doing ViewCategory = asCategoryInfo; // Keep the display "clean" until somethign is selected. asCategoryInfo .Select(_ => true) .ToProperty(this, x => x.ValidCategorySelected, out _validCategorySelected, false); }
public ScheduledNotifierLoggerViewModel() { OnUIThreadCommand = new ReactiveCommand() .WithSubscribe(() => { _logger.Trace("On UIThread"); _logger.Warn("On UIThread"); }, CompositeDisposable.Add); OnAsyncTaskCommand = new AsyncReactiveCommand() .WithSubscribe(async() => await Task.Run(() => { _logger.Debug("On AsyncTask"); _logger.Error("On AsyncTask"); }), CompositeDisposable.Add); OnDefaultSchedulerCommand = new ReactiveCommand().AddTo(CompositeDisposable); OnDefaultSchedulerCommand .ObserveOn(Scheduler.Default) .Subscribe(x => { _logger.Info("On DefaultScheduler"); _logger.Fatal("On AsyncTask"); }) .AddTo(CompositeDisposable); ClearErrorLogsCommand = new ReactiveCommand().AddTo(CompositeDisposable); // 全てのログ AllLogs = _logger .ToReadOnlyReactiveCollection(onReset: ClearErrorLogsCommand.ToUnit()) .AddTo(CompositeDisposable); // Warn以上のログ (ReactiveCommand.ToUnit() を渡すせば要素をクリアできる) ErrorLogs = _logger .Where(x => x.Level >= LogLevel.Warn) .ToReadOnlyReactiveCollection(onReset: ClearErrorLogsCommand.ToUnit()) .AddTo(CompositeDisposable); }
public DemoViewModel([CanBeNull] IEnumerable <IElevationProvider> providers = null) { _availableElevationProviders = providers ?? Locator.Current.GetService <IEnumerable <IElevationProvider> >(); if (_availableElevationProviders == null) { throw new ArgumentNullException("providers"); } _writeableBitmap = new WriteableBitmap(pixelWidthInt, pixelHeightInt, 96, 96, PixelFormats.Rgb24, null); var epAvailable = this.WhenAnyValue(t => t.SelectedElevationProvider) .Select(sep => sep != null); _retrieveElevationsCommand = ReactiveCommand.CreateAsyncTask(epAvailable, _ => refreshElevationsAsync()); _retrieveElevationsCommand.ObserveOn(RxApp.MainThreadScheduler) .Subscribe(s => ElevationValueStats = s); _retrieveElevationsCommand.ThrownExceptions.Subscribe(e => UserError.Throw("Error during retrieval of elevation data", e)); _selectedElevationProvider = _availableElevationProviders.FirstOrDefault(); _progress = _progresSubject.ToProperty(this, t => t.Progress, scheduler: RxApp.MainThreadScheduler); }
private void SetupEditCommand() { EditCommand = ReactiveCommand.CreateFromTask <Unit, Phone>(async unit => { var phoneWindow = new PhoneWindow(new PhoneViewModel(SelectedPhone)); if (phoneWindow.ShowDialog() == true) { var phone = await _repository.UpdateAsync(new Phone(phoneWindow.PhoneViewModel)).ConfigureAwait(false); return(phone); } return(new Phone(SelectedPhone)); }, this.WhenAny(vm => vm.SelectedPhone, change => change.Value != null)); EditCommand.ObserveOn(MainThreadScheduler).Subscribe(updatedPhone => { SelectedPhone.Price = updatedPhone.Price; SelectedPhone.Company = updatedPhone.Company; SelectedPhone.Title = updatedPhone.Title; }); EditCommand.ThrownExceptions.Subscribe(ex => { MessageBox.Show(ex.Message); }); }
public SelectedToolViewModel(UIView toolboxView, AvailableToolsContainerViewController container, MeetingViewModel meeting, IReactiveList <IToolViewModel> selectedTools, NavigationPaneViewModel navigationPane) { SelectedTools = selectedTools; IsEmpty = SelectedTools.Count == 0; SelectedTools.Changed.ObserveOn(RxApp.MainThreadScheduler) .Subscribe(p => { IsEmpty = SelectedTools.Count == 0; }); var showAvailableToolsCommand = new ReactiveCommand(); showAvailableToolsCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(_ => { UIView.BeginAnimations(null); UIView.SetAnimationDuration(0.25); toolboxView.Alpha = 1.0f; UIView.CommitAnimations(); }); container.CloseToolboxRequested += (s, e) => { UIView.BeginAnimations(null); UIView.SetAnimationDuration(0.25); toolboxView.Alpha = 0.0f; UIView.CommitAnimations(); }; ShowAvailableToolsCommand = showAvailableToolsCommand; navigationPane.ShowToolLibraryAction = () => ShowAvailableToolsCommand.Execute(null); var setActiveToolsCommand = new ReactiveCommand(); setActiveToolsCommand.ObserveOn(RxApp.MainThreadScheduler) .Subscribe(tool => meeting.ActiveTool = (IToolViewModel)tool); SetActiveToolCommand = setActiveToolsCommand; }
public SearchViewModel() { SearchResults = new ObservableCollection <SearchResult>(); // ReactiveCommand has built-in support for background operations and // guarantees that this block will only run exactly once at a time, and // that the CanExecute will auto-disable and that property IsExecuting will // be set according whilst it is running. Search = ReactiveCommand.CreateAsyncTask( // Here we're describing here, in a *declarative way*, the conditions in // which the Search command is enabled. Now our Command IsEnabled is // perfectly efficient, because we're only updating the UI in the scenario // when it should change. Observable .CombineLatest( this.WhenAnyValue(vm => vm.SearchQuery).Select(searchQuery => !string.IsNullOrEmpty(searchQuery)).DistinctUntilChanged(), this.WhenAnyObservable(x => x.Search.IsExecuting).DistinctUntilChanged(), (hasSearchQuery, isExecuting) => hasSearchQuery && !isExecuting) .Do(cps => System.Diagnostics.Debug.WriteLine($"Can Perform Search: {cps}")) .DistinctUntilChanged(), async _ => { var random = new Random(Guid.NewGuid().GetHashCode()); await Task.Delay(random.Next(250, 2500)); //This is just here so simulate a network type exception if (DateTime.Now.Second % 3 == 0) { throw new TimeoutException("Unable to connec to web service"); } var searchService = Locator.CurrentMutable.GetService <IDuckDuckGoApi>(); var searchResult = await searchService.Search(SearchQuery); return(searchResult .RelatedTopics .Select(rt => new SearchResult { DisplayText = rt.Text, ImageUrl = rt?.Icon?.Url ?? string.Empty }) .ToList()); }); //// ReactiveCommands are themselves IObservables, whose value are the results //// from the async method, guaranteed to arrive on the given scheduler. //// We're going to take the list of search results that the background //// operation loaded, and them into our SearchResults. Search .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(searchResult => { SearchResults.Clear(); foreach (var item in searchResult) { SearchResults.Add(item); } }); // ThrownExceptions is any exception thrown from the CreateFromObservable piped // to this Observable. Subscribing to this allows you to handle errors on // the UI thread. Search .ThrownExceptions .Subscribe(async ex => { var result = await UserError.Throw("Potential Network Connectivity Error", ex); if (result == RecoveryOptionResult.RetryOperation && Search.CanExecute(null)) { Search.Execute(null); } }); //Behaviors this.WhenAnyValue(x => x.SearchQuery) .Throttle(TimeSpan.FromSeconds(.75), TaskPoolScheduler.Default) .Do(x => System.Diagnostics.Debug.WriteLine($"Throttle fired for {x}")) .ObserveOn(RxApp.MainThreadScheduler) .InvokeCommand(Search); }
public AnnotationToolViewModel(AgentAnnotationViewModel agentAnnotations) { AgentAnnotations = agentAnnotations; _isEditingSub = AgentAnnotations.WhenAnyValue(p => p.IsEditing, p => p).Where(p => !p).Subscribe(_ => ClearAllActive()); _activeToolSub = this.WhenAnyValue(p => p.RectActive, p => p.EllipseActive, p => p.FreeDrawActive, p => p.LineActive, (a, b, c, d) => { return !a && !b && !c && !d; }).Where(p => p).Subscribe(_ => { Type = null; }); _undoCommand = new ReactiveCommand( agentAnnotations.WhenAny(p => p.ContainsAnnotationsForCurrentPage, p => p.Value).Select(p => p)); _undoCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => { ClearAllActive(); agentAnnotations.UndoAnnotation(); }); _rectCommand = new ReactiveCommand(); _rectCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => { ClearAllActive(); RectActive = true; Type = AnnotationType.Rectangle; }); _ellipseCommand = new ReactiveCommand(); _ellipseCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => { ClearAllActive(); EllipseActive = true; Type = AnnotationType.Ellipse; }); _lineCommand = new ReactiveCommand(); _lineCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => { ClearAllActive(); LineActive = true; Type = AnnotationType.Line; }); _freeDrawCommand = new ReactiveCommand(); _freeDrawCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => { ClearAllActive(); FreeDrawActive = true; Type = AnnotationType.AdHoc; }); }
public NavigationPaneViewModel(INavigationService navigation) { IsToolLibraryButtonHidden = true; IsUpcomingMeetingsButtonHidden = true; var invokeToolLibraryCommand = new ReactiveCommand(this.WhenAnyValue(p => p.IsToolLibraryButtonHidden, p => !p)); invokeToolLibraryCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => { if (ShowToolLibraryAction != null && !IsToolLibraryButtonHidden) { HideNavPaneCommand.Execute(null); ShowToolLibraryAction(); } }); InvokeToolLibraryCommand = invokeToolLibraryCommand; var invokeUpcomingMeetingsCommand = new ReactiveCommand(this.WhenAnyValue(p => p.IsUpcomingMeetingsButtonHidden, p => !p)); invokeUpcomingMeetingsCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => { if (!IsUpcomingMeetingsButtonHidden) { HideNavPaneCommand.Execute(null); navigation.BackCommand.Execute(null); } }); InvokeUpcomingMeetingsCommand = invokeUpcomingMeetingsCommand; var showSettingsCommand = new ReactiveCommand(); //showSettingsCommand.RegisterAsync(async p => //{ //}); ShowSettingsCommand = showSettingsCommand; var showPrivacyPolicyCommand = new ReactiveCommand(); //showPrivacyPolicyCommand.RegisterAsync(async p => //{ //}); ShowPrivacyPolicyCommand = showPrivacyPolicyCommand; var hideNavPaneCommand = new ReactiveCommand(); hideNavPaneCommand.ObserveOn(RxApp.MainThreadScheduler) .Subscribe(p => { if (HideNavPaneAction != null) { HideNavPaneAction(); } }); HideNavPaneCommand = hideNavPaneCommand; var logoutCommand = new ReactiveCommand(); logoutCommand.ObserveOn(RxApp.MainThreadScheduler).Subscribe(p => navigation.PopToRoot()); LogoutCommand = logoutCommand; }