Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add view model tests, inject file dialogs.
  • Loading branch information
comintern committed Dec 13, 2018
1 parent ecf8307 commit aa01470
Show file tree
Hide file tree
Showing 13 changed files with 734 additions and 66 deletions.
Expand Up @@ -31,18 +31,21 @@ public class AddRemoveReferencesPresenterFactory : IAddRemoveReferencesPresenter
private readonly IConfigProvider<ReferenceSettings> _settings;
private readonly IRegisteredLibraryFinderService _finder;
private readonly IReferenceReconciler _reconciler;
private readonly IFileSystemBrowserFactory _browser;

public AddRemoveReferencesPresenterFactory(IVBE vbe,
RubberduckParserState state,
IConfigProvider<ReferenceSettings> settingsProvider,
IRegisteredLibraryFinderService finder,
IReferenceReconciler reconciler)
IReferenceReconciler reconciler,
IFileSystemBrowserFactory browser)
{
_vbe = vbe;
_state = state;
_settings = settingsProvider;
_finder = finder;
_reconciler = reconciler;
_browser = browser;
}

public AddRemoveReferencesPresenter Create(ProjectDeclaration project)
Expand Down Expand Up @@ -106,7 +109,7 @@ public AddRemoveReferencesPresenter Create(ProjectDeclaration project)
item.FullPath.Equals(proj.FullPath, StringComparison.OrdinalIgnoreCase))));
}

return new AddRemoveReferencesPresenter(new AddRemoveReferencesDialog(new AddRemoveReferencesViewModel(model, _reconciler)));
return new AddRemoveReferencesPresenter(new AddRemoveReferencesDialog(new AddRemoveReferencesViewModel(model, _reconciler, _browser)));
}

private IEnumerable<ReferenceModel> GetUserProjectFolderModels(IReferenceSettings settings)
Expand Down
129 changes: 117 additions & 12 deletions Rubberduck.Core/UI/AddRemoveReferences/AddRemoveReferencesViewModel.cs
Expand Up @@ -82,11 +82,13 @@ static AddRemoveReferencesViewModel()
private readonly ObservableCollection<ReferenceModel> _available;
private readonly ObservableCollection<ReferenceModel> _project;
private readonly IReferenceReconciler _reconciler;
private readonly IFileSystemBrowserFactory _browser;

public AddRemoveReferencesViewModel(IAddRemoveReferencesModel model, IReferenceReconciler reconciler)
public AddRemoveReferencesViewModel(IAddRemoveReferencesModel model, IReferenceReconciler reconciler, IFileSystemBrowserFactory browser)
{
Model = model;
_reconciler = reconciler;
_browser = browser;

_available = new ObservableCollection<ReferenceModel>(model.References
.Where(reference => !reference.IsReferenced).OrderBy(reference => reference.Description));
Expand All @@ -110,25 +112,48 @@ public AddRemoveReferencesViewModel(IAddRemoveReferencesModel model, IReferenceR
ApplyCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteApplyCommand);
}

/// <summary>
/// The IAddRemoveReferencesModel for the view.
/// </summary>
public IAddRemoveReferencesModel Model { get; set; }

/// <summary>
/// Hides the projects filter if the host does not support them. Statically set.
/// </summary>
public bool ProjectsVisible => HostHasProjects;

/// <summary>
/// The number of built-in (locked) references of the project.
/// </summary>
public int BuiltInReferenceCount { get; }

/// <summary>
/// Adds a reference to the project.
/// </summary>
public ICommand AddCommand { get; }

/// <summary>
/// Removes a reference from the project and makes it "available".
/// </summary>
public ICommand RemoveCommand { get; }

/// <summary>
/// Prompts user for a .tlb, .dll, or .ocx file, and attempts to append it to <see cref="ProjectReferences"/>.
/// Prompts the user to browse for a reference.
/// </summary>
public ICommand BrowseCommand { get; }

/// <summary>
/// Closes the dialog and indicates changes are to be saved.
/// </summary>
public CommandBase OkCommand { get; }

/// <summary>
/// Closes the dialog and indicates changes are not to be saved.
/// </summary>
public CommandBase CancelCommand { get; }

/// <summary>
/// Applies all changes to project references.
/// Applies any changes without closing the dialog.
/// </summary>
public ICommand ApplyCommand { get; }

