Notes
This commit is contained in:
parent
4c2b5cca93
commit
43b4d50e43
28 changed files with 674 additions and 249 deletions
|
|
@ -1,6 +1,61 @@
|
|||
using System.Reactive.Linq;
|
||||
using System.Reactive.Subjects;
|
||||
|
||||
using ReactiveUI;
|
||||
|
||||
namespace InkForge.Desktop.Models;
|
||||
|
||||
public class Note
|
||||
public class Note : ReactiveObject
|
||||
{
|
||||
|
||||
private readonly ObservableAsPropertyHelper<Note?> _parent;
|
||||
private readonly BehaviorSubject<int?> _parentId = new(default);
|
||||
private DateTimeOffset _createdTime;
|
||||
private int _id;
|
||||
private string _name = default!;
|
||||
private DateTimeOffset _updatedTime;
|
||||
|
||||
public DateTimeOffset CreatedTime
|
||||
{
|
||||
get => _createdTime;
|
||||
set => this.RaiseAndSetIfChanged(ref _createdTime, value);
|
||||
}
|
||||
|
||||
public int Id
|
||||
{
|
||||
get => _id;
|
||||
set => this.RaiseAndSetIfChanged(ref _id, value);
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get => _name;
|
||||
set => this.RaiseAndSetIfChanged(ref _name, value);
|
||||
}
|
||||
|
||||
public DateTimeOffset UpdatedTime
|
||||
{
|
||||
get => _updatedTime;
|
||||
set => this.RaiseAndSetIfChanged(ref _updatedTime, value);
|
||||
}
|
||||
|
||||
public Note? Parent
|
||||
{
|
||||
get => _parent.Value;
|
||||
set => ParentId = value?.Id;
|
||||
}
|
||||
|
||||
public int? ParentId
|
||||
{
|
||||
get => _parentId.Value;
|
||||
set => _parentId.OnNext(value);
|
||||
}
|
||||
|
||||
public Note(NoteStore noteStore)
|
||||
{
|
||||
_parent = _parentId.Select(id => id switch
|
||||
{
|
||||
{ } => noteStore.Watch((int)id),
|
||||
_ => Observable.Empty<Note?>(),
|
||||
}).Switch(null).ToProperty(this, nameof(Parent), deferSubscription: true);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
88
app/InkForge.Desktop/Models/NoteStore.cs
Normal file
88
app/InkForge.Desktop/Models/NoteStore.cs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
using System.Collections.ObjectModel;
|
||||
using System.Reactive.Linq;
|
||||
|
||||
using DynamicData;
|
||||
|
||||
using InkForge.Data;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace InkForge.Desktop.Models;
|
||||
|
||||
public class NoteStore
|
||||
{
|
||||
private readonly IDbContextFactory<NoteDbContext> _dbContextFactory;
|
||||
private readonly SourceCache<Note, int> _notesCache = new(m => m.Id);
|
||||
|
||||
public ReadOnlyObservableCollection<Note> Notes { get; }
|
||||
|
||||
public NoteStore(IDbContextFactory<NoteDbContext> dbContextFactory)
|
||||
{
|
||||
_dbContextFactory = dbContextFactory;
|
||||
}
|
||||
|
||||
public void AddNote(Note note)
|
||||
{
|
||||
using var dbContext = _dbContextFactory.CreateDbContext();
|
||||
var entity = ToEntity(note);
|
||||
var entry = dbContext.Notes.Add(entity);
|
||||
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public Note CreateNote() => new(this);
|
||||
|
||||
public Note? GetById(int id)
|
||||
{
|
||||
if (((Note?)_notesCache.Lookup(id)) is not Note note)
|
||||
{
|
||||
using var dbContext = _dbContextFactory.CreateDbContext();
|
||||
if (dbContext.Notes.Find(id) is not { } dbNote)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
note = ToNote(dbNote);
|
||||
}
|
||||
|
||||
return note;
|
||||
}
|
||||
|
||||
public IObservable<Note?> Watch(int id)
|
||||
{
|
||||
return _notesCache.WatchValue(id);
|
||||
}
|
||||
|
||||
private NoteEntity ToEntity(Note note)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Id = note.Id,
|
||||
Value = new()
|
||||
{
|
||||
Name = note.Name,
|
||||
Created = note.CreatedTime,
|
||||
Updated = note.UpdatedTime,
|
||||
},
|
||||
Parent = note.Parent switch
|
||||
{
|
||||
{ Id: { } parentId } => new()
|
||||
{
|
||||
Id = parentId
|
||||
},
|
||||
_ => null,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private Note ToNote(NoteEntity entity)
|
||||
{
|
||||
var note = CreateNote();
|
||||
note.Id = entity.Id;
|
||||
note.Name = entity.Value.Name;
|
||||
note.CreatedTime = entity.Value.Created;
|
||||
note.UpdatedTime = entity.Value.Updated;
|
||||
note.ParentId = entity.Parent?.Id;
|
||||
return note;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,14 +1,11 @@
|
|||
using InkForge.Data;
|
||||
using InkForge.Desktop.Data.Options;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace InkForge.Desktop.Models;
|
||||
|
||||
public sealed class Workspace : IDisposable
|
||||
{
|
||||
private readonly IDbContextFactory<NoteDbContext> _dbContextFactory;
|
||||
private bool _disposedValue;
|
||||
private IServiceScope? _scope;
|
||||
|
||||
|
|
@ -21,20 +18,9 @@ public sealed class Workspace : IDisposable
|
|||
public Workspace(IServiceScope scope)
|
||||
{
|
||||
_scope = scope;
|
||||
_dbContextFactory = Services.GetRequiredService<IDbContextFactory<NoteDbContext>>();
|
||||
}
|
||||
|
||||
// public Note AddNote(Note? parent)
|
||||
// {
|
||||
// }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (!_disposedValue)
|
||||
{
|
||||
|
|
@ -47,4 +33,14 @@ public sealed class Workspace : IDisposable
|
|||
_disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
// private async Task LoadNotes()
|
||||
// {
|
||||
// await using var dbContext = await _dbContextFactory.CreateDbContextAsync().ConfigureAwait(false);
|
||||
// await foreach (var asdf in dbContext.Notes.AsAsyncEnumerable().ConfigureAwait(false))
|
||||
// {
|
||||
|
||||
// }
|
||||
// _ = (await dbContext.Notes.ToListAsync().ConfigureAwait(false));
|
||||
// }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue