Make beatmap download buttons inherit BeatmapDownloadTrackingComposite

- Move DownloadTrackingComposite into the online namespace
This commit is contained in:
naoey
2019-06-11 23:01:01 +05:30
parent 4a6074865e
commit ab27d82cd5
8 changed files with 55 additions and 32 deletions

View File

@ -11,6 +11,7 @@ using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Online;
using osu.Game.Online.API;
using osu.Game.Overlays.Direct;
using osu.Game.Users;
@ -19,7 +20,7 @@ using osuTK.Graphics;
namespace osu.Game.Overlays.BeatmapSet.Buttons
{
public class DownloadButton : DownloadTrackingComposite, IHasTooltip
public class DownloadButton : BeatmapDownloadTrackingComposite, IHasTooltip
{
private readonly bool noVideo;

View File

@ -13,6 +13,7 @@ using osu.Game.Beatmaps.Drawables;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online;
using osu.Game.Overlays.BeatmapSet.Buttons;
using osu.Game.Overlays.Direct;
using osuTK;
@ -21,7 +22,7 @@ using DownloadButton = osu.Game.Overlays.BeatmapSet.Buttons.DownloadButton;
namespace osu.Game.Overlays.BeatmapSet
{
public class Header : DownloadTrackingComposite
public class Header : BeatmapDownloadTrackingComposite
{
private const float transition_duration = 200;
private const float tabs_height = 50;

View File

@ -0,0 +1,16 @@
using osu.Framework.Bindables;
using osu.Game.Beatmaps;
using osu.Game.Online;
namespace osu.Game.Overlays.Direct
{
public abstract class BeatmapDownloadTrackingComposite : DownloadTrackingComposite<BeatmapSetInfo, BeatmapManager>
{
public Bindable<BeatmapSetInfo> BeatmapSet => ModelInfo;
public BeatmapDownloadTrackingComposite(BeatmapSetInfo set = null)
: base(set)
{
}
}
}

View File

@ -9,11 +9,12 @@ using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online;
using osuTK;
namespace osu.Game.Overlays.Direct
{
public class DownloadButton : DownloadTrackingComposite
public class DownloadButton : BeatmapDownloadTrackingComposite
{
private readonly bool noVideo;
private readonly SpriteIcon icon;

View File

@ -7,11 +7,12 @@ using osu.Framework.Graphics;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online;
using osuTK.Graphics;
namespace osu.Game.Overlays.Direct
{
public class DownloadProgressBar : DownloadTrackingComposite
public class DownloadProgressBar : BeatmapDownloadTrackingComposite
{
private readonly ProgressBar progressBar;

View File

@ -1,13 +0,0 @@
// 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.
namespace osu.Game.Overlays.Direct
{
public enum DownloadState
{
NotDownloaded,
Downloading,
Downloaded,
LocallyAvailable
}
}

View File

@ -1,133 +0,0 @@
// 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.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
using osu.Game.Beatmaps;
using osu.Game.Online.API;
namespace osu.Game.Overlays.Direct
{
/// <summary>
/// A component which tracks a beatmap through potential download/import/deletion.
/// </summary>
public abstract class DownloadTrackingComposite : CompositeDrawable
{
public readonly Bindable<BeatmapSetInfo> BeatmapSet = new Bindable<BeatmapSetInfo>();
private BeatmapManager beatmaps;
/// <summary>
/// Holds the current download state of the beatmap, whether is has already been downloaded, is in progress, or is not downloaded.
/// </summary>
protected readonly Bindable<DownloadState> State = new Bindable<DownloadState>();
protected readonly Bindable<double> Progress = new Bindable<double>();
protected DownloadTrackingComposite(BeatmapSetInfo beatmapSet = null)
{
BeatmapSet.Value = beatmapSet;
}
[BackgroundDependencyLoader(true)]
private void load(BeatmapManager beatmaps)
{
this.beatmaps = beatmaps;
BeatmapSet.BindValueChanged(setInfo =>
{
if (setInfo.NewValue == null)
attachDownload(null);
else if (beatmaps.GetAllUsableBeatmapSetsEnumerable().Any(s => s.OnlineBeatmapSetID == setInfo.NewValue.OnlineBeatmapSetID))
State.Value = DownloadState.LocallyAvailable;
else
attachDownload(beatmaps.GetExistingDownload(setInfo.NewValue));
}, true);
beatmaps.DownloadBegan += download =>
{
if (download.Info.OnlineBeatmapSetID == BeatmapSet.Value?.OnlineBeatmapSetID)
attachDownload(download);
};
beatmaps.ItemAdded += setAdded;
beatmaps.ItemRemoved += setRemoved;
}
#region Disposal
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (beatmaps != null)
{
beatmaps.DownloadBegan -= attachDownload;
beatmaps.ItemAdded -= setAdded;
}
State.UnbindAll();
attachDownload(null);
}
#endregion
private ArchiveDownloadModelRequest<BeatmapSetInfo> attachedRequest;
private void attachDownload(ArchiveDownloadModelRequest<BeatmapSetInfo> request)
{
if (attachedRequest != null)
{
attachedRequest.Failure -= onRequestFailure;
attachedRequest.DownloadProgressed -= onRequestProgress;
attachedRequest.Success -= onRequestSuccess;
}
attachedRequest = request;
if (attachedRequest != null)
{
if (attachedRequest.Progress == 1)
{
State.Value = DownloadState.Downloaded;
Progress.Value = 1;
}
else
{
State.Value = DownloadState.Downloading;
Progress.Value = attachedRequest.Progress;
attachedRequest.Failure += onRequestFailure;
attachedRequest.DownloadProgressed += onRequestProgress;
attachedRequest.Success += onRequestSuccess;
}
}
else
{
State.Value = DownloadState.NotDownloaded;
}
}
private void onRequestSuccess(string _) => Schedule(() => State.Value = DownloadState.Downloaded);
private void onRequestProgress(float progress) => Schedule(() => Progress.Value = progress);
private void onRequestFailure(Exception e) => Schedule(() => attachDownload(null));
private void setAdded(BeatmapSetInfo s, bool existing) => setDownloadStateFromManager(s, DownloadState.LocallyAvailable);
private void setRemoved(BeatmapSetInfo s) => setDownloadStateFromManager(s, DownloadState.NotDownloaded);
private void setDownloadStateFromManager(BeatmapSetInfo s, DownloadState state) => Schedule(() =>
{
if (s.OnlineBeatmapSetID != BeatmapSet.Value?.OnlineBeatmapSetID)
return;
State.Value = state;
});
}
}