relocating commands here, adding confirmations, etc

This commit is contained in:
Jake Norbut committed 2019-08-20 10:16:21 -05:00
1 parent aa3dda2c57
commit a8cd07e4d4
1 file changed
+58 -17
@@ -8,12 +8,15 @@ using System.Threading.Tasks;
using mbts;
using mbts.ui;
using mbts.ui.shell;
using bts.spark;
using System.Windows;
namespace mbts.ui.shell
{
public class WorkspacesDialogViewModel : ViewModelBaseWithValidation
{
private WorkspacesModel _work_model;
IRaiseCanExecuteChangedCommand _load_command, _delete_command, _save_command;
public WorkspacesDialogViewModel(WorkspacesModel wm)
{
@@ -25,36 +28,74 @@ namespace mbts.ui.shell
public bool IsPublicWorkspace { get; set; }
public bool ShowWorkspaces { get; set; }
public ICommand SaveWorkspace
WorkspaceViewModel _selected_workspace;
public WorkspaceViewModel selected_workspace {
get => _selected_workspace;
set {
if (Set(ref _selected_workspace, value)) {
LoadCommand.TryRaiseCanExecuteChanged();
DeleteCommand.TryRaiseCanExecuteChanged();
SaveCommand.TryRaiseCanExecuteChanged();
}
}
}
private bool _command_can_execute() => selected_workspace != null;
private void _clear_error_state() => clear_error(nameof(SaveName));
public ICommand LoadCommand => _load_command ??
(_load_command = new DelegateCommand(
() => {
_work_model.load_workspace(selected_workspace.Definition, selected_workspace.Name).ignore();
_clear_error_state();
}, _command_can_execute));
public ICommand DeleteCommand => _delete_command ??
(_delete_command = new DelegateCommand(
() => {
if (BTSMessageBox.Show(
$"Are you sure you with to delete the {selected_workspace.Name} workspace?",
"Delete Existing Workspace",
BTSMessageBoxButtons.Yes | BTSMessageBoxButtons.No
, MessageBoxImage.Question) == BTSMessageBoxButtons.Yes) {
_work_model.delete_workspace(selected_workspace.Name, selected_workspace.Definition).ignore();
_clear_error_state();
}
}, _command_can_execute));
public ICommand SaveCommand => _save_command ??
(_save_command = new DelegateCommand(
() => {
if (BTSMessageBox.Show(
$"Overwrite {selected_workspace.Name} with current layout?",
"Save Existing Workspace",
BTSMessageBoxButtons.Yes | BTSMessageBoxButtons.No
, MessageBoxImage.Question) == BTSMessageBoxButtons.Yes) {
_work_model.save_workspace(selected_workspace.Name, ispublic:selected_workspace.Definition.is_shared);
_clear_error_state();
}
}, _command_can_execute));
public ICommand SaveNewWorkspace
{
get
{
return new DelegateCommand(arg => {
_clear_error_state();
// Check for errors in the input
if (string.IsNullOrEmpty(SaveName))
{
set_error("SaveName", "Workspace must be named");
if (string.IsNullOrEmpty(SaveName)) {
set_error(nameof(SaveName), "Workspace must be named");
return;
}
if (SaveName.Contains(WorkspacesModel.ACTIVE_WORKSPACE_PREFIX))
{
set_error("SaveName", "Workspace name cannot contains '{0}'".fmt(WorkspacesModel.ACTIVE_WORKSPACE_PREFIX));
if (SaveName.Contains(WorkspacesModel.ACTIVE_WORKSPACE_PREFIX)) {
set_error(nameof(SaveName), "Workspace name cannot contains '{0}'".fmt(WorkspacesModel.ACTIVE_WORKSPACE_PREFIX));
return;
}
else
{
clear_error("SaveName");
if (Workspaces.Any(x => x.Name.Replace(WorkspacesModel.SHARED_MONIKER, string.Empty) == SaveName && x.IsShared == IsPublicWorkspace)) {
set_error(nameof(SaveName), $"Workspace already exists '{SaveName}'");
return;
}
var save_candidate_already_exists = Workspaces.Any(x => x.Name.Replace(WorkspacesModel.SHARED_MONIKER, string.Empty) == SaveName && x.IsShared == IsPublicWorkspace);
// Save
_work_model.save_workspace(SaveName, ispublic: IsPublicWorkspace);
// if we just overwrote something, have the vm reflect it
if (save_candidate_already_exists)
Workspaces.Where(x => x.Name.Replace(WorkspacesModel.SHARED_MONIKER, string.Empty) == SaveName && x.IsShared == IsPublicWorkspace).ForEach(x => x.VisualState = "Overridden");
});
}
}
}
}