Files
Libation/Source/LibationAvalonia/ViewModels/MainVM.ScanAuto.cs
T
Cursor Agentandrmcrackan 0f4cfac3b0 Name the account and the real cause when auto-scan pauses for a login
Ported from #1949. The reporter's log paused auto-scan on a second account that
had never been logged in, while the dialog blamed an expired session and named no
account, so there was nothing to act on.

AccountCredentialStatus tells a never-registered account apart from one holding an
expired access token, by looking for a refresh token to renew from. AutoScanRunner
now hands the AuthenticationRequiredException to the notification so the prompt can
name the account, which means digging that exception back out of the wrappers the
scan adds on the way up. Same distinction in the log line and in the exception
message ApiExtended throws when interactive login is unavailable, which is what the
CLI and Docker users see.

Co-authored-by: rmcrackan <rmcrackan@gmail.com>
2026-08-16 16:55:46 +00:00

79 lines
2.3 KiB
C#

using ApplicationServices;
using AudibleUtilities;
using Dinah.Core;
using LibationFileManager;
using LibationUiBase;
using LibationUiBase.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace LibationAvalonia.ViewModels;
partial class MainVM
{
private readonly InterruptableTimer autoScanTimer = new(TimeSpan.FromMinutes(5));
private AutoScanRunner? autoScanRunner;
private void Configure_ScanAuto()
{
autoScanRunner = new AutoScanRunner(
isAutoScanEnabled: () => Configuration.Instance.AutoScan,
pauseTimer: () => autoScanTimer.Stop(),
resumeTimer: () => autoScanTimer.PerformNow(),
notifyAuthRequired: notifyAutoScanAuthRequiredAsync);
autoScanTimer.Elapsed += async (_, __) => await autoScanRunner.RunAsync();
MainWindow.Loaded += startAutoScan;
AccountsSettingsPersister.Saving += accountsPreSave;
AccountsSettingsPersister.Saved += accountsPostSave;
Configuration.Instance.PropertyChanged += startAutoScan;
}
private async Task notifyAutoScanAuthRequiredAsync(AuthenticationRequiredException ex)
{
await MessageBox.Show(
MainWindow,
AutoScanAuthPrompt.FormatBody(ex),
AutoScanAuthPrompt.Caption,
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
private List<(string AccountId, string LocaleName)>? preSaveDefaultAccounts;
private List<(string AccountId, string LocaleName)> getDefaultAccounts()
{
using var persister = AudibleApiStorage.GetAccountsSettingsPersister();
return persister.AccountsSettings
.GetAll()
.Where(a => a.LibraryScan)
.Select(a => (a.AccountId, a.Locale?.Name))
.OfType<(string, string)>()
.ToList();
}
private void accountsPreSave(object? sender = null, EventArgs? e = null)
=> preSaveDefaultAccounts = getDefaultAccounts();
private void accountsPostSave(object? sender = null, EventArgs? e = null)
{
if (getDefaultAccounts().Except(preSaveDefaultAccounts ?? Enumerable.Empty<(string AccountId, string LocaleName)>()).Any())
startAutoScan();
}
[PropertyChangeFilter(nameof(Configuration.AutoScan))]
private void startAutoScan(object? sender = null, EventArgs? e = null)
{
autoScanRunner?.OnAutoScanSettingChanged();
AutoScanChecked = Configuration.Instance.AutoScan;
if (AutoScanChecked)
autoScanTimer.PerformNow();
else
autoScanTimer.Stop();
}
}