1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-17 05:13:21 +08:00

Move URL implementation to extension methods and share with other usages

This commit is contained in:
Dean Herbert 2024-08-22 02:08:01 +09:00
parent 18a3ab2ffd
commit 87123d99bf
No known key found for this signature in database
5 changed files with 52 additions and 12 deletions

View File

@ -2,6 +2,8 @@
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Localisation;
using osu.Game.Online.API;
using osu.Game.Rulesets;
using osu.Game.Screens.Select;
namespace osu.Game.Beatmaps
@ -48,5 +50,16 @@ namespace osu.Game.Beatmaps
}
private static string getVersionString(IBeatmapInfo beatmapInfo) => string.IsNullOrEmpty(beatmapInfo.DifficultyName) ? string.Empty : $"[{beatmapInfo.DifficultyName}]";
/// <summary>
/// Get the beatmap info page URL, or <c>null</c> if unavailable.
/// </summary>
public static string? GetOnlineURL(this IBeatmapInfo beatmapInfo, IAPIProvider api, IRulesetInfo? ruleset = null)
{
if (beatmapInfo.OnlineID <= 0 || beatmapInfo.BeatmapSet == null)
return null;
return $@"{api.WebsiteRootUrl}/beatmapsets/{beatmapInfo.BeatmapSet.OnlineID}#{ruleset?.ShortName ?? beatmapInfo.Ruleset.ShortName}/{beatmapInfo.OnlineID}";
}
}
}

View File

@ -6,6 +6,8 @@ using System.Linq;
using osu.Game.Database;
using osu.Game.Extensions;
using osu.Game.Models;
using osu.Game.Online.API;
using osu.Game.Rulesets;
namespace osu.Game.Beatmaps
{
@ -29,5 +31,19 @@ namespace osu.Game.Beatmaps
/// <param name="filename">The name of the file to get the storage path of.</param>
public static RealmNamedFileUsage? GetFile(this IHasRealmFiles model, string filename) =>
model.Files.SingleOrDefault(f => string.Equals(f.Filename, filename, StringComparison.OrdinalIgnoreCase));
/// <summary>
/// Get the beatmapset info page URL, or <c>null</c> if unavailable.
/// </summary>
public static string? GetOnlineURL(this IBeatmapSetInfo beatmapSetInfo, IAPIProvider api, IRulesetInfo? ruleset = null)
{
if (beatmapSetInfo.OnlineID <= 0)
return null;
if (ruleset != null)
return $@"{api.WebsiteRootUrl}/beatmapsets/{beatmapSetInfo.OnlineID}#{ruleset.ShortName}";
return $@"{api.WebsiteRootUrl}/beatmapsets/{beatmapSetInfo.OnlineID}";
}
}
}

View File

@ -200,7 +200,8 @@ namespace osu.Game.Overlays.BeatmapSet
private void updateExternalLink()
{
if (externalLink != null) externalLink.Link = $@"{api.WebsiteRootUrl}/beatmapsets/{BeatmapSet.Value?.OnlineID}#{Picker.Beatmap.Value?.Ruleset.ShortName}/{Picker.Beatmap.Value?.OnlineID}";
if (externalLink != null)
externalLink.Link = Picker.Beatmap.Value?.GetOnlineURL(api) ?? BeatmapSet.Value?.GetOnlineURL(api);
}
[BackgroundDependencyLoader]

View File

