osukey/osu.Game/Overlays/BeatmapListing/BeatmapListingPager.cs
「空白」 35e7cee458 Squash commits from private fork
Temporary comments left to-remove later
2020-05-12 03:18:47 +09:00

93 lines
3.1 KiB
C#

// 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.Collections.Generic;
using System.Linq;
using osu.Game.Beatmaps;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Rulesets;
namespace osu.Game.Overlays.BeatmapListing
{
public class BeatmapListingPager
{
private readonly IAPIProvider api;
private readonly RulesetStore rulesets;
private readonly string query;
private readonly RulesetInfo ruleset;
private readonly SearchCategory searchCategory;
private readonly SortCriteria sortCriteria;
private readonly SortDirection sortDirection;
public event PageFetchHandler PageFetched;
private SearchBeatmapSetsRequest getSetsRequest;
private SearchBeatmapSetsResponse lastResponse;
/// <summary> Reports end of results </summary>
private bool isLastPageFetched = false;
/// <summary> Job in process lock flag </summary>
private bool isFetching => getSetsRequest != null;
/// <summary> Whether beatmaps should be appended or replaced </summary>
public bool IsPastFirstPage { get; private set; } = false;
/// <summary> call FetchNextPage() safe-check </summary>
public bool CanFetchNextPage => !isLastPageFetched && !isFetching;
public BeatmapListingPager(IAPIProvider api, RulesetStore rulesets, string query, RulesetInfo ruleset, SearchCategory searchCategory = SearchCategory.Any, SortCriteria sortCriteria = SortCriteria.Ranked, SortDirection sortDirection = SortDirection.Descending)
{
this.api = api;
this.rulesets = rulesets;
this.query = query;
this.ruleset = ruleset;
this.searchCategory = searchCategory;
this.sortCriteria = sortCriteria;
this.sortDirection = sortDirection;
}
public void FetchNextPage()
{
if (isFetching)
return;
if (lastResponse != null)
IsPastFirstPage = true;
getSetsRequest = new SearchBeatmapSetsRequest(
query,
ruleset,
lastResponse?.Cursor,
searchCategory,
sortCriteria,
sortDirection);
getSetsRequest.Success += response =>
{
var sets = response.BeatmapSets.Select(responseJson => responseJson.ToBeatmapSet(rulesets)).ToList();
if (sets.Count == 0)
isLastPageFetched = true;
lastResponse = response;
getSetsRequest = null;
PageFetched?.Invoke(sets);
};
api.Queue(getSetsRequest);
}
public void Reset()
{
isLastPageFetched = false;
IsPastFirstPage = false;
lastResponse = null;
getSetsRequest?.Cancel();
getSetsRequest = null;
}
public delegate void PageFetchHandler(List<BeatmapSetInfo> sets);
}
}