feat(Documents) Notes/Docs page improvements and warning cleanup
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
using System.Net.Http.Json;
|
||||
using Model.Immortal.Notes;
|
||||
using Model.Notes;
|
||||
|
||||
#if NO_SQL
|
||||
|
||||
@@ -16,50 +16,115 @@ public class NoteService : INoteService {
|
||||
private bool isLoaded;
|
||||
|
||||
|
||||
private event Action _onChange;
|
||||
private event Action OnChange = default!;
|
||||
|
||||
private void NotifyDataChanged() {
|
||||
_onChange?.Invoke();
|
||||
OnChange?.Invoke();
|
||||
}
|
||||
|
||||
public NoteService(HttpClient httpClient) {
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
public List<NoteModel> NoteModels { get; set; }
|
||||
public List<NoteContentModel> NoteContentModels { get; set; } = default!;
|
||||
public List<NoteConnectionModel> NoteConnectionModels { get; set; }
|
||||
public List<NoteSectionModel> NoteSectionModels { get; set; }
|
||||
|
||||
public List<NoteContentModel> NoteContentModelsByPageOrder { get; set; } = new();
|
||||
|
||||
|
||||
public void Subscribe(Action action) {
|
||||
_onChange += action;
|
||||
OnChange += action;
|
||||
}
|
||||
|
||||
public void Unsubscribe(Action action) {
|
||||
_onChange -= action;
|
||||
OnChange -= action;
|
||||
}
|
||||
|
||||
public bool IsLoaded() {
|
||||
return isLoaded;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task Load() {
|
||||
|
||||
if (isLoaded) {
|
||||
return;
|
||||
}
|
||||
|
||||
NoteModels = (await httpClient.GetFromJsonAsync<NoteModel[]>("generated/NoteModels.json") ?? Array.Empty<NoteModel>()).ToList();
|
||||
NoteContentModels = (await httpClient.GetFromJsonAsync<NoteContentModel[]>("generated/NoteContentModels.json") ?? Array.Empty<NoteContentModel>()).ToList();
|
||||
NoteConnectionModels = (await httpClient.GetFromJsonAsync<NoteConnectionModel[]>("generated/NoteConnectionModels.json") ?? Array.Empty<NoteConnectionModel>()).ToList();
|
||||
NoteSectionModels = (await httpClient.GetFromJsonAsync<NoteSectionModel[]>("generated/NoteSectionModels.json") ?? Array.Empty<NoteSectionModel>()).ToList();
|
||||
|
||||
|
||||
isLoaded = true;
|
||||
|
||||
SortSQL();
|
||||
|
||||
NotifyDataChanged();
|
||||
}
|
||||
|
||||
private NoteContentModel? ContentById(int id)
|
||||
{
|
||||
foreach (var data in NoteContentModels!)
|
||||
if (data.Id == id)
|
||||
return data;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void SortSQL()
|
||||
{
|
||||
foreach (var connection in NoteConnectionModels)
|
||||
{
|
||||
ContentById(connection.ParentId)!.NoteContentModels.Add(ContentById(connection.ChildId));
|
||||
ContentById(connection.ChildId)!.Parent = ContentById(connection.ParentId);
|
||||
}
|
||||
|
||||
foreach (var content in NoteContentModels)
|
||||
{
|
||||
if (content.NoteSectionModelId != null)
|
||||
{
|
||||
foreach (var section in NoteSectionModels)
|
||||
{
|
||||
if (section.Id == content.NoteSectionModelId)
|
||||
{
|
||||
section.NoteContentModels.Add(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ByPageOrder();
|
||||
}
|
||||
|
||||
private void ByPageOrder()
|
||||
{
|
||||
NoteContentModelsByPageOrder = new List<NoteContentModel>();
|
||||
|
||||
int order = 1;
|
||||
foreach (var note in NoteContentModels)
|
||||
{
|
||||
if (note.Parent != null) continue;
|
||||
|
||||
note.PageOrder = order++;
|
||||
NoteContentModelsByPageOrder.Add(note);
|
||||
|
||||
void GetAllChildren(NoteContentModel docs)
|
||||
{
|
||||
foreach (var doc in docs.NoteContentModels)
|
||||
{
|
||||
doc.PageOrder = order++;
|
||||
NoteContentModelsByPageOrder.Add(doc);
|
||||
|
||||
if (doc.NoteContentModels.Count > 0) GetAllChildren(doc);
|
||||
}
|
||||
}
|
||||
|
||||
GetAllChildren(note);
|
||||
}
|
||||
|
||||
NoteContentModelsByPageOrder = NoteContentModelsByPageOrder.OrderBy(noteContent => noteContent.PageOrder).ToList();
|
||||
}
|
||||
|
||||
public void Update() {
|
||||
NotifyDataChanged();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user