Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically use in-game language for wiki pages #29401

Merged
merged 4 commits into from
Aug 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 45 additions & 29 deletions osu.Game/Overlays/WikiOverlay.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using System;
using System.Linq;
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Extensions;
using osu.Game.Localisation;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
Expand All @@ -24,25 +23,37 @@ public partial class WikiOverlay : OnlineOverlay<WikiHeader>
public string CurrentPath => path.Value;

private readonly Bindable<string> path = new Bindable<string>(INDEX_PATH);

private readonly Bindable<APIWikiPage> wikiData = new Bindable<APIWikiPage>();
private readonly Bindable<APIWikiPage?> wikiData = new Bindable<APIWikiPage?>();
private readonly IBindable<Language> language = new Bindable<Language>();

[Resolved]
private IAPIProvider api { get; set; }
private IAPIProvider api { get; set; } = null!;

private GetWikiRequest request;
[Resolved]
private OsuGameBase game { get; set; } = null!;

private CancellationTokenSource cancellationToken;
private GetWikiRequest? request;
private CancellationTokenSource? cancellationToken;
private WikiArticlePage? articlePage;

private bool displayUpdateRequired = true;

private WikiArticlePage articlePage;

public WikiOverlay()
: base(OverlayColourScheme.Orange, false)
{
}

protected override void LoadComplete()
{
base.LoadComplete();

path.BindValueChanged(onPathChanged);
wikiData.BindTo(Header.WikiPageData);

language.BindTo(game.CurrentLanguage);
language.BindValueChanged(onLangChanged);
}

public void ShowPage(string pagePath = INDEX_PATH)
{
path.Value = pagePath.Trim('/');
Expand All @@ -55,13 +66,6 @@ public void ShowPage(string pagePath = INDEX_PATH)
ShowParentPage = showParentPage,
};

protected override void LoadComplete()
{
base.LoadComplete();
path.BindValueChanged(onPathChanged);
wikiData.BindTo(Header.WikiPageData);
}

protected override void PopIn()
{
base.PopIn();
Expand Down Expand Up @@ -100,25 +104,18 @@ protected override void UpdateAfterChildren()
}
}

private void onPathChanged(ValueChangedEvent<string> e)
private void loadPage(string path, Language lang)
{
// the path could change as a result of redirecting to a newer location of the same page.
// we already have the correct wiki data, so we can safely return here.
if (e.NewValue == wikiData.Value?.Path)
return;

if (e.NewValue == "error")
return;

cancellationToken?.Cancel();
request?.Cancel();

string[] values = e.NewValue.Split('/', 2);
// Language code + path, or just path1 + path2 in case
string[] values = path.Split('/', 2);

if (values.Length > 1 && LanguageExtensions.TryParseCultureCode(values[0], out var language))
request = new GetWikiRequest(values[1], language);
if (values.Length > 1 && LanguageExtensions.TryParseCultureCode(values[0], out var parsedLang))
request = new GetWikiRequest(values[1], parsedLang);
else
request = new GetWikiRequest(e.NewValue);
request = new GetWikiRequest(path, lang);

Loading.Show();

Expand All @@ -132,6 +129,25 @@ private void onPathChanged(ValueChangedEvent<string> e)
api.PerformAsync(request);
}

private void onPathChanged(ValueChangedEvent<string> e)
{
// the path could change as a result of redirecting to a newer location of the same page.
// we already have the correct wiki data, so we can safely return here.
if (e.NewValue == wikiData.Value?.Path)
return;

if (e.NewValue == "error")
return;

loadPage(e.NewValue, language.Value);
}

private void onLangChanged(ValueChangedEvent<Language> e)
{
// Path unmodified, just reload the page with new language value.
loadPage(path.Value, e.NewValue);
}

private void onSuccess(APIWikiPage response)
{
wikiData.Value = response;
Expand Down
Loading