feat(app): library, jobs, settings, server, welcome and about screens
Local-mode composition root (SQLite index, one JobEngine fed by a single handler registration list), keyset-paged library with thumbnail cache and hover scrub, live job list with throttled updates and the shell job bar, settings with live template validation and encoder self-test, server connection with trust-on-first-use and DPAPI token, first-run welcome, About with update check, and --capture screenshots with synthetic data. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
94 files changed
+10130
-374
No files matched your search
@@ -0,0 +1,630 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Threading;
|
||||
using Titano.App.Infrastructure;
|
||||
using Titano.App.Jobs;
|
||||
using Titano.App.Library;
|
||||
using Titano.App.Shell;
|
||||
using Titano.Core.Diagnostics;
|
||||
using Titano.Core.IO;
|
||||
using Titano.Core.Localization;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Data.Repositories;
|
||||
using Titano.Ui.Mvvm;
|
||||
|
||||
namespace Titano.App.Sections;
|
||||
|
||||
/// <summary>
|
||||
/// Library (home, BRIEF §12.2): sequences of the index in a virtualised card grid or table, filtered by search,
|
||||
/// year, camera, status and tag, loaded page by page (keyset paging) as the view scrolls, with a selection panel.
|
||||
/// </summary>
|
||||
public sealed class LibraryViewModel : SectionViewModel
|
||||
{
|
||||
private const int AnalysingPollMs = 250;
|
||||
|
||||
private readonly DispatcherTimer _searchDelay;
|
||||
private readonly HashSet<Guid> _analysing = [];
|
||||
private LibraryPager? _pager;
|
||||
private TitanoDatabase? _db;
|
||||
private string _searchText = string.Empty;
|
||||
private FilterOption<int?> _year;
|
||||
private FilterOption<string?> _camera;
|
||||
private FilterOption<StatusFilter> _status;
|
||||
private FilterOption<string?> _tag;
|
||||
private FilterOption<SequenceSort> _sort;
|
||||
private IReadOnlyList<FilterOption<int?>> _years;
|
||||
private IReadOnlyList<FilterOption<string?>> _cameras;
|
||||
private IReadOnlyList<FilterOption<string?>> _tags;
|
||||
private IReadOnlyList<FilterOption<StatusFilter>> _statuses;
|
||||
private IReadOnlyList<FilterOption<SequenceSort>> _sorts;
|
||||
private bool _isLoading;
|
||||
private bool _hasLoaded;
|
||||
private bool _suppressReload;
|
||||
private int _layoutIndex;
|
||||
private SequenceItem? _selected;
|
||||
private int _selectionCount;
|
||||
private bool _hasArchive;
|
||||
|
||||
public LibraryViewModel(ShellViewModel shell)
|
||||
: base(SectionKey.Library, "Icon.Library")
|
||||
{
|
||||
Shell = shell;
|
||||
_years = [AnyYear()];
|
||||
_cameras = [AnyCamera()];
|
||||
_tags = [AnyTag()];
|
||||
_year = _years[0];
|
||||
_camera = _cameras[0];
|
||||
_tag = _tags[0];
|
||||
_statuses = BuildStatusOptions();
|
||||
_status = _statuses[0];
|
||||
_sorts = BuildSortOptions();
|
||||
_sort = _sorts.FirstOrDefault(s => s.Value == shell.Services.Settings.LibrarySort) ?? _sorts[0];
|
||||
Loc.CultureChanged += OnCultureChanged;
|
||||
_layoutIndex = shell.Services.Settings.LibraryLayout == LibraryLayout.List ? 1 : 0;
|
||||
_searchDelay = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(250) };
|
||||
_searchDelay.Tick += (_, _) =>
|
||||
{
|
||||
_searchDelay.Stop();
|
||||
Reload();
|
||||
};
|
||||
|
||||
LoadMoreCommand = new RelayCommand(() => _ = LoadMoreAsync());
|
||||
ClearFiltersCommand = new RelayCommand(ClearFilters);
|
||||
RescanCommand = new AsyncRelayCommand(RescanAsync, () => _db is not null && _hasArchive);
|
||||
OpenCommand = new RelayCommand(() =>
|
||||
{
|
||||
if (Selected is { } s) Shell.OpenSequence(s.Id);
|
||||
}, () => Selected is not null);
|
||||
RenderCommand = OpenCommand;
|
||||
AnalyseCommand = new AsyncRelayCommand(() => EnqueueAsync(JobKind.DeepAnalyse), () => SelectionCount > 0 && _db is not null);
|
||||
ProxiesCommand = new AsyncRelayCommand(() => EnqueueAsync(JobKind.Proxies), () => SelectionCount > 0 && _db is not null);
|
||||
VerifyCommand = new AsyncRelayCommand(() => EnqueueAsync(JobKind.Verify), () => SelectionCount > 0 && _db is not null);
|
||||
RevealCommand = new AsyncRelayCommand(RevealAsync, () => Selected is not null && _db is not null);
|
||||
ExportListCommand = new AsyncRelayCommand(ExportListAsync, () => SelectionCount > 0);
|
||||
ClearSelectionCommand = new RelayCommand(ClearSelection, () => SelectionCount > 0);
|
||||
SortByCommand = new RelayCommand(p =>
|
||||
{
|
||||
if (p is "Date") SelectedSort = Sorts.First(o => o.Value == (_sort.Value == SequenceSort.Newest ? SequenceSort.Oldest : SequenceSort.Newest));
|
||||
else if (p is SequenceSort sort) SelectedSort = Sorts.First(o => o.Value == sort);
|
||||
else if (p is string name && Enum.TryParse<SequenceSort>(name, out var parsed)) SelectedSort = Sorts.First(o => o.Value == parsed);
|
||||
});
|
||||
OpenItemCommand = new RelayCommand(p =>
|
||||
{
|
||||
if (p is SequenceItem item) Shell.OpenSequence(item.Id);
|
||||
});
|
||||
AnalyseItemCommand = new AsyncRelayCommand(async (p, _) =>
|
||||
{
|
||||
if (p is SequenceItem item && Shell.Services.Jobs is { } jobs)
|
||||
{
|
||||
await jobs.EnqueueAsync(JobKind.DeepAnalyse, [item.Id], item.Title);
|
||||
Shell.Notify(Loc.L("Library.Queued", JobText.Kind(JobKind.DeepAnalyse), item.Title), actionText: Loc.L("Shell.Nav.Jobs"),
|
||||
actionToolTip: Loc.L("Tooltips.Nav.Jobs"), action: () => Shell.Navigate(SectionKey.Jobs));
|
||||
}
|
||||
});
|
||||
ImportCardCommand = new RelayCommand(() => Shell.Navigate(SectionKey.Import));
|
||||
AdoptFolderCommand = new AsyncRelayCommand(AdoptFolderAsync);
|
||||
ConnectCommand = new RelayCommand(() => Shell.Navigate(SectionKey.Server));
|
||||
}
|
||||
|
||||
public ShellViewModel Shell { get; }
|
||||
|
||||
public ObservableCollection<SequenceItem> Items { get; } = [];
|
||||
|
||||
public ICommand LoadMoreCommand { get; }
|
||||
public ICommand ClearFiltersCommand { get; }
|
||||
public ICommand RescanCommand { get; }
|
||||
public ICommand OpenCommand { get; }
|
||||
public ICommand RenderCommand { get; }
|
||||
public ICommand AnalyseCommand { get; }
|
||||
public ICommand ProxiesCommand { get; }
|
||||
public ICommand VerifyCommand { get; }
|
||||
public ICommand RevealCommand { get; }
|
||||
public ICommand ExportListCommand { get; }
|
||||
public ICommand ClearSelectionCommand { get; }
|
||||
public ICommand SortByCommand { get; }
|
||||
public ICommand OpenItemCommand { get; }
|
||||
public ICommand AnalyseItemCommand { get; }
|
||||
public ICommand ImportCardCommand { get; }
|
||||
public ICommand AdoptFolderCommand { get; }
|
||||
public ICommand ConnectCommand { get; }
|
||||
|
||||
// ---------------- Layout ----------------
|
||||
|
||||
/// <summary>0 = grid, 1 = list (the SegmentedControl's index).</summary>
|
||||
public int LayoutIndex
|
||||
{
|
||||
get => _layoutIndex;
|
||||
set
|
||||
{
|
||||
if (value is < 0 or > 1 || !Set(ref _layoutIndex, value)) return;
|
||||
Shell.Services.Settings.LibraryLayout = value == 1 ? LibraryLayout.List : LibraryLayout.Grid;
|
||||
OnPropertyChanged(nameof(IsGrid));
|
||||
OnPropertyChanged(nameof(IsList));
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsGrid => _layoutIndex == 0 && HasItems;
|
||||
public bool IsList => _layoutIndex == 1 && HasItems;
|
||||
|
||||
// ---------------- Filters ----------------
|
||||
|
||||
public string SearchText
|
||||
{
|
||||
get => _searchText;
|
||||
set
|
||||
{
|
||||
if (!Set(ref _searchText, value ?? string.Empty)) return;
|
||||
_searchDelay.Stop();
|
||||
_searchDelay.Start();
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyList<FilterOption<int?>> Years { get => _years; private set => Set(ref _years, value); }
|
||||
public IReadOnlyList<FilterOption<string?>> Cameras { get => _cameras; private set => Set(ref _cameras, value); }
|
||||
public IReadOnlyList<FilterOption<StatusFilter>> Statuses { get => _statuses; private set => Set(ref _statuses, value); }
|
||||
public IReadOnlyList<FilterOption<string?>> Tags { get => _tags; private set => Set(ref _tags, value); }
|
||||
public IReadOnlyList<FilterOption<SequenceSort>> Sorts { get => _sorts; private set => Set(ref _sorts, value); }
|
||||
|
||||
public FilterOption<int?> SelectedYear
|
||||
{
|
||||
get => _year;
|
||||
set => SetFilter(ref _year, value ?? Years[0], nameof(IsYearFiltered));
|
||||
}
|
||||
|
||||
public FilterOption<string?> SelectedCamera
|
||||
{
|
||||
get => _camera;
|
||||
set => SetFilter(ref _camera, value ?? Cameras[0], nameof(IsCameraFiltered));
|
||||
}
|
||||
|
||||
public FilterOption<StatusFilter> SelectedStatus
|
||||
{
|
||||
get => _status;
|
||||
set => SetFilter(ref _status, value ?? Statuses[0], nameof(IsStatusFiltered));
|
||||
}
|
||||
|
||||
public FilterOption<string?> SelectedTag
|
||||
{
|
||||
get => _tag;
|
||||
set => SetFilter(ref _tag, value ?? Tags[0], nameof(IsTagFiltered));
|
||||
}
|
||||
|
||||
public FilterOption<SequenceSort> SelectedSort
|
||||
{
|
||||
get => _sort;
|
||||
set
|
||||
{
|
||||
if (value is null || !Set(ref _sort, value)) return;
|
||||
Shell.Services.Settings.LibrarySort = value.Value;
|
||||
OnPropertyChanged(nameof(SortColumn));
|
||||
Reload();
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsYearFiltered => !_year.IsAny;
|
||||
public bool IsCameraFiltered => !_camera.IsAny;
|
||||
public bool IsStatusFiltered => !_status.IsAny;
|
||||
public bool IsTagFiltered => !_tag.IsAny;
|
||||
|
||||
/// <summary>The sort as a column name for the list header ("Newest", "Title", …).</summary>
|
||||
public string SortColumn => _sort.Value.ToString();
|
||||
|
||||
/// <summary>The filter the toolbar describes.</summary>
|
||||
public LibraryFilter Filter => new()
|
||||
{
|
||||
Text = _searchText,
|
||||
Year = _year.Value,
|
||||
Camera = _camera.Value,
|
||||
Status = _status.Value,
|
||||
Tag = _tag.Value,
|
||||
Sort = _sort.Value,
|
||||
};
|
||||
|
||||
// ---------------- State ----------------
|
||||
|
||||
public bool IsLoading
|
||||
{
|
||||
get => _isLoading;
|
||||
private set => Set(ref _isLoading, value);
|
||||
}
|
||||
|
||||
public bool HasItems => Items.Count > 0;
|
||||
|
||||
/// <summary>Loaded, nothing indexed and no filter: the first-run empty state (one sentence, three choices).</summary>
|
||||
public bool IsLibraryEmpty => _hasLoaded && !HasItems && !_isLoading && Filter.IsUnfiltered;
|
||||
|
||||
/// <summary>Loaded, but the filters match nothing.</summary>
|
||||
public bool IsNoMatch => _hasLoaded && !HasItems && !_isLoading && !Filter.IsUnfiltered;
|
||||
|
||||
// ---------------- Selection ----------------
|
||||
|
||||
/// <summary>The selected sequence when exactly one is selected.</summary>
|
||||
public SequenceItem? Selected
|
||||
{
|
||||
get => _selected;
|
||||
private set => Set(ref _selected, value);
|
||||
}
|
||||
|
||||
public int SelectionCount
|
||||
{
|
||||
get => _selectionCount;
|
||||
private set
|
||||
{
|
||||
if (!Set(ref _selectionCount, value)) return;
|
||||
OnPropertyChanged(nameof(HasSelection));
|
||||
OnPropertyChanged(nameof(IsSingleSelection));
|
||||
OnPropertyChanged(nameof(IsMultiSelection));
|
||||
OnPropertyChanged(nameof(SelectionTitle));
|
||||
OnPropertyChanged(nameof(SelectionSummary));
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasSelection => _selectionCount > 0;
|
||||
public bool IsSingleSelection => _selectionCount == 1;
|
||||
public bool IsMultiSelection => _selectionCount > 1;
|
||||
|
||||
/// <summary>"3 selected".</summary>
|
||||
public string SelectionTitle => Loc.Plural("Library.Selection.Count", _selectionCount);
|
||||
|
||||
/// <summary>"10,240 photos · 182 GB" of the selection.</summary>
|
||||
public string SelectionSummary
|
||||
{
|
||||
get
|
||||
{
|
||||
var chosen = SelectedItems().ToList();
|
||||
return Loc.Plural("Shell.Status.Photos", chosen.Sum(i => (long)i.Sequence.FrameCount)) + " · " + Formatting.Bytes(chosen.Sum(i => i.Sequence.TotalBytes));
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<SequenceItem> SelectedItems() => Items.Where(i => i.IsSelected);
|
||||
|
||||
// ---------------- Lifecycle ----------------
|
||||
|
||||
/// <summary>Binds to the index and loads filters, totals and the first page.</summary>
|
||||
public async Task AttachAsync()
|
||||
{
|
||||
_db = Shell.Services.Database;
|
||||
if (_db is null)
|
||||
{
|
||||
_hasLoaded = true;
|
||||
RaiseState();
|
||||
return;
|
||||
}
|
||||
_pager = new LibraryPager((q, ct) => _db.Sequences.ListAsync(q, ct));
|
||||
if (Shell.Services.Jobs is { } jobs)
|
||||
{
|
||||
jobs.JobChanged += OnJobChanged;
|
||||
}
|
||||
await RefreshFiltersAsync();
|
||||
await ReloadAsync();
|
||||
}
|
||||
|
||||
/// <summary>Re-reads filter choices and totals, then the list (after imports, archive changes).</summary>
|
||||
public async Task RefreshAsync()
|
||||
{
|
||||
await RefreshFiltersAsync();
|
||||
await ReloadAsync();
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
Loc.CultureChanged -= OnCultureChanged;
|
||||
_searchDelay.Stop();
|
||||
_pager?.Cancel();
|
||||
if (Shell.Services.Jobs is { } jobs) jobs.JobChanged -= OnJobChanged;
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>The view calls this when the list scrolls near its end.</summary>
|
||||
public Task LoadMoreAsync() => ApplyAsync(_pager?.LoadMoreAsync());
|
||||
|
||||
/// <summary>The view calls this when container selection changed.</summary>
|
||||
public void OnSelectionChanged()
|
||||
{
|
||||
var count = 0;
|
||||
SequenceItem? single = null;
|
||||
foreach (var item in Items)
|
||||
{
|
||||
if (!item.IsSelected) continue;
|
||||
count++;
|
||||
single = count == 1 ? item : null;
|
||||
}
|
||||
Selected = count == 1 ? single : null;
|
||||
SelectionCount = count;
|
||||
OnPropertyChanged(nameof(SelectionSummary));
|
||||
CommandManager.InvalidateRequerySuggested();
|
||||
}
|
||||
|
||||
private void Reload()
|
||||
{
|
||||
if (_suppressReload) return;
|
||||
_ = ReloadAsync();
|
||||
}
|
||||
|
||||
private async Task ReloadAsync()
|
||||
{
|
||||
if (_pager is null) return;
|
||||
await ApplyAsync(_pager.ResetAsync(Filter));
|
||||
}
|
||||
|
||||
private async Task ApplyAsync(Task<LibraryPage?>? pending)
|
||||
{
|
||||
if (pending is null) return;
|
||||
IsLoading = true;
|
||||
RaiseState();
|
||||
try
|
||||
{
|
||||
var page = await pending;
|
||||
if (page is null) return;
|
||||
if (page.IsFirst)
|
||||
{
|
||||
foreach (var old in Items) old.PropertyChanged -= OnItemChanged;
|
||||
Items.Clear();
|
||||
Selected = null;
|
||||
SelectionCount = 0;
|
||||
}
|
||||
foreach (var sequence in page.Items)
|
||||
{
|
||||
var item = new SequenceItem(sequence, Shell.Services.Thumbnails) { IsAnalysing = _analysing.Contains(sequence.Id) };
|
||||
item.PropertyChanged += OnItemChanged;
|
||||
Items.Add(item);
|
||||
}
|
||||
_hasLoaded = true;
|
||||
}
|
||||
catch (Exception ex) when (ex is DatabaseException or InvalidOperationException)
|
||||
{
|
||||
Shell.ReportError(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsLoading = _pager?.IsLoading ?? false;
|
||||
RaiseState();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RefreshFiltersAsync()
|
||||
{
|
||||
if (_db is null) return;
|
||||
try
|
||||
{
|
||||
var stats = await _db.Sequences.GetStatsAsync(null);
|
||||
var tags = await _db.Tags.ListAsync();
|
||||
_hasArchive = (await _db.Archives.ListAsync()).Count > 0;
|
||||
Shell.SetLibraryTotals(stats.Sequences, stats.Photos, stats.Bytes);
|
||||
|
||||
_suppressReload = true;
|
||||
var year = _year.Value;
|
||||
var camera = _camera.Value;
|
||||
var tag = _tag.Value;
|
||||
Years = [AnyYear(), .. stats.ByYear.OrderByDescending(y => y.Year).Select(y => new FilterOption<int?>(y.Year, YearText(y.Year), YearText(y.Year)))];
|
||||
Cameras =
|
||||
[
|
||||
AnyCamera(),
|
||||
.. stats.ByCamera.Where(c => !string.IsNullOrWhiteSpace(c.Model))
|
||||
.GroupBy(c => c.Model!, StringComparer.Ordinal)
|
||||
.Select(g => (Model: g.Key, Name: Core.Naming.PathTemplate.CameraName(g.First().Make, g.Key)))
|
||||
.OrderBy(c => c.Name, StringComparer.CurrentCultureIgnoreCase)
|
||||
.Select(c => new FilterOption<string?>(c.Model, c.Name, c.Name)),
|
||||
];
|
||||
Tags = [AnyTag(), .. tags.OrderBy(t => t.Name, StringComparer.CurrentCultureIgnoreCase).Select(t => new FilterOption<string?>(t.Name, t.Name, t.Name))];
|
||||
SelectedYear = Years.FirstOrDefault(o => o.Value == year) ?? Years[0];
|
||||
SelectedCamera = Cameras.FirstOrDefault(o => o.Value == camera) ?? Cameras[0];
|
||||
SelectedTag = Tags.FirstOrDefault(o => o.Value == tag) ?? Tags[0];
|
||||
}
|
||||
catch (DatabaseException ex)
|
||||
{
|
||||
Log.Warning("library", "Library statistics unavailable", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_suppressReload = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetFilter<T>(ref FilterOption<T> field, FilterOption<T> value, string flagName, [System.Runtime.CompilerServices.CallerMemberName] string? propertyName = null)
|
||||
{
|
||||
if (!Set(ref field, value, propertyName)) return;
|
||||
OnPropertyChanged(flagName);
|
||||
Reload();
|
||||
}
|
||||
|
||||
private void ClearFilters()
|
||||
{
|
||||
_suppressReload = true;
|
||||
_searchDelay.Stop();
|
||||
SearchText = string.Empty;
|
||||
_searchDelay.Stop();
|
||||
SelectedYear = Years[0];
|
||||
SelectedCamera = Cameras[0];
|
||||
SelectedStatus = Statuses[0];
|
||||
SelectedTag = Tags[0];
|
||||
_suppressReload = false;
|
||||
Reload();
|
||||
}
|
||||
|
||||
private void ClearSelection()
|
||||
{
|
||||
foreach (var item in Items) item.IsSelected = false;
|
||||
OnSelectionChanged();
|
||||
}
|
||||
|
||||
private void OnItemChanged(object? sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
if (e.PropertyName == nameof(SequenceItem.IsSelected)) OnSelectionChanged();
|
||||
}
|
||||
|
||||
private void RaiseState()
|
||||
{
|
||||
OnPropertyChanged(nameof(HasItems));
|
||||
OnPropertyChanged(nameof(IsGrid));
|
||||
OnPropertyChanged(nameof(IsList));
|
||||
OnPropertyChanged(nameof(IsLibraryEmpty));
|
||||
OnPropertyChanged(nameof(IsNoMatch));
|
||||
}
|
||||
|
||||
private void OnJobChanged(Job job)
|
||||
{
|
||||
// Engine events arrive on worker threads.
|
||||
System.Windows.Application.Current?.Dispatcher.BeginInvoke(() => ApplyJob(job));
|
||||
}
|
||||
|
||||
private void ApplyJob(Job job)
|
||||
{
|
||||
if (job.Kind is JobKind.LightAnalyse or JobKind.DeepAnalyse)
|
||||
{
|
||||
foreach (var id in job.TargetIds)
|
||||
{
|
||||
if (job.State == JobState.Running) _analysing.Add(id);
|
||||
else _analysing.Remove(id);
|
||||
}
|
||||
foreach (var item in Items.Where(i => job.TargetIds.Contains(i.Id))) item.IsAnalysing = _analysing.Contains(item.Id);
|
||||
}
|
||||
if (job.State == JobState.Done) _ = RefreshTargetsAsync(job);
|
||||
}
|
||||
|
||||
/// <summary>A finished job changed some sequences: refresh those rows in place and the totals.</summary>
|
||||
private async Task RefreshTargetsAsync(Job job)
|
||||
{
|
||||
if (_db is null) return;
|
||||
try
|
||||
{
|
||||
foreach (var item in Items.Where(i => job.TargetIds.Contains(i.Id)).ToList())
|
||||
{
|
||||
if (await _db.Sequences.GetAsync(item.Id) is { } fresh) item.Update(fresh);
|
||||
}
|
||||
if (job.Kind is JobKind.Scan or JobKind.Rebuild or JobKind.Import or JobKind.Adopt or JobKind.Move or JobKind.UploadCommit) await RefreshAsync();
|
||||
}
|
||||
catch (DatabaseException ex)
|
||||
{
|
||||
Log.Warning("library", "Refresh after a job failed", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task EnqueueAsync(JobKind kind)
|
||||
{
|
||||
if (Shell.Services.Jobs is not { } jobs) return;
|
||||
var chosen = SelectedItems().ToList();
|
||||
if (chosen.Count == 0) return;
|
||||
var label = chosen.Count == 1 ? chosen[0].Title : Loc.Plural("Library.Selection.Count", chosen.Count);
|
||||
await jobs.EnqueueAsync(kind, [.. chosen.Select(i => i.Id)], label);
|
||||
Shell.Notify(Loc.L("Library.Queued", JobText.Kind(kind), label), actionText: Loc.L("Shell.Nav.Jobs"),
|
||||
actionToolTip: Loc.L("Tooltips.Nav.Jobs"), action: () => Shell.Navigate(SectionKey.Jobs));
|
||||
}
|
||||
|
||||
private async Task RescanAsync()
|
||||
{
|
||||
if (_db is null || Shell.Services.Jobs is not { } jobs) return;
|
||||
var archives = await _db.Archives.ListAsync();
|
||||
foreach (var archive in archives) await jobs.EnqueueAsync(JobKind.Scan, [archive.Id], archive.Name);
|
||||
if (archives.Count > 0)
|
||||
Shell.Notify(Loc.L("Library.Queued", JobText.Kind(JobKind.Scan), archives[0].Name), actionText: Loc.L("Shell.Nav.Jobs"),
|
||||
actionToolTip: Loc.L("Tooltips.Nav.Jobs"), action: () => Shell.Navigate(SectionKey.Jobs));
|
||||
}
|
||||
|
||||
private async Task RevealAsync()
|
||||
{
|
||||
if (_db is null || Selected is not { } item) return;
|
||||
var archive = await _db.Archives.GetAsync(item.Sequence.ArchiveId);
|
||||
if (archive is null) return;
|
||||
ShellActions.Reveal(Path.Combine(archive.RootPath, item.Sequence.Folder.Replace('/', Path.DirectorySeparatorChar)));
|
||||
}
|
||||
|
||||
private async Task AdoptFolderAsync()
|
||||
{
|
||||
var folder = Shell.Services.Dialogs.PickFolder(Loc.L("Library.Adopt.Title"));
|
||||
if (folder is null) return;
|
||||
await Shell.Services.ArchiveSetup.AdoptAsync(folder);
|
||||
await RefreshAsync();
|
||||
}
|
||||
|
||||
/// <summary>Writes the selected rows as CSV (UTF-8, invariant numbers) into a chosen folder.</summary>
|
||||
private async Task ExportListAsync()
|
||||
{
|
||||
var chosen = SelectedItems().Select(i => i.Sequence).ToList();
|
||||
if (chosen.Count == 0) return;
|
||||
var folder = Shell.Services.Dialogs.PickFolder(Loc.L("Library.Export.Title"));
|
||||
if (folder is null) return;
|
||||
var path = Path.Combine(folder, $"Titano sequences {DateTime.Now:yyyy-MM-dd HHmm}.csv");
|
||||
var csv = Csv(chosen);
|
||||
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));
|
||||
}
|
||||
|
||||
/// <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\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("\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>
|
||||
private void OnCultureChanged(object? sender, EventArgs e)
|
||||
{
|
||||
_suppressReload = true;
|
||||
try
|
||||
{
|
||||
var status = _status.Value;
|
||||
var sort = _sort.Value;
|
||||
Statuses = BuildStatusOptions();
|
||||
Sorts = BuildSortOptions();
|
||||
Years = [AnyYear(), .. Years.Skip(1)];
|
||||
Cameras = [AnyCamera(), .. Cameras.Skip(1)];
|
||||
Tags = [AnyTag(), .. Tags.Skip(1)];
|
||||
_status = Statuses.First(o => o.Value == status);
|
||||
_sort = Sorts.First(o => o.Value == sort);
|
||||
_year = _year.IsAny ? Years[0] : _year;
|
||||
_camera = _camera.IsAny ? Cameras[0] : _camera;
|
||||
_tag = _tag.IsAny ? Tags[0] : _tag;
|
||||
foreach (var item in Items) item.Update(item.Sequence);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_suppressReload = false;
|
||||
}
|
||||
OnAllPropertiesChanged();
|
||||
}
|
||||
|
||||
private static string YearText(int year) => year.ToString(CultureInfo.InvariantCulture);
|
||||
|
||||
private static FilterOption<int?> AnyYear() => new(null, Loc.L("Library.Filter.AnyYear"), Loc.L("Library.Filter.Year"), IsAny: true);
|
||||
|
||||
private static FilterOption<string?> AnyCamera() => new(null, Loc.L("Library.Filter.AnyCamera"), Loc.L("Library.Filter.Camera"), IsAny: true);
|
||||
|
||||
private static FilterOption<string?> AnyTag() => new(null, Loc.L("Library.Filter.AnyTag"), Loc.L("Library.Filter.Tags"), IsAny: true);
|
||||
|
||||
private static IReadOnlyList<FilterOption<StatusFilter>> BuildStatusOptions() =>
|
||||
[
|
||||
new(StatusFilter.Any, Loc.L("Library.Filter.AnyStatus"), Loc.L("Library.Filter.Status"), IsAny: true),
|
||||
.. Enum.GetValues<StatusFilter>().Where(s => s != StatusFilter.Any)
|
||||
.Select(s => new FilterOption<StatusFilter>(s, Loc.L("Library.Filter.Status." + s), Loc.L("Library.Filter.Status." + s))),
|
||||
];
|
||||
|
||||
private static IReadOnlyList<FilterOption<SequenceSort>> BuildSortOptions() =>
|
||||
[.. Enum.GetValues<SequenceSort>().Select(s => new FilterOption<SequenceSort>(s, Loc.L("Library.Sort." + s), Loc.L("Library.Sort." + s)))];
|
||||
}
|
||||
Reference in new issue
Block a user