-
Notifications
You must be signed in to change notification settings - Fork 1
/
PaginatorLib.cs
112 lines (91 loc) · 2.84 KB
/
PaginatorLib.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using BBRAPIModules;
using System;
using System.Collections.Generic;
using System.Linq;
namespace BBRModules
{
[Module("A library to simplify paginating strings, lists, and parameters with a specifiable page size.", "1.0.1")]
public class PaginatorLib : BattleBitModule
{
public int PageNum { get; set; } = 0;
public List<object> Objects { get; set; }
public int PageSize { get; set; } = 10;
public PaginatorLib Create() => new PaginatorLib();
public PaginatorLib Create(IEnumerable<object> objects) => new PaginatorLib(objects);
public PaginatorLib()
{
Objects = new();
}
public PaginatorLib(IEnumerable<object> objects)
{
Objects = new(objects);
}
public PaginatorLib AddObject(object obj)
{
Objects.Add(obj);
return this;
}
public PaginatorLib AddObjects(params object[] objects)
{
foreach (object obj in objects)
{
Objects.Add(objects);
}
return this;
}
public PaginatorLib AddObjects(IEnumerable<object> objects)
{
foreach (object obj in objects)
{
Objects.Add(objects);
}
return this;
}
public PaginatorLib Insert(object obj, int index)
{
Objects.Insert(index, obj);
return this;
}
public PaginatorLib RemoveObject(object obj)
{
Objects.Remove(obj);
return this;
}
public PaginatorLib RemoveObjectAt(int index)
{
Objects.RemoveAt(index);
return this;
}
public int GetPageSize() => PageSize;
public PaginatorLib SetPageSize(int pageSize)
{
PageSize = pageSize;
return this;
}
public int CountPages() => (int) Math.Ceiling((double) Objects.Count / PageSize);
public List<object> GetPage(int pageNum)
{
if (pageNum > CountPages())
return new();
return Objects.Take(new Range((pageNum * PageSize) - PageSize, pageNum * PageSize)).ToList()!;
}
public List<string> GetPageAsStrings(int pageNum)
{
if (pageNum > CountPages())
return new();
return Objects.Take(new Range((pageNum * PageSize) - PageSize, pageNum * PageSize)).Select(o => o.ToString()).ToList()!;
}
public int GetNextPageNumber()
{
if (PageNum + 1 > CountPages())
PageNum = 0;
PageNum++;
return PageNum;
}
public bool HasNextPage() => !(PageNum + 1 > CountPages());
public void ResetPageNumber()
{
PageNum = 0;
}
}
}