Skip to content

Commit

Permalink
Refactor last state feature by review
Browse files Browse the repository at this point in the history
  • Loading branch information
wh0o7 committed Nov 3, 2023
1 parent 32a4c9b commit de694af
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions TeachersTimetable/Services/ParseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public async Task UpdateTimetableTick()
await this._botService.SendAdminMessageAsync(new SendMessageArgs(0, "End parse day"));
}

if (parseWeek || parseDay) await SaveState(StatePath);
if (parseWeek || parseDay) SaveState(StatePath);
Console.WriteLine("End update tick");
}
catch (Exception e)
Expand All @@ -409,7 +409,7 @@ public async Task UpdateTimetableTick()
}
}

private Task SaveState(string filePath)
private void SaveState(string filePath)
{
var stateToSave = new
{
Expand All @@ -422,19 +422,16 @@ private Task SaveState(string filePath)

string json = JsonConvert.SerializeObject(stateToSave);
File.WriteAllText(filePath, json);
return Task.CompletedTask;
}

private void LoadState(string filePath)
{
if (File.Exists(filePath))
{
var state = JsonConvert.DeserializeObject<dynamic>(File.ReadAllText(filePath));
_weekInterval = state!.WeekInterval.ToObject<DateTime?[]>();
_thHeaders = state.ThHeaders.ToObject<List<string>>();
LastDayHtmlContent = state.LastDayHtmlContent;
LastWeekHtmlContent = state.LastWeekHtmlContent;
Timetable = state.Timetable.ToObject<List<Timetable>>();
}
if (!File.Exists(filePath)) return;
var state = JsonConvert.DeserializeObject<dynamic>(File.ReadAllText(filePath));
_weekInterval = state!.WeekInterval.ToObject<DateTime?[]>();
_thHeaders = state.ThHeaders.ToObject<List<string>>();
LastDayHtmlContent = state.LastDayHtmlContent;
LastWeekHtmlContent = state.LastWeekHtmlContent;
Timetable = state.Timetable.ToObject<List<Timetable>>();
}
}

0 comments on commit de694af

Please sign in to comment.