blob: dc729a36cb2b1aa18989160a005f35f7961ca3e3 (
plain)
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
|
/***************************************************************************************************
Copyright (C) 2025 The Qt Company Ltd.
SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/
using System.Text.Json;
using System.Text.Json.Serialization;
namespace ColorPalette
{
public abstract class ResourceData
{
[Qt.Ignore]
[JsonExtensionData]
public Dictionary<string, JsonElement> Data { get; set; }
public abstract object this[string role] { get; }
public virtual void Add(PaginatedResource resPg)
{ }
public virtual void Update(PaginatedResource resPg)
{ }
public virtual void Remove(PaginatedResource resPg)
{ }
}
public interface IResourcePlugin
{
abstract static List<string> Roles { get; }
}
public sealed partial class PaginatedResource
{
private static List<Func<JsonElement, ResourceData>> Deserializers { get; } = new();
private static HashSet<string> ResourceRoles { get; } = new();
private List<ResourceData> Resources { get; set; } = new();
internal static void RegisterResourceType<T>()
where T : ResourceData, IResourcePlugin
{
foreach (string role in T.Roles)
ResourceRoles.Add(role);
Deserializers.Add(json => JsonSerializer.Deserialize<T>(json));
}
private void RefreshData(List<JsonElement> json)
{
Resources.Clear();
foreach (var element in json) {
ResourceData tentative = null;
foreach (var fnDeserialize in Deserializers) {
if (fnDeserialize(element) is not { } candidate)
continue;
if (tentative == null) {
tentative = candidate;
continue;
}
int tentativeOverflow = tentative.Data?.Count ?? 0;
int candidateOverflow = candidate.Data?.Count ?? 0;
if (tentativeOverflow > candidateOverflow)
tentative = candidate;
}
if (tentative != null)
Resources.Add(tentative);
}
}
}
}
|