diff --git a/src/Ombi.Core/Engine/V2/MovieSearchEngineV2.cs b/src/Ombi.Core/Engine/V2/MovieSearchEngineV2.cs index 1b5f1d881..3029c4fc1 100644 --- a/src/Ombi.Core/Engine/V2/MovieSearchEngineV2.cs +++ b/src/Ombi.Core/Engine/V2/MovieSearchEngineV2.cs @@ -109,9 +109,16 @@ namespace Ombi.Core.Engine.V2 // Pages of 20 if(toLoad > 20) { - throw new ApplicationException("Please load less than 20 items at a time due to a API limit"); + throw new ApplicationException("Please load less than or equal to 20 items at a time due to a API limit"); } + // TheMovieDb only shows pages of 20, let's work out how many we need to load + + var page = Math.Round((decimal)(currentlyLoaded / 10) / 2, 0); + if(page == 0) + { + // First page + } var result = await MovieApi.PopularMovies(langCode); diff --git a/src/Ombi.Helpers.Tests/PagnationHelperTests.cs b/src/Ombi.Helpers.Tests/PagnationHelperTests.cs new file mode 100644 index 000000000..36b34ad49 --- /dev/null +++ b/src/Ombi.Helpers.Tests/PagnationHelperTests.cs @@ -0,0 +1,41 @@ +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Ombi.Helpers.Tests +{ + [TestFixture] + public class PaginationHelperTests + { + [TestCaseSource(nameof(TestData))] + [Ignore("https://stackoverflow.com/questions/55710966/working-out-how-many-items-to-take-in-a-paginated-list")] + public void TestPaginationPagesToLoad(int currentlyLoaded, int toLoad, int maxItemsPerPage, int[] expectedPages) + { + var result = PaginationHelper.GetNextPages(currentlyLoaded, toLoad, maxItemsPerPage); + var pages = result.Select(x => x.Page).ToArray(); + + Assert.That(pages.Length, Is.EqualTo(expectedPages.Length), "Did not contain the correct amount of pages"); + for (int i = 0; i < pages.Length; i++) + { + Assert.That(pages[i], Is.EqualTo(expectedPages[i])); + } + + + } + + public static IEnumerable TestData + { + get + { + yield return new TestCaseData(0, 10, 20, new [] { 1 }).SetName("Load_First_Page"); + yield return new TestCaseData(20, 10, 20, new [] { 2 }).SetName("Load_Second_Page"); + yield return new TestCaseData(0, 20, 20, new [] { 2 }).SetName("Load_Full_First_Page_Should_Get_NextPage"); + yield return new TestCaseData(20, 20, 20, new [] { 3 }).SetName("Load_Full_Second_Page_Should_Get_Next_Page"); + yield return new TestCaseData(10, 20, 20, new [] { 1, 2 }).SetName("Load_Half_First_Page_And_Half_Second_Page"); + yield return new TestCaseData(19, 20, 20, new [] { 1, 2 }).SetName("Load_End_First_Page_And_Most_Second_Page"); + } + } + } +} diff --git a/src/Ombi.Helpers/PagnationHelper.cs b/src/Ombi.Helpers/PagnationHelper.cs new file mode 100644 index 000000000..0dee8fcd5 --- /dev/null +++ b/src/Ombi.Helpers/PagnationHelper.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Ombi.Helpers +{ + public static class PaginationHelper + { + public static List GetNextPages(int currentlyLoaded, int toLoad, int maxItemsPerPage) + { + var pagesToLoad = new List(); + + if (currentlyLoaded == maxItemsPerPage) + { + currentlyLoaded++; + } + + var a = currentlyLoaded / maxItemsPerPage; + //var currentPage = Convert.ToInt32(Math.Round((decimal)((decimal)currentlyLoaded / (decimal)maxItemsPerPage), 2, MidpointRounding.AwayFromZero)); + var currentPage = Convert.ToInt32(Math.Ceiling((decimal)((decimal)currentlyLoaded / (decimal)maxItemsPerPage))); + if (currentlyLoaded < maxItemsPerPage) + { + currentPage = 1; + } + + + var toBeLoaded = (currentlyLoaded + toLoad)+1; + //var toBeLoadedPage = Convert.ToInt32(Math.Round((decimal)((decimal)toBeLoaded / (decimal)maxItemsPerPage), 2, MidpointRounding.AwayFromZero)); + var toBeLoadedPage = Convert.ToInt32(Math.Ceiling((decimal)((decimal)toBeLoaded / (decimal)maxItemsPerPage))); + + + if (currentlyLoaded == 0) + { + // If we have not loaded any yet, then we should only load + // the first page + currentPage = toBeLoadedPage; + } + var extraPageNeeded = (toBeLoadedPage != currentPage); + if(extraPageNeeded) + { + // Add the first page + pagesToLoad.Add(new PagesToLoad + { + Page = currentPage, + Skip = currentlyLoaded + }); + // Add extra page + pagesToLoad.Add(new PagesToLoad + { + Page = toBeLoadedPage, + Skip = (currentlyLoaded + toLoad) - maxItemsPerPage, + Take = toLoad + }); + } + else + { + pagesToLoad.Add(new PagesToLoad + { + Page = currentPage, + Skip = currentlyLoaded, + Take = toLoad + }); + } + + return pagesToLoad; + } + } + + public class PagesToLoad + { + public int Page { get; set; } + public int Take { get; set; } + public int Skip { get; set; } + } +} diff --git a/src/Ombi.TheMovieDbApi/TheMovieDbApi.cs b/src/Ombi.TheMovieDbApi/TheMovieDbApi.cs index e184a4e7e..c5aafca7a 100644 --- a/src/Ombi.TheMovieDbApi/TheMovieDbApi.cs +++ b/src/Ombi.TheMovieDbApi/TheMovieDbApi.cs @@ -171,7 +171,7 @@ namespace Ombi.Api.TheMovieDb var request = new Request($"movie/popular", BaseUri, HttpMethod.Get); request.FullUri = request.FullUri.AddQueryParameter("api_key", ApiToken); request.FullUri = request.FullUri.AddQueryParameter("language", langageCode); - request.FullUri = request.FullUri.AddQueryParameter("page", page,ToString()); + request.FullUri = request.FullUri.AddQueryParameter("page", page.ToString()); AddRetry(request); var result = await Api.Request>(request); return Mapper.Map>(result.results); diff --git a/src/Ombi/ClientApp/src/app/discover/discover.component.html b/src/Ombi/ClientApp/src/app/discover/discover.component.html index 97766ca24..cffab5d79 100644 --- a/src/Ombi/ClientApp/src/app/discover/discover.component.html +++ b/src/Ombi/ClientApp/src/app/discover/discover.component.html @@ -1,34 +1,28 @@ +
-
- -
-
- - - -
-
-
- -
-
-
-
- - -
+
+
+ + +
+
+
+ +
+
+
+ +
+
diff --git a/src/Ombi/ClientApp/src/app/discover/discover.component.scss b/src/Ombi/ClientApp/src/app/discover/discover.component.scss index 728ff23c5..8e05d0955 100644 --- a/src/Ombi/ClientApp/src/app/discover/discover.component.scss +++ b/src/Ombi/ClientApp/src/app/discover/discover.component.scss @@ -16,4 +16,8 @@ .loading-spinner { margin: 10%; -} \ No newline at end of file +} +#scroller { + height: 100vh; + overflow: scroll; + } \ No newline at end of file