Esempio n. 1
0
        public NewObjectViewModel(ICttObjectTrackingService cttObjectTrackingService, INavigationService navigationService, ISystemTrayService systemTrayService, IMessageBoxService messageBoxService)
        {
            _cttObjectTrackingService = cttObjectTrackingService;
            _navigationService        = navigationService;
            _systemTrayService        = systemTrayService;
            _messageBoxService        = messageBoxService;

            AddObjectCommand = new RelayCommand(() =>
            {
                if (string.IsNullOrWhiteSpace(ObjectId))
                {
                    _messageBoxService.Show("Por favor indique o número do objecto", "Erro");

                    return;
                }

                _systemTrayService.SetProgressIndicator(string.Format("A adicionar: {0}...", ObjectId));

                _cttObjectTrackingService.GetCttObjectTrackingStatus(ObjectId, result =>
                {
                    _systemTrayService.HideProgressIndicator();

                    if (result.Error != null)
                    {
                        _messageBoxService.Show(string.Format("Não foi possível adicionar o objecto \"{0}\"", ObjectId), "Erro");

                        ScanBarcodeCommand.RaiseCanExecuteChanged();
                        AddObjectCommand.RaiseCanExecuteChanged();
                    }
                    else
                    {
                        MessengerInstance.Send <AddObjectMessage>(new AddObjectMessage(_description, result.ETag, result.Data));

                        _navigationService.GoBack();

                        ObjectId = null;
                    }
                }, null);

                ScanBarcodeCommand.RaiseCanExecuteChanged();
                AddObjectCommand.RaiseCanExecuteChanged();
            }, () => !IsBusy);

            ScanBarcodeCommand = new RelayCommand(() =>
            {
                _navigationService.NavigateTo(new Uri("/View/ScanBarcodePage.xaml", UriKind.Relative));
            }, () => !IsBusy);

            MessengerInstance.Register <ScannedBarcodeMessage>(this, message =>
            {
                ObjectId = message.ObjectId;
            });
        }
        public MainViewModel(IMainModel model, ICttObjectTrackingService cttObjectTrackingService, INavigationService navigationService, ISystemTrayService systemTrayService, IMessageBoxService messageBoxService, IClipboardService clipboardService, IEmailComposeService emailComposeService, ISmsComposeService smsComposeService)
        {
            _model = model;
            _cttObjectTrackingService = cttObjectTrackingService;
            _navigationService        = navigationService;
            _systemTrayService        = systemTrayService;
            _messageBoxService        = messageBoxService;
            _clipboardService         = clipboardService;
            _emailComposeService      = emailComposeService;
            _smsComposeService        = smsComposeService;

            TrackedObjects = _model.TrackedObjects
                             .Select(x => new ObjectDetailsViewModel(x))
                             .ToObservableCollection();

            ShowObjectDetailsCommand = new RelayCommand <ObjectDetailsViewModel>(item =>
            {
                var objectId = item.Model.ObjectId;

                _navigationService.NavigateTo(new Uri("/View/ObjectDetailsPage.xaml?" + objectId, UriKind.Relative));
            });

            NewObjectCommand = new RelayCommand(() =>
            {
                _navigationService.NavigateTo(new Uri("/View/NewObjectPage.xaml", UriKind.Relative));
            }, () => !IsBusy);

            EnableSelectionCommand = new RelayCommand(() =>
            {
                IsSelectionEnabled = true;
            }, () => !IsTrackedObjectsEmpty && !IsBusy);

            RefreshCommand = new RelayCommand(() =>
            {
                var enumerator = TrackedObjects.GetEnumerator();

                RefreshNext(enumerator);
            }, () => !IsTrackedObjectsEmpty && !IsBusy);

            DeleteObjectsCommand = new RelayCommand <IList>(items =>
            {
                if (items == null || items.Count == 0)
                {
                    return;
                }

                _messageBoxService.Show("Está prestes a apagar os objectos seleccionados", "Tem a certeza?", new string[] { "eliminar", "cancelar" }, button =>
                {
                    if (button != 0)
                    {
                        return;
                    }

                    var itemsToRemove = items
                                        .Cast <ObjectDetailsViewModel>()
                                        .ToArray();

                    foreach (var item in itemsToRemove)
                    {
                        _model.TrackedObjects.Remove(item.Model);

                        TrackedObjects.Remove(item);
                    }

                    IsTrackedObjectsEmpty = (TrackedObjects.Count == 0);

                    _model.Save();
                });
            }, items => !IsBusy);

            ShowAboutCommand = new RelayCommand(() =>
            {
                _navigationService.NavigateTo(new Uri("/View/AboutPage.xaml", UriKind.Relative));
            });

            BackKeyPressCommand = new RelayCommand <CancelEventArgs>(e =>
            {
                if (IsSelectionEnabled)
                {
                    IsSelectionEnabled = false;

                    e.Cancel = true;
                }
            });

            CopyCodeCommand = new RelayCommand <ObjectDetailsViewModel>(item =>
            {
                _clipboardService.SetText(item.Model.ObjectId);
            });

            MailCodeCommand = new RelayCommand <ObjectDetailsViewModel>(item =>
            {
                _emailComposeService.Show("CTT Objectos", string.Format("\nO seu código de tracking: {0}\n\nPode consultar o estado entrando em http://www.ctt.pt e utilizando a opção \"Pesquisa de Objectos\".\n\nEnviado por CTT Objectos (http://bit.ly/cttobjectos)", item.Model.ObjectId));
            });

            TextCodeCommand = new RelayCommand <ObjectDetailsViewModel>(item =>
            {
                _smsComposeService.Show(string.Empty, item.Model.ObjectId);
            });

            MessengerInstance.Register <AddObjectMessage>(this, message =>
            {
                var objectViewModel = TrackedObjects.FirstOrDefault(x => x.Model.ObjectId == message.Model.ObjectId);

                if (objectViewModel == null)
                {
                    var objectModel = new ObjectModel(message.Description, message.ETag, message.Model);

                    _model.TrackedObjects.Add(objectModel);

                    TrackedObjects.Add(new ObjectDetailsViewModel(objectModel));

                    IsTrackedObjectsEmpty = false;
                }
                else
                {
                    objectViewModel.Model.Description = message.Description;
                    objectViewModel.Model.State       = message.Model;
                }

                _model.Save();
            });

            IsTrackedObjectsEmpty = (TrackedObjects.Count == 0);
        }