2020-04-16 08:42:21 +00: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.
|
|
|
|
|
|
|
|
|
|
using osu.Framework.Allocation;
|
2020-10-22 05:19:12 +00:00
|
|
|
|
using osu.Framework.Bindables;
|
2020-04-16 08:42:21 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2020-09-03 07:27:31 +00:00
|
|
|
|
/// A subview containing online content, to be displayed inside a <see cref="FullscreenOverlay{T}"/>.
|
2020-04-16 08:42:21 +00:00
|
|
|
|
/// </summary>
|
2020-04-20 06:32:50 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Automatically performs a data fetch on load.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <typeparam name="T">The type of the API response.</typeparam>
|
2020-10-22 05:19:12 +00:00
|
|
|
|
public abstract class OverlayView<T> : CompositeDrawable
|
2020-04-16 08:42:21 +00:00
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
[Resolved]
|
|
|
|
|
protected IAPIProvider API { get; private set; }
|
|
|
|
|
|
2020-04-20 06:32:50 +00:00
|
|
|
|
private APIRequest<T> request;
|
|
|
|
|
|
2020-04-16 08:42:21 +00:00
|
|
|
|
protected OverlayView()
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 05:19:12 +00:00
|
|
|
|
private readonly IBindable<APIState> apiState = new Bindable<APIState>();
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
2020-04-16 08:42:21 +00:00
|
|
|
|
{
|
2020-10-22 05:19:12 +00:00
|
|
|
|
apiState.BindTo(API.State);
|
|
|
|
|
apiState.BindValueChanged(onlineStateChanged, true);
|
2020-04-16 08:42:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 06:32:50 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create the API request for fetching data.
|
|
|
|
|
/// </summary>
|
2020-04-16 08:42:21 +00:00
|
|
|
|
protected abstract APIRequest<T> CreateRequest();
|
|
|
|
|
|
2020-04-20 06:32:50 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fired when results arrive from the main API request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="response"></param>
|
2020-04-16 08:42:21 +00:00
|
|
|
|
protected abstract void OnSuccess(T response);
|
|
|
|
|
|
2020-04-20 06:32:50 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Force a re-request for data from the API.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected void PerformFetch()
|
|
|
|
|
{
|
|
|
|
|
request?.Cancel();
|
|
|
|
|
|
|
|
|
|
request = CreateRequest();
|
|
|
|
|
request.Success += response => Schedule(() => OnSuccess(response));
|
|
|
|
|
|
|
|
|
|
API.Queue(request);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 05:19:12 +00:00
|
|
|
|
private void onlineStateChanged(ValueChangedEvent<APIState> state) => Schedule(() =>
|
2020-04-16 08:42:21 +00:00
|
|
|
|
{
|
2020-10-22 05:19:12 +00:00
|
|
|
|
switch (state.NewValue)
|
2020-04-16 08:42:21 +00:00
|
|
|
|
{
|
|
|
|
|
case APIState.Online:
|
2020-04-20 06:32:50 +00:00
|
|
|
|
PerformFetch();
|
2020-04-16 08:42:21 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2020-10-22 05:19:12 +00:00
|
|
|
|
});
|
2020-04-16 08:42:21 +00:00
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
request?.Cancel();
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|