Expand All @@ -142,10 +167,20 @@ public AddRemoveReferencesViewModel(IAddRemoveReferencesModel model, IReferenceR
/// </summary>
public ICommand MoveDownCommand { get; }

/// <summary>
/// Pins the selected reference from the available list.
/// </summary>
public ICommand PinLibraryCommand { get; }

/// <summary>
/// Pins the selected reference from the referenced list.
/// </summary>
public ICommand PinReferenceCommand { get; }

/// <summary>
/// Delegate for AddCommand.
/// </summary>
/// <param name="parameter">Ignored</param>
private void ExecuteAddCommand(object parameter)
{
if (SelectedLibrary == null)
Expand All @@ -158,8 +193,13 @@ private void ExecuteAddCommand(object parameter)
EvaluateProjectDirty();
ProjectReferences.Refresh();
_available.Remove(SelectedLibrary);
AvailableReferences.Refresh();
}

/// <summary>
/// Delegate for RemoveCommand.
/// </summary>
/// <param name="parameter">Ignored</param>
private void ExecuteRemoveCommand(object parameter)
{
if (SelectedReference == null)
Expand All @@ -179,18 +219,21 @@ private void ExecuteRemoveCommand(object parameter)

EvaluateProjectDirty();
ProjectReferences.Refresh();
AvailableReferences.Refresh();
}


/// <summary>
/// Delegate for BrowseCommand.
/// </summary>
/// <param name="parameter">Ignored</param>
private void ExecuteBrowseCommand(object parameter)
{
using (var dialog = new OpenFileDialog
using (var dialog = _browser.CreateOpenFileDialog())
{
Filter = string.Join("|", FileFilters),
Title = RubberduckUI.References_BrowseCaption
})
{
dialog.ShowDialog();
if (string.IsNullOrEmpty(dialog.FileName))
dialog.Filter = string.Join("|", FileFilters);
dialog.Title = RubberduckUI.References_BrowseCaption;
var result = dialog.ShowDialog();
if (result != DialogResult.OK || string.IsNullOrEmpty(dialog.FileName))
{
return;
}
Expand Down Expand Up @@ -221,6 +264,10 @@ private void ExecuteBrowseCommand(object parameter)
}
}

/// <summary>
/// Delegate for ApplyCommand.
/// </summary>
/// <param name="parameter">Ignored</param>
private void ExecuteApplyCommand(object parameter)
{
var changed = _reconciler.ReconcileReferences(Model, _available.ToList());
Expand All @@ -233,6 +280,10 @@ private void ExecuteApplyCommand(object parameter)
ProjectReferences.Refresh();
}

/// <summary>
/// Delegate for MoveUpCommand.
/// </summary>
/// <param name="parameter">Ignored</param>
private void ExecuteMoveUpCommand(object parameter)
{
if (SelectedReference == null || SelectedReference.IsBuiltIn || SelectedReference.Priority == 1)
Expand All @@ -253,6 +304,10 @@ private void ExecuteMoveUpCommand(object parameter)
ProjectReferences.Refresh();
}

/// <summary>
/// Delegate for MoveDownCommand.
/// </summary>
/// <param name="parameter">Ignored</param>
private void ExecuteMoveDownCommand(object parameter)
{
if (SelectedReference == null || SelectedReference.IsBuiltIn || SelectedReference.Priority == _project.Count)
Expand All @@ -273,6 +328,10 @@ private void ExecuteMoveDownCommand(object parameter)
ProjectReferences.Refresh();
}

/// <summary>
/// Delegate for PinLibraryCommand.
/// </summary>
/// <param name="parameter">Ignored</param>
private void ExecutePinLibraryCommand(object parameter)
{
if (SelectedLibrary == null)
Expand All @@ -283,16 +342,23 @@ private void ExecutePinLibraryCommand(object parameter)
AvailableReferences.Refresh();
}

/// <summary>
/// Delegate for PinReferenceCommand.
/// </summary>
/// <param name="parameter">Ignored</param>
private void ExecutePinReferenceCommand(object parameter)
{
if (SelectedReference == null || SelectedReference.IsBuiltIn)
if (SelectedReference == null)
{
return;
}
SelectedReference.IsPinned = !SelectedReference.IsPinned;
ProjectReferences.Refresh();
}

/// <summary>
/// Ordered collection of the project's currently selected references.
/// </summary>
public ICollectionView ProjectReferences
{
get
Expand All @@ -303,6 +369,9 @@ public ICollectionView ProjectReferences
}
}

/// <summary>
/// Collection of references not currently selected for the project, filtered by the current filter.
/// </summary>
public ICollectionView AvailableReferences
{
get
Expand All @@ -314,16 +383,29 @@ public ICollectionView AvailableReferences
}

private string _filter;

