/// <summary> /// Get the last page number of the tally. This may be determined solely /// from the thread range info, or might require information from the /// provided first page, where we can extract how many pages are in the thread. /// </summary> /// <param name="quest">The quest being tallied.</param> /// <param name="adapter">The forum adapter that handles the quest's thread.</param> /// <param name="threadRangeInfo">The range of posts that are wanted in the tally.</param> /// <param name="firstPage">The first page of the tally, from which we can get the page range of the thread.</param> /// <returns>Returns the last page number of the tally.</returns> private async Task <int> GetLastPageNumber(IQuest quest, IForumAdapter2 adapter, ThreadRangeInfo threadRangeInfo, Task <HtmlDocument?> firstPage) { // Check for quick results first. if (threadRangeInfo.Pages > 0) { // If the page range has already been determined, use that. return(threadRangeInfo.Pages); } if (!quest.ReadToEndOfThread && !threadRangeInfo.IsThreadmarkSearchResult) { // If we're not reading to the end of the thread, just calculate // what the last page number will be. Pages to scan will be the // difference in pages +1. return(ThreadInfo.GetPageNumberOfPost(quest.EndPost, quest)); } // If we're reading to the end of the thread (end post 0, or based on a threadmark), // then we need to load the first page to find out how many pages there are in the thread. var page = await firstPage.ConfigureAwait(false); if (page == null) { throw new InvalidOperationException($"Unable to load first page of {quest.ThreadName}"); } return(adapter.GetThreadInfo(page).Pages); }
/// <summary> /// Get the starting page for tallying for the provided quest, using either /// the explicitly determined page number, or the calculated page number /// based on the starting post and the quest's post per page. /// </summary> /// <param name="quest">The quest this info is for.</param> /// <returns>Returns the page number to start tallying.</returns> public int GetStartPage(IQuest quest) { if (ByNumber) { return(ThreadInfo.GetPageNumberOfPost(Number, quest)); } return(Page); }
/// <summary> /// Determines the page number range that will be loaded for the quest. /// Returns a tuple of first page number, last page number, and pages to scan. /// </summary> /// <param name="quest">The quest being tallied.</param> /// <param name="adapter">The forum adapter for the quest.</param> /// <param name="threadRangeInfo">The thread range info, as provided by the adapter.</param> /// <param name="token">The cancellation token.</param> /// <returns>Returns a tuple of the page number info that was determined.</returns> private async Task <(int firstPageNumber, int lastPageNumber, int pagesToScan)> GetPagesToScanAsync( IQuest quest, IForumAdapter adapter, ThreadRangeInfo threadRangeInfo, CancellationToken token) { IPageProvider pageProvider = ViewModelService.MainViewModel.PageProvider; int firstPageNumber = threadRangeInfo.GetStartPage(quest); int lastPageNumber = 0; int pagesToScan = 0; if (threadRangeInfo.Pages > 0) { // If the startInfo obtained the thread pages info, just use that. lastPageNumber = threadRangeInfo.Pages; } else if (quest.ReadToEndOfThread || threadRangeInfo.IsThreadmarkSearchResult) { // If we're reading to the end of the thread (end post 0, or based on a threadmark), // then we need to load the first page to find out how many pages there are in the thread. // Make sure to bypass the cache, since it may have changed since the last load. string firstPageUrl = adapter.GetUrlForPage(firstPageNumber, quest.PostsPerPage); HtmlDocument page = await pageProvider.GetPage(firstPageUrl, $"Page {firstPageNumber}", CachingMode.BypassCache, ShouldCache.Yes, SuppressNotifications.No, token) .ConfigureAwait(false); if (page == null) { throw new InvalidOperationException($"Unable to load web page: {firstPageUrl}"); } lastPageNumber = adapter.GetThreadInfo(page).Pages; } else { // If we're not reading to the end of the thread, just calculate // what the last page number will be. Pages to scan will be the // difference in pages +1. lastPageNumber = ThreadInfo.GetPageNumberOfPost(quest.EndPost, quest); } pagesToScan = lastPageNumber - firstPageNumber + 1; return(firstPageNumber, lastPageNumber, pagesToScan); }