2025-02-06 22:25:45 +08:00
|
|
|
|
// 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.
|
|
|
|
|
|
2025-02-10 09:45:13 +08:00
|
|
|
|
using System;
|
2025-02-06 22:25:45 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Framework.Allocation;
|
2025-02-10 09:45:13 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2025-02-14 12:53:42 +08:00
|
|
|
|
using osu.Framework.Graphics.Cursor;
|
|
|
|
|
using osu.Framework.Localisation;
|
2025-02-07 18:13:51 +08:00
|
|
|
|
using osu.Game.Beatmaps;
|
2025-02-06 22:25:45 +08:00
|
|
|
|
using osu.Game.Collections;
|
|
|
|
|
using osu.Game.Database;
|
|
|
|
|
using osu.Game.Graphics.UserInterfaceV2;
|
|
|
|
|
using osu.Game.Online.Rooms;
|
|
|
|
|
using osu.Game.Overlays;
|
|
|
|
|
using osu.Game.Overlays.Notifications;
|
2025-02-07 18:13:51 +08:00
|
|
|
|
using Realms;
|
2025-02-06 22:25:45 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.OnlinePlay.Playlists
|
|
|
|
|
{
|
2025-02-14 12:53:42 +08:00
|
|
|
|
public partial class AddPlaylistToCollectionButton : RoundedButton, IHasTooltip
|
2025-02-06 22:25:45 +08:00
|
|
|
|
{
|
|
|
|
|
private readonly Room room;
|
2025-02-10 09:45:13 +08:00
|
|
|
|
private readonly Bindable<int> downloadedBeatmapsCount = new Bindable<int>(0);
|
|
|
|
|
private readonly Bindable<bool> collectionExists = new Bindable<bool>(false);
|
|
|
|
|
private IDisposable? beatmapSubscription;
|
|
|
|
|
private IDisposable? collectionSubscription;
|
2025-02-06 22:25:45 +08:00
|
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
|
private RealmAccess realmAccess { get; set; } = null!;
|
|
|
|
|
|
|
|
|
|
[Resolved(canBeNull: true)]
|
|
|
|
|
private INotificationOverlay? notifications { get; set; }
|
|
|
|
|
|
|
|
|
|
public AddPlaylistToCollectionButton(Room room)
|
|
|
|
|
{
|
|
|
|
|
this.room = room;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2025-02-07 18:13:51 +08:00
|
|
|
|
private void load()
|
2025-02-06 22:25:45 +08:00
|
|
|
|
{
|
|
|
|
|
Action = () =>
|
|
|
|
|
{
|
2025-02-07 19:04:29 +08:00
|
|
|
|
if (room.Playlist.Count == 0)
|
2025-02-06 22:25:45 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2025-02-14 12:53:42 +08:00
|
|
|
|
var beatmaps = getBeatmapsForPlaylist(realmAccess.Realm).ToArray();
|
2025-02-06 22:25:45 +08:00
|
|
|
|
|
2025-02-07 18:13:51 +08:00
|
|
|
|
var collection = realmAccess.Realm.All<BeatmapCollection>().FirstOrDefault(c => c.Name == room.Name);
|
2025-02-06 22:25:45 +08:00
|
|
|
|
|
2025-02-07 18:13:51 +08:00
|
|
|
|
if (collection == null)
|
|
|
|
|
{
|
2025-02-14 12:53:42 +08:00
|
|
|
|
collection = new BeatmapCollection(room.Name);
|
2025-02-07 18:13:51 +08:00
|
|
|
|
realmAccess.Realm.Write(() => realmAccess.Realm.Add(collection));
|
|
|
|
|
notifications?.Post(new SimpleNotification { Text = $"Created new collection: {room.Name}" });
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-02-14 12:53:42 +08:00
|
|
|
|
notifications?.Post(new SimpleNotification { Text = $"Updated collection: {room.Name}" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
collection.ToLive(realmAccess).PerformWrite(c =>
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in beatmaps)
|
2025-02-06 22:25:45 +08:00
|
|
|
|
{
|
2025-02-14 12:53:42 +08:00
|
|
|
|
if (!c.BeatmapMD5Hashes.Contains(item.MD5Hash))
|
2025-02-07 18:13:51 +08:00
|
|
|
|
c.BeatmapMD5Hashes.Add(item.MD5Hash);
|
2025-02-14 12:53:42 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2025-02-06 22:25:45 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
2025-02-10 09:45:13 +08:00
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
2025-02-10 14:27:28 +08:00
|
|
|
|
if (room.Playlist.Count > 0)
|
2025-02-14 12:53:42 +08:00
|
|
|
|
{
|
|
|
|
|
beatmapSubscription =
|
|
|
|
|
realmAccess.RegisterForNotifications(getBeatmapsForPlaylist, (sender, _) => downloadedBeatmapsCount.Value = sender.Count);
|
|
|
|
|
}
|
2025-02-10 09:45:13 +08:00
|
|
|
|
|
2025-02-14 12:53:42 +08:00
|
|
|
|
collectionSubscription = realmAccess.RegisterForNotifications(r => r.All<BeatmapCollection>().Where(c => c.Name == room.Name), (sender, _) => collectionExists.Value = sender.Any());
|
2025-02-10 09:45:13 +08:00
|
|
|
|
|
2025-02-14 12:53:42 +08:00
|
|
|
|
downloadedBeatmapsCount.BindValueChanged(_ => updateButtonText());
|
|
|
|
|
collectionExists.BindValueChanged(_ => updateButtonText(), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IQueryable<BeatmapInfo> getBeatmapsForPlaylist(Realm r)
|
|
|
|
|
{
|
|
|
|
|
return r.All<BeatmapInfo>().Filter(string.Join(" OR ", room.Playlist.Select(item => $"(OnlineID == {item.Beatmap.OnlineID})").Distinct()));
|
|
|
|
|
}
|
2025-02-10 09:45:13 +08:00
|
|
|
|
|
2025-02-14 12:53:42 +08:00
|
|
|
|
private void updateButtonText()
|
|
|
|
|
{
|
|
|
|
|
if (!collectionExists.Value)
|
|
|
|
|
Text = $"Create new collection with {downloadedBeatmapsCount.Value} beatmaps";
|
|
|
|
|
else
|
|
|
|
|
Text = $"Update collection with {downloadedBeatmapsCount.Value} beatmaps";
|
2025-02-10 09:45:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
|
|
beatmapSubscription?.Dispose();
|
|
|
|
|
collectionSubscription?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-14 12:53:42 +08:00
|
|
|
|
public LocalisableString TooltipText => "Only downloaded beatmaps will be added to the collection";
|
2025-02-06 22:25:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|