Add menu option to remove Plus books from Audible

This commit is contained in:
Michael Bucari-Tovo
2026-01-08 13:00:47 -07:00
parent 804bac5c4c
commit 1514de54da
3 changed files with 90 additions and 0 deletions

View File

@@ -351,6 +351,20 @@ namespace LibationAvalonia.Views
})
});
}
#endregion
#region Remove Audible Plus Books from Audible Library
if (entries.Length != 1 || ctx.RemoveFromAudibleEnabled)
{
args.ContextMenuItems.Add(new Separator());
args.ContextMenuItems.Add(new MenuItem
{
Header = ctx.RemoveFromAudibleText,
IsEnabled = ctx.RemoveFromAudibleEnabled,
Command = ReactiveCommand.CreateFromTask(ctx.RemoveFromAudibleAsync)
});
}
#endregion
if (entries.Length > 1)

View File

@@ -1,9 +1,15 @@
using ApplicationServices;
using DataLayer;
using Dinah.Core;
using DocumentFormat.OpenXml.Office2010.ExcelAc;
using DocumentFormat.OpenXml.Wordprocessing;
using FileLiberator;
using LibationFileManager;
using LibationFileManager.Templates;
using LibationUiBase.Forms;
using Lucene.Net.Messages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@@ -17,6 +23,7 @@ public class GridContextMenu
public string SetDownloadedText => $"Set Download status to '{Accelerator}Downloaded'";
public string SetNotDownloadedText => $"Set Download status to '{Accelerator}Not Downloaded'";
public string RemoveText => $"{Accelerator}Remove from library";
public string RemoveFromAudibleText => $"Remove Plus {(GridEntries.Count(e => e.LibraryBook.IsAudiblePlus) == 1 ? "Book" : "Books")} from Audible Library";
public string LocateFileText => $"{Accelerator}Locate file...";
public string LocateFileDialogTitle => $"Locate the audio file for '{GridEntries[0].Book?.TitleWithSubtitle ?? "[null]"}'";
public string LocateFileErrorMessage => "Error saving book's location";
@@ -37,6 +44,7 @@ public class GridContextMenu
public bool ConvertToMp3Enabled => LibraryBookEntries.Any(ge => ge.Book?.UserDefinedItem.BookStatus is LiberatedStatus.Liberated);
public bool DownloadAsChaptersEnabled => LibraryBookEntries.Any(ge => ge.Book?.UserDefinedItem.BookStatus is not LiberatedStatus.Error);
public bool ReDownloadEnabled => LibraryBookEntries.Any(ge => ge.Book?.UserDefinedItem.BookStatus is LiberatedStatus.Liberated);
public bool RemoveFromAudibleEnabled => LibraryBookEntries.Any(ge => ge.LibraryBook.IsAudiblePlus);
private GridEntry[] GridEntries { get; }
public LibraryBookEntry[] LibraryBookEntries { get; }
@@ -84,6 +92,64 @@ public class GridContextMenu
await LibraryBookEntries.Select(e => e.LibraryBook).RemoveBooksAsync();
}
public async Task RemoveFromAudibleAsync()
{
List<LibraryBook> removedFromAudible = [];
List<LibraryBook> failedToRemove = [];
foreach (var entry in LibraryBookEntries.Select(l => l.LibraryBook).Where(lb => lb.IsAudiblePlus))
{
try
{
var api = await entry.GetApiAsync();
var success = await api.RemoveItemFromLibraryAsync(entry.Book.AudibleProductId);
if (success)
{
removedFromAudible.Add(entry);
}
else
{
failedToRemove.Add(entry);
}
}
catch (Exception ex)
{
Serilog.Log.Logger.Error(ex, "Failed to remove book from audible account. {@Book}", entry.LogFriendly());
failedToRemove.Add(entry);
}
}
if (failedToRemove.Count > 0)
{
var count = failedToRemove.Count;
string bookBooks = count == 1 ? "book" : "books";
var message = $"""
Failed to remove {count} {bookBooks} from Audible.
{failedToRemove.AggregateTitles()}
""";
await MessageBoxBase.Show(message, $"Failed to Remove {bookBooks.FirstCharToUpper()} from Audible");
}
try
{
await removedFromAudible.PermanentlyDeleteBooksAsync();
}
catch (Exception ex)
{
Serilog.Log.Logger.Error(ex, "Failed to delete locally removed from Audible books.");
var count = removedFromAudible.Count;
string bookBooks = count == 1 ? "book" : "books";
var message = $"""
Failed to delete {count} {bookBooks} from Libation.
{removedFromAudible.AggregateTitles()}
""";
await MessageBoxBase.Show(message, $"Failed to Delete {bookBooks.FirstCharToUpper()} from Libation");
}
}
public ITemplateEditor CreateTemplateEditor<T>(LibraryBook libraryBook, string existingTemplate)
where T : Templates, ITemplate, new()
{

View File

@@ -262,7 +262,17 @@ namespace LibationWinForms.GridView
}
#endregion
#region Remove Audible Plus Books from Audible Library
if (entries.Length != 1 || ctx.RemoveFromAudibleEnabled)
{
ctxMenu.Items.Add(new ToolStripSeparator());
var removeFromAudibleMenuItem = new ToolStripMenuItem() { Text = ctx.RemoveFromAudibleText, Enabled = ctx.RemoveFromAudibleEnabled };
removeFromAudibleMenuItem.Click += async (_, _) => await ctx.RemoveFromAudibleAsync();
ctxMenu.Items.Add(removeFromAudibleMenuItem);
}
#endregion
if (entries.Length > 1)
return;