2019-08-13 02:16:41 +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.
|
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2021-05-19 20:28:12 +08:00
|
|
|
|
using System;
|
2019-12-09 01:49:58 +08:00
|
|
|
|
using System.Threading;
|
2019-08-10 23:06:52 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2019-08-10 18:22:45 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2021-05-19 20:28:12 +08:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-05-24 13:26:44 +08:00
|
|
|
|
using osu.Game.Online.API.Requests;
|
2019-08-10 18:53:34 +08:00
|
|
|
|
using osu.Game.Overlays.News;
|
2020-07-09 01:07:29 +08:00
|
|
|
|
using osu.Game.Overlays.News.Displays;
|
2021-05-19 20:28:12 +08:00
|
|
|
|
using osu.Game.Overlays.News.Sidebar;
|
2019-08-10 18:22:45 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays
|
|
|
|
|
{
|
2022-11-24 13:32:20 +08:00
|
|
|
|
public partial class NewsOverlay : OnlineOverlay<NewsHeader>
|
2019-08-10 18:22:45 +08:00
|
|
|
|
{
|
2021-05-26 13:17:40 +08:00
|
|
|
|
private readonly Bindable<string> article = new Bindable<string>();
|
2020-07-12 20:45:48 +08:00
|
|
|
|
|
2021-05-19 20:28:12 +08:00
|
|
|
|
private readonly Container sidebarContainer;
|
2021-05-19 20:36:05 +08:00
|
|
|
|
private readonly NewsSidebar sidebar;
|
2021-05-20 13:30:40 +08:00
|
|
|
|
private readonly Container content;
|
|
|
|
|
|
2021-05-26 13:37:52 +08:00
|
|
|
|
private GetNewsRequest request;
|
2021-05-26 13:28:20 +08:00
|
|
|
|
|
2021-05-24 13:26:44 +08:00
|
|
|
|
private Cursor lastCursor;
|
2021-05-26 13:28:20 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The year currently being displayed. If null, the main listing is being displayed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int? displayedYear;
|
2021-05-24 13:26:44 +08:00
|
|
|
|
|
2021-05-20 13:11:42 +08:00
|
|
|
|
private CancellationTokenSource cancellationToken;
|
|
|
|
|
|
2021-05-20 13:12:34 +08:00
|
|
|
|
private bool displayUpdateRequired = true;
|
|
|
|
|
|
2020-01-24 17:24:35 +08:00
|
|
|
|
public NewsOverlay()
|
2021-02-18 17:04:41 +08:00
|
|
|
|
: base(OverlayColourScheme.Purple, false)
|
2020-01-24 17:24:35 +08:00
|
|
|
|
{
|
2021-05-20 13:30:40 +08:00
|
|
|
|
Child = new GridContainer
|
2021-05-19 20:28:12 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
RowDimensions = new[]
|
|
|
|
|
{
|
|
|
|
|
new Dimension(GridSizeMode.AutoSize)
|
|
|
|
|
},
|
|
|
|
|
ColumnDimensions = new[]
|
|
|
|
|
{
|
|
|
|
|
new Dimension(GridSizeMode.AutoSize),
|
|
|
|
|
new Dimension()
|
|
|
|
|
},
|
|
|
|
|
Content = new[]
|
|
|
|
|
{
|
|
|
|
|
new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
sidebarContainer = new Container
|
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.X,
|
2021-05-19 20:36:05 +08:00
|
|
|
|
Child = sidebar = new NewsSidebar()
|
2021-05-19 20:28:12 +08:00
|
|
|
|
},
|
|
|
|
|
content = new Container
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-20 13:30:40 +08:00
|
|
|
|
};
|
2020-01-24 17:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 23:24:13 +08:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
2020-08-05 17:50:37 +08:00
|
|
|
|
|
|
|
|
|
// should not be run until first pop-in to avoid requesting data before user views.
|
2021-05-26 13:29:46 +08:00
|
|
|
|
article.BindValueChanged(a =>
|
|
|
|
|
{
|
|
|
|
|
if (a.NewValue == null)
|
|
|
|
|
loadListing();
|
|
|
|
|
else
|
|
|
|
|
loadArticle(a.NewValue);
|
|
|
|
|
});
|
2020-08-03 15:03:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-20 14:15:46 +08:00
|
|
|
|
protected override NewsHeader CreateHeader() => new NewsHeader { ShowFrontPage = ShowFrontPage };
|
2021-01-18 15:48:12 +08:00
|
|
|
|
|
2020-08-03 15:03:42 +08:00
|
|
|
|
protected override void PopIn()
|
|
|
|
|
{
|
|
|
|
|
base.PopIn();
|
|
|
|
|
|
|
|
|
|
if (displayUpdateRequired)
|
|
|
|
|
{
|
|
|
|
|
article.TriggerChange();
|
|
|
|
|
displayUpdateRequired = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void PopOutComplete()
|
|
|
|
|
{
|
|
|
|
|
base.PopOutComplete();
|
|
|
|
|
displayUpdateRequired = true;
|
2020-07-08 23:24:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ShowFrontPage()
|
|
|
|
|
{
|
2020-07-12 20:45:48 +08:00
|
|
|
|
article.Value = null;
|
2020-07-09 08:40:14 +08:00
|
|
|
|
Show();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 20:36:05 +08:00
|
|
|
|
public void ShowYear(int year)
|
|
|
|
|
{
|
2021-05-26 13:28:20 +08:00
|
|
|
|
loadListing(year);
|
2021-05-19 20:36:05 +08:00
|
|
|
|
Show();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 08:40:14 +08:00
|
|
|
|
public void ShowArticle(string slug)
|
|
|
|
|
{
|
2020-07-12 20:45:48 +08:00
|
|
|
|
article.Value = slug;
|
2020-07-08 23:24:13 +08:00
|
|
|
|
Show();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-20 13:12:34 +08:00
|
|
|
|
protected void LoadDisplay(Drawable display)
|
|
|
|
|
{
|
|
|
|
|
ScrollFlow.ScrollToStart();
|
2021-05-24 13:26:44 +08:00
|
|
|
|
LoadComponentAsync(display, loaded =>
|
|
|
|
|
{
|
|
|
|
|
content.Child = loaded;
|
|
|
|
|
Loading.Hide();
|
|
|
|
|
}, (cancellationToken = new CancellationTokenSource()).Token);
|
2021-05-20 13:12:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void UpdateAfterChildren()
|
|
|
|
|
{
|
|
|
|
|
base.UpdateAfterChildren();
|
|
|
|
|
sidebarContainer.Height = DrawHeight;
|
|
|
|
|
sidebarContainer.Y = Math.Clamp(ScrollFlow.Current - Header.DrawHeight, 0, Math.Max(ScrollFlow.ScrollContent.DrawHeight - DrawHeight - Header.DrawHeight, 0));
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 13:28:20 +08:00
|
|
|
|
private void loadListing(int? year = null)
|
2021-05-19 20:36:05 +08:00
|
|
|
|
{
|
|
|
|
|
Header.SetFrontPage();
|
|
|
|
|
|
2021-05-26 13:28:20 +08:00
|
|
|
|
displayedYear = year;
|
2021-05-24 13:26:44 +08:00
|
|
|
|
lastCursor = null;
|
|
|
|
|
|
2021-05-26 13:37:52 +08:00
|
|
|
|
beginLoading(true);
|
|
|
|
|
|
|
|
|
|
request = new GetNewsRequest(displayedYear);
|
|
|
|
|
request.Success += response => Schedule(() =>
|
2021-05-19 20:36:05 +08:00
|
|
|
|
{
|
2021-05-26 13:37:52 +08:00
|
|
|
|
lastCursor = response.Cursor;
|
2021-05-24 13:26:44 +08:00
|
|
|
|
sidebar.Metadata.Value = response.SidebarMetadata;
|
|
|
|
|
|
2021-05-26 21:49:39 +08:00
|
|
|
|
var listing = new ArticleListing(getMorePosts);
|
2021-05-26 20:35:38 +08:00
|
|
|
|
listing.AddPosts(response.NewsPosts, response.Cursor != null);
|
|
|
|
|
LoadDisplay(listing);
|
2021-05-24 13:26:44 +08:00
|
|
|
|
});
|
2021-05-19 20:36:05 +08:00
|
|
|
|
|
2021-05-26 13:37:52 +08:00
|
|
|
|
API.PerformAsync(request);
|
2020-07-08 23:24:13 +08:00
|
|
|
|
}
|
2019-12-09 01:49:58 +08:00
|
|
|
|
|
2021-05-26 13:29:46 +08:00
|
|
|
|
private void getMorePosts()
|
|
|
|
|
{
|
2021-05-26 13:37:52 +08:00
|
|
|
|
beginLoading(false);
|
|
|
|
|
|
|
|
|
|
request = new GetNewsRequest(displayedYear, lastCursor);
|
|
|
|
|
request.Success += response => Schedule(() =>
|
2021-05-26 13:29:46 +08:00
|
|
|
|
{
|
2021-05-26 13:37:52 +08:00
|
|
|
|
lastCursor = response.Cursor;
|
2021-05-26 13:29:46 +08:00
|
|
|
|
if (content.Child is ArticleListing listing)
|
2021-05-26 20:35:38 +08:00
|
|
|
|
listing.AddPosts(response.NewsPosts, response.Cursor != null);
|
2021-05-26 13:29:46 +08:00
|
|
|
|
});
|
2021-05-26 13:37:52 +08:00
|
|
|
|
|
|
|
|
|
API.PerformAsync(request);
|
2021-05-26 13:29:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 13:37:52 +08:00
|
|
|
|
private void loadArticle(string article)
|
2021-05-26 13:28:20 +08:00
|
|
|
|
{
|
2021-05-26 13:37:52 +08:00
|
|
|
|
// This is not yet implemented nor called from anywhere.
|
|
|
|
|
beginLoading(true);
|
2021-05-26 13:28:20 +08:00
|
|
|
|
|
2021-05-26 13:37:52 +08:00
|
|
|
|
Header.SetArticle(article);
|
|
|
|
|
LoadDisplay(Empty());
|
2021-05-26 13:28:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 13:37:52 +08:00
|
|
|
|
private void beginLoading(bool showLoadingOverlay)
|
2021-05-20 13:11:42 +08:00
|
|
|
|
{
|
2021-05-26 13:37:52 +08:00
|
|
|
|
request?.Cancel();
|
2021-05-20 13:11:42 +08:00
|
|
|
|
cancellationToken?.Cancel();
|
2021-05-26 13:37:52 +08:00
|
|
|
|
|
|
|
|
|
if (showLoadingOverlay)
|
|
|
|
|
Loading.Show();
|
2021-05-20 13:11:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 23:24:13 +08:00
|
|
|
|
protected override void Dispose(bool isDisposing)
|
2019-08-10 23:06:52 +08:00
|
|
|
|
{
|
2021-05-26 13:37:52 +08:00
|
|
|
|
request?.Cancel();
|
2020-07-08 23:24:13 +08:00
|
|
|
|
cancellationToken?.Cancel();
|
|
|
|
|
base.Dispose(isDisposing);
|
2019-08-10 18:22:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|