1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-05 02:43:16 +08:00

Extract common implementation of delete dialog

This commit is contained in:
Bartłomiej Dach 2022-07-23 21:20:27 +02:00
parent 9ac322d337
commit a0d093be5c
No known key found for this signature in database
GPG Key ID: BCECCD4FA41F6497
9 changed files with 105 additions and 137 deletions

View File

@ -3,33 +3,17 @@
using System; using System;
using Humanizer; using Humanizer;
using osu.Framework.Graphics.Sprites;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.Overlays.Dialog; using osu.Game.Overlays.Dialog;
namespace osu.Game.Collections namespace osu.Game.Collections
{ {
public class DeleteCollectionDialog : PopupDialog public class DeleteCollectionDialog : DeleteConfirmationDialog
{ {
public DeleteCollectionDialog(Live<BeatmapCollection> collection, Action deleteAction) public DeleteCollectionDialog(Live<BeatmapCollection> collection, Action deleteAction)
{ {
HeaderText = "Confirm deletion of";
BodyText = collection.PerformRead(c => $"{c.Name} ({"beatmap".ToQuantity(c.BeatmapMD5Hashes.Count)})"); BodyText = collection.PerformRead(c => $"{c.Name} ({"beatmap".ToQuantity(c.BeatmapMD5Hashes.Count)})");
DeleteAction = deleteAction;
Icon = FontAwesome.Regular.TrashAlt;
Buttons = new PopupDialogButton[]
{
new PopupDialogOkButton
{
Text = @"Yes. Go for it.",
Action = deleteAction
},
new PopupDialogCancelButton
{
Text = @"No! Abort mission!",
},
};
} }
} }
} }

View File

@ -0,0 +1,29 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Localisation;
namespace osu.Game.Localisation
{
public static class DeleteConfirmationDialogStrings
{
private const string prefix = @"osu.Game.Resources.Localisation.DeleteConfirmationDialog";
/// <summary>
/// "Confirm deletion of"
/// </summary>
public static LocalisableString HeaderText => new TranslatableString(getKey(@"header_text"), @"Confirm deletion of");
/// <summary>
/// "Yes. Go for it."
/// </summary>
public static LocalisableString Confirm => new TranslatableString(getKey(@"confirm"), @"Yes. Go for it.");
/// <summary>
/// "No! Abort mission"
/// </summary>
public static LocalisableString Cancel => new TranslatableString(getKey(@"cancel"), @"No! Abort mission");
private static string getKey(string key) => $@"{prefix}:{key}";
}
}

View File

@ -0,0 +1,42 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Graphics.Sprites;
using osu.Game.Localisation;
namespace osu.Game.Overlays.Dialog
{
/// <summary>
/// Base class for various confirmation dialogs that concern deletion actions.
/// Differs from <see cref="ConfirmDialog"/> in that the confirmation button is a "dangerous" one
/// (requires the confirm button to be held).
/// </summary>
public abstract class DeleteConfirmationDialog : PopupDialog
{
/// <summary>
/// The action which performs the deletion.
/// </summary>
protected Action? DeleteAction { get; set; }
protected DeleteConfirmationDialog()
{
HeaderText = DeleteConfirmationDialogStrings.HeaderText;
Icon = FontAwesome.Regular.TrashAlt;
Buttons = new PopupDialogButton[]
{
new PopupDialogDangerousButton
{
Text = DeleteConfirmationDialogStrings.Confirm,
Action = () => DeleteAction?.Invoke()
},
new PopupDialogCancelButton
{
Text = DeleteConfirmationDialogStrings.Cancel
}
};
}
}
}

View File

@ -1,34 +1,17 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using System; using System;
using osu.Framework.Graphics.Sprites;
using osu.Game.Overlays.Dialog; using osu.Game.Overlays.Dialog;
namespace osu.Game.Overlays.Settings.Sections.Maintenance namespace osu.Game.Overlays.Settings.Sections.Maintenance
{ {
public class MassDeleteConfirmationDialog : PopupDialog public class MassDeleteConfirmationDialog : DeleteConfirmationDialog
{ {
public MassDeleteConfirmationDialog(Action deleteAction) public MassDeleteConfirmationDialog(Action deleteAction)
{ {
BodyText = "Everything?"; BodyText = "Everything?";
DeleteAction = deleteAction;
Icon = FontAwesome.Regular.TrashAlt;
HeaderText = @"Confirm deletion of";
Buttons = new PopupDialogButton[]
{
new PopupDialogDangerousButton
{
Text = @"Yes. Go for it.",
Action = deleteAction
},
new PopupDialogCancelButton
{
Text = @"No! Abort mission!",
},
};
} }
} }
} }

View File

@ -1,8 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using System; using System;
namespace osu.Game.Overlays.Settings.Sections.Maintenance namespace osu.Game.Overlays.Settings.Sections.Maintenance

View File

