Skip to content

Commit

Permalink
Merge pull request #28745 from peppy/music-controller-nullability
Browse files Browse the repository at this point in the history
Apply nullability to `MusicController`
  • Loading branch information
peppy authored Jul 5, 2024
2 parents 482ac32 + d21eec9 commit bab1216
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions osu.Game/Overlays/MusicController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// 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.

#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
Expand All @@ -28,7 +25,7 @@ namespace osu.Game.Overlays
public partial class MusicController : CompositeDrawable
{
[Resolved]
private BeatmapManager beatmaps { get; set; }
private BeatmapManager beatmaps { get; set; } = null!;

/// <summary>
/// Point in time after which the current track will be restarted on triggering a "previous track" action.
Expand All @@ -49,25 +46,28 @@ public partial class MusicController : CompositeDrawable
/// Fired when the global <see cref="WorkingBeatmap"/> has changed.
/// Includes direction information for display purposes.
/// </summary>
public event Action<WorkingBeatmap, TrackChangeDirection> TrackChanged;
public event Action<WorkingBeatmap, TrackChangeDirection>? TrackChanged;

[Resolved]
private IBindable<WorkingBeatmap> beatmap { get; set; }
private IBindable<WorkingBeatmap> beatmap { get; set; } = null!;

[Resolved]
private IBindable<IReadOnlyList<Mod>> mods { get; set; }
private IBindable<IReadOnlyList<Mod>> mods { get; set; } = null!;

[NotNull]
public DrawableTrack CurrentTrack { get; private set; } = new DrawableTrack(new TrackVirtual(1000));

[Resolved]
private RealmAccess realm { get; set; }
private RealmAccess realm { get; set; } = null!;

protected override void LoadComplete()
{
base.LoadComplete();

beatmap.BindValueChanged(b => changeBeatmap(b.NewValue), true);
beatmap.BindValueChanged(b =>
{
if (b.NewValue != null)
changeBeatmap(b.NewValue);
}, true);
mods.BindValueChanged(_ => ResetTrackAdjustments(), true);
}

Expand All @@ -76,6 +76,9 @@ protected override void LoadComplete()
/// </summary>
public void ReloadCurrentTrack()
{
if (current == null)
return;

changeTrack();
TrackChanged?.Invoke(current, TrackChangeDirection.None);
}
Expand All @@ -90,7 +93,7 @@ public void ReloadCurrentTrack()
/// </summary>
public bool TrackLoaded => CurrentTrack.TrackLoaded;

private ScheduledDelegate seekDelegate;
private ScheduledDelegate? seekDelegate;

public void SeekTo(double position)
{
Expand Down Expand Up @@ -192,7 +195,7 @@ public bool TogglePause()
/// Play the previous track or restart the current track if it's current time below <see cref="restart_cutoff_point"/>.
/// </summary>
/// <param name="onSuccess">Invoked when the operation has been performed successfully.</param>
public void PreviousTrack(Action<PreviousTrackResult> onSuccess = null) => Schedule(() =>
public void PreviousTrack(Action<PreviousTrackResult>? onSuccess = null) => Schedule(() =>
{
PreviousTrackResult res = prev();
if (res != PreviousTrackResult.None)
Expand All @@ -218,7 +221,7 @@ private PreviousTrackResult prev()

queuedDirection = TrackChangeDirection.Prev;

var playableSet = getBeatmapSets().AsEnumerable().TakeWhile(i => !i.Equals(current.BeatmapSetInfo)).LastOrDefault()
var playableSet = getBeatmapSets().AsEnumerable().TakeWhile(i => !i.Equals(current?.BeatmapSetInfo)).LastOrDefault()
?? getBeatmapSets().LastOrDefault();

if (playableSet != null)
Expand All @@ -236,7 +239,7 @@ private PreviousTrackResult prev()
/// </summary>
/// <param name="onSuccess">Invoked when the operation has been performed successfully.</param>
/// <returns>A <see cref="ScheduledDelegate"/> of the operation.</returns>
public void NextTrack(Action onSuccess = null) => Schedule(() =>
public void NextTrack(Action? onSuccess = null) => Schedule(() =>
{
bool res = next();
if (res)
Expand All @@ -250,7 +253,7 @@ private bool next()

queuedDirection = TrackChangeDirection.Next;

var playableSet = getBeatmapSets().AsEnumerable().SkipWhile(i => !i.Equals(current.BeatmapSetInfo)).ElementAtOrDefault(1)
var playableSet = getBeatmapSets().AsEnumerable().SkipWhile(i => !i.Equals(current?.BeatmapSetInfo)).ElementAtOrDefault(1)
?? getBeatmapSets().FirstOrDefault();

var playableBeatmap = playableSet?.Beatmaps.FirstOrDefault();
Expand All @@ -272,7 +275,7 @@ private void restartTrack()
Schedule(() => CurrentTrack.RestartAsync());
}

private WorkingBeatmap current;
private WorkingBeatmap? current;

private TrackChangeDirection? queuedDirection;

Expand All @@ -289,7 +292,7 @@ private void changeBeatmap(WorkingBeatmap newWorking)

TrackChangeDirection direction = TrackChangeDirection.None;

bool audioEquals = newWorking?.BeatmapInfo?.AudioEquals(current?.BeatmapInfo) == true;
bool audioEquals = newWorking.BeatmapInfo?.AudioEquals(current?.BeatmapInfo) == true;

if (current != null)
{
Expand All @@ -304,7 +307,7 @@ private void changeBeatmap(WorkingBeatmap newWorking)
{
// figure out the best direction based on order in playlist.
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();
int next = getBeatmapSets().AsEnumerable().TakeWhile(b => !b.Equals(newWorking.BeatmapSetInfo)).Count();

direction = last > next ? TrackChangeDirection.Prev : TrackChangeDirection.Next;
}
Expand Down Expand Up @@ -361,7 +364,7 @@ private DrawableTrack getQueuedTrack()
{
// Important to keep this in its own method to avoid inadvertently capturing unnecessary variables in the callback.
// Can lead to leaks.
var queuedTrack = new DrawableTrack(current.LoadTrack());
var queuedTrack = new DrawableTrack(current!.LoadTrack());
queuedTrack.Completed += onTrackCompleted;
return queuedTrack;
}
Expand Down Expand Up @@ -390,7 +393,7 @@ public bool ApplyModTrackAdjustments
}
}

private AudioAdjustments modTrackAdjustments;
private AudioAdjustments? modTrackAdjustments;

/// <summary>
/// Resets the adjustments currently applied on <see cref="CurrentTrack"/> and applies the mod adjustments if <see cref="ApplyModTrackAdjustments"/> is <c>true</c>.
Expand Down

0 comments on commit bab1216

Please sign in to comment.