Skip to content

Commit 9505d47

Browse files
committed
Fix crash on codex operations with empty selection
1 parent 72fdc53 commit 9505d47

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

src/Models/CodexCollection.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,8 @@ public void DeleteCodex(Codex toDelete)
585585

586586
public void DeleteCodices(IList<Codex> toDelete)
587587
{
588+
if (!toDelete.Any()) return;
589+
588590
Notification deleteWarnNotification = Notification.AreYouSureNotification;
589591
deleteWarnNotification.Body = $"You are about to remove {toDelete.Count} item{(toDelete.Count > 1 ? @"s" : @"")}. " +
590592
$"This cannot be undone. " +
@@ -604,7 +606,8 @@ public void DeleteCodices(IList<Codex> toDelete)
604606

605607
public void BanishCodices(IList<Codex> toBanish)
606608
{
607-
if (toBanish is null) return;
609+
if (!toBanish.SafeAny()) return;
610+
608611
IEnumerable<string> toBanishPaths = toBanish.Select(codex => codex.Sources.Path);
609612
IEnumerable<string> toBanishURLs = toBanish.Select(codex => codex.Sources.SourceURL);
610613
IEnumerable<string> toBanishStrings = toBanishPaths

src/Models/CodexProperties/EnumerableProperty.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public override bool HasNewValue(IHasCodexMetadata toEvaluate, IHasCodexMetadata
3333
return true;
3434
}
3535

36-
return !newVal!.SequenceEqual(refVal);
36+
return !newVal.SequenceEqual(refVal);
3737
}
3838
}
3939
}

src/Services/CoverService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public static async Task GetCover(Codex codex, ChooseMetaDataViewModel? chooseMe
8787

8888
public static async Task GetCover(List<Codex> codices)
8989
{
90+
if (!codices.Any()) return;
91+
9092
var progressVM = ProgressViewModel.GetInstance();
9193
progressVM.ResetCounter();
9294
progressVM.TotalAmount = codices.Count;

src/Tools/ExtensionMethods.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Collections.ObjectModel;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Globalization;
67
using System.Linq;
78
using System.Reflection;
@@ -172,7 +173,7 @@ public static IEnumerable<T> Flatten<T>(this IEnumerable<T> l, string method = "
172173
}
173174
}
174175

175-
public static bool SafeAny<T>(this IEnumerable<T>? l) => l is not null && l.Any();
176+
public static bool SafeAny<T>([NotNullWhen(true)] this IEnumerable<T>? l) => l is not null && l.Any();
176177
#endregion
177178
}
178179
}

src/ViewModels/CodexViewModel.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static bool CanOpenCodexOnline(Codex? toOpen)
108108
public RelayCommand<IList> OpenSelectedCodicesCommand => _openSelectedCodicesCommand ??= new(l => OpenSelectedCodices(l?.Cast<Codex>().ToList()));
109109
public static bool OpenSelectedCodices(IList<Codex>? toOpen)
110110
{
111-
if (toOpen is null) return false;
111+
if (!toOpen.SafeAny()) return false;
112112

113113
if (toOpen.Count == 1)
114114
{
@@ -152,7 +152,7 @@ public static void EditCodex(Codex? toEdit)
152152
public static void EditCodices(IList? toEdit)
153153
{
154154
List<Codex>? toEditList = toEdit?.Cast<Codex>().ToList();
155-
if (toEditList is null) return;
155+
if (!toEditList.SafeAny()) return;
156156

157157
if (toEditList.Count == 1)
158158
{
@@ -186,7 +186,8 @@ public static void FavoriteCodex(Codex? toFavorite)
186186
private static void FavoriteCodices(IList? toFavorite)
187187
{
188188
List<Codex>? toFavoriteList = toFavorite?.Cast<Codex>().ToList();
189-
if (toFavoriteList is null) return;
189+
if (!toFavoriteList.SafeAny()) return;
190+
190191
if (toFavoriteList.Count == 1)
191192
{
192193
FavoriteCodex(toFavoriteList.First());
@@ -247,6 +248,8 @@ public void MoveToCollection(object[]? par)
247248
/// <param name="toMoveList"></param>
248249
public static void MoveToCollection(CodexCollection targetCollection, List<Codex> toMoveList)
249250
{
251+
if (!toMoveList.Any()) return;
252+
250253
//Check if target Collection is valid
251254
if (targetCollection.DirectoryName == MainViewModel.CollectionVM.CurrentCollection.DirectoryName)
252255
{
@@ -324,6 +327,8 @@ public static void DeleteCodex(Codex? toDelete)
324327
public RelayCommand<IList> DeleteCodicesCommand => _deleteCodicesCommand ??= new(DeleteCodices);
325328
public static void DeleteCodices(IList? toDelete)
326329
{
330+
if (toDelete == null || toDelete.Count == 0) return;
331+
327332
MainViewModel.CollectionVM.CurrentCollection.DeleteCodices(toDelete?.Cast<Codex>().ToList() ?? new());
328333
MainViewModel.CollectionVM.FilterVM.ReFilter();
329334
}
@@ -341,6 +346,8 @@ public static void DeleteCodices(IList? toDelete)
341346
public RelayCommand<IList> BanishCodicesCommand => _banishCodicesCommand ??= new(BanishCodices);
342347
public static void BanishCodices(IList? toBanish)
343348
{
349+
if (toBanish == null || toBanish.Count == 0) return;
350+
344351
MainViewModel.CollectionVM.CurrentCollection.BanishCodices(toBanish?.Cast<Codex>().ToList() ?? new());
345352
DeleteCodices(toBanish);
346353
}
@@ -362,6 +369,8 @@ public static async Task StartGetMetaDataProcess(Codex? codex)
362369
}
363370
public static async Task StartGetMetaDataProcess(IList<Codex> codices)
364371
{
372+
if (!codices.Any()) return;
373+
365374
var progressVM = ProgressViewModel.GetInstance();
366375
progressVM.ResetCounter();
367376
progressVM.Text = "Getting MetaData";

0 commit comments

Comments
 (0)