1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 13:47:24 +08:00
osu-lazer/osu.Game/Overlays/DashboardOverlay.cs

157 lines
4.7 KiB
C#
Raw Normal View History

2020-04-16 16:01:36 +08: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 System;
using System.Threading;
2020-04-16 16:01:36 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2020-04-16 16:01:36 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
2020-04-16 16:01:36 +08:00
using osu.Game.Overlays.Dashboard;
using osu.Game.Overlays.Dashboard.Friends;
2020-04-16 16:01:36 +08:00
namespace osu.Game.Overlays
{
public class DashboardOverlay : FullscreenOverlay<DashboardOverlayHeader>
2020-04-16 16:01:36 +08:00
{
private CancellationTokenSource cancellationToken;
2020-04-20 14:34:48 +08:00
private Container content;
private LoadingLayer loading;
private OverlayScrollContainer scrollFlow;
2020-04-16 16:01:36 +08:00
public DashboardOverlay()
: base(OverlayColourScheme.Purple, new DashboardOverlayHeader
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Depth = -float.MaxValue
})
2020-04-20 14:34:48 +08:00
{
}
private readonly IBindable<APIState> apiState = new Bindable<APIState>();
[BackgroundDependencyLoader]
private void load(IAPIProvider api)
{
apiState.BindTo(api.State);
apiState.BindValueChanged(onlineStateChanged, true);
2020-04-16 16:01:36 +08:00
Children = new Drawable[]
{
new Box
2020-04-16 16:01:36 +08:00
{
RelativeSizeAxes = Axes.Both,
Colour = ColourProvider.Background5
2020-04-16 16:01:36 +08:00
},
scrollFlow = new OverlayScrollContainer
2020-04-16 16:01:36 +08:00
{
RelativeSizeAxes = Axes.Both,
ScrollbarVisible = false,
Child = new FillFlowContainer
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
Header,
2020-04-16 16:01:36 +08:00
content = new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y
}
}
}
},
loading = new LoadingLayer(true),
2020-04-16 16:01:36 +08:00
};
}
protected override void LoadComplete()
{
base.LoadComplete();
2020-04-20 14:34:48 +08:00
Header.Current.BindValueChanged(onTabChanged);
}
private bool displayUpdateRequired = true;
protected override void PopIn()
{
base.PopIn();
// We don't want to create a new display on every call, only when exiting from fully closed state.
if (displayUpdateRequired)
{
Header.Current.TriggerChange();
displayUpdateRequired = false;
}
}
protected override void PopOutComplete()
{
base.PopOutComplete();
loadDisplay(Empty());
displayUpdateRequired = true;
}
private void loadDisplay(Drawable display)
{
scrollFlow.ScrollToStart();
LoadComponentAsync(display, loaded =>
{
if (API.IsLoggedIn)
loading.Hide();
content.Child = loaded;
}, (cancellationToken = new CancellationTokenSource()).Token);
}
private void onTabChanged(ValueChangedEvent<DashboardOverlayTabs> tab)
{
cancellationToken?.Cancel();
loading.Show();
if (!API.IsLoggedIn)
{
loadDisplay(Empty());
return;
}
switch (tab.NewValue)
{
case DashboardOverlayTabs.Friends:
loadDisplay(new FriendDisplay());
break;
case DashboardOverlayTabs.CurrentlyPlaying:
loadDisplay(new CurrentlyPlayingDisplay());
break;
default:
throw new NotImplementedException($"Display for {tab.NewValue} tab is not implemented");
}
}
private void onlineStateChanged(ValueChangedEvent<APIState> state) => Schedule(() =>
{
if (State.Value == Visibility.Hidden)
return;
Header.Current.TriggerChange();
});
protected override void Dispose(bool isDisposing)
{
cancellationToken?.Cancel();
base.Dispose(isDisposing);
}
2020-04-16 16:01:36 +08:00
}
}