1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-24 06:32:56 +08:00

Add playlist collection button w/ tests

This commit is contained in:
Layendan 2025-02-06 07:25:45 -07:00
parent 129ab03ec4
commit b7483b9442
3 changed files with 182 additions and 0 deletions

View File

@ -0,0 +1,94 @@
// 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 System.Diagnostics;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Graphics;
using osu.Framework.Platform;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Online.Rooms;
using osu.Game.Overlays;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Osu;
using osu.Game.Screens.OnlinePlay.Playlists;
using osuTK;
namespace osu.Game.Tests.Visual.Playlists
{
public partial class TestSceneAddPlaylistToCollectionButton : OsuTestScene
{
private BeatmapManager manager = null!;
private BeatmapSetInfo importedBeatmap = null!;
private Room room = null!;
[BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio)
{
Dependencies.Cache(new RealmRulesetStore(Realm));
Dependencies.Cache(manager = new BeatmapManager(LocalStorage, Realm, API, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm);
}
[Cached(typeof(INotificationOverlay))]
private NotificationOverlay notificationOverlay = new NotificationOverlay
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
};
[SetUpSteps]
public void SetUpSteps()
{
importBeatmap();
setupRoom();
AddStep("create button", () =>
{
AddRange(new Drawable[]
{
notificationOverlay,
new AddPlaylistToCollectionButton(room)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(300, 40),
}
});
});
}
private void importBeatmap() => AddStep("import beatmap", () =>
{
var beatmap = CreateBeatmap(new OsuRuleset().RulesetInfo);
Debug.Assert(beatmap.BeatmapInfo.BeatmapSet != null);
importedBeatmap = manager.Import(beatmap.BeatmapInfo.BeatmapSet)!.Value.Detach();
});
private void setupRoom() => AddStep("setup room", () =>
{
room = new Room
{
Name = "my awesome room",
MaxAttempts = 5,
Host = API.LocalUser.Value
};
room.RecentParticipants = [room.Host];
room.EndDate = DateTimeOffset.Now.AddMinutes(5);
room.Playlist =
[
new PlaylistItem(importedBeatmap.Beatmaps.First())
{
RulesetID = new OsuRuleset().RulesetInfo.OnlineID
}
];
});
}
}

View File

@ -0,0 +1,78 @@
// 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.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Extensions;
using osu.Game.Collections;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Online.Rooms;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
namespace osu.Game.Screens.OnlinePlay.Playlists
{
public partial class AddPlaylistToCollectionButton : RoundedButton
{
private readonly Room room;
[Resolved]
private RealmAccess realmAccess { get; set; } = null!;
[Resolved]
private BeatmapLookupCache beatmapLookupCache { get; set; } = null!;
[Resolved(canBeNull: true)]
private INotificationOverlay? notifications { get; set; }
public AddPlaylistToCollectionButton(Room room)
{
this.room = room;
Text = "Add Maps to Collection";
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
BackgroundColour = colours.Gray5;
Action = () =>
{
int[] ids = room.Playlist.Select(item => item.Beatmap.OnlineID).Where(onlineId => onlineId > 0).ToArray();
if (ids.Length == 0)
{
notifications?.Post(new SimpleErrorNotification { Text = "Cannot add local beatmaps" });
return;
}
beatmapLookupCache.GetBeatmapsAsync(ids).ContinueWith(task => Schedule(() =>
{
var beatmaps = task.GetResultSafely().Where(item => item?.BeatmapSet != null).ToList();
var collection = realmAccess.Realm.All<BeatmapCollection>().FirstOrDefault(c => c.Name == room.Name);
if (collection == null)
{
collection = new BeatmapCollection(room.Name, beatmaps.Select(i => i!.MD5Hash).Distinct().ToList());
realmAccess.Realm.Write(() => realmAccess.Realm.Add(collection));
notifications?.Post(new SimpleNotification { Text = $"Created new playlist: {room.Name}" });
}
else
{
collection.ToLive(realmAccess).PerformWrite(c =>
{
beatmaps = beatmaps.Where(i => !c.BeatmapMD5Hashes.Contains(i!.MD5Hash)).ToList();
foreach (var item in beatmaps)
c.BeatmapMD5Hashes.Add(item!.MD5Hash);
notifications?.Post(new SimpleNotification { Text = $"Updated playlist: {room.Name}" });
});
}
}), TaskContinuationOptions.OnlyOnRanToCompletion);
};
}
}
}

View File

@ -153,11 +153,21 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
}
}
},
new Drawable[]
{
new AddPlaylistToCollectionButton(Room)
{
Margin = new MarginPadding { Top = 5 },
RelativeSizeAxes = Axes.X,
Size = new Vector2(1, 40)
}
}
},
RowDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize),
new Dimension(),
new Dimension(GridSizeMode.AutoSize),
}
},
// Spacer