Skip to content

Commit 754fd0e

Browse files
committed
refactor: Changed method parameter types from concrete collections to interfaces for improved flexibility
- Changed method parameters in TemplateReader, NodeRenderer, and Drawer from List<T> to IReadOnlyList<T> or IEnumerable<T> - Made methods more generic and reduced dependencies on specific collection implementations - Simplified the DrawListAsync method by directly using the incoming List<int> parameter - Changed the DrawAsync and RegisterFunctions methods to static
1 parent 4c48926 commit 754fd0e

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

src/Render/Drawer.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ namespace Limekuma.Render;
88

99
public sealed class Drawer
1010
{
11-
public async Task<Image> DrawBestsAsync(CommonUser user, IList<CommonRecord> ever,
12-
IList<CommonRecord> current, int everTotal, int currentTotal, string? condition, string prober, IList<string> tags) =>
11+
public async Task<Image> DrawBestsAsync(CommonUser user, IReadOnlyList<CommonRecord> ever,
12+
IReadOnlyList<CommonRecord> current, int everTotal, int currentTotal, string? condition, string prober, IEnumerable<string> tags) =>
1313
await DrawBestsAsync(user, ever, current, everTotal, currentTotal, condition, prober, tags, "./Resources/Layouts/bests.xml");
1414

15-
public async Task<Image> DrawBestsAsync(CommonUser user, IList<CommonRecord> ever,
16-
IList<CommonRecord> current, int everTotal, int currentTotal, string? condition, string prober, IList<string> tags, string xmlPath)
15+
public async Task<Image> DrawBestsAsync(CommonUser user, IReadOnlyList<CommonRecord> ever,
16+
IReadOnlyList<CommonRecord> current, int everTotal, int currentTotal, string? condition, string prober, IEnumerable<string> tags, string xmlPath)
1717
{
1818
int everMax = ever.Count > 0 ? ever[0].DXRating : 0;
1919
int everMin = ever.Count > 0 ? ever[^1].DXRating : 0;
@@ -39,14 +39,13 @@ public async Task<Image> DrawBestsAsync(CommonUser user, IList<CommonRecord> eve
3939
return await DrawAsync(scope, xmlPath);
4040
}
4141

42-
public async Task<Image> DrawListAsync(CommonUser user, IList<CommonRecord> records, int page, int total,
43-
IList<int> counts, int startIndex, string condition, string prober, IList<string> tags) =>
42+
public async Task<Image> DrawListAsync(CommonUser user, IReadOnlyList<CommonRecord> records, int page, int total,
43+
List<int> counts, int startIndex, string condition, string prober, IEnumerable<string> tags) =>
4444
await DrawListAsync(user, records, page, total, counts, startIndex, condition, prober, tags, "./Resources/Layouts/list.xml");
4545

46-
public async Task<Image> DrawListAsync(CommonUser user, IList<CommonRecord> records, int page, int total,
47-
IList<int> counts, int startIndex, string condition, string prober, IList<string> tags, string xmlPath)
46+
public async Task<Image> DrawListAsync(CommonUser user, IReadOnlyList<CommonRecord> records, int page, int total,
47+
List<int> counts, int startIndex, string condition, string prober, IEnumerable<string> tags, string xmlPath)
4848
{
49-
List<int> countList = [.. counts];
5049
int totalCount = counts.Count > 0 ? counts[^1] : 0;
5150
bool mayMask = records.Any(r => r.DXScore is 0 && (r.DXStar > 0 || r.Rank > Ranks.A));
5251
Dictionary<string, object?> scope = new(StringComparer.OrdinalIgnoreCase)
@@ -55,8 +54,8 @@ public async Task<Image> DrawListAsync(CommonUser user, IList<CommonRecord> reco
5554
["pageRecords"] = records,
5655
["pageNumber"] = page,
5756
["totalPages"] = total,
58-
["rankCounts"] = countList[..7],
59-
["comboCounts"] = countList[7..^1],
57+
["rankCounts"] = counts[..7],
58+
["comboCounts"] = counts[7..^1],
6059
["totalCount"] = totalCount,
6160
["startIndex"] = startIndex,
6261
["condition"] = condition,
@@ -67,7 +66,7 @@ public async Task<Image> DrawListAsync(CommonUser user, IList<CommonRecord> reco
6766
return await DrawAsync(scope, xmlPath);
6867
}
6968

70-
private async Task<Image> DrawAsync(IDictionary<string, object?> scope, string xmlPath)
69+
private static async Task<Image> DrawAsync(IDictionary<string, object?> scope, string xmlPath)
7170
{
7271
AsyncNCalcEngine expr = new();
7372
RegisterFunctions(expr);
@@ -77,7 +76,7 @@ private async Task<Image> DrawAsync(IDictionary<string, object?> scope, string x
7776
return NodeRenderer.Render((CanvasNode)tree, assets, assets);
7877
}
7978

80-
private void RegisterFunctions(AsyncNCalcEngine expr)
79+
private static void RegisterFunctions(AsyncNCalcEngine expr)
8180
{
8281
expr.RegisterFunction("ToString", (object x) => Convert.ToString(x));
8382
expr.RegisterFunction("Count", (IList x) => x.Count);

src/Render/NodeRenderer.Layout.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ explicitSize is { } value
1313
? desired
1414
: fallback;
1515

16-
private static List<List<(Node Node, Size Size)>> ResolveStackLines(List<(Node Node, Size Size)> items, bool isRow,
16+
private static List<List<(Node Node, Size Size)>> ResolveStackLines(IEnumerable<(Node Node, Size Size)> items, bool isRow,
1717
bool wrap, float spacing, float containerMain)
1818
{
1919
List<List<(Node Node, Size Size)>> lines = [];
@@ -43,11 +43,11 @@ explicitSize is { } value
4343
return lines;
4444
}
4545

46-
private static List<(float Main, int Cross)> ResolveStackLineSizes(List<List<(Node Node, Size Size)>> lines, bool isRow,
46+
private static List<(float Main, int Cross)> ResolveStackLineSizes(IEnumerable<IReadOnlyList<(Node Node, Size Size)>> lines, bool isRow,
4747
float spacing)
4848
{
4949
List<(float Main, int Cross)> lineSizes = [];
50-
foreach (List<(Node Node, Size Size)> line in lines)
50+
foreach (IReadOnlyList<(Node Node, Size Size)> line in lines)
5151
{
5252
float lineMain = line.Sum(i => isRow ? i.Size.Width : i.Size.Height);
5353
if (line.Count > 1)

src/Render/TemplateReader.NodeParsing.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ private async Task<List<Node>> ParseChildrenAsync(XElement element, object? scop
259259
return children;
260260
}
261261

262-
private async Task<(Node Node, int LastConsumedIndex)> ParseConditionalChainAsync(IReadOnlyList<XElement> siblings,
262+
private async Task<(Node Node, int LastConsumedIndex)> ParseConditionalChainAsync(List<XElement> siblings,
263263
int index, object? scope)
264264
{
265265
XElement ifElement = siblings[index];
@@ -400,7 +400,7 @@ private static InvalidOperationException BuildInvalidForItemsException(string ex
400400
return result;
401401
}
402402

403-
private static void CopyParentScope(object? parent, IDictionary<string, object?> target)
403+
private static void CopyParentScope(object? parent, Dictionary<string, object?> target)
404404
{
405405
if (parent is not IDictionary dictionary)
406406
{

0 commit comments

Comments
 (0)