|
84 | 84 |
|
85 | 85 | await _outputEditor.SetValue(string.Empty);
|
86 | 86 |
|
87 |
| - var results = SchemaRunner.Run(userCode, _currentLesson!); |
| 87 | + _currentLesson!.UserCode = userCode; |
| 88 | + var results = SchemaRunner.Run(_currentLesson); |
88 | 89 |
|
89 | 90 | await _outputEditor.SetValue(string.Join(Environment.NewLine, results!));
|
90 | 91 | _nextButtonDisabled = !CanMoveToNextLesson();
|
| 92 | + await SaveLessonPlanCompletion(); |
91 | 93 | }
|
92 | 94 | catch (Exception e)
|
93 | 95 | {
|
|
113 | 115 | return LoadLesson();
|
114 | 116 | }
|
115 | 117 |
|
116 |
| - private Task LoadLesson() |
| 118 | + private async Task LoadLesson() |
117 | 119 | {
|
118 | 120 | _currentLesson ??= _lessons[0];
|
119 | 121 | Instructions = SchemaRunner.BuildInstructions(_currentLesson);
|
120 | 122 | _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); |
122 | 126 | }
|
123 | 127 |
|
124 | 128 | private bool CanMoveToNextLesson()
|
|
131 | 135 | protected override async Task OnInitializedAsync()
|
132 | 136 | {
|
133 | 137 | await DownloadLessonPlan();
|
134 |
| - |
135 | 138 | await LoadLesson();
|
136 | 139 |
|
137 | 140 | await base.OnInitializedAsync();
|
|
144 | 147 | var json = yaml.ToJsonNode().FirstOrDefault();
|
145 | 148 | Console.WriteLine(json.AsJsonString());
|
146 | 149 | _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)!; |
147 | 178 | }
|
148 | 179 |
|
149 | 180 | private async Task SelectLesson(Guid lessonId)
|
|
0 commit comments