Skip to content

Commit 617ce28

Browse files
committed
saving progress; better scrollbar
1 parent c84315e commit 617ce28

File tree

7 files changed

+62
-18
lines changed

7 files changed

+62
-18
lines changed

Diff for: LearnJsonEverything/Services/CompilationHelpers.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ public static class CompilationHelpers
6161
return _references;
6262
}
6363

64-
public static (ILessonRunner<T>?, string[]) GetRunner<T>(LessonData lesson, string userCode)
64+
public static (ILessonRunner<T>?, string[]) GetRunner<T>(LessonData lesson)
6565
{
6666
if (_references is null)
6767
return (null, ["Compilation assemblies still loading. Please wait until complete and try again."]);
6868

6969
var fullSource = lesson.ContextCode
70-
.Replace("/* USER CODE */", userCode);
70+
.Replace("/* USER CODE */", lesson.UserCode ?? string.Empty);
7171

7272
Console.WriteLine($"Compiling...\n\n{fullSource}");
7373

Diff for: LearnJsonEverything/Services/LessonData.cs

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ public class LessonData
1313
public string ContextCode { get; set; }
1414
public JsonArray Tests { get; set; }
1515
public bool Achieved { get; set; }
16+
public string? UserCode { get; set; }
1617
}

Diff for: LearnJsonEverything/Services/LessonPlan.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections;
22
using System.Text.Json;
3+
using System.Text.Json.Nodes;
34
using System.Text.Json.Serialization;
45

56
namespace LearnJsonEverything.Services;

Diff for: LearnJsonEverything/Services/Runners/SchemaRunner.cs

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
using System.Diagnostics.CodeAnalysis;
2-
using System.Reflection;
3-
using System.Text.Json;
1+
using System.Text.Json;
42
using System.Text.Json.Nodes;
53
using Json.More;
64
using Json.Schema;
7-
using Microsoft.CodeAnalysis;
8-
using Microsoft.CodeAnalysis.CSharp;
9-
10-
using static LearnJsonEverything.Services.SerializationHelpers;
115

126
namespace LearnJsonEverything.Services.Runners;
137

@@ -55,9 +49,9 @@ .. tests.Select(test => $"|`{test.Instance.AsJsonString()}`|{test.IsValid}|")
5549
return string.Join(Environment.NewLine, lines);
5650
}
5751

58-
public static string[] Run(string userCode, LessonData lesson)
52+
public static string[] Run(LessonData lesson)
5953
{
60-
var (runner, errors) = CompilationHelpers.GetRunner<EvaluationResults>(lesson, userCode);
54+
var (runner, errors) = CompilationHelpers.GetRunner<EvaluationResults>(lesson);
6155

6256
if (runner is null) return errors;
6357

Diff for: LearnJsonEverything/Services/SerializationHelpers.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ public static string ToLiteral(this string valueTextForCompiler)
2020
[JsonSerializable(typeof(JsonObject))]
2121
[JsonSerializable(typeof(JsonArray))]
2222
[JsonSerializable(typeof(LessonPlan))]
23-
[JsonSerializable(typeof(LessonData))]
2423
[JsonSerializable(typeof(LessonData[]))]
25-
[JsonSerializable(typeof(SchemaRunner.SchemaTest))]
24+
[JsonSerializable(typeof(SchemaSaveData[]))]
2625
[JsonSerializable(typeof(SchemaRunner.SchemaTest[]))]
2726
[JsonSourceGenerationOptions(WriteIndented = true, PropertyNameCaseInsensitive = true)]
28-
internal partial class SerializerContext : JsonSerializerContext;
27+
internal partial class SerializerContext : JsonSerializerContext;
28+
29+
public record SchemaSaveData(Guid id, bool completed, string? userCode);

Diff for: LearnJsonEverything/Shared/Teacher.razor

+35-4
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,12 @@
8484

8585
await _outputEditor.SetValue(string.Empty);
8686

87-
var results = SchemaRunner.Run(userCode, _currentLesson!);
87+
_currentLesson!.UserCode = userCode;
88+
var results = SchemaRunner.Run(_currentLesson);
8889

8990
await _outputEditor.SetValue(string.Join(Environment.NewLine, results!));
9091
_nextButtonDisabled = !CanMoveToNextLesson();
92+
await SaveLessonPlanCompletion();
9193
}
9294
catch (Exception e)
9395
{
@@ -113,12 +115,14 @@
113115
return LoadLesson();
114116
}
115117

116-
private Task LoadLesson()
118+
private async Task LoadLesson()
117119
{
118120
_currentLesson ??= _lessons[0];
119121
Instructions = SchemaRunner.BuildInstructions(_currentLesson);
120122
_nextButtonDisabled = !CanMoveToNextLesson();
121-
return _outputEditor.SetValue(string.Empty);
123+
if (_currentLesson.UserCode is not null)
124+
await _codeEditor.SetValue(_currentLesson.UserCode);
125+
await _outputEditor.SetValue(string.Empty);
122126
}
123127

124128
private bool CanMoveToNextLesson()
@@ -131,7 +135,6 @@
131135
protected override async Task OnInitializedAsync()
132136
{
133137
await DownloadLessonPlan();
134-
135138
await LoadLesson();
136139

137140
await base.OnInitializedAsync();
@@ -144,6 +147,34 @@
144147
var json = yaml.ToJsonNode().FirstOrDefault();
145148
Console.WriteLine(json.AsJsonString());
146149
_lessons = json.Deserialize(SerializerContext.Default.LessonPlan)!;
150+
151+
var completed = await LoadLessonPlanCompletion();
152+
foreach (var saveData in completed)
153+
{
154+
var lesson = _lessons[saveData.id];
155+
lesson.Achieved = saveData.completed;
156+
lesson.UserCode = saveData.userCode;
157+
}
158+
159+
_currentLesson = _lessons.SkipWhile(x => x.Achieved).FirstOrDefault() ?? _lessons.Last();
160+
}
161+
162+
private async Task SaveLessonPlanCompletion()
163+
{
164+
var completionData = _lessons.Where(x => x.UserCode is not null)
165+
.Select(x => new SchemaSaveData(x.Id, x.Achieved, x.UserCode))
166+
.ToArray();
167+
var json = JsonSerializer.Serialize(completionData, SerializerContext.Default.SchemaSaveDataArray);
168+
169+
await DataManager.Set(LessonSource, json);
170+
}
171+
172+
private async Task<SchemaSaveData[]> LoadLessonPlanCompletion()
173+
{
174+
var json = await DataManager.Get(LessonSource);
175+
if (json is null) return [];
176+
177+
return JsonSerializer.Deserialize(json, SerializerContext.Default.SchemaSaveDataArray)!;
147178
}
148179

149180
private async Task SelectLesson(Guid lessonId)

Diff for: LearnJsonEverything/Shared/Teacher.razor.css

+16
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,20 @@
4747

4848
ol, ul {
4949
padding-left: .5rem;
50+
}
51+
52+
div::-webkit-scrollbar {
53+
width: 5px !important;
54+
height: 5px !important;
55+
}
56+
57+
div::-webkit-scrollbar-track {
58+
-webkit-box-shadow: none !important;
59+
background-color: transparent;
60+
}
61+
62+
div::-webkit-scrollbar-thumb {
63+
background-color: #CCBCD9 !important;
64+
border: 1px solid #1a1a1a !important;
65+
border-radius: 2.5px !important;
5066
}

0 commit comments

Comments
 (0)