@ -55,7 +55,6 @@ namespace osu.Game.Screens.Select.Carousel
private Action<BeatmapInfo>? selectRequested;
private Action<BeatmapInfo>? hideRequested;
private Action? copyBeatmapSetUrl;
private Triangles triangles = null!;
@ -82,6 +81,12 @@ namespace osu.Game.Screens.Select.Carousel
[Resolved]
private IBindable<IReadOnlyList<Mod>> mods { get; set; } = null!;
[Resolved]
private Clipboard clipboard { get; set; } = null!;
[Resolved]
private IAPIProvider api { get; set; } = null!;
private IBindable<StarDifficulty?> starDifficultyBindable = null!;
private CancellationTokenSource? starDifficultyCancellationSource;
@ -92,7 +97,7 @@ namespace osu.Game.Screens.Select.Carousel
}
[BackgroundDependencyLoader]
private void load(BeatmapManager? manager, SongSelect? songSelect, Clipboard clipboard, IAPIProvider api)
private void load(BeatmapManager? manager, SongSelect? songSelect, IAPIProvider api)
{
Header.Height = height;
@ -105,9 +110,6 @@ namespace osu.Game.Screens.Select.Carousel
if (manager != null)
hideRequested = manager.Hide;
if (beatmapInfo.BeatmapSet != null)
copyBeatmapSetUrl += () => clipboard.SetText($@"{api.WebsiteRootUrl}/beatmapsets/{beatmapInfo.BeatmapSet.OnlineID}#{beatmapInfo.Ruleset.ShortName}/{beatmapInfo.OnlineID}");
Header.Children = new Drawable[]
{
background = new Box
@ -294,7 +296,8 @@ namespace osu.Game.Screens.Select.Carousel
items.Add(new OsuMenuItem("Collections") { Items = collectionItems });
items.Add(new OsuMenuItem("Copy link", MenuItemType.Standard, () => copyBeatmapSetUrl?.Invoke()));
if (beatmapInfo.GetOnlineURL(api) is string url)
items.Add(new OsuMenuItem("Copy link", MenuItemType.Standard, () => clipboard.SetText(url)));
if (hideRequested != null)
items.Add(new OsuMenuItem(CommonStrings.ButtonsHide.ToSentence(), MenuItemType.Destructive, () => hideRequested(beatmapInfo)));

View File

@ -33,7 +33,6 @@ namespace osu.Game.Screens.Select.Carousel
private Action<BeatmapSetInfo> restoreHiddenRequested = null!;
private Action<int>? viewDetails;
private Action? copyBeatmapSetUrl;
[Resolved]
private IDialogOverlay? dialogOverlay { get; set; }
@ -44,6 +43,15 @@ namespace osu.Game.Screens.Select.Carousel
[Resolved]
private RealmAccess realm { get; set; } = null!;
[Resolved]
private Clipboard clipboard { get; set; } = null!;
[Resolved]
private IAPIProvider api { get; set; } = null!;
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; } = null!;
public IEnumerable<DrawableCarouselItem> DrawableBeatmaps => beatmapContainer?.IsLoaded != true ? Enumerable.Empty<DrawableCarouselItem>() : beatmapContainer.AliveChildren;
private Container<DrawableCarouselItem>? beatmapContainer;
@ -70,7 +78,7 @@ namespace osu.Game.Screens.Select.Carousel
}
[BackgroundDependencyLoader]
private void load(BeatmapSetOverlay? beatmapOverlay, SongSelect? songSelect, Clipboard clipboard, IBindable<RulesetInfo> ruleset, IAPIProvider api)
private void load(BeatmapSetOverlay? beatmapOverlay, SongSelect? songSelect)
{
if (songSelect != null)
mainMenuItems = songSelect.CreateForwardNavigationMenuItemsForBeatmap(() => (((CarouselBeatmapSet)Item!).GetNextToSelect() as CarouselBeatmap)!.BeatmapInfo);
@ -83,8 +91,6 @@ namespace osu.Game.Screens.Select.Carousel
if (beatmapOverlay != null)
viewDetails = beatmapOverlay.FetchAndShowBeatmapSet;
copyBeatmapSetUrl += () => clipboard.SetText($@"{api.WebsiteRootUrl}/beatmapsets/{beatmapSet.OnlineID}#{ruleset.Value.ShortName}");
}
protected override void Update()
@ -294,7 +300,8 @@ namespace osu.Game.Screens.Select.Carousel
if (beatmapSet.Beatmaps.Any(b => b.Hidden))
items.Add(new OsuMenuItem("Restore all hidden", MenuItemType.Standard, () => restoreHiddenRequested(beatmapSet)));
items.Add(new OsuMenuItem("Copy link", MenuItemType.Standard, () => copyBeatmapSetUrl?.Invoke()));
if (beatmapSet.GetOnlineURL(api, ruleset.Value) is string url)
items.Add(new OsuMenuItem("Copy link", MenuItemType.Standard, () => clipboard.SetText(url)));
if (dialogOverlay != null)
items.Add(new OsuMenuItem("Delete...", MenuItemType.Destructive, () => dialogOverlay.Push(new BeatmapDeleteDialog(beatmapSet))));