Standardise request/response handling

This commit is contained in:
Dean Herbert
2019-12-05 14:26:36 +09:00
parent cd473f207a
commit 6e9157d59c
2 changed files with 55 additions and 49 deletions

View File

@ -8,13 +8,14 @@ namespace osu.Game.Online.API.Requests
{ {
public class GetUserRankingsRequest : GetRankingsRequest<GetUsersResponse> public class GetUserRankingsRequest : GetRankingsRequest<GetUsersResponse>
{ {
public readonly UserRankingsType Type;
private readonly string country; private readonly string country;
private readonly UserRankingsType type;
public GetUserRankingsRequest(RulesetInfo ruleset, UserRankingsType type = UserRankingsType.Performance, int page = 1, string country = null) public GetUserRankingsRequest(RulesetInfo ruleset, UserRankingsType type = UserRankingsType.Performance, int page = 1, string country = null)
: base(ruleset, page) : base(ruleset, page)
{ {
this.type = type; this.Type = type;
this.country = country; this.country = country;
} }
@ -28,7 +29,7 @@ namespace osu.Game.Online.API.Requests
return req; return req;
} }
protected override string TargetPostfix() => type.ToString().ToLowerInvariant(); protected override string TargetPostfix() => Type.ToString().ToLowerInvariant();
} }
public enum UserRankingsType public enum UserRankingsType

View File

@ -26,10 +26,10 @@ namespace osu.Game.Overlays
private readonly BasicScrollContainer scrollFlow; private readonly BasicScrollContainer scrollFlow;
private readonly Box background; private readonly Box background;
private readonly Container contentPlaceholder; private readonly Container tableContainer;
private readonly DimmedLoadingLayer loading; private readonly DimmedLoadingLayer loading;
private APIRequest request; private APIRequest lastRequest;
private CancellationTokenSource cancellationToken; private CancellationTokenSource cancellationToken;
[Resolved] [Resolved]
@ -68,7 +68,7 @@ namespace osu.Game.Overlays
AutoSizeAxes = Axes.Y, AutoSizeAxes = Axes.Y,
Children = new Drawable[] Children = new Drawable[]
{ {
contentPlaceholder = new Container tableContainer = new Container
{ {
Anchor = Anchor.TopCentre, Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre, Origin = Anchor.TopCentre,
@ -132,76 +132,81 @@ namespace osu.Game.Overlays
private void loadNewContent() private void loadNewContent()
{ {
scrollFlow.ScrollToStart();
loading.Show(); loading.Show();
cancellationToken?.Cancel(); cancellationToken?.Cancel();
request?.Cancel(); lastRequest?.Cancel();
var request = createScopedRequest();
lastRequest = request;
if (request == null)
{
loadTable(null);
return;
}
request.Success += () => loadTable(createTableFromResponse(request));
request.Failure += _ => loadTable(null);
api.Queue(request);
}
private APIRequest createScopedRequest()
{
switch (scope.Value) switch (scope.Value)
{ {
default:
contentPlaceholder.Clear();
loading.Hide();
return;
case RankingsScope.Performance: case RankingsScope.Performance:
createPerformanceTable(); return new GetUserRankingsRequest(ruleset.Value, country: country.Value?.FlagName);
return;
case RankingsScope.Country: case RankingsScope.Country:
createCountryTable(); return new GetCountryRankingsRequest(ruleset.Value);
return;
case RankingsScope.Score: case RankingsScope.Score:
createScoreTable(); return new GetUserRankingsRequest(ruleset.Value, UserRankingsType.Score);
return;
} }
return null;
} }
private void createCountryTable() private Drawable createTableFromResponse(APIRequest request)
{ {
request = new GetCountryRankingsRequest(ruleset.Value); switch (request)
((GetCountryRankingsRequest)request).Success += rankings => Schedule(() =>
{ {
var table = new CountriesTable(1, rankings.Countries); case GetUserRankingsRequest userRequest:
loadTable(table); switch (userRequest.Type)
}); {
case UserRankingsType.Performance:
return new PerformanceTable(1, userRequest.Result.Users);
api.Queue(request); case UserRankingsType.Score:
} return new ScoresTable(1, userRequest.Result.Users);
}
private void createPerformanceTable() return null;
{
request = new GetUserRankingsRequest(ruleset.Value, country: country.Value?.FlagName);
((GetUserRankingsRequest)request).Success += rankings => Schedule(() =>
{
var table = new PerformanceTable(1, rankings.Users);
loadTable(table);
});
api.Queue(request); case GetCountryRankingsRequest countryRequest:
} return new CountriesTable(1, countryRequest.Result.Countries);
}
private void createScoreTable() return null;
{
request = new GetUserRankingsRequest(ruleset.Value, UserRankingsType.Score);
((GetUserRankingsRequest)request).Success += rankings => Schedule(() =>
{
var table = new ScoresTable(1, rankings.Users);
loadTable(table);
});
api.Queue(request);
} }
private void loadTable(Drawable table) private void loadTable(Drawable table)
{ {
scrollFlow.ScrollToStart();
if (table == null)
{
tableContainer.Clear();
loading.Hide();
return;
}
LoadComponentAsync(table, t => LoadComponentAsync(table, t =>
{ {
contentPlaceholder.Child = t;
loading.Hide(); loading.Hide();
tableContainer.Child = table;
}, (cancellationToken = new CancellationTokenSource()).Token); }, (cancellationToken = new CancellationTokenSource()).Token);
} }
} }