Exemplo n.º 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);
                }
            });
        }