private MainViewModel GetMainViewModel(Preset[] presets = null) { if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA) { throw new ThreadStateException("The current threads apartment state is not STA"); } _mockTimerManager = new Mock <ITimerManager>(); _presetCollectionManager = new PresetCollection(); try { _cycleBellManager = new CycleBellManager("some_file_name", _presetCollectionManager, _mockTimerManager.Object); } catch (ArgumentNullException) { } var stubAlarm = new Mock <IAlarm>(); var scoll = new ObservableCollection <Uri>(); stubAlarm.Setup(a => a.DefaultSoundCollection).Returns(new ReadOnlyObservableCollection <Uri>(scoll)); var mainViewModel = _cycleBellManager != null ? new MainViewModel(_mockDialogRegistrator.Object, _cycleBellManager, stubAlarm.Object) : throw new ArgumentNullException(nameof(_cycleBellManager), @"Instance of ICycleBellManager is null"); Window wnd = new FakeMainWindow(); wnd.DataContext = mainViewModel; return(mainViewModel); }
public MainViewModel(IDialogRegistrator dialogRegistrator, ICycleBellManager cycleBellManager, IAlarm alarm) { void InitializeCommands() { RemoveSelectedPresetCommand = new ActionCommand(RemoveSelectedPreset, CanRemoveSelectedPreset); ExportPresetsCommand = new ActionCommand(ExportPresets, CanExportPresets); ClearPresetsCommand = new ActionCommand(ClearPresets, CanExportPresets); } void LoadTimerManager() { _timerManager = _manager.TimerManager; _timerManager.TimerStarted += UpdateIsRunningAndTimerStateProperties; _timerManager.TimerStopped += UpdateIsRunningAndTimerStateProperties; } void LoadPresetViewModelCollection() { if (_manager.PresetCollection.Presets.Any(_manager.IsNewPreset)) { throw new ArgumentException("ICycleBellManager.IOpenSave.Presets contain NewPreset"); } PresetViewModelCollection = new ObservableCollection <PresetViewModel>(_manager.PresetCollection.Presets.Where(p => p.IsNotNew()).Select(p => new PresetViewModel(p, this))); if (PresetViewModelCollection.Count > 0) { SelectedPreset = PresetViewModelCollection[0]; } ((INotifyCollectionChanged)(_manager.PresetCollection.Presets)).CollectionChanged += OnPresetCollectionChangedEventHandler; } void LoadDefaultSounds() { DefaultSoundVmCollection = new List <SoundMenuItemViewModel>( Alarm.DefaultSoundCollection .Select(s => { var ind1 = s.LocalPath.LastIndexOf('\\'); if (ind1 == -1) { ind1 = 0; } else { ind1 += 1; } var name = s.LocalPath.Substring(ind1); var ind2 = name.LastIndexOf('.'); if (ind2 > -1) { name = name.Substring(0, ind2); } return(new SoundMenuItemViewModel { Name = name, Command = new ActionCommand(o => { Alarm.SetDefaultSound(s); OnMediaEnded(this, EventArgs.Empty); }) }); }) ); } _dialogRegistrator = dialogRegistrator ?? throw new ArgumentNullException(nameof(dialogRegistrator)); Alarm = alarm ?? throw new ArgumentNullException(nameof(alarm), @"Alarm cannot be null."); _manager = cycleBellManager ?? throw new ArgumentNullException(nameof(cycleBellManager)); _manager.CantCreateNewPresetEvent += OnCantCreateNewPresetEventHandler; LoadTimerManager(); InitializeCommands(); LoadPresetViewModelCollection(); if (Alarm.DefaultSoundCollection.Any()) { LoadDefaultSounds(); } _timer = new Timer(ClearStatusBarText, null, Timeout.Infinite, Timeout.Infinite); }