diff --git a/src/Titano.App/Import/ImportChoices.cs b/src/Titano.App/Import/ImportChoices.cs index df2b009e..bcc62d93 100644 --- a/src/Titano.App/Import/ImportChoices.cs +++ b/src/Titano.App/Import/ImportChoices.cs @@ -1,4 +1,5 @@ using Titano.Core.Import; +using Titano.Core.Model; using Titano.Core.Naming; using Titano.Data.Archiving; @@ -92,3 +93,19 @@ public sealed record ImportChoices private static bool UsesTitle(string template) => template.Contains("{title", StringComparison.OrdinalIgnoreCase); } + +/// +/// Camera profiles (BRIEF §13): a card without saved settings imports with the settings saved last by another card +/// of the same camera model, so a new card for a known camera needs no setup. +/// +public static class CameraProfiles +{ + public static CardProfile? Find(string? make, string? model, IEnumerable cards) => + model is null + ? null + : cards.Where(c => c.ProfileJson is { Length: > 0 } && string.Equals(c.CameraModel, model, StringComparison.Ordinal) + && string.Equals(c.CameraMake, make, StringComparison.Ordinal)) + .MaxBy(c => c.LastSeen) is { } source + ? ImportService.ProfileOf(source) + : null; +} diff --git a/src/Titano.App/Sections/ImportViewModel.cs b/src/Titano.App/Sections/ImportViewModel.cs index 28855312..3ca93253 100644 --- a/src/Titano.App/Sections/ImportViewModel.cs +++ b/src/Titano.App/Sections/ImportViewModel.cs @@ -81,6 +81,7 @@ public sealed class ImportViewModel : SectionViewModel private string? _autoStartKey; private bool _activeTemplateIsFolder; private bool _refreshing; + private bool _profileResolved; public ImportViewModel(ShellViewModel shell) : base(SectionKey.Import, "Icon.Import") @@ -552,8 +553,13 @@ public sealed class ImportViewModel : SectionViewModel Details = null; Result = null; Nickname.Load(_selected?.Card?.Nickname ?? string.Empty); - var profile = _selected?.Card is { } card ? ImportService.ProfileOf(card) : DefaultProfile(); - LoadChoices(profile); + var card = _selected?.Card; + // A card that has a camera but no saved settings takes the camera profile now; one seen for the first time + // takes it once its first plan names the camera (InheritedProfile). + _profileResolved = _selected?.Kind != SourceKind.Card || card is { ProfileJson.Length: > 0 } || card?.CameraModel is not null; + LoadChoices(card is { ProfileJson.Length: > 0 } ? ImportService.ProfileOf(card) + : card is not null && CameraProfiles.Find(card.CameraMake, card.CameraModel, _cards.Where(c => c.Id != card.Id)) is { } camera ? camera + : DefaultProfile()); _ = LoadIdentityAsync(); OnPropertyChanged(string.Empty); Replan(); @@ -730,9 +736,11 @@ public sealed class ImportViewModel : SectionViewModel if (cts.IsCancellationRequested) return; _existingFolders = folders; _rawPlan = plan; + if (InheritedProfile(plan) is { } camera) LoadChoices(camera); ApplyPlan(); SetStatus(ImportPhase.Ready, null); - if (_autoStartKey is { } key && key == _selected?.Key) + // An inherited profile that groups or names differently plans again first. + if (_autoStartKey is { } key && key == _selected?.Key && !_planDelay.IsEnabled) { _autoStartKey = null; if (StartBlocker is null) await StartAsync(); @@ -748,6 +756,14 @@ public sealed class ImportViewModel : SectionViewModel } } + /// The camera profile of a card seen for the first time, once its first plan names the camera (BRIEF §13). + private CardProfile? InheritedProfile(ImportPlan plan) + { + if (_profileResolved || _selected?.Card is not { } card) return null; + _profileResolved = true; + return plan.Sessions is [var first, ..] ? CameraProfiles.Find(first.CameraMake, first.CameraModel, _cards.Where(c => c.Id != card.Id)) : null; + } + /// The selected card's id, registering an inserted card on first sight. private async Task RegisteredCardIdAsync() { diff --git a/tests/Titano.App.Tests/Import/ImportChoicesTests.cs b/tests/Titano.App.Tests/Import/ImportChoicesTests.cs index f8b2b030..e1f85ba0 100644 --- a/tests/Titano.App.Tests/Import/ImportChoicesTests.cs +++ b/tests/Titano.App.Tests/Import/ImportChoicesTests.cs @@ -1,5 +1,8 @@ +using System.Text.Json; +using System.Text.Json.Serialization; using Titano.App.Import; using Titano.Core.Import; +using Titano.Core.Model; using Titano.Data.Archiving; using Titano.Testing; @@ -66,4 +69,30 @@ public sealed class ImportChoicesTests Assert.False(a.TitlesChangeNames); Assert.True((a with { FolderTemplate = "{year}/{Title:upper}" }).TitlesChangeNames); } + + [Test] + public void ACardWithoutSettingsTakesTheLastSavedProfileOfItsCamera() + { + var older = Card("Canon", "Canon EOS R5", new CardProfile { SessionGapMinutes = 20 }, daysAgo: 30); + var newer = Card("Canon", "Canon EOS R5", new CardProfile { SessionGapMinutes = 90 }, daysAgo: 2); + var other = Card("SONY", "ILCE-7M4", new CardProfile { SessionGapMinutes = 5 }, daysAgo: 1); + var unsaved = Card("Canon", "Canon EOS R5", null, daysAgo: 0); + Assert.Equal(90, CameraProfiles.Find("Canon", "Canon EOS R5", [older, newer, other, unsaved])!.SessionGapMinutes); + Assert.Null(CameraProfiles.Find("Canon", "Canon EOS R6", [older, newer, other])); + Assert.Null(CameraProfiles.Find("Canon", null, [older, newer])); + Assert.Null(CameraProfiles.Find("Canon", "Canon EOS R5", [unsaved]), "only saved settings are shared"); + } + + private static MediaCard Card(string make, string model, CardProfile? profile, int daysAgo) => new() + { + Id = Guid.CreateVersion7(), + Identity = Guid.NewGuid().ToString("N"), + CameraMake = make, + CameraModel = model, + ProfileJson = profile is null ? null : JsonSerializer.Serialize(profile, StoredProfile), + LastSeen = DateTimeOffset.UtcNow.AddDays(-daysAgo), + }; + + /// How the card repository stores a profile (camel case, enum names). + private static readonly JsonSerializerOptions StoredProfile = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, Converters = { new JsonStringEnumConverter() } }; }