Add meeting schedule state management services and related models
This commit introduces several new services and models to manage the meeting schedule state within localStorage. The MeetingScheduleState class is created to track scheduled teams, absent students, time slot counts, extended teams, and excluded students. Additionally, the IMeetingScheduleStateService interface and its implementation, MeetingScheduleStateService, are added to handle loading and saving state data. The MeetingScheduleClipboardService and MeetingScheduleDataService are also introduced to facilitate clipboard operations and data loading from the database, respectively. These enhancements improve the overall functionality and user experience of the meeting scheduling feature.
This commit is contained in:
@@ -2,16 +2,21 @@
|
||||
@attribute [Authorize]
|
||||
@using System.Text
|
||||
@using Core.Calculation
|
||||
@using Core.Models
|
||||
@using Core.Utility
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using WebApp.Components.Shared.Components
|
||||
@using WebApp.Components.Features.MeetingSchedule
|
||||
@using Core.Utility
|
||||
@using WebApp.Models
|
||||
@using WebApp.Services
|
||||
@inject IConfiguration Configuration
|
||||
@inject AppDbContext Context
|
||||
@inject ClipboardService ClipboardService
|
||||
@inject LocalStorageService LocalStorage
|
||||
@inject IDialogService DialogService
|
||||
@inject ISnackbar Snackbar
|
||||
@inject IMeetingScheduleStateService StateService
|
||||
@inject IMeetingScheduleDataService DataService
|
||||
@inject IMeetingScheduleClipboardService ClipboardFormatService
|
||||
@inject LocalStorageService LocalStorage
|
||||
|
||||
<PageHeader Title="@($"{Configuration["ChapterSettings:Shortname"]} TSA Schedule {Configuration["ChapterSettings:CompetitionYear"]}")">
|
||||
<ActionButtons>
|
||||
@@ -180,37 +185,31 @@
|
||||
|
||||
private void AddRegionals()
|
||||
{
|
||||
_scheduledTeams
|
||||
= _teams.Where(e => e.Event.RegionalEvent).Concat(_scheduledTeams).Distinct();
|
||||
_scheduledTeams = _scheduledTeams.AddRegionals(_teams);
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private void AddHighLevelOfEffort()
|
||||
{
|
||||
_scheduledTeams
|
||||
= _teams.Where(e => e.Event.LevelOfEffort >= 3).Concat(_scheduledTeams).Distinct();
|
||||
_scheduledTeams = _scheduledTeams.AddHighLevelOfEffort(_teams);
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private void RemoveIndividual()
|
||||
{
|
||||
_scheduledTeams
|
||||
= _scheduledTeams.Where(t => t.Event.EventFormat != EventFormat.Individual);
|
||||
_scheduledTeams = _scheduledTeams.RemoveIndividual();
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private void RemoveLowLevelOfEffort()
|
||||
{
|
||||
_scheduledTeams
|
||||
= _scheduledTeams.Where(t => t.Event.LevelOfEffort > 1);
|
||||
_scheduledTeams = _scheduledTeams.RemoveLowLevelOfEffort();
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private void Invert()
|
||||
{
|
||||
var rt = _scheduledTeams.ToArray();
|
||||
_scheduledTeams
|
||||
= _teams.Where(t => !rt.Contains(t));
|
||||
_scheduledTeams = _scheduledTeams.Invert(_teams);
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
@@ -273,32 +272,15 @@
|
||||
]
|
||||
);
|
||||
|
||||
_teams
|
||||
= await Context.Teams
|
||||
.AsNoTracking()
|
||||
.Include(e => e.Event)
|
||||
.Include(e => e.Students)
|
||||
.OrderBy(e => e.Event.Name)
|
||||
.ThenBy(e => e.Identifier)
|
||||
.ToArrayAsync();
|
||||
|
||||
_students =
|
||||
await Context.Students
|
||||
.AsNoTracking()
|
||||
.Include(e => e.Teams)
|
||||
.ThenInclude(t => t.Event)
|
||||
.Include(e => e.Teams)
|
||||
.ThenInclude(t => t.Captain)
|
||||
.Include(e => e.EventRankings)
|
||||
.ThenInclude(e => e.EventDefinition)
|
||||
.OrderBy(e => e.FirstName).ToArrayAsync();
|
||||
_teams = await DataService.LoadTeamsAsync();
|
||||
_students = await DataService.LoadStudentsAsync();
|
||||
|
||||
// Load saved selections from localStorage
|
||||
await LoadScheduledTeams();
|
||||
await LoadAbsentStudents();
|
||||
await LoadTimeSlotCount();
|
||||
await LoadExtendedTeams();
|
||||
await LoadExcludedStudents();
|
||||
_scheduledTeams = await StateService.LoadScheduledTeamsAsync(_teams);
|
||||
_absentStudents = await StateService.LoadAbsentStudentsAsync(_students);
|
||||
_parameters.TimeSlots = await StateService.LoadTimeSlotCountAsync(2);
|
||||
_extendedTeams = await StateService.LoadExtendedTeamsAsync(_teams);
|
||||
_excludedStudents = await StateService.LoadExcludedStudentsAsync();
|
||||
|
||||
// Initialize last saved state from loaded values
|
||||
_lastSavedState = await MeetingScheduleState.FromLocalStorage(LocalStorage, _teams, _students);
|
||||
@@ -314,84 +296,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SaveScheduledTeams()
|
||||
{
|
||||
var teamIds = _scheduledTeams.Select(t => t.Id).ToArray();
|
||||
await LocalStorage.SetIntArrayAsync("MeetingSchedule_ScheduledTeams", teamIds);
|
||||
}
|
||||
|
||||
private async Task LoadScheduledTeams()
|
||||
{
|
||||
var teamIds = await LocalStorage.GetIntArrayAsync("MeetingSchedule_ScheduledTeams");
|
||||
if (teamIds.Length > 0)
|
||||
{
|
||||
_scheduledTeams = _teams.Where(t => teamIds.Contains(t.Id)).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SaveAbsentStudents()
|
||||
{
|
||||
var studentIds = _absentStudents.Select(s => s.Id).ToArray();
|
||||
await LocalStorage.SetIntArrayAsync("MeetingSchedule_AbsentStudents", studentIds);
|
||||
}
|
||||
|
||||
private async Task LoadAbsentStudents()
|
||||
{
|
||||
var studentIds = await LocalStorage.GetIntArrayAsync("MeetingSchedule_AbsentStudents");
|
||||
if (studentIds.Length > 0)
|
||||
{
|
||||
_absentStudents = _students.Where(s => studentIds.Contains(s.Id)).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SaveTimeSlotCount()
|
||||
{
|
||||
await LocalStorage.SetIntAsync("MeetingSchedule_TimeSlotCount", _parameters.TimeSlots);
|
||||
}
|
||||
|
||||
private async Task LoadTimeSlotCount()
|
||||
{
|
||||
var timeSlots = await LocalStorage.GetIntAsync("MeetingSchedule_TimeSlotCount", defaultValue: 2);
|
||||
if (timeSlots > 0)
|
||||
{
|
||||
_parameters.TimeSlots = timeSlots;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SaveExtendedTeams()
|
||||
{
|
||||
var teamIds = _extendedTeams.Select(t => t.Id).ToArray();
|
||||
await LocalStorage.SetIntArrayAsync("MeetingSchedule_ExtendedTeams", teamIds);
|
||||
}
|
||||
|
||||
private async Task LoadExtendedTeams()
|
||||
{
|
||||
var teamIds = await LocalStorage.GetIntArrayAsync("MeetingSchedule_ExtendedTeams");
|
||||
if (teamIds.Length > 0)
|
||||
{
|
||||
_extendedTeams = _teams.Where(t => teamIds.Contains(t.Id)).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SaveExcludedStudents()
|
||||
{
|
||||
var exclusions = _excludedStudents.Keys
|
||||
.Where(k => _excludedStudents[k])
|
||||
.Select(k => new ExcludedStudent(k.teamId, k.timeSlotIndex, k.studentId))
|
||||
.ToArray();
|
||||
await LocalStorage.SetJsonAsync("MeetingSchedule_ExcludedStudents", exclusions);
|
||||
}
|
||||
|
||||
private async Task LoadExcludedStudents()
|
||||
{
|
||||
var exclusions = await LocalStorage.GetJsonAsync<ExcludedStudent[]>("MeetingSchedule_ExcludedStudents");
|
||||
if (exclusions != null && exclusions.Length > 0)
|
||||
{
|
||||
_excludedStudents = exclusions.ToDictionary(
|
||||
e => (e.TeamId, e.TimeSlotIndex, e.StudentId),
|
||||
_ => true);
|
||||
}
|
||||
}
|
||||
|
||||
private int GetTimeSlotIndex(string timeSlotName)
|
||||
{
|
||||
@@ -425,40 +329,12 @@
|
||||
return _excludedStudents.TryGetValue(key, out var isExcluded) && isExcluded;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates teams with excluded students and absent students filtered out for overlap calculation.
|
||||
/// </summary>
|
||||
private Team[] GetTeamsWithoutExcludedStudents(Team[] teams, int timeSlotIndex)
|
||||
{
|
||||
var absentStudentIds = _absentStudents.Select(s => s.Id).ToHashSet();
|
||||
|
||||
return teams.Select(team =>
|
||||
{
|
||||
// Find excluded students for this team in this time slot
|
||||
// Also exclude absent students from overlap calculations
|
||||
// More efficient: iterate through team students and check exclusions
|
||||
var includedStudents = team.Students
|
||||
.Where(s => !IsStudentExcluded(team.Id, timeSlotIndex, s.Id) && !absentStudentIds.Contains(s.Id))
|
||||
.ToList();
|
||||
|
||||
// If no students are excluded, return original team
|
||||
if (includedStudents.Count == team.Students.Count)
|
||||
return team;
|
||||
|
||||
// Create a temporary team with excluded and absent students removed
|
||||
return new Team
|
||||
{
|
||||
Id = team.Id,
|
||||
Event = team.Event,
|
||||
Students = includedStudents,
|
||||
Captain = team.Captain,
|
||||
Identifier = team.Identifier
|
||||
};
|
||||
}).ToArray();
|
||||
return OverlapCalculationHelper.GetTeamsWithoutExcludedStudents(teams, timeSlotIndex, _excludedStudents, absentStudentIds);
|
||||
}
|
||||
|
||||
private record ExcludedStudent(int TeamId, int TimeSlotIndex, int StudentId);
|
||||
|
||||
private bool IsDirty()
|
||||
{
|
||||
if (_lastSavedState == null)
|
||||
@@ -507,26 +383,7 @@
|
||||
.ToArray();
|
||||
|
||||
// Create PartialTeam instances for teams with excluded students
|
||||
// Aggregate exclusions across all time slots (since we don't know assignment yet)
|
||||
var teamsForScheduling = _scheduledTeams.Select(team =>
|
||||
{
|
||||
// Find all students excluded for this team across all time slots
|
||||
var excludedStudentIds = _excludedStudents.Keys
|
||||
.Where(k => k.teamId == team.Id && _excludedStudents[k])
|
||||
.Select(k => k.studentId)
|
||||
.Distinct()
|
||||
.ToHashSet();
|
||||
|
||||
if (excludedStudentIds.Count == 0)
|
||||
return team;
|
||||
|
||||
var excludedStudents = team.Students.Where(s => excludedStudentIds.Contains(s.Id)).ToList();
|
||||
if (excludedStudents.Count == 0)
|
||||
return team;
|
||||
|
||||
// Create PartialTeam with excluded students
|
||||
return team.CloneWithOmittedStudents(excludedStudents);
|
||||
}).ToArray();
|
||||
var teamsForScheduling = PartialTeam.CreatePartialTeamsFromExclusions(_scheduledTeams, _excludedStudents);
|
||||
|
||||
var teamScheduler = new TeamScheduler(teamsForScheduling, _parameters.TimeSlots, availableStudents);
|
||||
_solution = teamScheduler.Solve();
|
||||
@@ -564,7 +421,11 @@
|
||||
// Post-process: extend teams to next consecutive time slot
|
||||
if (_extendedTeams.Any())
|
||||
{
|
||||
ExtendTeamsInSolution(_solution, _extendedTeams, availableStudents);
|
||||
TeamSchedulerPostProcessor.ExtendTeamsInSolution(
|
||||
_solution,
|
||||
_extendedTeams,
|
||||
availableStudents,
|
||||
GetTeamsWithoutExcludedStudents);
|
||||
}
|
||||
|
||||
// Try recommendation strategies in priority order
|
||||
@@ -590,6 +451,13 @@
|
||||
_excludedStudents);
|
||||
await currentState.SaveToLocalStorage(LocalStorage);
|
||||
_lastSavedState = currentState;
|
||||
|
||||
// Also save via state service for consistency
|
||||
await StateService.SaveScheduledTeamsAsync(_scheduledTeams);
|
||||
await StateService.SaveAbsentStudentsAsync(_absentStudents);
|
||||
await StateService.SaveTimeSlotCountAsync(_parameters.TimeSlots);
|
||||
await StateService.SaveExtendedTeamsAsync(_extendedTeams);
|
||||
await StateService.SaveExcludedStudentsAsync(_excludedStudents);
|
||||
|
||||
await InvokeAsync(StateHasChanged); // let the UI know that the solution has been found
|
||||
|
||||
@@ -602,91 +470,18 @@
|
||||
_solutionData.ReloadServerData();
|
||||
}
|
||||
|
||||
// TODO: Move team extension logic into Core.Calculation.TeamScheduler to handle extended teams
|
||||
// as part of the constraint programming model rather than post-processing
|
||||
private void ExtendTeamsInSolution(TeamSchedulerSolution solution, IEnumerable<Team> extendedTeams, Student[] allStudents)
|
||||
{
|
||||
if (solution.TimeSlots == null || !solution.TimeSlots.Any())
|
||||
return;
|
||||
|
||||
var extendedTeamsList = extendedTeams.ToList();
|
||||
if (!extendedTeamsList.Any())
|
||||
return;
|
||||
|
||||
var extendedTeamIds = extendedTeamsList.Select(t => t.Id).ToHashSet();
|
||||
|
||||
// Find which time slot each extended team is in and extend both forward and backward
|
||||
for (int slotIndex = 0; slotIndex < solution.TimeSlots.Length; slotIndex++)
|
||||
{
|
||||
var currentSlot = solution.TimeSlots[slotIndex];
|
||||
var teamsToExtend = currentSlot.Teams.Where(t => extendedTeamIds.Contains(t.Id)).ToList();
|
||||
|
||||
if (!teamsToExtend.Any())
|
||||
continue;
|
||||
|
||||
// Extend forward: add to next time slot (if exists)
|
||||
if (slotIndex + 1 < solution.TimeSlots.Length)
|
||||
{
|
||||
var nextSlot = solution.TimeSlots[slotIndex + 1];
|
||||
var nextSlotTeamsList = nextSlot.Teams.ToList();
|
||||
var nextSlotTeamIds = nextSlotTeamsList.Select(t => t.Id).ToHashSet();
|
||||
|
||||
foreach (var team in teamsToExtend)
|
||||
{
|
||||
if (!nextSlotTeamIds.Contains(team.Id))
|
||||
{
|
||||
nextSlotTeamsList.Add(team);
|
||||
nextSlotTeamIds.Add(team.Id);
|
||||
}
|
||||
}
|
||||
|
||||
nextSlot.Teams = nextSlotTeamsList.ToArray();
|
||||
var nextSlotIndex = slotIndex + 1;
|
||||
var nextSlotTeamsForOverlap = GetTeamsWithoutExcludedStudents(nextSlot.Teams, nextSlotIndex);
|
||||
nextSlot.StudentOverlaps = TeamSchedulerSolution.GetStudentTeamOverlaps(nextSlotTeamsForOverlap);
|
||||
// Use teams without excluded students so students excluded from all teams appear as unscheduled
|
||||
nextSlot.UnscheduledStudents = TeamSchedulerSolution.GetStudentsNotInTimSlot(nextSlotTeamsForOverlap, allStudents);
|
||||
}
|
||||
|
||||
// Extend backward: add to previous time slot (if exists)
|
||||
if (slotIndex > 0)
|
||||
{
|
||||
var previousSlot = solution.TimeSlots[slotIndex - 1];
|
||||
var previousSlotTeamsList = previousSlot.Teams.ToList();
|
||||
var previousSlotTeamIds = previousSlotTeamsList.Select(t => t.Id).ToHashSet();
|
||||
|
||||
foreach (var team in teamsToExtend)
|
||||
{
|
||||
if (!previousSlotTeamIds.Contains(team.Id))
|
||||
{
|
||||
previousSlotTeamsList.Add(team);
|
||||
previousSlotTeamIds.Add(team.Id);
|
||||
}
|
||||
}
|
||||
|
||||
previousSlot.Teams = previousSlotTeamsList.ToArray();
|
||||
var previousSlotIndex = slotIndex - 1;
|
||||
var previousSlotTeamsForOverlap = GetTeamsWithoutExcludedStudents(previousSlot.Teams, previousSlotIndex);
|
||||
previousSlot.StudentOverlaps = TeamSchedulerSolution.GetStudentTeamOverlaps(previousSlotTeamsForOverlap);
|
||||
// Use teams without excluded students so students excluded from all teams appear as unscheduled
|
||||
previousSlot.UnscheduledStudents = TeamSchedulerSolution.GetStudentsNotInTimSlot(previousSlotTeamsForOverlap, allStudents);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async Task CopyToClipboard()
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
foreach (var timeslot in _solution.TimeSlots)
|
||||
{
|
||||
AppendScheduledTeams(sb, timeslot);
|
||||
AppendUnscheduledStudents(sb, timeslot);
|
||||
sb.Append(Environment.NewLine);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await ClipboardService.WriteTextAsync(sb.ToString());
|
||||
var text = ClipboardFormatService.FormatScheduleForClipboard(
|
||||
_solution,
|
||||
_teams,
|
||||
_absentStudents,
|
||||
_excludedStudents,
|
||||
GetTimeSlotIndex);
|
||||
await ClipboardService.WriteTextAsync(text);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -717,220 +512,6 @@
|
||||
// Note: Success message is already shown in the dialog, no need to show another here
|
||||
}
|
||||
|
||||
private void AppendScheduledTeams(StringBuilder sb, TeamScheduleTimeSlot timeslot)
|
||||
{
|
||||
var timeSlotIndex = GetTimeSlotIndex(timeslot.Name);
|
||||
foreach (var scheduledTeam in timeslot.Teams.OrderBy(e => e.ToString()))
|
||||
{
|
||||
var teamName = scheduledTeam.ToString();
|
||||
|
||||
if (scheduledTeam.Event.EventFormat is EventFormat.Individual)
|
||||
{
|
||||
sb.Append(teamName);
|
||||
}
|
||||
else
|
||||
{
|
||||
var studentsList = FormatStudentList(scheduledTeam, timeslot, timeSlotIndex);
|
||||
sb.Append($"{teamName} - {studentsList}");
|
||||
}
|
||||
sb.Append(Environment.NewLine);
|
||||
}
|
||||
}
|
||||
|
||||
private string FormatStudentList(Team team, TeamScheduleTimeSlot timeslot, int timeSlotIndex)
|
||||
{
|
||||
// Filter out excluded students for this team and time slot
|
||||
var excludedStudentIds = _excludedStudents.Keys
|
||||
.Where(k => k.teamId == team.Id && k.timeSlotIndex == timeSlotIndex && _excludedStudents[k])
|
||||
.Select(k => k.studentId)
|
||||
.ToHashSet();
|
||||
|
||||
var includedStudents = team.Students
|
||||
.Where(s => !excludedStudentIds.Contains(s.Id))
|
||||
.ToList();
|
||||
|
||||
// Create a temporary team with only included students for formatting
|
||||
var teamForFormatting = new Team
|
||||
{
|
||||
Id = team.Id,
|
||||
Event = team.Event,
|
||||
Students = includedStudents,
|
||||
Captain = team.Captain,
|
||||
Identifier = team.Identifier
|
||||
};
|
||||
|
||||
return TeamStudentNameFormatter.FormatStudentList(
|
||||
teamForFormatting,
|
||||
new TeamStudentNameFormatter.FormatOptions
|
||||
{
|
||||
Ordering = TeamStudentNameFormatter.OrderingStyle.CaptainFirst,
|
||||
MarkOverlaps = true,
|
||||
HasOverlaps = timeslot.StudentHasOverlaps,
|
||||
MarkAbsent = true,
|
||||
AbsentStudents = _absentStudents.ToList()
|
||||
});
|
||||
}
|
||||
|
||||
private string FormatStudentName(Student student, TeamScheduleTimeSlot timeslot)
|
||||
{
|
||||
// Find the team this student belongs to for formatting context
|
||||
var team = _teams.FirstOrDefault(t => t.Students.Contains(student));
|
||||
if (team == null)
|
||||
{
|
||||
// No team context, use StudentNameFormatter directly
|
||||
return StudentNameFormatter.FormatStudentName(
|
||||
student,
|
||||
new StudentNameFormatter.FormatOptions
|
||||
{
|
||||
HasOverlap = timeslot.StudentHasOverlaps(student),
|
||||
IsAbsent = _absentStudents.Contains(student)
|
||||
});
|
||||
}
|
||||
|
||||
return TeamStudentNameFormatter.FormatStudentName(
|
||||
student,
|
||||
team,
|
||||
new TeamStudentNameFormatter.FormatOptions
|
||||
{
|
||||
MarkOverlaps = true,
|
||||
HasOverlaps = timeslot.StudentHasOverlaps,
|
||||
MarkAbsent = true,
|
||||
AbsentStudents = _absentStudents.ToList()
|
||||
});
|
||||
}
|
||||
|
||||
private void AppendUnscheduledStudents(StringBuilder sb, TeamScheduleTimeSlot timeslot)
|
||||
{
|
||||
if (!timeslot.UnscheduledStudents.Any())
|
||||
return;
|
||||
|
||||
sb.Append("--Unscheduled");
|
||||
sb.Append(Environment.NewLine);
|
||||
|
||||
foreach (var student in timeslot.UnscheduledStudents)
|
||||
{
|
||||
var studentName = StudentNameFormatter.FormatStudentName(
|
||||
student,
|
||||
new StudentNameFormatter.FormatOptions
|
||||
{
|
||||
IsAbsent = _absentStudents.Contains(student)
|
||||
});
|
||||
|
||||
var unassignedTeams = _solution.StudentUnassignedTeams(student);
|
||||
var teamsList = string.Join(", ", unassignedTeams.Select(e => e.ToString()));
|
||||
|
||||
sb.Append($"{studentName} - {teamsList}");
|
||||
sb.Append(Environment.NewLine);
|
||||
}
|
||||
}
|
||||
|
||||
private class MeetingScheduleState : IEquatable<MeetingScheduleState>
|
||||
{
|
||||
public HashSet<int> ScheduledTeamIds { get; set; } = [];
|
||||
public HashSet<int> AbsentStudentIds { get; set; } = [];
|
||||
public int TimeSlotCount { get; set; }
|
||||
public HashSet<int> ExtendedTeamIds { get; set; } = [];
|
||||
public HashSet<(int teamId, int timeSlotIndex, int studentId)> ExcludedStudents { get; set; } = [];
|
||||
|
||||
public bool Equals(MeetingScheduleState? other)
|
||||
{
|
||||
if (other == null) return false;
|
||||
return ScheduledTeamIds.SetEquals(other.ScheduledTeamIds) &&
|
||||
AbsentStudentIds.SetEquals(other.AbsentStudentIds) &&
|
||||
TimeSlotCount == other.TimeSlotCount &&
|
||||
ExtendedTeamIds.SetEquals(other.ExtendedTeamIds) &&
|
||||
ExcludedStudents.SetEquals(other.ExcludedStudents);
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj) => Equals(obj as MeetingScheduleState);
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
var hash = new HashCode();
|
||||
hash.Add(ScheduledTeamIds.Count);
|
||||
foreach (var id in ScheduledTeamIds.OrderBy(x => x))
|
||||
hash.Add(id);
|
||||
hash.Add(AbsentStudentIds.Count);
|
||||
foreach (var id in AbsentStudentIds.OrderBy(x => x))
|
||||
hash.Add(id);
|
||||
hash.Add(TimeSlotCount);
|
||||
hash.Add(ExtendedTeamIds.Count);
|
||||
foreach (var id in ExtendedTeamIds.OrderBy(x => x))
|
||||
hash.Add(id);
|
||||
hash.Add(ExcludedStudents.Count);
|
||||
foreach (var key in ExcludedStudents.OrderBy(x => x))
|
||||
hash.Add(key);
|
||||
return hash.ToHashCode();
|
||||
}
|
||||
|
||||
public static MeetingScheduleState FromCurrent(
|
||||
IEnumerable<Team> scheduledTeams,
|
||||
IEnumerable<Student> absentStudents,
|
||||
int timeSlotCount,
|
||||
IEnumerable<Team> extendedTeams,
|
||||
Dictionary<(int teamId, int timeSlotIndex, int studentId), bool> excludedStudents)
|
||||
{
|
||||
return new MeetingScheduleState
|
||||
{
|
||||
ScheduledTeamIds = scheduledTeams.Select(t => t.Id).ToHashSet(),
|
||||
AbsentStudentIds = absentStudents.Select(s => s.Id).ToHashSet(),
|
||||
TimeSlotCount = timeSlotCount,
|
||||
ExtendedTeamIds = extendedTeams.Select(t => t.Id).ToHashSet(),
|
||||
ExcludedStudents = excludedStudents.Keys
|
||||
.Where(k => excludedStudents[k])
|
||||
.ToHashSet()
|
||||
};
|
||||
}
|
||||
|
||||
public static async Task<MeetingScheduleState?> FromLocalStorage(
|
||||
LocalStorageService localStorage,
|
||||
Team[] allTeams,
|
||||
Student[] allStudents)
|
||||
{
|
||||
// Load scheduled teams
|
||||
var scheduledTeamIds = await localStorage.GetIntArrayAsync("MeetingSchedule_ScheduledTeams");
|
||||
var absentStudentIds = await localStorage.GetIntArrayAsync("MeetingSchedule_AbsentStudents");
|
||||
var timeSlotCount = await localStorage.GetIntAsync("MeetingSchedule_TimeSlotCount", defaultValue: 2);
|
||||
var extendedTeamIds = await localStorage.GetIntArrayAsync("MeetingSchedule_ExtendedTeams");
|
||||
var exclusions = await localStorage.GetJsonAsync<ExcludedStudent[]>("MeetingSchedule_ExcludedStudents");
|
||||
|
||||
// If no state exists, return null
|
||||
if (scheduledTeamIds.Length == 0 && absentStudentIds.Length == 0 &&
|
||||
timeSlotCount == 2 && extendedTeamIds.Length == 0 &&
|
||||
(exclusions == null || exclusions.Length == 0))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var excludedStudentsSet = new HashSet<(int teamId, int timeSlotIndex, int studentId)>();
|
||||
if (exclusions != null && exclusions.Length > 0)
|
||||
{
|
||||
foreach (var exclusion in exclusions)
|
||||
{
|
||||
excludedStudentsSet.Add((exclusion.TeamId, exclusion.TimeSlotIndex, exclusion.StudentId));
|
||||
}
|
||||
}
|
||||
|
||||
return new MeetingScheduleState
|
||||
{
|
||||
ScheduledTeamIds = scheduledTeamIds.ToHashSet(),
|
||||
AbsentStudentIds = absentStudentIds.ToHashSet(),
|
||||
TimeSlotCount = timeSlotCount > 0 ? timeSlotCount : 2,
|
||||
ExtendedTeamIds = extendedTeamIds.ToHashSet(),
|
||||
ExcludedStudents = excludedStudentsSet
|
||||
};
|
||||
}
|
||||
|
||||
public async Task SaveToLocalStorage(LocalStorageService localStorage)
|
||||
{
|
||||
await localStorage.SetIntArrayAsync("MeetingSchedule_ScheduledTeams", ScheduledTeamIds.ToArray());
|
||||
await localStorage.SetIntArrayAsync("MeetingSchedule_AbsentStudents", AbsentStudentIds.ToArray());
|
||||
await localStorage.SetIntAsync("MeetingSchedule_TimeSlotCount", TimeSlotCount);
|
||||
await localStorage.SetIntArrayAsync("MeetingSchedule_ExtendedTeams", ExtendedTeamIds.ToArray());
|
||||
|
||||
var exclusions = ExcludedStudents.Select(e => new ExcludedStudent(e.teamId, e.timeSlotIndex, e.studentId)).ToArray();
|
||||
await localStorage.SetJsonAsync("MeetingSchedule_ExcludedStudents", exclusions);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user