Пример #1
0
        protected JobViewModelBase(T job, AbstractValidator <T> validator, IDataClient client, IDialogCoordinator dialogCoordinator, object dialogContext) : base(job, validator)
        {
            PreChangeName = job.Name;
            //Note the use of job vs Job below. The parameter has the type T which is necessary for the  client stuff to work
            Job = job;

            //Save job
            var saveCanExecute = this
                                 .WhenAnyValue(x => x.HasErrors)
                                 .Select(x => x == false);

            Save = ReactiveCommand.CreateFromTask(async _ =>
            {
                //First delete the existing job (if there is an existing job), with the old name
                string tmpName = Name;
                Name           = PreChangeName;
                await client.DeleteJob(job).ConfigureAwait(true); //failure is OK here, it might not exist on the server if it's newly added
                Name = tmpName;

                //then add it with the new values
                var result = await client.AddJob(job).ConfigureAwait(true);
                if (await result.DisplayErrors(dialogContext, dialogCoordinator).ConfigureAwait(true))
                {
                    return;
                }
                PreChangeName = Name;
            },
                                                  saveCanExecute);

            //this is here because we need to know the job type
            Delete = ReactiveCommand.CreateFromTask(async _ => (ApiResponse)await client.DeleteJob(job).ConfigureAwait(false));
        }
Пример #2
0
        protected JobViewModelBase(T job, AbstractValidator <T> validator, IDataClient client, IDialogCoordinator dialogCoordinator) : base(job, validator)
        {
            PreChangeName = job.Name;
            Job           = job;
            _typedJob     = job;

            //Save job
            var saveCanExecute = this
                                 .WhenAnyValue(x => x.HasErrors)
                                 .Select(x => x == false);

            Save = ReactiveCommand.CreateFromTask(async _ =>
            {
                //First delete the existing job (if there is an existing job), with the old name
                string tmpName = Name;
                Name           = PreChangeName;
                await client.DeleteJob(_typedJob); //failure is OK here, it might not exist on the server if it's newly added
                Name = tmpName;

                //then add it with the new values
                var result = await client.AddJob(_typedJob).ConfigureAwait(true);
                if (await result.DisplayErrors(this, dialogCoordinator))
                {
                    return;
                }

                PreChangeName = Name;
            },
                                                  saveCanExecute);
        }
Пример #3
0
        private void CreateCommands()
        {
            Add = ReactiveCommand.Create(ExecuteAdd);

            //get existing jobs and tags/instruments
            Load = ReactiveCommand.CreateFromTask(async _ =>
            {
                var dataUpdateJobs        = _client.GetaDataUpdateJobs();
                var econReleaseUpdateJobs = _client.GetEconomicReleaseUpdateJobs();
                var tags               = _client.GetTags();
                var instruments        = _client.GetInstruments();
                var econReleaseSources = _client.GetEconomicReleaseDataSources();

                await Task.WhenAll(dataUpdateJobs, econReleaseUpdateJobs, tags, instruments, econReleaseSources).ConfigureAwait(false);

                var responses = new ApiResponse[] { dataUpdateJobs.Result, econReleaseUpdateJobs.Result, tags.Result, instruments.Result, econReleaseSources.Result };
                if (await responses.DisplayErrors(this, DialogCoordinator).ConfigureAwait(true))
                {
                    return(null);
                }

                Tags.AddRange(tags.Result.Result);
                Instruments.AddRange(instruments.Result.Result);
                EconomicReleaseDataSources.AddRange(econReleaseSources.Result.Result);

                var jobs = new List <IJobSettings>();
                jobs.AddRange(dataUpdateJobs.Result.Result);
                jobs.AddRange(econReleaseUpdateJobs.Result.Result);

                return(jobs);
            });
            Load.Subscribe(jobs =>
            {
                if (jobs == null)
                {
                    return;
                }

                foreach (var job in jobs)
                {
                    Jobs.Add(GetJobViewModel(job));
                }
            });

            //Delete job
            var deleteCanExecute = this
                                   .WhenAnyValue(x => x.SelectedJob)
                                   .Select(x => x != null && !string.IsNullOrEmpty(x.PreChangeName));

            Delete = ReactiveCommand.CreateFromTask(async _ =>
            {
                //Give a dialog to confirm the deletion
                MessageDialogResult dialogResult = await DialogCoordinator.ShowMessageAsync(this,
                                                                                            "Delete Job",
                                                                                            string.Format("Are you sure you want to delete {0}?", SelectedJob.Name),
                                                                                            MessageDialogStyle.AffirmativeAndNegative);

                if (dialogResult != MessageDialogResult.Affirmative)
                {
                    return;
                }

                //If the name has changed but hasn't been saved, we change it back to be in sync with the server
                SelectedJob.Name = SelectedJob.PreChangeName;

                //Request deletion
                var response = await _client.DeleteJob(SelectedJob.Job);
                if (await response.DisplayErrors(this, DialogCoordinator))
                {
                    return;
                }

                //if it was successful, remove the VM from the list
                Jobs.Remove(SelectedJob);
                SelectedJob = null;
            },
                                                    deleteCanExecute);
        }