diff --git a/i18n/en/Settings.resx b/i18n/en/Settings.resx index 12316821..4df33790 100644 --- a/i18n/en/Settings.resx +++ b/i18n/en/Settings.resx @@ -8,6 +8,7 @@ Index backed up Back up now Data folder + Data kept outside OneDrivelong Diagnostics Diagnostics bundle saved Save the diagnostics bundle to diff --git a/i18n/it/Settings.it.resx b/i18n/it/Settings.it.resx index 487c29f2..f61c0972 100644 --- a/i18n/it/Settings.it.resx +++ b/i18n/it/Settings.it.resx @@ -8,6 +8,7 @@ Copia dell'indice salvata Salva una copia Cartella dati + Dati tenuti fuori da OneDrive Diagnostica Pacchetto diagnostico salvato Salva il pacchetto in diff --git a/src/Titano.App/Sections/SettingsViewModel.cs b/src/Titano.App/Sections/SettingsViewModel.cs index bdd1ae23..f899372e 100644 --- a/src/Titano.App/Sections/SettingsViewModel.cs +++ b/src/Titano.App/Sections/SettingsViewModel.cs @@ -45,6 +45,12 @@ public sealed class SettingsViewModel : SectionViewModel private bool _isTesting; private CancellationTokenSource? _selfTest; private Archive? _archive; + + /// + /// The open of the archive at the folder chosen last (), with the archive it found + /// there: edits of the Archive rows made meanwhile wait for it (). + /// + private Task _retarget = Task.FromResult(null); private bool _isDetectingFormats; private IReadOnlyList _backups = []; private BackupOption? _selectedBackup; @@ -82,7 +88,7 @@ public sealed class SettingsViewModel : SectionViewModel _s.LastArchivePath = v.Length == 0 ? null : Path.GetFullPath(v); Save(); UpdateArchiveFree(); - _ = RetargetArchiveAsync(); + _retarget = RetargetArchiveAsync(); }, () => Loc.L("Tooltips.Settings.ArchiveRoot")); FolderTemplate = new EditableField(_s.FolderTemplate, v => NamingPreview.Validate(v, isFolder: true), v => { @@ -770,7 +776,8 @@ public sealed class SettingsViewModel : SectionViewModel /// /// Loads what needs the services (certificates, cache usage, the open archive's settings), follows the server session, - /// applies "Background priority" while Titano is in front and says how a restore staged before this start went. + /// applies "Background priority" while Titano is in front and says how a restore staged before this start went, and + /// where the data of a portable copy in a OneDrive folder went. /// public void Attach() { @@ -798,6 +805,10 @@ public sealed class SettingsViewModel : SectionViewModel Shell.Notify(Loc.L("Settings.Advanced.RestoreFailed"), Loc.L("Errors.Backup.Deferred"), isError: true); break; } + // BRIEF §6.2: a portable copy in a folder OneDrive synchronises keeps its data in this user's folder instead. + if (Shell.Services.Environment.SyncedFolder is not null) + Shell.Notify(Loc.L("Settings.Advanced.DataOutsideOneDrive"), Formatting.LeftToRight(DataFolder), Loc.L("Common.Open"), + Loc.L("Tooltips.Settings.OpenDataFolder"), () => ShellActions.Reveal(DataFolder)); Startup.Refresh(); } @@ -868,33 +879,39 @@ public sealed class SettingsViewModel : SectionViewModel /// /// Takes the archive and server values other screens set (opening an archive, connecting), unless the field holds an - /// edit not committed yet. + /// edit not committed yet, and the sign-in entry as Task Manager may have left it. /// private void ReloadShared() { if (!ArchiveRoot.IsDirty) ArchiveRoot.Load(_s.LastArchivePath ?? string.Empty); if (!DefaultServer.IsDirty) DefaultServer.Load(ServerAddress.Display(_s.ServerUrl)); + OnPropertyChanged(nameof(StartWithWindows)); UpdateArchiveFree(); _ = LoadArchiveAsync(); if (_page == SettingsPage.Advanced) LoadBackups(); } /// - /// Shows the open archive's own templates, proxy size and thumbnail size (its archive.json; the index's while it is - /// offline) unless a field holds an edit; they also become the defaults of new archives. The open archive is the one - /// at the archive folder of this PC, never another one of the index: with none registered there, the rows edit only - /// this PC's defaults. + /// Shows the open archive's own templates, proxy size and thumbnail size unless a field holds an edit; they also become + /// the defaults of new archives. They come from its archive.json, which also brings the index up to a change another + /// process sharing the archive made, so imports follow it too. While the file cannot be read (offline), the templates + /// and proxy size are the index's and the thumbnail size stays this PC's: only the file knows the archive's. The open + /// archive is the one at the archive folder of this PC, never another one of the index: with no folder, or none + /// registered there, the rows edit only this PC's defaults. Returns that archive (null for none). /// - private async Task LoadArchiveAsync() + private async Task LoadArchiveAsync() { - if (Shell.Services.Database is not { } db || Shell.Services.Archives is not { } archives) return; + if (Shell.Services.Database is not { } db || Shell.Services.Archives is not { } archives) return null; + Archive? archive = null; try { var registered = await db.Archives.ListAsync(); - var archive = _s.LastArchivePath is { Length: > 0 } root ? CurrentArchive.At(registered, root) : CurrentArchive.Pick(registered, null); + archive = _s.LastArchivePath is { Length: > 0 } root ? CurrentArchive.At(registered, root) : null; _archive = archive; - if (archive is null) return; - var settings = await Task.Run(() => archives.GetSettingsAsync(archive.Id)); + if (archive is null) return null; + var settings = await Task.Run(() => archives.RefreshSettingsAsync(archive.Id)) + ?? new ArchiveSettings(archive.Name, new LayoutTemplates { Folder = archive.FolderTemplate, File = archive.FileTemplate }, + new ProxyPolicy { LongEdge = archive.ProxyLongEdge }, new ThumbnailPolicy { LongEdge = _s.ThumbnailLongEdge }); if (!FolderTemplate.IsDirty) FolderTemplate.Load(settings.Templates.Folder); if (!FileTemplate.IsDirty) FileTemplate.Load(settings.Templates.File); var changed = _s.FolderTemplate != settings.Templates.Folder || _s.FileTemplate != settings.Templates.File @@ -915,20 +932,41 @@ public sealed class SettingsViewModel : SectionViewModel { Log.Warning("settings", "The open archive's settings could not be read", ex); } + return archive; + } + + /// + /// The archive an Archive row edits, as the folder was when the row was edited (, the + /// latest folder change then): the open one, or, while the archive at that folder is still opening, the one found + /// there once it has (null for none), so the edit is neither lost nor sent to a folder chosen later. + /// + private async Task ArchiveAtFolderAsync(Task retarget) + { + if (retarget.IsCompleted) return _archive; + // Its failures were told already; only the archive it found matters here. + await ((Task)retarget).ConfigureAwait(ConfigureAwaitOptions.ContinueOnCapturedContext | ConfigureAwaitOptions.SuppressThrowing); + return retarget.IsCompletedSuccessfully ? retarget.Result : null; } /// /// Writes one Archive row into the open archive's archive.json and index through the archive engine (BRIEF §7). When - /// that fails (offline, read-only, busy) the user hears why and the rows show the archive's values again. + /// that fails (offline, read-only, busy) the user hears why and the rows show the archive's values again. An edit made + /// while the archive at a folder just chosen is opening waits for it, then goes to that archive (or stays this PC's + /// default when there is none), instead of being lost. /// private async Task UpdateArchiveAsync(Func edit) { - if (_archive is not { } archive || Shell.Services.Archives is not { } archives) return; + var retarget = _retarget; + var waited = !retarget.IsCompleted; + if (await ArchiveAtFolderAsync(retarget) is not { } archive || Shell.Services.Archives is not { } archives) return; try { var updated = await Task.Run(() => archives.UpdateSettingsAsync(archive.Id, edit)); // The archive folder may have changed meanwhile: the rows then belong to another archive. if (_archive?.Id == updated.Id) _archive = updated; + // The open showed the archive's values over the edit: unless another folder was chosen since, the rows (and + // this PC's defaults) take what it holds now. + if (waited && _retarget == retarget) await LoadArchiveAsync(); } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException or InvalidDataException or NewerFormatException or DatabaseException) @@ -950,9 +988,9 @@ public sealed class SettingsViewModel : SectionViewModel /// /// The archive folder changed: an archive already there is registered as at start (relinked when it moved) and given /// its rescan, and the rows show its settings; any other folder leaves them with this PC's defaults for new archives. - /// Later edits never reach the archive shown before. + /// Later edits never reach the archive shown before. Returns the archive found there (null for none). /// - private async Task RetargetArchiveAsync() + private async Task RetargetArchiveAsync() { _archive = null; if (_s.LastArchivePath is { } root && Shell.Services.Archives is { } archives) @@ -968,7 +1006,7 @@ public sealed class SettingsViewModel : SectionViewModel Shell.Notify(Loc.L("Errors.Archive.Open"), ArchiveActions.Cause(ex), isError: true); } } - await LoadArchiveAsync(); + return await LoadArchiveAsync(); } /// @@ -1306,19 +1344,8 @@ public sealed class SettingsViewModel : SectionViewModel UiLanguage.Apply(null); break; case SettingsPage.Archive: - // The rows are the open archive's own: resetting them changes it for everyone sharing it, so only when confirmed. - var open = _archive; - if (open is not null && !ConfirmArchiveReset(open)) return; - _s.FolderTemplate = defaults.FolderTemplate; - _s.FileTemplate = defaults.FileTemplate; - _s.ProxyLongEdge = defaults.ProxyLongEdge; - _s.ThumbnailLongEdge = defaults.ThumbnailLongEdge; - _s.ScanIntervalMinutes = defaults.ScanIntervalMinutes; - FolderTemplate.Load(_s.FolderTemplate); - FileTemplate.Load(_s.FileTemplate); - UpdatePreview(); - if (open is not null) _ = PushArchiveSettingsAsync(); - break; + _ = ResetArchiveAsync(defaults); + return; case SettingsPage.Import: _s.ImportSessionGapMinutes = defaults.ImportSessionGapMinutes; _s.ImportMinimumFrames = defaults.ImportMinimumFrames; @@ -1367,6 +1394,28 @@ public sealed class SettingsViewModel : SectionViewModel Shell.Server.Refresh(); } + /// + /// Archive › Reset section. The rows are the open archive's own: resetting them changes it for everyone sharing it, so + /// only when confirmed. The archive at a folder just chosen is opened first, so the question names it and the reset + /// reaches it instead of being undone when it opens. + /// + private async Task ResetArchiveAsync(AppSettings defaults) + { + var open = await ArchiveAtFolderAsync(_retarget); + if (open is not null && !ConfirmArchiveReset(open)) return; + _s.FolderTemplate = defaults.FolderTemplate; + _s.FileTemplate = defaults.FileTemplate; + _s.ProxyLongEdge = defaults.ProxyLongEdge; + _s.ThumbnailLongEdge = defaults.ThumbnailLongEdge; + _s.ScanIntervalMinutes = defaults.ScanIntervalMinutes; + FolderTemplate.Load(_s.FolderTemplate); + FileTemplate.Load(_s.FileTemplate); + UpdatePreview(); + Save(); + OnAllPropertiesChanged(); + if (open is not null) await PushArchiveSettingsAsync(); + } + private void LoadImportFields() { SessionGap.Load(_s.ImportSessionGapMinutes.ToString(CultureInfo.CurrentCulture)); @@ -1484,7 +1533,7 @@ public sealed class SettingsViewModel : SectionViewModel ApplyAll(imported); // Settings of a PC never rewrite the shared archive.json: the imported templates and sizes are the defaults of new // archives, and the rows show the own settings of the archive at the (possibly imported) archive folder. - _ = RetargetArchiveAsync(); + _retarget = RetargetArchiveAsync(); Shell.Notify(Loc.L("Settings.Import.Done")); } diff --git a/tests/Titano.App.Tests/Settings/SettingsScreenSmokeTests.cs b/tests/Titano.App.Tests/Settings/SettingsScreenSmokeTests.cs index ebe92133..8d2a3408 100644 --- a/tests/Titano.App.Tests/Settings/SettingsScreenSmokeTests.cs +++ b/tests/Titano.App.Tests/Settings/SettingsScreenSmokeTests.cs @@ -169,6 +169,51 @@ public sealed class SettingsScreenSmokeTests installs[0].Command.Execute(null); }); + [Test] + public Task ArchiveRowsEditOnlyTheArchiveAtTheFolderAndKeepEditsMadeWhileItOpens() => ScreenHost.RunAsync(async () => + { + await using var app = await ScreenSmokeTests.SampleApp.StartAsync(); + var shell = app.Shell; + await app.Sample.FillAsync(); + var settings = shell.Settings; + var db = shell.Services.Database!; + var archive = Assert.NotNull(await Titano.App.Archiving.CurrentArchive.FindAsync(db, null)); + var manifestPath = new Titano.Core.Archiving.ArchivePaths(archive.RootPath).ManifestPath; + static Titano.Core.Archiving.ArchiveManifest Read(string path) => Titano.Core.Archiving.ManifestStore.ReadArchive(path); + shell.Navigate(SectionKey.Settings); + settings.Page = SettingsPage.Archive; + + // No archive folder: a template edit is this PC's default only, never the first archive of the index. + settings.ArchiveRoot.Text = string.Empty; + Assert.True(settings.ArchiveRoot.Commit()); + settings.FolderTemplate.Text = "{camera}/{date}"; + Assert.True(settings.FolderTemplate.Commit()); + + // The archive's folder is chosen and a template edited at once, while that archive is still opening: the edit + // reaches its archive.json instead of being undone when it opens, and the row shows it. + settings.ArchiveRoot.Text = archive.RootPath; + Assert.True(settings.ArchiveRoot.Commit()); + settings.FileTemplate.Text = "{date}_{n:0000}"; + Assert.True(settings.FileTemplate.Commit()); + await WaitAsync(() => Read(manifestPath).LayoutTemplates.File == "{date}_{n:0000}", "the edit reached the archive that was opening"); + await WaitAsync(() => settings.FileTemplate.Text == "{date}_{n:0000}" && shell.Services.Settings.FileTemplate == "{date}_{n:0000}", "the row shows it"); + Assert.NotEqual("{camera}/{date}", Read(manifestPath).LayoutTemplates.Folder); + + // Offline, only archive.json knows the thumbnail size: this PC's stays instead of becoming the default. + Titano.Core.Archiving.ManifestStore.WriteArchive(manifestPath, Read(manifestPath) with { ThumbnailPolicy = new Titano.Core.Archiving.ThumbnailPolicy { LongEdge = 480 } }); + shell.Navigate(SectionKey.Library); + shell.Navigate(SectionKey.Settings); + await WaitAsync(() => shell.Services.Settings.ThumbnailLongEdge == 480, "the archive's thumbnail size is shown"); + var row = Assert.NotNull(await db.Archives.GetAsync(archive.Id)); + await db.Archives.UpsertAsync(row with { ProxyLongEdge = 2560 }); + System.IO.File.Move(manifestPath, manifestPath + ".away"); + shell.Navigate(SectionKey.Library); + shell.Navigate(SectionKey.Settings); + await WaitAsync(() => shell.Services.Settings.ProxyLongEdge == 2560, "the index's proxy size is shown while offline"); + Assert.Equal(480, shell.Services.Settings.ThumbnailLongEdge); + Assert.Equal(480, settings.SelectedThumbnailSize.Value); + }); + [Test] public Task TheFirstRunDialogKeepsTheKeyboardInside() => ScreenHost.RunAsync(async () => {