Exemplo n.º 1
0
        public MainWindow()
        {
            InitializeComponent();

            SelectFilesButton   = ButtonSelectFiles;
            SelectFoldersButton = ButtonSelectFolders;
            SyncButton          = ButtonSync;
            ClearButton         = ButtonClear;
            FileListView        = ListViewFileList;
            SyncSlider          = SyncOptionSlider;

            model = new MainWindowViewModel
            {
                Overwrite = GetSyncValue(SyncSlider.Value)
            };

            FileListView.ItemsSource = model.Files;

            DataContext = model;

            SelectFilesButton.Click += new RoutedEventHandler((sender, target) =>
            {
                var worker = new BackgroundWorker();

                _progress = new Views.ProgressBarWindow
                {
                    WindowStartupLocation = WindowStartupLocation.CenterOwner,
                    Owner = this
                };

                worker.DoWork += delegate(object a, DoWorkEventArgs args)
                {
                    model.SelectFiles();
                };

                worker.RunWorkerCompleted += delegate(object b, RunWorkerCompletedEventArgs args)
                {
                    FileListView.ItemsSource = null;
                    FileListView.ItemsSource = model.Files;

                    model.StatusMessage       = $"Done!";
                    _progress.IsIndeterminate = false;
                    _progress.Close();

                    //FileCountMessageBox.Text = $"{model.Files.Count} files selected";
                };

                _progress.IsIndeterminate = true;
                _progress.Show();
                worker.RunWorkerAsync();
            });

            SelectFoldersButton.Click += new RoutedEventHandler((sender, target) =>
            {
                var worker = new BackgroundWorker();

                _progress = new Views.ProgressBarWindow
                {
                    WindowStartupLocation = WindowStartupLocation.CenterOwner,
                    Owner = this
                };

                _dispatcher = _progress.Dispatcher;

                var update = new UpdateProgress(UpdateProgressValue);

                worker.DoWork += delegate(object a, DoWorkEventArgs args)
                {
                    Dispatcher.Invoke(() => model.SelectFolders());
                    //Dispatcher.Invoke(() => _progress.IsIndeterminate = false);

                    //int num = 0, total = model.Directories.Count;

                    //var collection = new FileCollection();

                    //foreach(var directory in model.Directories)
                    //{
                    //    var files = directory.Files;
                    //    var subdirs = Directory.GetAllContents(directory);

                    //    collection.AddRange(files);
                    //    num += 1;

                    //    float value = num / (float)total;
                    //    value *= 100;

                    //    _dispatcher.Invoke(update, value);
                    //}

                    //model.Files = collection;
                };

                worker.RunWorkerCompleted += delegate(object b, RunWorkerCompletedEventArgs args)
                {
                    FileListView.ItemsSource = null;
                    FileListView.ItemsSource = model.Files;
                    model.StatusMessage      = "Files scanned; ready to sync.";
                    _progress.Close();
                };

                _progress.IsIndeterminate = true;
                _progress.Show();
                worker.RunWorkerAsync();
            });

            SyncButton.Click += new RoutedEventHandler((sender, target) =>
            {
                model.Sync();
            });

            ClearButton.Click += new RoutedEventHandler((sender, target) =>
            {
                model.Clear();
            });

            SyncOptionSlider.ValueChanged += new RoutedPropertyChangedEventHandler <double>((sender, target) =>
            {
                var value = (sender as Slider).Value;

                model.Overwrite = GetSyncValue(value);
            });
        }