Beispiel #1
0
        public MainViewModel(
            [Dependency(Dispatchers.Current)] IDispatcherProvider dispatcher,
            IFactory <IManagementViewModel> managementViewModelFactory,
            IFactory <IConfigurationViewModel> configurationViewModelFactory,
            IMessageBoxService messageBoxService,
            IThreadsController threadsController,
            IFile file)
        {
#if DEBUG
            var configurationModel = new ConfigurationModel()
            {
                BufferSize          = "10000",
                DestinationFileName = "Test.data",
                DestinationFolder   = @"C:\Users\nero0\Desktop\Новая папка (2)",
                SourceFilePath      = @"C:\Users\nero0\Downloads\Brainstorm - Years And Seconds.mp3"
            };
#else
            var configurationModel = new ConfigurationModel();
#endif
            ManagementViewModel    = managementViewModelFactory.ConstructWith(threadsController).And(configurationModel).Create();
            ConfigurationViewModel = configurationViewModelFactory.ConstructWith <IConfigurationModel>(configurationModel).Create();

            Cancel = threadsController.WriterState.Amb(threadsController.ReaderState)
                     .Select(state => state != WorkerState.Unstarted && state != WorkerState.Stopped)
                     .ToReactiveCommand(dispatcher.Scheduler, false);
            Cancel.Subscribe(threadsController.Cancel);

            StartCopying = threadsController.WriterState.Amb(threadsController.ReaderState)
                           .Select(state => state == WorkerState.Unstarted || state == WorkerState.Stopped)
                           .CombineLatest(ConfigurationViewModel.HasNotErrors, (state, err) => state && err)
                           .ToReactiveCommand(dispatcher.Scheduler);

            StartCopying.Subscribe(() =>
            {
                var destPath   = Path.Combine(configurationModel.DestinationFolder, configurationModel.DestinationFileName);
                var bufferSize = int.Parse(configurationModel.BufferSize);
                if (file.Exists(destPath))
                {
                    if (
                        messageBoxService.ShowYesNowQuestion(
                            $"File {destPath} already exists. Would you like to replace it?", "Information") ==
                        MessageBoxResult.Yes)
                    {
                        threadsController.StartReadAndWrite(configurationModel.SourceFilePath, destPath, bufferSize);
                    }
                }
                else
                {
                    threadsController.StartReadAndWrite(configurationModel.SourceFilePath, destPath, bufferSize);
                }
            });
        }
        public ManagementViewModel([Dependency(Dispatchers.Current)] IDispatcherProvider dispatcher,
                                   IMessageBoxService messageBoxService,
                                   IThreadsController threadsController)
        {
            threadsController.Result.Subscribe(r =>
            {
                if (r.Result == Result.Error)
                {
                    messageBoxService.ShowErrorMessage(r.Exception.ToString(), "Error");
                }
                else
                {
                    messageBoxService.ShowInformation(r.Result.ToString(), "Information");
                }
            });

            Progress =
                threadsController.Progress
                .Sample(TimeSpan.FromMilliseconds(50))
                .ToReactiveProperty(dispatcher.Scheduler);

            BufferSize =
                threadsController.CurrentBufferSize
                .Sample(TimeSpan.FromMilliseconds(50))
                .ToReactiveProperty(dispatcher.Scheduler);

            ReaderState = threadsController.ReaderState.ToReactiveProperty(dispatcher.Scheduler);
            WriterState = threadsController.WriterState.ToReactiveProperty(dispatcher.Scheduler);

            PauseReading = threadsController.ReaderState.Select(x => x == WorkerState.Running)
                           .ToReactiveCommand(dispatcher.Scheduler);
            PauseReading.Subscribe(threadsController.PauseReading);

            ResumeReading = threadsController.ReaderState.Select(x => x == WorkerState.Suspended)
                            .ToReactiveCommand(dispatcher.Scheduler);
            ResumeReading.Subscribe(threadsController.ResumeReading);


            PauseWriting = threadsController.WriterState.Select(x => x == WorkerState.Running)
                           .ToReactiveCommand(dispatcher.Scheduler);
            PauseWriting.Subscribe(threadsController.PauseWriting);

            ResumeWriting = threadsController.WriterState.Select(x => x == WorkerState.Suspended)
                            .ToReactiveCommand(dispatcher.Scheduler);
            ResumeWriting.Subscribe(threadsController.ResumeWriting);
        }