/// <summary>
/// The currently selected filter. Should be a member of ReferenceFilter.
/// </summary>
public string SelectedFilter
{
get => _filter;
set
{
if (!Enum.TryParse<ReferenceFilter>(value, out _))
{
return;
}
_filter = value;
AvailableReferences.Refresh();
}
}

/// <summary>
/// Applies selected filter and any search term to CollectionViewSource.
/// </summary>
/// <param name="reference">The ReferenceModel to test.</param>
/// <returns>Returns true if the passed reference is included in the filtered result.</returns>
private bool Filter(ReferenceModel reference)
{
var filtered = false;
Expand Down Expand Up @@ -353,6 +435,10 @@ private bool Filter(ReferenceModel reference)
}

private string _search = string.Empty;

/// <summary>
/// Search term for filtering AvailableReferences.
/// </summary>
public string Search
{
get => _search;
Expand All @@ -364,6 +450,10 @@ public string Search
}

private ReferenceModel _selection;

/// <summary>
/// The currently selected Reference in the focused list.
/// </summary>
public ReferenceModel CurrentSelection
{
get => _selection;
Expand All @@ -375,6 +465,10 @@ public ReferenceModel CurrentSelection
}

private ReferenceModel _reference;

/// <summary>
/// The currently selected Reference for the project.
/// </summary>
public ReferenceModel SelectedReference
{
get => _reference;
Expand All @@ -386,6 +480,10 @@ public ReferenceModel SelectedReference
}

private ReferenceModel _library;

/// <summary>
/// The currently selected available (not included in the project) Reference.
/// </summary>
public ReferenceModel SelectedLibrary
{
get => _library;
Expand All @@ -397,6 +495,10 @@ public ReferenceModel SelectedLibrary
}

private bool _dirty;

/// <summary>
/// Indicated whether any changes were made to the project's references.
/// </summary>
public bool IsProjectDirty
{
get => _dirty;
Expand All @@ -407,6 +509,9 @@ public bool IsProjectDirty
}
}

/// <summary>
/// Tests to see if any changes have been made to the project and sets IsProjectDirty to the appropriate value.
/// </summary>
private void EvaluateProjectDirty()
{
var selected = _project.Select(reference => (reference.Priority, reference.ToReferenceInfo())).ToList();
Expand Down
4 changes: 2 additions & 2 deletions Rubberduck.Core/UI/Command/ExportAllCommand.cs
Expand Up @@ -11,9 +11,9 @@ namespace Rubberduck.UI.Command
public class ExportAllCommand : CommandBase
{
private readonly IVBE _vbe;
private readonly IFolderBrowserFactory _factory;
private readonly IFileSystemBrowserFactory _factory;

public ExportAllCommand(IVBE vbe, IFolderBrowserFactory folderBrowserFactory) : base(LogManager.GetCurrentClassLogger())
public ExportAllCommand(IVBE vbe, IFileSystemBrowserFactory folderBrowserFactory) : base(LogManager.GetCurrentClassLogger())
{
_vbe = vbe;
_factory = folderBrowserFactory;
Expand Down
18 changes: 16 additions & 2 deletions Rubberduck.Core/UI/FileBrowserDialogFactory.cs
@@ -1,7 +1,11 @@
namespace Rubberduck.UI
{
public interface IFolderBrowserFactory
public interface IFileSystemBrowserFactory
{
IOpenFileDialog CreateOpenFileDialog();

ISaveFileDialog CreateSaveFileDialog();

IFolderBrowser CreateFolderBrowser(string description);

IFolderBrowser CreateFolderBrowser(string description, bool showNewFolderButton);
Expand All @@ -10,7 +14,7 @@ public interface IFolderBrowserFactory
string rootFolder);
}

public class DialogFactory : IFolderBrowserFactory
public class DialogFactory : IFileSystemBrowserFactory
{
private readonly IEnvironmentProvider _environment;
private readonly bool _oldSchool;
Expand Down Expand Up @@ -48,5 +52,15 @@ public IFolderBrowser CreateFolderBrowser(string description, bool showNewFolder
? new ModernFolderBrowser(_environment, description, showNewFolderButton, rootFolder) as IFolderBrowser
: new FolderBrowser(_environment, description, showNewFolderButton, rootFolder);
}

public IOpenFileDialog CreateOpenFileDialog()
{
return new OpenFileDialog();
}

public ISaveFileDialog CreateSaveFileDialog()
{
return new SaveFileDialog();
}
}
}

0 comments on commit aa01470

Please sign in to comment.