1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 16:52:54 +08:00

Completely remove subscription from MusicController

This commit is contained in:
Dean Herbert 2022-01-22 04:27:07 +09:00
parent 83b0e4572a
commit 1a776a9587
2 changed files with 16 additions and 62 deletions

View File

@ -34,13 +34,13 @@ namespace osu.Game.Overlays.Music
private BeatmapManager beatmaps { get; set; }
[Resolved]
private RealmContextFactory realmFactory { get; set; }
private RealmAccess realm { get; set; }
private IDisposable beatmapSubscription;
private IQueryable<BeatmapSetInfo> availableBeatmaps => realmFactory.Context
.All<BeatmapSetInfo>()
.Where(s => !s.DeletePending);
private IQueryable<BeatmapSetInfo> availableBeatmaps => realm.Realm
.All<BeatmapSetInfo>()
.Where(s => !s.DeletePending);
private FilterControl filter;
private Playlist list;
@ -105,7 +105,7 @@ namespace osu.Game.Overlays.Music
// tests might bind externally, in which case we don't want to involve realm.
if (beatmapSets.Count == 0)
beatmapSubscription = realmFactory.RegisterForNotifications(realm => availableBeatmaps, beatmapsChanged);
beatmapSubscription = realm.RegisterForNotifications(realm => availableBeatmaps, beatmapsChanged);
list.Items.BindTo(beatmapSets);
beatmap.BindValueChanged(working => list.SelectedSet.Value = working.NewValue.BeatmapSetInfo, true);

View File

@ -16,7 +16,6 @@ using osu.Framework.Threading;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Rulesets.Mods;
using Realms;
namespace osu.Game.Overlays
{
@ -25,8 +24,6 @@ namespace osu.Game.Overlays
/// </summary>
public class MusicController : CompositeDrawable
{
private IDisposable beatmapSubscription;
[Resolved]
private BeatmapManager beatmaps { get; set; }
@ -35,8 +32,6 @@ namespace osu.Game.Overlays
/// </summary>
private const double restart_cutoff_point = 5000;
private readonly BindableList<BeatmapSetInfo> beatmapSets = new BindableList<BeatmapSetInfo>();
/// <summary>
/// Whether the user has requested the track to be paused. Use <see cref="IsPlaying"/> to determine whether the track is still playing.
/// </summary>
@ -69,50 +64,11 @@ namespace osu.Game.Overlays
mods.BindValueChanged(_ => ResetTrackAdjustments(), true);
}
private IQueryable<BeatmapSetInfo> queryRealmBeatmapSets() =>
realm.Realm
.All<BeatmapSetInfo>()
.Where(s => !s.DeletePending);
protected override void LoadComplete()
{
base.LoadComplete();
beatmapSubscription = realm.RegisterForNotifications(r => queryRealmBeatmapSets(), beatmapsChanged);
}
private void beatmapsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet changes, Exception error)
{
if (changes == null)
{
beatmapSets.Clear();
foreach (var s in sender)
beatmapSets.Add(s);
return;
}
foreach (int i in changes.InsertedIndices)
beatmapSets.Insert(i, sender[i]);
foreach (int i in changes.DeletedIndices.OrderByDescending(i => i))
beatmapSets.RemoveAt(i);
}
/// <summary>
/// Forcefully reload the current <see cref="WorkingBeatmap"/>'s track from disk.
/// </summary>
public void ReloadCurrentTrack() => changeTrack();
/// <summary>
/// Change the position of a <see cref="BeatmapSetInfo"/> in the current playlist.
/// </summary>
/// <param name="beatmapSetInfo">The beatmap to move.</param>
/// <param name="index">The new position.</param>
public void ChangeBeatmapSetPosition(BeatmapSetInfo beatmapSetInfo, int index)
{
beatmapSets.Remove(beatmapSetInfo);
beatmapSets.Insert(index, beatmapSetInfo);
}
/// <summary>
/// Returns whether the beatmap track is playing.
/// </summary>
@ -238,11 +194,12 @@ namespace osu.Game.Overlays
queuedDirection = TrackChangeDirection.Prev;
var playable = beatmapSets.TakeWhile(i => !i.Equals(current.BeatmapSetInfo)).LastOrDefault() ?? beatmapSets.LastOrDefault();
var playableSet = getBeatmapSets().AsEnumerable().TakeWhile(i => !i.Equals(current.BeatmapSetInfo)).LastOrDefault()
?? getBeatmapSets().LastOrDefault();
if (playable != null)
if (playableSet != null)
{
changeBeatmap(beatmaps.GetWorkingBeatmap(playable.Beatmaps.First()));
changeBeatmap(beatmaps.GetWorkingBeatmap(playableSet.Beatmaps.First()));
restartTrack();
return PreviousTrackResult.Previous;
}
@ -269,7 +226,9 @@ namespace osu.Game.Overlays
queuedDirection = TrackChangeDirection.Next;
var playableSet = beatmapSets.SkipWhile(i => i.ID != current.BeatmapSetInfo.ID).ElementAtOrDefault(1) ?? beatmapSets.FirstOrDefault();
var playableSet = getBeatmapSets().AsEnumerable().SkipWhile(i => !i.Equals(current.BeatmapSetInfo)).ElementAtOrDefault(1)
?? getBeatmapSets().FirstOrDefault();
var playableBeatmap = playableSet?.Beatmaps.FirstOrDefault();
if (playableBeatmap != null)
@ -293,6 +252,8 @@ namespace osu.Game.Overlays
private TrackChangeDirection? queuedDirection;
private IQueryable<BeatmapSetInfo> getBeatmapSets() => realm.Realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending);
private void beatmapChanged(ValueChangedEvent<WorkingBeatmap> beatmap) => changeBeatmap(beatmap.NewValue);
private void changeBeatmap(WorkingBeatmap newWorking)
@ -320,8 +281,8 @@ namespace osu.Game.Overlays
else
{
// figure out the best direction based on order in playlist.
int last = beatmapSets.TakeWhile(b => !b.Equals(current.BeatmapSetInfo)).Count();
int next = newWorking == null ? -1 : beatmapSets.TakeWhile(b => !b.Equals(newWorking.BeatmapSetInfo)).Count();
int last = getBeatmapSets().AsEnumerable().TakeWhile(b => !b.Equals(current.BeatmapSetInfo)).Count();
int next = newWorking == null ? -1 : getBeatmapSets().AsEnumerable().TakeWhile(b => !b.Equals(newWorking.BeatmapSetInfo)).Count();
direction = last > next ? TrackChangeDirection.Prev : TrackChangeDirection.Next;
}
@ -435,13 +396,6 @@ namespace osu.Game.Overlays
mod.ApplyToTrack(CurrentTrack);
}
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
beatmapSubscription?.Dispose();
}
}
public enum TrackChangeDirection