150 lines
4.2 KiB
C#
150 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Windows.Input;
|
|
using file_finder__test;
|
|
|
|
namespace YourNamespace
|
|
{
|
|
public class ScanSettingsViewModel : INotifyPropertyChanged
|
|
{
|
|
private int _option; // 0 = all drives, 1 = specific, 2 = direct path
|
|
private int _specificOption; // index of selected directory for specific option
|
|
private string _rootPath; // path for direct path
|
|
private string _selectedOption; // content of selected RadioButton
|
|
|
|
public List<string> Dirs { get; set; } = new List<string>();
|
|
|
|
public int Option
|
|
{
|
|
get => _option;
|
|
private set
|
|
{
|
|
if (_option != value)
|
|
{
|
|
_option = value;
|
|
OnPropertyChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
public int SpecificOption
|
|
{
|
|
get => _specificOption;
|
|
private set
|
|
{
|
|
if (_specificOption != value)
|
|
{
|
|
_specificOption = value;
|
|
OnPropertyChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
public string RootPath
|
|
{
|
|
get => _rootPath;
|
|
set
|
|
{
|
|
if (_rootPath != value)
|
|
{
|
|
_rootPath = value;
|
|
OnPropertyChanged();
|
|
// Notify owner when root path changes
|
|
StateChanged?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
}
|
|
|
|
public string SelectedOption
|
|
{
|
|
get => _selectedOption;
|
|
set
|
|
{
|
|
if (_selectedOption != value)
|
|
{
|
|
_selectedOption = value;
|
|
OnPropertyChanged();
|
|
UpdateOptionState();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Event to notify the owner MainWindow
|
|
public event EventHandler StateChanged;
|
|
|
|
|
|
public ScanSettingsViewModel()
|
|
{
|
|
Option = 2;
|
|
RootPath = "M:/Media";
|
|
|
|
SelectPathCommand = new RelayCommand(o => { });
|
|
_ = InitializeAsync();
|
|
}
|
|
|
|
private async Task InitializeAsync()
|
|
{
|
|
|
|
FileScanner fileScanner= new FileScanner();
|
|
var drives = await fileScanner.ScanAllDrivesAsync();
|
|
foreach (var drive in drives)
|
|
{
|
|
Dirs.Add(drive);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public ICommand SelectPathCommand { get; set; }
|
|
|
|
private void UpdateOptionState()
|
|
{
|
|
if (SelectedOption == "scan all drives")
|
|
{
|
|
Option = 0;
|
|
}
|
|
else if (SelectedOption == "direct path")
|
|
{
|
|
Option = 2;
|
|
// RootPath already bound to textbox
|
|
}
|
|
else
|
|
{
|
|
// Must be one of the dynamic dirs
|
|
Option = 1;
|
|
SpecificOption = Dirs.IndexOf(SelectedOption);
|
|
|
|
}
|
|
|
|
// Notify owner
|
|
StateChanged?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
private void OnPropertyChanged([CallerMemberName] string name = null)
|
|
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
|
}
|
|
|
|
public class RelayCommand : ICommand
|
|
{
|
|
private readonly Action<object> _execute;
|
|
private readonly Predicate<object> _canExecute;
|
|
|
|
public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
|
|
{
|
|
_execute = execute;
|
|
_canExecute = canExecute;
|
|
}
|
|
|
|
public bool CanExecute(object parameter) => _canExecute == null || _canExecute(parameter);
|
|
public void Execute(object parameter) => _execute(parameter);
|
|
public event EventHandler CanExecuteChanged
|
|
{
|
|
add { CommandManager.RequerySuggested += value; }
|
|
remove { CommandManager.RequerySuggested -= value; }
|
|
}
|
|
}
|
|
}
|