Skip to content

Commit

Permalink
Disable "Create Request" button when there are no folders
Browse files Browse the repository at this point in the history
The *model* guards against this, but the *view model* explicitly assumes
there are folders and (silently) crashes. Now the view model has a
proper CanExecute observable.
  • Loading branch information
TwelveBaud committed Sep 21, 2021
1 parent 67155ca commit 67318d5
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/Client/Workbooks/ViewModels/WorkbookViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
Expand Down Expand Up @@ -49,6 +50,7 @@ public WorkbookViewModel(
this.programStateManager = programStateManager;

var isRenamingBehaviorSubject = new BehaviorSubject<bool>(false);
var deferredCanCreateItemBehaviorSubject = new BehaviorSubject<bool>(false);

this.isRenamingObservableAsPropertyHelper = isRenamingBehaviorSubject
.ToProperty(this, viewModel => viewModel.IsRenaming);
Expand Down Expand Up @@ -117,18 +119,20 @@ public WorkbookViewModel(

this.NewRequestInteraction = new Interaction<WorkbookModel, PathModel?>();

this.NewRequestCommand = ReactiveCommand.CreateFromTask(async () =>
{
var newRequestPath = await this.NewRequestInteraction.Handle(workbookModel);
if (newRequestPath != null)
this.NewRequestCommand = ReactiveCommand.CreateFromTask(
async () =>
{
workbookModel = workbookModel.NewRequest(newRequestPath);
this.WorkbookItems.Update(workbookModel);
}
var newRequestPath = await this.NewRequestInteraction.Handle(workbookModel);
return Unit.Default;
});
if (newRequestPath != null)
{
workbookModel = workbookModel.NewRequest(newRequestPath);
this.WorkbookItems.Update(workbookModel);
}
return Unit.Default;
},
deferredCanCreateItemBehaviorSubject);

this.OpenEnvironmentsCommand = ReactiveCommand.Create(() => new OpenEnvironmentsResult(
workbookModel,
Expand Down Expand Up @@ -161,6 +165,11 @@ public WorkbookViewModel(
this.SaveCommand,
openRequestCommand,
workbookState);

// This has to be constructed by hand rather than intuited from a lambda expression because Expression has different arity than Expression<Func<WorkbookViewModel, int>>
Expression watchChain = Expression.Property(Expression.Property(Expression.Property(Expression.Property(Expression.Parameter(typeof(WorkbookViewModel)), "WorkbookItems"), "FolderItems"), "Items"), "Count");
this.SubscribeToExpressionChain<WorkbookViewModel, int>(watchChain).Select(countProperty => countProperty.Value > 0).Subscribe(deferredCanCreateItemBehaviorSubject);
deferredCanCreateItemBehaviorSubject.OnNext(this.WorkbookItems.FolderItems.Items.Count > 0); // Reinject the current value once load completes.
}

/// <summary>
Expand Down

0 comments on commit 67318d5

Please sign in to comment.