Implement button to delete all beatmap videos

This commit is contained in:
Noah M 2022-05-18 01:09:58 -05:00
parent 02198d0436
commit 69351d2cdf
5 changed files with 76 additions and 0 deletions

View File

@ -319,6 +319,18 @@ public void Delete(Expression<Func<BeatmapSetInfo, bool>>? filter = null, bool s
}); });
} }
public void DeleteVideos(Expression<Func<BeatmapSetInfo, bool>>? filter = null, bool silent = false)
{
realm.Write(r =>
{
var items = r.All<BeatmapSetInfo>().Where(s => !s.DeletePending && !s.Protected);
if (filter != null)
items = items.Where(filter);
beatmapModelManager.DeleteVideos(items.ToList(), silent);
});
}
public void UndeleteAll() public void UndeleteAll()
{ {
realm.Run(r => beatmapModelManager.Undelete(r.All<BeatmapSetInfo>().Where(s => s.DeletePending).ToList())); realm.Run(r => beatmapModelManager.Undelete(r.All<BeatmapSetInfo>().Where(s => s.DeletePending).ToList()));

View File

@ -29,6 +29,11 @@ public static class MaintenanceSettingsStrings
/// </summary> /// </summary>
public static LocalisableString DeleteAllBeatmaps => new TranslatableString(getKey(@"delete_all_beatmaps"), @"Delete ALL beatmaps"); public static LocalisableString DeleteAllBeatmaps => new TranslatableString(getKey(@"delete_all_beatmaps"), @"Delete ALL beatmaps");
/// <summary>
/// "Delete ALL beatmap videos"
/// </summary>
public static LocalisableString DeleteAllBeatmapVideos => new TranslatableString(getKey(@"delete_all_beatmap_videos"), @"Delete ALL beatmap videos");
/// <summary> /// <summary>
/// "Import scores from stable" /// "Import scores from stable"
/// </summary> /// </summary>

View File

@ -28,6 +28,7 @@ public class GeneralSettings : SettingsSubsection
private SettingsButton deleteSkinsButton; private SettingsButton deleteSkinsButton;
private SettingsButton restoreButton; private SettingsButton restoreButton;
private SettingsButton undeleteButton; private SettingsButton undeleteButton;
private SettingsButton deleteBeatmapVideosButton;
[BackgroundDependencyLoader(permitNulls: true)] [BackgroundDependencyLoader(permitNulls: true)]
private void load(BeatmapManager beatmaps, ScoreManager scores, SkinManager skins, [CanBeNull] CollectionManager collectionManager, [CanBeNull] LegacyImportManager legacyImportManager, IDialogOverlay dialogOverlay) private void load(BeatmapManager beatmaps, ScoreManager scores, SkinManager skins, [CanBeNull] CollectionManager collectionManager, [CanBeNull] LegacyImportManager legacyImportManager, IDialogOverlay dialogOverlay)
@ -58,6 +59,19 @@ private void load(BeatmapManager beatmaps, ScoreManager scores, SkinManager skin
} }
}); });
Add(deleteBeatmapVideosButton = new DangerousSettingsButton
{
Text = MaintenanceSettingsStrings.DeleteAllBeatmapVideos,
Action = () =>
{
dialogOverlay?.Push(new MassVideoDeleteConfirmationDialog(() =>
{
deleteBeatmapVideosButton.Enabled.Value = false;
Task.Run(() => beatmaps.DeleteVideos()).ContinueWith(t => Schedule(() => deleteBeatmapsButton.Enabled.Value = true));
}));
}
});
if (legacyImportManager?.SupportsImportFromStable == true) if (legacyImportManager?.SupportsImportFromStable == true)
{ {
Add(importScoresButton = new SettingsButton Add(importScoresButton = new SettingsButton

View File

@ -29,4 +29,11 @@ public MassDeleteConfirmationDialog(Action deleteAction)
}; };
} }
} }
public class MassVideoDeleteConfirmationDialog : MassDeleteConfirmationDialog
{
public MassVideoDeleteConfirmationDialog(Action deleteAction) : base(deleteAction)
{
BodyText = "All beatmap videos? This cannot be undone!";
}
}
} }

View File

@ -132,6 +132,44 @@ public void Delete(List<TModel> items, bool silent = false)
notification.State = ProgressNotificationState.Completed; notification.State = ProgressNotificationState.Completed;
} }
/// <summary>
/// Delete videos from a list of items.
/// This will post notifications tracking progress.
/// </summary>
public void DeleteVideos(List<TModel> items, bool silent = false)
{
if (items.Count == 0) return;
var notification = new ProgressNotification
{
Progress = 0,
Text = $"Preparing to delete all {HumanisedModelName} videos...",
CompletionText = $"Deleted all {HumanisedModelName} videos!",
State = ProgressNotificationState.Active,
};
if (!silent)
PostNotification?.Invoke(notification);
int i = 0;
foreach (var b in items)
{
if (notification.State == ProgressNotificationState.Cancelled)
// user requested abort
return;
notification.Text = $"Deleting videos from {HumanisedModelName}s ({++i} of {items.Count})";
var video = b.Files.FirstOrDefault(f => f.Filename.EndsWith(".mp4") || f.Filename.EndsWith(".avi") || f.Filename.EndsWith(".mov") || f.Filename.EndsWith(".flv"));
if (video != null)
DeleteFile(b, video);
notification.Progress = (float)i / items.Count;
}
notification.State = ProgressNotificationState.Completed;
}
/// <summary> /// <summary>
/// Restore multiple items that were previously deleted. /// Restore multiple items that were previously deleted.
/// This will post notifications tracking progress. /// This will post notifications tracking progress.