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 AssignedTicketsViewModel(IScreen screen, ISession session)
        {
            HostScreen = screen;
            GoBack = HostScreen.Router.NavigateBack;

            Tickets = new ReactiveList<TicketItemViewModel>();
            Employees = new ReactiveList<Employee>();

            _isFetchingTickets = session.IsWorking
                .ToProperty(this, x => x.IsFetchingTickets);

            this.WhenAnyValue(x => x.SelectedEmployee)
                .Where(x => x != null)
                .Subscribe(x =>
                    {
                        Tickets.Clear();

                        session.FetchMergedResults(
                            QueryHelper.QueryOnTicketsAndEvents,
                            QueryHelper.Filter<TicketWithEvent>(z =>
                                (x.Id == int.MinValue || z.EmployeeId == x.Id) &&
                                z.TicketStatus == TicketStatus.Assigned))
                            .ObserveOn(RxApp.MainThreadScheduler)
                            .Select(y => new TicketItemViewModel(y.Description, y.TicketStatus ?? TicketStatus.Open))
                            .Subscribe(y => Tickets.Add(y));
                    });

            SelectedEmployee = new Employee { Name = "All", TicketEvents = null, Id = int.MinValue };
            Employees.Add(SelectedEmployee);

            session.FetchResults<Employee>()
                .ObserveOn(RxApp.MainThreadScheduler)
                .Subscribe(x => Employees.Add(x));

            session.ThrownExceptions.Subscribe(x =>
                {
                    Console.WriteLine(x.Message);
                });
        }
        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
        }