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)); }
public CreateTicketsViewModel(ISession session, IScreen screen = null) { #region Initialization HostScreen = screen ?? new DefaultScreen(RxApp.DependencyResolver); GoBack = HostScreen.Router.NavigateBack; CurrentBatch = new ReactiveList<TicketItemViewModel>(); Customers = new ReactiveList<Customer>(); #endregion #region Populate Customer List session.FetchResults<Customer>() .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(c => Customers.Add(c)); #endregion #region Wire Up Commands AddTicket = new ReactiveCommand( this.WhenAny( x => x.CurrentBatch, y => y.Problem, z => z.SelectedCustomer, (x, y, z) => x.Value.Count() <= 100 && !string.IsNullOrWhiteSpace(y.Value) && z.Value != null)); AddTicket.Subscribe(_ => { CurrentBatch.Add(new TicketItemViewModel(Problem, CurrentBatch.Remove)); Problem = default(string); }); var anyInList = CurrentBatch.Changed.Select(_ => CurrentBatch.Any()); var isCustomerSelected = this.WhenAnyValue(x => x.SelectedCustomer).Select(x => x != null); var shouldSaveChanges = Observable.CombineLatest(anyInList, isCustomerSelected, (x, y) => x && y); SaveChanges = new ReactiveCommand(shouldSaveChanges.StartWith(false)); _isExecuting = session.IsWorking.ToProperty(this, x => x.IsExecuting, false); SaveChanges.Select(x => CurrentBatch.ToList()) .Subscribe(x => { using (INotifyWhenComplete token = session.ScopedChanges()) { var ticketTaker = session.Take<Ticket>(); token.Completion .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(b => { if (b) (CurrentBatch as IList).Clear(); }); foreach (var item in x) { ticketTaker.Add(new Ticket { Description = item.Description, CustomerId = SelectedCustomer.Id }); } } }); #endregion }