/// <summary> /// Creates a new schedule with a random identifier /// and makes the client join it using the specified user name. /// </summary> public async Task CreateAndJoinScheduleAsync(string userName, string catalogId, ErrorReporter errorReporter) { if (string.IsNullOrWhiteSpace(userName)) { errorReporter.Throw( new ArgumentException("The name of the specified user is invalid", nameof(userName)), UserErrorsViewModel.UserNameInvalidMessage); } if (State != SessionState.Default) { errorReporter.Throw( new InvalidOperationException(), UserErrorsViewModel.WrongSessionStateMessage); } SharedScheduleVM = await _scheduleManager.CreateScheduleAsync(catalogId, errorReporter); TailoredScheduleVM = ScheduleViewModel.CreateFrom(SharedScheduleVM.Schedule, errorReporter); SearchVM = new CourseSearchViewModel(SharedScheduleVM.Schedule.CourseCatalogue, SharedScheduleVM.Schedule); ExportVM = new ScheduleExportViewModel(SharedScheduleVM.Schedule); // Add user to list of current users Name = userName; await SharedScheduleVM.AddUserAsync(this, errorReporter); CourseListVM = new CourseListViewModel(SharedScheduleVM.Schedule, User); State = SessionState.JoinedSchedule; }
/// <summary> /// Depending on the current state this method /// removes the user from the schedule he/she has joined /// or cancels the join attempt. /// </summary> public async Task DisconnectAsync() { switch (State) { case SessionState.Default: // Nothing to do break; case SessionState.JoiningSchedule: SharedScheduleVM = null; break; case SessionState.JoinedSchedule: SharedScheduleVM.RemoveUser(this); await _scheduleManager.SaveScheduleAsync(SharedScheduleVM); SharedScheduleVM = null; TailoredScheduleVM = null; SearchVM = null; ExportVM = null; CourseListVM = null; Name = null; break; default: throw new NotImplementedException(); } State = SessionState.Default; await Task.FromResult(0); }
/// <summary> /// Joins the schedule specified by <see cref="BeginJoinSchedule(string)"/> /// by adding the calling client to the schedule's list of users. /// </summary> /// <remarks> /// After this call, synchronization of <see cref="TailoredScheduleVM"/> /// and <see cref="SearchVM"/> with the calling client should start. /// </remarks> /// <param name="userName"> /// User name (either one of the schedule's known but currently unused user names /// (see <see cref="SharedScheduleViewModel.AvailableUserNames"/> or a new name). /// </param> /// <param name="errorReporter">Object used to throw exceptions</param> /// <returns>True if the join completion was successful</returns> public async Task CompleteJoinScheduleAsync(string userName, ErrorReporter errorReporter) { if (State != SessionState.JoiningSchedule) { errorReporter.Throw(UserErrorsViewModel.GenericErrorMessage + " (Falscher SessionState)"); } if (string.IsNullOrWhiteSpace(userName)) { errorReporter.Throw("Es wurde ein ungültiger Name eingegeben"); } Name = userName; // This fails if user name is invalid (empty) or already used by another client await SharedScheduleVM.AddUserAsync(this, errorReporter); TailoredScheduleVM = ScheduleViewModel.CreateFrom(SharedScheduleVM.Schedule, errorReporter); SearchVM = new CourseSearchViewModel(SharedScheduleVM.Schedule.CourseCatalogue, SharedScheduleVM.Schedule); ExportVM = new ScheduleExportViewModel(SharedScheduleVM.Schedule); CourseListVM = new CourseListViewModel(SharedScheduleVM.Schedule, User); State = SessionState.JoinedSchedule; }