osu/osu.Game/Overlays/WikiOverlay.cs

149 lines
4.2 KiB
C#
Raw Normal View History

2021-04-21 09:21:07 +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.
2021-05-20 06:58:17 +00:00
using System.Linq;
2021-05-19 12:09:03 +00:00
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
2021-04-21 09:21:07 +00:00
using osu.Game.Overlays.Wiki;
2021-05-19 12:09:03 +00:00
using osu.Game.Overlays.Wiki.Markdown;
2021-04-21 09:21:07 +00:00
namespace osu.Game.Overlays
{
public class WikiOverlay : OnlineOverlay<WikiHeader>
{
2021-05-19 12:09:03 +00:00
private const string index_path = "Main_Page";
private readonly Bindable<string> path = new Bindable<string>(index_path);
private readonly Bindable<APIWikiPage> wikiData = new Bindable<APIWikiPage>();
[Resolved]
private IAPIProvider api { get; set; }
private GetWikiRequest request;
private CancellationTokenSource cancellationToken;
private bool displayUpdateRequired = true;
2021-04-21 09:21:07 +00:00
public WikiOverlay()
: base(OverlayColourScheme.Orange, false)
{
}
2021-06-01 05:01:08 +00:00
public void ShowPage(string pagePath = index_path)
{
path.Value = pagePath.Trim('/');
Show();
}
protected override WikiHeader CreateHeader() => new WikiHeader
{
ShowIndexPage = () => ShowPage(),
ShowParentPage = showParentPage,
};
protected override void LoadComplete()
{
base.LoadComplete();
path.BindValueChanged(onPathChanged);
wikiData.BindTo(Header.WikiPageData);
}
protected override void PopIn()
{
base.PopIn();
if (displayUpdateRequired)
{
path.TriggerChange();
displayUpdateRequired = false;
}
}
protected override void PopOutComplete()
{
base.PopOutComplete();
displayUpdateRequired = true;
}
protected void LoadDisplay(Drawable display)
{
ScrollFlow.ScrollToStart();
LoadComponentAsync(display, loaded =>
{
Child = loaded;
Loading.Hide();
}, (cancellationToken = new CancellationTokenSource()).Token);
}
2021-05-19 12:09:03 +00:00
private void onPathChanged(ValueChangedEvent<string> e)
{
cancellationToken?.Cancel();
request?.Cancel();
request = new GetWikiRequest(e.NewValue);
Loading.Show();
request.Success += response => Schedule(() => onSuccess(response));
2021-05-25 07:47:39 +00:00
request.Failure += _ => Schedule(() => LoadDisplay(Empty()));
2021-05-19 12:09:03 +00:00
api.PerformAsync(request);
}
private void onSuccess(APIWikiPage response)
{
wikiData.Value = response;
2021-05-25 07:20:04 +00:00
if (response.Layout == "main_page")
{
LoadDisplay(new WikiMainPage
{
2021-05-25 07:37:14 +00:00
Markdown = response.Markdown,
Padding = new MarginPadding
{
Vertical = 20,
Horizontal = 50,
},
2021-05-25 07:20:04 +00:00
});
}
else
2021-05-19 12:09:03 +00:00
{
2021-05-25 07:20:04 +00:00
LoadDisplay(new WikiMarkdownContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
CurrentPath = $@"{api.WebsiteRootUrl}/wiki/{path.Value}/",
2021-05-25 07:20:04 +00:00
Text = response.Markdown,
2021-05-25 07:37:14 +00:00
DocumentMargin = new MarginPadding(0),
DocumentPadding = new MarginPadding
{
Vertical = 20,
Left = 30,
Right = 50,
},
2021-05-25 07:20:04 +00:00
});
}
2021-05-19 12:09:03 +00:00
}
2021-05-20 06:58:17 +00:00
private void showParentPage()
{
var parentPath = string.Join("/", path.Value.Split('/').SkipLast(1));
ShowPage(parentPath);
}
2021-05-19 12:09:03 +00:00
protected override void Dispose(bool isDisposing)
{
cancellationToken?.Cancel();
request?.Cancel();
base.Dispose(isDisposing);
}
2021-04-21 09:21:07 +00:00
}
}