Files
OpenNest/OpenNest/Document.cs
T
ajandClaude Opus 5.5 e82f26b976 feat(ui): rename nests from Nest Info in Database mode
The nest name box was always read-only, so a nest could only be renamed
by Save As, which leaves the old copy behind. In Database mode Nest Info
now edits the name (uploaded with the next Save), as does the saved-nest
browser's Nest Information (saved immediately). File mode keeps it
read-only because the file name is the nest name. Renaming to a name
another active nest already has asks first, and declining reopens the
dialog with the edits kept; a duplicate accepted there is not asked about
again by the nest's first Save.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 11:09:08 -04:00

79 lines
2.9 KiB
C#

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using OpenNest.Data;
using OpenNest.Engine.BestFit;
using OpenNest.IO;
namespace OpenNest
{
public class Document
{
public Nest Nest { get; set; }
public DateTime LastSaveDate { get; private set; }
public string LastSavePath { get; private set; }
private readonly NestSaveSession remoteSession = new();
public Guid RemoteId => remoteSession.RemoteId;
public Guid PendingCreateId => remoteSession.PendingCreateId;
public bool PendingCreateRequiresReconciliation => remoteSession.PendingCreateId != Guid.Empty && remoteSession.RequiresReload;
public bool PendingCreateWasCopy => remoteSession.PendingCreateWasCopy;
public bool IsFirstDatabaseSave(string serverUrl) => remoteSession.IsFirstSave(serverUrl);
/// <summary>
/// A duplicate name the user already accepted in Nest Info, so the first Save
/// does not ask about it again.
/// </summary>
public string AcceptedDuplicateName { get; set; }
public void BindRemote(Guid id, string serverUrl) => remoteSession.Bind(id, serverUrl);
public void BindRemote(Guid id, string serverUrl, int revision) => remoteSession.Bind(id, serverUrl, revision);
public Task<NestRecord> ReconcilePendingCreateAsync(INestRepository repository, string serverUrl,
CancellationToken cancellationToken = default) =>
remoteSession.ReconcilePendingCreateAsync(repository, serverUrl, cancellationToken);
public int RemoteRevision => remoteSession.Revision;
public string Name => Nest?.Name;
public bool HasSavePath => File.Exists(LastSavePath);
public void SaveAs(string path)
{
var name = Path.GetFileNameWithoutExtension(path);
Nest.Name = name;
LastSaveDate = DateTime.Now;
LastSavePath = path;
var writer = new NestWriter(Nest) { BestFitStore = BestFitCacheStore.Shared };
writer.Write(path);
}
/// <summary>Serializes the current nest to a .nest archive in memory.</summary>
public byte[] Serialize()
{
using var stream = new MemoryStream();
new NestWriter(Nest) { BestFitStore = BestFitCacheStore.Shared }.Write(stream);
return stream.ToArray();
}
/// <summary>Uploads this nest, preserving its server identity only on success.</summary>
public async Task<NestRecord> SaveToDatabaseAsync(
INestRepository repository, string serverUrl, bool saveCopy = false,
CancellationToken cancellationToken = default)
{
var bytes = Serialize();
var saved = await remoteSession.SaveAsync(
repository, serverUrl, Nest, bytes, saveCopy, cancellationToken);
LastSaveDate = DateTime.Now;
return saved;
}
}
}