fix(app): store and close the editor before its sequence moves to the trash

Delete first lets the editor of a sequence it moves store its last edit and
close (or stops when the user keeps an edit that cannot be stored), and reports
a sidecar written by a newer Titano instead of failing. A remote card whose
proxy download ended before it was recorded opens at once, as on the Server
screen; the Library reads its saved views again on refresh; the CSV writer of
the Library and the Archive manager moves into LibraryCsv.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-03 16:44:03 +00:00
1 parent fb0d8cceae
commit 743418d944
4 files changed
+70 -41

No files matched your search

+16 -3
View File
@@ -1,6 +1,7 @@
using System.IO; using System.IO;
using Titano.App.Infrastructure; using Titano.App.Infrastructure;
using Titano.App.Shell; using Titano.App.Shell;
using Titano.Core.Archiving;
using Titano.Core.Diagnostics; using Titano.Core.Diagnostics;
using Titano.Core.Localization; using Titano.Core.Localization;
using Titano.Data; using Titano.Data;
@@ -27,7 +28,7 @@ public static class SequenceDeletion
shell.Notify(Loc.L("Archive.Delete.Nothing"), string.Join(Environment.NewLine, DeletePlans.Blocked(plan, DeletePlans.ListedLines))); shell.Notify(Loc.L("Archive.Delete.Nothing"), string.Join(Environment.NewLine, DeletePlans.Blocked(plan, DeletePlans.ListedLines)));
return false; return false;
} }
if (!shell.Services.Dialogs.Confirm(request)) return false; if (!shell.Services.Dialogs.Confirm(request) || !await CloseEditorAsync(shell, plan)) return false;
var entries = await Task.Run(() => archives.DeleteAsync(plan, Environment.UserName)); var entries = await Task.Run(() => archives.DeleteAsync(plan, Environment.UserName));
Log.Info("archive", $"{entries.Count} sequence(s) moved to the trash, {plan.Steps.Count - entries.Count} removed from the index only"); Log.Info("archive", $"{entries.Count} sequence(s) moved to the trash, {plan.Steps.Count - entries.Count} removed from the index only");
await RefreshAsync(shell); await RefreshAsync(shell);
@@ -36,8 +37,8 @@ public static class SequenceDeletion
entries.Count == 0 ? null : () => _ = RestoreAsync(shell, entries)); entries.Count == 0 ? null : () => _ = RestoreAsync(shell, entries));
return true; return true;
} }
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or StaleDeletePlanException or ArgumentException catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or StaleDeletePlanException or NewerFormatException
or DatabaseException) or ArgumentException or DatabaseException)
{ {
Log.Warning("archive", "Delete failed", ex); Log.Warning("archive", "Delete failed", ex);
shell.Notify(Loc.L("Archive.Delete.Failed"), ex.Message); shell.Notify(Loc.L("Archive.Delete.Failed"), ex.Message);
@@ -69,6 +70,18 @@ public static class SequenceDeletion
await RefreshAsync(shell); await RefreshAsync(shell);
} }
/// <summary>
/// The editor of a sequence about to move stores its last edit (so the trash keeps it) and closes; false when that
/// edit could not be stored and the user kept it.
/// </summary>
private static async Task<bool> CloseEditorAsync(ShellViewModel shell, DeletePlan plan)
{
if (shell.Sequence.Editor is null || shell.CurrentSequenceId is not { } open || plan.Steps.All(s => s.Sequence.Id != open)) return true;
if (!await shell.Sequence.ConfirmCloseAsync()) return false;
await shell.Sequence.CloseAsync();
return true;
}
private static async Task RefreshAsync(ShellViewModel shell) private static async Task RefreshAsync(ShellViewModel shell)
{ {
await shell.Library.RefreshAsync(); await shell.Library.RefreshAsync();
+38
View File
@@ -0,0 +1,38 @@
using System.Globalization;
using System.Text;
using Titano.Core.Model;
namespace Titano.App.Library;
/// <summary>The Library's "Export list as CSV" (BRIEF §12.2): RFC 4180, UTF-8, invariant numbers and dates.</summary>
public static class LibraryCsv
{
/// <summary>One row per sequence; the header is English because the file is data, not UI.</summary>
public static string Write(IEnumerable<Sequence> sequences)
{
var inv = CultureInfo.InvariantCulture;
var sb = new StringBuilder("title,first_capture,last_capture,camera_make,camera_model,frames,bytes,folder,tags,origin\r\n");
foreach (var s in sequences)
{
sb.Append(Field(s.DisplayTitle)).Append(',')
.Append(s.FirstCapture?.ToString("yyyy-MM-dd HH:mm:ss", inv)).Append(',')
.Append(s.LastCapture?.ToString("yyyy-MM-dd HH:mm:ss", inv)).Append(',')
.Append(Field(s.CameraMake)).Append(',')
.Append(Field(s.CameraModel)).Append(',')
.Append(s.FrameCount.ToString(inv)).Append(',')
.Append(s.TotalBytes.ToString(inv)).Append(',')
.Append(Field(s.Folder)).Append(',')
.Append(Field(string.Join(';', s.Tags))).Append(',')
.Append(s is { Origin: SequenceOrigin.Server, SyncState: SyncState.RemoteOnly } ? "server" : "local").Append("\r\n");
}
return sb.ToString();
}
private static string Field(string? value)
{
if (string.IsNullOrEmpty(value)) return string.Empty;
// Formula-looking values are prefixed so spreadsheets do not execute them.
if (value[0] is '=' or '+' or '-' or '@') value = "'" + value;
return value.IndexOfAny([',', '"', '\r', '\n']) >= 0 ? "\"" + value.Replace("\"", "\"\"", StringComparison.Ordinal) + "\"" : value;
}
}
+1 -1
View File
@@ -742,7 +742,7 @@ public sealed class ArchiveViewModel : SectionViewModel
var folder = Services.Dialogs.PickFolder(Loc.L("Library.Export.Title")); var folder = Services.Dialogs.PickFolder(Loc.L("Library.Export.Title"));
if (folder is null || set.Count == 0) return; if (folder is null || set.Count == 0) return;
var path = Path.Combine(folder, $"Titano sequences {DateTime.Now:yyyy-MM-dd HHmm}.csv"); var path = Path.Combine(folder, $"Titano sequences {DateTime.Now:yyyy-MM-dd HHmm}.csv");
var csv = LibraryViewModel.Csv(set); var csv = LibraryCsv.Write(set);
await Task.Run(() => AtomicFile.WriteAllText(path, csv)); await Task.Run(() => AtomicFile.WriteAllText(path, csv));
Shell.Notify(Loc.L("Library.Export.Done"), Path.GetFileName(path), Loc.L("Library.Export.Show"), Loc.L("Tooltips.Library.ShowExport"), Shell.Notify(Loc.L("Library.Export.Done"), Path.GetFileName(path), Loc.L("Library.Export.Show"), Loc.L("Tooltips.Library.ShowExport"),
() => ShellActions.Reveal(path)); () => ShellActions.Reveal(path));
+15 -37
View File
@@ -2,7 +2,6 @@ using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Text;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Threading; using System.Windows.Threading;
using Titano.App.Archiving; using Titano.App.Archiving;
@@ -498,9 +497,10 @@ public sealed class LibraryViewModel : SectionViewModel
await ReloadAsync(); await ReloadAsync();
} }
/// <summary>Re-reads filter choices, totals and the server's cards, then the list (after imports, archive changes).</summary> /// <summary>Re-reads saved views, filter choices, totals and the server's cards, then the list (after imports, archive changes).</summary>
public async Task RefreshAsync() public async Task RefreshAsync()
{ {
await LoadViewsAsync();
await RefreshFiltersAsync(); await RefreshFiltersAsync();
await RefreshRemoteAsync(reload: false); await RefreshRemoteAsync(reload: false);
await ReloadAsync(); await ReloadAsync();
@@ -907,13 +907,17 @@ public sealed class LibraryViewModel : SectionViewModel
} }
foreach (var item in Items.Where(i => job.TargetIds.Contains(i.Id))) item.IsAnalysing = _analysing.Contains(item.Id); foreach (var item in Items.Where(i => job.TargetIds.Contains(i.Id))) item.IsAnalysing = _analysing.Contains(item.Id);
} }
// A remote card's "Open" waited for this proxy download; it opens if the Library is still in front. OpenWhenCached(job);
var ended = job.State is JobState.Done or JobState.Failed or JobState.Cancelled;
if (ended && _openWhenCached.Remove(job.Id, out var remoteId) && job.State == JobState.Done && ReferenceEquals(Shell.Current, this))
Shell.OpenSequence(remoteId);
if (job.State == JobState.Done) _ = RefreshTargetsAsync(job); if (job.State == JobState.Done) _ = RefreshTargetsAsync(job);
} }
/// <summary>A remote card's "Open" waited for this proxy download; it opens if the Library is still in front.</summary>
private void OpenWhenCached(Job job)
{
if (job.State is not (JobState.Done or JobState.Failed or JobState.Cancelled) || !_openWhenCached.Remove(job.Id, out var remoteId)) return;
if (job.State == JobState.Done && ReferenceEquals(Shell.Current, this)) Shell.OpenSequence(remoteId);
}
/// <summary>A finished job changed some sequences: refresh those rows in place and the totals.</summary> /// <summary>A finished job changed some sequences: refresh those rows in place and the totals.</summary>
private async Task RefreshTargetsAsync(Job job) private async Task RefreshTargetsAsync(Job job)
{ {
@@ -943,7 +947,10 @@ public sealed class LibraryViewModel : SectionViewModel
Shell.OpenSequence(item.Id); Shell.OpenSequence(item.Id);
return; return;
} }
if (await _actions.OpenRemoteAsync(item.Sequence) is { } job) _openWhenCached[job] = item.Id; if (await _actions.OpenRemoteAsync(item.Sequence) is not { } job) return;
_openWhenCached[job] = item.Id;
// A download that finished before it was recorded here opens at once.
if (_db is not null && await _db.Jobs.GetAsync(job) is { State: JobState.Done or JobState.Failed or JobState.Cancelled } ended) OpenWhenCached(ended);
} }
/// <summary>The card's Sync: the transfer or settings exchange its sync row offers; a conflict is settled on the Server screen.</summary> /// <summary>The card's Sync: the transfer or settings exchange its sync row offers; a conflict is settled on the Server screen.</summary>
@@ -1064,40 +1071,11 @@ public sealed class LibraryViewModel : SectionViewModel
var folder = Shell.Services.Dialogs.PickFolder(Loc.L("Library.Export.Title")); var folder = Shell.Services.Dialogs.PickFolder(Loc.L("Library.Export.Title"));
if (folder is null) return; if (folder is null) return;
var path = Path.Combine(folder, $"Titano sequences {DateTime.Now:yyyy-MM-dd HHmm}.csv"); var path = Path.Combine(folder, $"Titano sequences {DateTime.Now:yyyy-MM-dd HHmm}.csv");
var csv = Csv(chosen); var csv = LibraryCsv.Write(chosen);
await Task.Run(() => AtomicFile.WriteAllText(path, csv)); await Task.Run(() => AtomicFile.WriteAllText(path, csv));
Shell.Notify(Loc.L("Library.Export.Done"), Path.GetFileName(path), Loc.L("Library.Export.Show"), Loc.L("Tooltips.Library.ShowExport"), () => ShellActions.Reveal(path)); Shell.Notify(Loc.L("Library.Export.Done"), Path.GetFileName(path), Loc.L("Library.Export.Show"), Loc.L("Tooltips.Library.ShowExport"), () => ShellActions.Reveal(path));
} }
/// <summary>RFC 4180 CSV of sequences (header in English: it is a data file).</summary>
public static string Csv(IEnumerable<Sequence> sequences)
{
var inv = CultureInfo.InvariantCulture;
var sb = new StringBuilder("title,first_capture,last_capture,camera_make,camera_model,frames,bytes,folder,tags,origin\r\n");
foreach (var s in sequences)
{
sb.Append(Field(s.DisplayTitle)).Append(',')
.Append(s.FirstCapture?.ToString("yyyy-MM-dd HH:mm:ss", inv)).Append(',')
.Append(s.LastCapture?.ToString("yyyy-MM-dd HH:mm:ss", inv)).Append(',')
.Append(Field(s.CameraMake)).Append(',')
.Append(Field(s.CameraModel)).Append(',')
.Append(s.FrameCount.ToString(inv)).Append(',')
.Append(s.TotalBytes.ToString(inv)).Append(',')
.Append(Field(s.Folder)).Append(',')
.Append(Field(string.Join(';', s.Tags))).Append(',')
.Append(s is { Origin: SequenceOrigin.Server, SyncState: SyncState.RemoteOnly } ? "server" : "local").Append("\r\n");
}
return sb.ToString();
static string Field(string? value)
{
if (string.IsNullOrEmpty(value)) return string.Empty;
// Formula-looking values are prefixed so spreadsheets do not execute them.
if (value[0] is '=' or '+' or '-' or '@') value = "'" + value;
return value.IndexOfAny([',', '"', '\r', '\n']) >= 0 ? "\"" + value.Replace("\"", "\"\"", StringComparison.Ordinal) + "\"" : value;
}
}
/// <summary>Option labels and row texts follow a language switch without reloading.</summary> /// <summary>Option labels and row texts follow a language switch without reloading.</summary>
private void OnCultureChanged(object? sender, EventArgs e) private void OnCultureChanged(object? sender, EventArgs e)
{ {