mirror of https://github.com/Ombi-app/Ombi
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.0 KiB
65 lines
2.0 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace Ombi.Helpers
|
|
{
|
|
public static class PaginationHelper
|
|
{
|
|
public static List<PagesToLoad> GetNextPages(int currentlyLoaded, int toTake, int maxItemsPerPage)
|
|
{
|
|
var result = new List<PagesToLoad>();
|
|
|
|
var firstPage = currentlyLoaded / maxItemsPerPage + 1;
|
|
var startPos = currentlyLoaded % maxItemsPerPage + 1;
|
|
|
|
var lastItemIndex = currentlyLoaded + toTake - 1;
|
|
var lastPage = lastItemIndex / maxItemsPerPage + 1;
|
|
var stopPos = lastItemIndex % maxItemsPerPage + 1;
|
|
|
|
while (currentlyLoaded >= maxItemsPerPage)
|
|
{
|
|
currentlyLoaded -= maxItemsPerPage;
|
|
}
|
|
if ((currentlyLoaded % maxItemsPerPage) == 0 && (currentlyLoaded % toTake) == 0)
|
|
{
|
|
currentlyLoaded = 0;
|
|
}
|
|
|
|
var page1 = new PagesToLoad { Page = firstPage, Skip = currentlyLoaded, Take = toTake };
|
|
|
|
if (toTake + startPos - 1 > maxItemsPerPage)
|
|
{
|
|
page1.Take = maxItemsPerPage - startPos + 1;
|
|
result.Add(page1);
|
|
|
|
for (var i = firstPage + 1; i < lastPage; i++)
|
|
{
|
|
var nextPage = new PagesToLoad { Page = i, Skip = 0, Take = maxItemsPerPage };
|
|
result.Add(nextPage);
|
|
}
|
|
|
|
var pageN = new PagesToLoad { Page = lastPage, Skip = 0, Take = stopPos };
|
|
result.Add(pageN);
|
|
}
|
|
else
|
|
{
|
|
if (page1.Skip + page1.Take > maxItemsPerPage)
|
|
{
|
|
page1.Skip = 0;
|
|
}
|
|
result.Add(page1);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
public class PagesToLoad
|
|
{
|
|
public int Page { get; set; }
|
|
public int Take { get; set; }
|
|
public int Skip { get; set; }
|
|
}
|
|
}
|