mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 07:38:33 -06:00
got debounced saving working. some of the assignment saving logic is real time
This commit is contained in:
52
Management/Features/Configuration/AssignmentEditorContext.cs
Normal file
52
Management/Features/Configuration/AssignmentEditorContext.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using LocalModels;
|
||||
using Management.Planner;
|
||||
|
||||
public class AssignmentEditorContext
|
||||
{
|
||||
public event Action? StateHasChanged;
|
||||
private CoursePlanner planner { get; }
|
||||
|
||||
public AssignmentEditorContext(CoursePlanner planner)
|
||||
{
|
||||
this.planner = planner;
|
||||
}
|
||||
|
||||
private LocalAssignment? _assignment;
|
||||
public LocalAssignment? Assignment
|
||||
{
|
||||
get { return _assignment; }
|
||||
set
|
||||
{
|
||||
_assignment = value;
|
||||
StateHasChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveAssignment(LocalAssignment newAssignment)
|
||||
{
|
||||
if (planner.LocalCourse != null)
|
||||
{
|
||||
Console.WriteLine(newAssignment.Description);
|
||||
var currentModule =
|
||||
planner.LocalCourse.Modules.First(
|
||||
m => m.Assignments.Select(a => a.Id).Contains(newAssignment.Id)
|
||||
) ?? throw new Exception("could not find current module in assignment description form");
|
||||
|
||||
var updatedModules = planner.LocalCourse.Modules
|
||||
.Select(
|
||||
m =>
|
||||
m.Name == currentModule.Name
|
||||
? currentModule with
|
||||
{
|
||||
Assignments = currentModule.Assignments
|
||||
.Select(a => a.Id == newAssignment.Id ? newAssignment : a)
|
||||
.ToArray()
|
||||
}
|
||||
: m
|
||||
)
|
||||
.ToArray();
|
||||
|
||||
planner.LocalCourse = planner.LocalCourse with { Modules = updatedModules };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,8 @@ public class CoursePlanner
|
||||
this.canvas = canvas;
|
||||
}
|
||||
|
||||
private Timer _debounceTimer;
|
||||
private int _debounceInterval = 1000;
|
||||
private LocalCourse? _localCourse { get; set; }
|
||||
public LocalCourse? LocalCourse
|
||||
{
|
||||
@@ -36,15 +38,30 @@ public class CoursePlanner
|
||||
|
||||
var verifiedCourse = value.GeneralCourseCleanup();
|
||||
|
||||
// ignore initial load of course
|
||||
if (_localCourse != null)
|
||||
{
|
||||
yamlManager.SaveCourse(verifiedCourse);
|
||||
}
|
||||
_debounceTimer?.Dispose();
|
||||
_debounceTimer = new Timer(
|
||||
(_) => saveCourseToFile(),
|
||||
null,
|
||||
_debounceInterval,
|
||||
Timeout.Infinite
|
||||
);
|
||||
|
||||
_localCourse = verifiedCourse;
|
||||
StateHasChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
private void saveCourseToFile()
|
||||
{
|
||||
_debounceTimer?.Dispose();
|
||||
// ignore initial load of course
|
||||
if (LocalCourse != null)
|
||||
{
|
||||
Console.WriteLine("Saving file");
|
||||
yamlManager.SaveCourse(LocalCourse);
|
||||
}
|
||||
}
|
||||
|
||||
public event Action? StateHasChanged;
|
||||
|
||||
public IEnumerable<CanvasAssignment>? CanvasAssignments { get; internal set; }
|
||||
|
||||
Reference in New Issue
Block a user