private async Task done() { if (!SaveEnabled) { return; } if (initialWorkspaceId != workspaceId) { var shouldContinue = await dialogService.Confirm( Resources.WorkspaceChangedAlertTitle, Resources.WorkspaceChangedAlertMessage, Resources.Ok, Resources.Cancel ); if (!shouldContinue) { return; } } var billable = await interactorFactory.AreProjectsBillableByDefault(workspaceId).Execute(); var createdProject = await interactorFactory.CreateProject(new CreateProjectDTO { Name = TrimmedName, Color = Color.ToHexString(), IsPrivate = IsPrivate, ClientId = clientId, Billable = billable, WorkspaceId = workspaceId }).Execute(); await navigationService.Close(this, createdProject.Id); }
private IObservable <Unit> done() { var nameIsAlreadyTaken = currentWorkspace .SelectMany(workspace => dataSource.Projects.GetAll(project => project.WorkspaceId == workspace.Id)) .Select(existingProjectsDictionary) .CombineLatest(currentClient, Name, checkNameIsTaken); var projectCreation = currentWorkspace.FirstAsync() .SelectMany(workspace => checkIfCanContinue(workspace) .SelectMany(shouldContinue => !shouldContinue ? Observable.Empty <Unit>() : getDto(workspace) .SelectMany(dto => interactorFactory.CreateProject(dto).Execute()) .Do(createdProject => Close(createdProject.Id)) .SelectUnit() ) ); return(nameIsAlreadyTaken.SelectMany(taken => { if (taken) { throw new Exception(Resources.ProjectNameTakenError); } return projectCreation; })); IObservable <bool> checkIfCanContinue(IThreadSafeWorkspace workspace) { if (initialWorkspaceId == workspace.Id) { return(Observable.Return(true)); } return(View.Confirm( Resources.WorkspaceChangedAlertTitle, Resources.WorkspaceChangedAlertMessage, Resources.Ok, Resources.Cancel )); } IObservable <CreateProjectDTO> getDto(IThreadSafeWorkspace workspace) => Observable.CombineLatest( Name.FirstAsync(), Color.FirstAsync(), currentClient.FirstAsync(), interactorFactory.AreProjectsBillableByDefault(workspace.Id).Execute(), IsPrivate.FirstAsync(), (name, color, client, billable, isPrivate) => new CreateProjectDTO { Name = name.Trim(), Color = color.ToHexString(), IsPrivate = isPrivate, ClientId = client?.Id, Billable = billable, WorkspaceId = workspace.Id } ); Dictionary <long, HashSet <string> > existingProjectsDictionary(IEnumerable <IThreadSafeProject> projectsInWorkspace) => projectsInWorkspace.Aggregate(new Dictionary <long, HashSet <string> >(), (dict, project) => { var key = project.ClientId ?? noClientId; if (dict.ContainsKey(key)) { dict[key].Add(project.Name); return(dict); } dict[key] = new HashSet <string> { project.Name }; return(dict); }); bool checkNameIsTaken(Dictionary <long, HashSet <string> > projectNameDictionary, IThreadSafeClient client, string name) { var key = client?.Id ?? noClientId; if (projectNameDictionary.TryGetValue(key, out var projectNames)) { return(projectNames.Contains(name.Trim())); } return(false); } }