mirror of
https://github.com/ppy/osu.git
synced 2025-03-25 18:57:18 +08:00
Add better messaging and handling of edge cases
This commit is contained in:
parent
601e6d8a70
commit
8561df40c5
@ -2,9 +2,9 @@
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Graphics.Cursor;
|
||||
using osu.Framework.Localisation;
|
||||
using osu.Game.Beatmaps;
|
||||
@ -21,13 +21,15 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
|
||||
public partial class AddPlaylistToCollectionButton : RoundedButton, IHasTooltip
|
||||
{
|
||||
private readonly Room room;
|
||||
private readonly Bindable<int> downloadedBeatmapsCount = new Bindable<int>(0);
|
||||
private readonly Bindable<bool> collectionExists = new Bindable<bool>(false);
|
||||
|
||||
private IDisposable? beatmapSubscription;
|
||||
private IDisposable? collectionSubscription;
|
||||
|
||||
private Live<BeatmapCollection>? collection;
|
||||
private HashSet<string> localBeatmapHashes = new HashSet<string>();
|
||||
|
||||
[Resolved]
|
||||
private RealmAccess realmAccess { get; set; } = null!;
|
||||
private RealmAccess realm { get; set; } = null!;
|
||||
|
||||
[Resolved(canBeNull: true)]
|
||||
private INotificationOverlay? notifications { get; set; }
|
||||
@ -45,29 +47,29 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
|
||||
if (room.Playlist.Count == 0)
|
||||
return;
|
||||
|
||||
var beatmaps = getBeatmapsForPlaylist(realmAccess.Realm).ToArray();
|
||||
var beatmaps = getBeatmapsForPlaylist(realm.Realm).ToArray();
|
||||
|
||||
var collection = realmAccess.Realm.All<BeatmapCollection>().FirstOrDefault(c => c.Name == room.Name);
|
||||
int countBefore = 0;
|
||||
int countAfter = 0;
|
||||
|
||||
if (collection == null)
|
||||
collection ??= realm.Realm.Write(() => realm.Realm.Add(new BeatmapCollection(room.Name)).ToLive(realm));
|
||||
collection.PerformWrite(c =>
|
||||
{
|
||||
collection = new BeatmapCollection(room.Name);
|
||||
realmAccess.Realm.Write(() => realmAccess.Realm.Add(collection));
|
||||
notifications?.Post(new SimpleNotification { Text = $"Created new collection: {room.Name}" });
|
||||
}
|
||||
else
|
||||
{
|
||||
notifications?.Post(new SimpleNotification { Text = $"Updated collection: {room.Name}" });
|
||||
}
|
||||
countBefore = c.BeatmapMD5Hashes.Count;
|
||||
|
||||
collection.ToLive(realmAccess).PerformWrite(c =>
|
||||
{
|
||||
foreach (var item in beatmaps)
|
||||
{
|
||||
if (!c.BeatmapMD5Hashes.Contains(item.MD5Hash))
|
||||
c.BeatmapMD5Hashes.Add(item.MD5Hash);
|
||||
}
|
||||
|
||||
countAfter = c.BeatmapMD5Hashes.Count;
|
||||
});
|
||||
|
||||
if (countBefore == 0)
|
||||
notifications?.Post(new SimpleNotification { Text = $"Created new collection \"{room.Name}\" with {countAfter} beatmaps." });
|
||||
else
|
||||
notifications?.Post(new SimpleNotification { Text = $"Added {countAfter - countBefore} beatmaps to collection \"{room.Name}\"." });
|
||||
};
|
||||
}
|
||||
|
||||
@ -75,16 +77,53 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
|
||||
{
|
||||
base.LoadComplete();
|
||||
|
||||
if (room.Playlist.Count > 0)
|
||||
Enabled.Value = false;
|
||||
|
||||
if (room.Playlist.Count == 0)
|
||||
return;
|
||||
|
||||
beatmapSubscription = realm.RegisterForNotifications(getBeatmapsForPlaylist, (sender, _) =>
|
||||
{
|
||||
beatmapSubscription =
|
||||
realmAccess.RegisterForNotifications(getBeatmapsForPlaylist, (sender, _) => downloadedBeatmapsCount.Value = sender.Count);
|
||||
}
|
||||
localBeatmapHashes = sender.Select(b => b.MD5Hash).ToHashSet();
|
||||
Schedule(updateButtonState);
|
||||
});
|
||||
|
||||
collectionSubscription = realmAccess.RegisterForNotifications(r => r.All<BeatmapCollection>().Where(c => c.Name == room.Name), (sender, _) => collectionExists.Value = sender.Any());
|
||||
collectionSubscription = realm.RegisterForNotifications(r => r.All<BeatmapCollection>().Where(c => c.Name == room.Name), (sender, _) =>
|
||||
{
|
||||
collection = sender.FirstOrDefault()?.ToLive(realm);
|
||||
Schedule(updateButtonState);
|
||||
});
|
||||
}
|
||||
|
||||
downloadedBeatmapsCount.BindValueChanged(_ => updateButtonText());
|
||||
collectionExists.BindValueChanged(_ => updateButtonText(), true);
|
||||
private void updateButtonState()
|
||||
{
|
||||
int countToAdd = getCountToBeAdded();
|
||||
|
||||
if (collection == null)
|
||||
Text = $"Create new collection with {countToAdd} beatmaps";
|
||||
else
|
||||
Text = $"Update collection with {countToAdd} beatmaps";
|
||||
|
||||
Enabled.Value = countToAdd > 0;
|
||||
}
|
||||
|
||||
private int getCountToBeAdded()
|
||||
{
|
||||
if (collection == null)
|
||||
return localBeatmapHashes.Count;
|
||||
|
||||
return collection.PerformRead(c =>
|
||||
{
|
||||
int count = localBeatmapHashes.Count;
|
||||
|
||||
foreach (string hash in localBeatmapHashes)
|
||||
{
|
||||
if (c.BeatmapMD5Hashes.Contains(hash))
|
||||
count--;
|
||||
}
|
||||
|
||||
return count;
|
||||
});
|
||||
}
|
||||
|
||||
private IQueryable<BeatmapInfo> getBeatmapsForPlaylist(Realm r)
|
||||
@ -92,14 +131,6 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
|
||||
return r.All<BeatmapInfo>().Filter(string.Join(" OR ", room.Playlist.Select(item => $"(OnlineID == {item.Beatmap.OnlineID})").Distinct()));
|
||||
}
|
||||
|
||||
private void updateButtonText()
|
||||
{
|
||||
if (!collectionExists.Value)
|
||||
Text = $"Create new collection with {downloadedBeatmapsCount.Value} beatmaps";
|
||||
else
|
||||
Text = $"Update collection with {downloadedBeatmapsCount.Value} beatmaps";
|
||||
}
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
{
|
||||
base.Dispose(isDisposing);
|
||||
@ -108,6 +139,19 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
|
||||
collectionSubscription?.Dispose();
|
||||
}
|
||||
|
||||
public LocalisableString TooltipText => "Only downloaded beatmaps will be added to the collection";
|
||||
public LocalisableString TooltipText
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Enabled.Value)
|
||||
return string.Empty;
|
||||
|
||||
int currentCollectionCount = collection?.PerformRead(c => c.BeatmapMD5Hashes.Count) ?? 0;
|
||||
if (room.Playlist.DistinctBy(i => i.Beatmap.OnlineID).Count() == currentCollectionCount)
|
||||
return "All beatmaps have been added!";
|
||||
|
||||
return "Download some beatmaps first.";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user