@ -1,43 +1,27 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics.Sprites;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Overlays.Dialog; using osu.Game.Overlays.Dialog;
using osu.Game.Scoring; using osu.Game.Scoring;
namespace osu.Game.Screens.Select namespace osu.Game.Screens.Select
{ {
public class BeatmapClearScoresDialog : PopupDialog public class BeatmapClearScoresDialog : DeleteConfirmationDialog
{ {
[Resolved] [Resolved]
private ScoreManager scoreManager { get; set; } private ScoreManager scoreManager { get; set; } = null!;
public BeatmapClearScoresDialog(BeatmapInfo beatmapInfo, Action onCompletion) public BeatmapClearScoresDialog(BeatmapInfo beatmapInfo, Action onCompletion)
{ {
BodyText = beatmapInfo.GetDisplayTitle(); BodyText = $"All local scores on {beatmapInfo.GetDisplayTitle()}";
Icon = FontAwesome.Solid.Eraser; DeleteAction = () =>
HeaderText = @"Clearing all local scores. Are you sure?";
Buttons = new PopupDialogButton[]
{ {
new PopupDialogOkButton Task.Run(() => scoreManager.Delete(beatmapInfo))
{ .ContinueWith(_ => onCompletion);
Text = @"Yes. Please.",
Action = () =>
{
Task.Run(() => scoreManager.Delete(beatmapInfo))
.ContinueWith(_ => onCompletion);
}
},
new PopupDialogCancelButton
{
Text = @"No, I'm still attached.",
},
}; };
} }
} }

View File

@ -1,43 +1,26 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics.Sprites;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Overlays.Dialog; using osu.Game.Overlays.Dialog;
namespace osu.Game.Screens.Select namespace osu.Game.Screens.Select
{ {
public class BeatmapDeleteDialog : PopupDialog public class BeatmapDeleteDialog : DeleteConfirmationDialog
{ {
private BeatmapManager manager; private readonly BeatmapSetInfo beatmapSet;
public BeatmapDeleteDialog(BeatmapSetInfo beatmapSet)
{
this.beatmapSet = beatmapSet;
BodyText = $@"{beatmapSet.Metadata.Artist} - {beatmapSet.Metadata.Title}";
}
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(BeatmapManager beatmapManager) private void load(BeatmapManager beatmapManager)
{ {
manager = beatmapManager; DeleteAction = () => beatmapManager.Delete(beatmapSet);
}
public BeatmapDeleteDialog(BeatmapSetInfo beatmap)
{
BodyText = $@"{beatmap.Metadata.Artist} - {beatmap.Metadata.Title}";
Icon = FontAwesome.Regular.TrashAlt;
HeaderText = @"Confirm deletion of";
Buttons = new PopupDialogButton[]
{
new PopupDialogDangerousButton
{
Text = @"Yes. Totally. Delete it.",
Action = () => manager?.Delete(beatmap),
},
new PopupDialogCancelButton
{
Text = @"Firetruck, I didn't mean to!",
},
};
} }
} }
} }

View File

@ -1,8 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Game.Overlays.Dialog; using osu.Game.Overlays.Dialog;
using osu.Game.Scoring; using osu.Game.Scoring;
@ -12,44 +10,25 @@ using osu.Game.Beatmaps;
namespace osu.Game.Screens.Select namespace osu.Game.Screens.Select
{ {
public class LocalScoreDeleteDialog : PopupDialog public class LocalScoreDeleteDialog : DeleteConfirmationDialog
{ {
private readonly ScoreInfo score; private readonly ScoreInfo score;
[Resolved]
private ScoreManager scoreManager { get; set; }
[Resolved]
private BeatmapManager beatmapManager { get; set; }
public LocalScoreDeleteDialog(ScoreInfo score) public LocalScoreDeleteDialog(ScoreInfo score)
{ {
this.score = score; this.score = score;
Debug.Assert(score != null);
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load(BeatmapManager beatmapManager, ScoreManager scoreManager)
{ {
BeatmapInfo beatmapInfo = beatmapManager.QueryBeatmap(b => b.ID == score.BeatmapInfoID); BeatmapInfo? beatmapInfo = beatmapManager.QueryBeatmap(b => b.ID == score.BeatmapInfoID);
Debug.Assert(beatmapInfo != null); Debug.Assert(beatmapInfo != null);
BodyText = $"{score.User} ({score.DisplayAccuracy}, {score.Rank})"; BodyText = $"{score.User} ({score.DisplayAccuracy}, {score.Rank})";
Icon = FontAwesome.Regular.TrashAlt; Icon = FontAwesome.Regular.TrashAlt;
HeaderText = "Confirm deletion of local score"; DeleteAction = () => scoreManager.Delete(score);
Buttons = new PopupDialogButton[]
{
new PopupDialogDangerousButton
{
Text = "Yes. Please.",
Action = () => scoreManager?.Delete(score)
},
new PopupDialogCancelButton
{
Text = "No, I'm still attached.",
},
};
} }
} }
} }

View File

@ -1,43 +1,29 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics.Sprites;
using osu.Game.Skinning; using osu.Game.Skinning;
using osu.Game.Overlays.Dialog; using osu.Game.Overlays.Dialog;
namespace osu.Game.Screens.Select namespace osu.Game.Screens.Select
{ {
public class SkinDeleteDialog : PopupDialog public class SkinDeleteDialog : DeleteConfirmationDialog
{ {
[Resolved] private readonly Skin skin;
private SkinManager manager { get; set; }
public SkinDeleteDialog(Skin skin) public SkinDeleteDialog(Skin skin)
{ {
this.skin = skin;
BodyText = skin.SkinInfo.Value.Name; BodyText = skin.SkinInfo.Value.Name;
Icon = FontAwesome.Regular.TrashAlt; }
HeaderText = @"Confirm deletion of";
Buttons = new PopupDialogButton[]
{
new PopupDialogDangerousButton
{
Text = @"Yes. Totally. Delete it.",
Action = () =>
{
if (manager == null)
return;
manager.Delete(skin.SkinInfo.Value); [BackgroundDependencyLoader]
manager.CurrentSkinInfo.SetDefault(); private void load(SkinManager manager)
}, {
}, DeleteAction = () =>
new PopupDialogCancelButton {
{ manager.Delete(skin.SkinInfo.Value);
Text = @"Firetruck, I didn't mean to!", manager.CurrentSkinInfo.SetDefault();
},
}; };
} }
} }