// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Online.API; namespace osu.Game.Overlays { /// /// A subview containing online content, to be displayed inside a . /// /// /// Automatically performs a data fetch on load. /// /// The type of the API response. public abstract class OverlayView : CompositeDrawable where T : class { [Resolved] protected IAPIProvider API { get; private set; } private APIRequest request; protected OverlayView() { RelativeSizeAxes = Axes.X; AutoSizeAxes = Axes.Y; } private readonly IBindable apiState = new Bindable(); [BackgroundDependencyLoader] private void load() { apiState.BindTo(API.State); apiState.BindValueChanged(onlineStateChanged, true); } /// /// Create the API request for fetching data. /// protected abstract APIRequest CreateRequest(); /// /// Fired when results arrive from the main API request. /// /// protected abstract void OnSuccess(T response); /// /// Force a re-request for data from the API. /// protected void PerformFetch() { request?.Cancel(); request = CreateRequest(); request.Success += response => Schedule(() => OnSuccess(response)); API.Queue(request); } private void onlineStateChanged(ValueChangedEvent state) => Schedule(() => { switch (state.NewValue) { case APIState.Online: PerformFetch(); break; } }); protected override void Dispose(bool isDisposing) { request?.Cancel(); base.Dispose(isDisposing); } } }