public ManualPickViewModel(FileOpenCommands fileCommands, Func <Task <IFlightAcademyClient> > clientFactory,
                                   IDialogService dialogService, SettingsViewModel settingsViewModel, INavigationService navigationService)
        {
            this.settingsViewModel = settingsViewModel;
            FileCommands           = fileCommands;

            video      = fileCommands.OpenVideoCommand.ToProperty(this, x => x.Video);
            flightFile = fileCommands.OpenDataCommand.ToProperty(this, x => x.FlightFile);

            LoadFlightsCommand = ReactiveCommand.CreateFromTask(() => GetFlights(clientFactory), settingsViewModel.IsAccountConfigured);
            summaries          = LoadFlightsCommand
                                 .ToProperty(this, x => x.FlightSummaries);

            MessageBus.Current
            .Listen <Unit>("LoadData")
            .InvokeCommand(LoadFlightsCommand);

            LoadFlightsCommand.ThrownExceptions.MessageOnException(dialogService);

            var manualSimulations = this
                                    .WhenAnyValue(x => x.Video, x => x.FlightFile, (v, f) => new { Video = v, FlightFile = f })
                                    .SelectMany(async x =>
            {
                var readFlight = await x.FlightFile.ReadFlight();
                return(new Simulation(x.Video, readFlight, new PresentationOptions()
                {
                    UnitPack = settingsViewModel.UnitPack,
                    Dashboard = settingsViewModel.VirtualDashboard,
                }));
            })
                                    .FirstOrDefaultAsync();

            PlayFromFileCommand = ReactiveCommand.CreateFromObservable(() => manualSimulations,
                                                                       this.WhenAnyValue(x => x.FlightFile, x => x.Video, (f, v) => f != null && v != null));

            var onlineSimulations = this
                                    .WhenAnyValue(x => x.Video, x => x.SelectedFlightSummary, (v, s) => new { Video = v, FlightSummary = s })
                                    .SelectMany(async x =>
            {
                var readFlight = await(await clientFactory()).GetFlight(x.FlightSummary.Id);
                return(new Simulation(x.Video, readFlight.ToFlight(), new PresentationOptions()
                {
                    UnitPack = settingsViewModel.UnitPack,
                    Dashboard = settingsViewModel.VirtualDashboard,
                }));
            })
                                    .FirstOrDefaultAsync();

            PlayFromOnlineFlightCommand = ReactiveCommand.CreateFromObservable(() => onlineSimulations,
                                                                               this.WhenAnyValue(x => x.SelectedFlightSummary, x => x.Video, (f, v) => f != null && v != null));

            PlayFromFileCommand.Subscribe(simulation => { PlaySimulation(navigationService, simulation); });
            PlayFromOnlineFlightCommand.Subscribe(simulation => { PlaySimulation(navigationService, simulation); });

            isAccountConfigured = settingsViewModel.IsAccountConfigured.ToProperty(this, x => x.IsAccountConfigured);
        }
Exemple #2
0
        public GalleryPickViewModel(Func <Task <IFlightAcademyClient> > clientFactory, SettingsViewModel settingsViewModel,
                                    IDialogService dialogService, INavigationService navigationService)
        {
            this.clientFactory     = clientFactory;
            this.settingsViewModel = settingsViewModel;
            this.navigationService = navigationService;

            var videoObs = Observable.FromAsync(async() =>
            {
                var folder = await StorageFolder.GetFolderFromPathAsync(settingsViewModel.VideoFolder);
                return(await folder.GetFilesAsync());
            }).OnErrorResumeNext(Observable.Empty <IReadOnlyList <StorageFile> >())
                           .Select(x => x.ToObservable())
                           .SelectMany(x => x)
                           .SelectMany(Video.Load)
                           .Where(x => x.RecordedInterval.HasValue)
                           .ToList();

            var summariesObs = Observable.FromAsync(async() =>
            {
                var flightAcademyClient = await clientFactory();
                return(await flightAcademyClient.GetFlights(0, 2000));
            });

            var zipped = videoObs.Zip(summariesObs,
                                      (videos, flight) =>
            {
                return(flight.Select((x, i) => CreateSimulationUnitViewModel(x, videos)).Where(x => x.Items.Any()));
            });

            var canLoadFlights = settingsViewModel
                                 .WhenAnyValue(x => x.VideoFolder, x => x.Username, x => x.Password, (videoFolder, user, pass) =>
                                               new[] { videoFolder, user, pass }
                                               .All(s => !string.IsNullOrEmpty(s)));

            LoadFlightsCommand = ReactiveCommand.CreateFromObservable(() => zipped, canLoadFlights);

            LoadFlightsCommand.ThrownExceptions.MessageOnException(dialogService)
            .DisposeWith(disposables);

            videopacks = LoadFlightsCommand.ToProperty(this, model => model.VideoPacks);
            isBusy     = LoadFlightsCommand.IsExecuting.ToProperty(this, x => x.IsBusy);

            MessageBus.Current
            .Listen <Unit>("LoadData")
            .InvokeCommand(LoadFlightsCommand);

            isAccountConfigured     = settingsViewModel.IsAccountConfigured.ToProperty(this, x => x.IsAccountConfigured);
            isVideoFolderConfigured = settingsViewModel.IsVideoFolderFolderConfigured.ToProperty(this, x => x.IsVideoFolderConfigured);
        }