forked from openfoodfacts/openfoodfacts-dart
/
KnowledgePanelElement.dart
272 lines (207 loc) · 7.81 KB
/
KnowledgePanelElement.dart
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import 'package:json_annotation/json_annotation.dart';
import 'package:openfoodfacts/model/KnowledgePanel.dart';
import '../interface/JsonObject.dart';
part 'KnowledgePanelElement.g.dart';
/// The type of Knowledge panel text description.
enum KnowledgePanelTextElementType {
/// The description summarizes the knowledge panel.
@JsonValue('summary')
SUMMARY,
@JsonValue('warning')
WARNING,
/// Disclaimer notes that the client may or may not choose to display.
@JsonValue('notes')
NOTES,
/// Default type of the text element, this is just a normal description.
@JsonValue('notes')
DEFAULT,
}
/// The type of Knowledge panel table column.
enum KnowledgePanelColumnType {
/// The column contains text elements.
@JsonValue('text')
TEXT,
/// The column has percentages.
@JsonValue('percent')
PERCENT,
}
/// Description element of the Knowledge panel.
@JsonSerializable()
class KnowledgePanelTextElement extends JsonObject {
/// HTML description of one Knowledge Panel Unit.
final String html;
/// Type of the text description, Client may choose to display the description
/// depending upon the type.
@JsonKey(
name: 'text_type',
unknownEnumValue: KnowledgePanelTextElementType.DEFAULT)
final KnowledgePanelTextElementType? type;
const KnowledgePanelTextElement({required this.html, this.type});
factory KnowledgePanelTextElement.fromJson(Map<String, dynamic> json) =>
_$KnowledgePanelTextElementFromJson(json);
@override
Map<String, dynamic> toJson() => _$KnowledgePanelTextElementToJson(this);
}
/// Image that represents the KnowledgePanel.
@JsonSerializable()
class KnowledgePanelImageElement extends JsonObject {
/// Url of the image.
final String url;
/// Width of the image.
///
/// This is just a suggestion coming from the server, the client may choose to
/// use its own dimensions for the image.
final int? width;
/// Height of the image.
///
/// This is just a suggestion coming from the server, the client may choose to
/// use its own dimensions for the image.
final int? height;
/// Alt Text of the image.
@JsonKey(name: 'alt_text')
final String? altText;
const KnowledgePanelImageElement(
{required this.url, this.width, this.height, this.altText});
factory KnowledgePanelImageElement.fromJson(Map<String, dynamic> json) =>
_$KnowledgePanelImageElementFromJson(json);
@override
Map<String, dynamic> toJson() => _$KnowledgePanelImageElementToJson(this);
}
/// Element representing a Panel group that contains 1+ KnowledgePanels.
@JsonSerializable()
class KnowledgePanelPanelGroupElement extends JsonObject {
/// Title of the panel group. Example: "Carbon Footprint" or "Labels" etc.
final String title;
/// List of panel ids that belong to this panel group.
@JsonKey(name: 'panel_ids')
final List<String> panelIds;
const KnowledgePanelPanelGroupElement(
{required this.title, required this.panelIds});
factory KnowledgePanelPanelGroupElement.fromJson(Map<String, dynamic> json) =>
_$KnowledgePanelPanelGroupElementFromJson(json);
@override
Map<String, dynamic> toJson() =>
_$KnowledgePanelPanelGroupElementToJson(this);
}
/// Element representing a Panel Id of a KnowledgePanel. This element is a
/// Knowledge panel itself, the KnowledgePanel can be found in the list of
/// Knowledge panels using the id.
@JsonSerializable()
class KnowledgePanelPanelIdElement extends JsonObject {
@JsonKey(name: 'panel_id')
final String panelId;
const KnowledgePanelPanelIdElement({required this.panelId});
factory KnowledgePanelPanelIdElement.fromJson(Map<String, dynamic> json) =>
_$KnowledgePanelPanelIdElementFromJson(json);
@override
Map<String, dynamic> toJson() => _$KnowledgePanelPanelIdElementToJson(this);
}
/// Provides the values for each table cell inside a KnowledgePanel table.
@JsonSerializable()
class KnowledgePanelTableCell extends JsonObject {
final String text;
final double? percent;
@JsonKey(name: 'icon_url')
final String? iconUrl;
/// Evaluation of the table cell.
@JsonKey(unknownEnumValue: Evaluation.UNKNOWN)
final Evaluation? evaluation;
const KnowledgePanelTableCell(
{required this.text, this.percent, this.iconUrl, this.evaluation});
factory KnowledgePanelTableCell.fromJson(Map<String, dynamic> json) =>
_$KnowledgePanelTableCellFromJson(json);
@override
Map<String, dynamic> toJson() => _$KnowledgePanelTableCellToJson(this);
}
/// A table row inside Table element of KonwledgePanel
@JsonSerializable()
class KnowledgePanelTableRowElement extends JsonObject {
final List<KnowledgePanelTableCell> values;
const KnowledgePanelTableRowElement({required this.values});
factory KnowledgePanelTableRowElement.fromJson(Map<String, dynamic> json) =>
_$KnowledgePanelTableRowElementFromJson(json);
@override
Map<String, dynamic> toJson() => _$KnowledgePanelTableRowElementToJson(this);
}
/// A descriptor that describes the type and label of each column.
@JsonSerializable()
class KnowledgePanelTableColumn extends JsonObject {
final String text;
@JsonKey(unknownEnumValue: KnowledgePanelColumnType.TEXT)
final KnowledgePanelColumnType? type;
const KnowledgePanelTableColumn({required this.text, required this.type});
factory KnowledgePanelTableColumn.fromJson(Map<String, dynamic> json) =>
_$KnowledgePanelTableColumnFromJson(json);
@override
Map<String, dynamic> toJson() => _$KnowledgePanelTableColumnToJson(this);
}
/// Element representing a tabular data for the KnowledgePanel.
@JsonSerializable()
class KnowledgePanelTableElement extends JsonObject {
final String id;
final String title;
@JsonKey(name: 'columns')
final List<KnowledgePanelTableColumn> columns;
final List<KnowledgePanelTableRowElement> rows;
const KnowledgePanelTableElement({
required this.id,
required this.title,
required this.columns,
required this.rows,
});
factory KnowledgePanelTableElement.fromJson(Map<String, dynamic> json) =>
_$KnowledgePanelTableElementFromJson(json);
@override
Map<String, dynamic> toJson() => _$KnowledgePanelTableElementToJson(this);
}
/// The type of Knowledge panel.
enum KnowledgePanelElementType {
/// The description summarizes the knowledge panel.
@JsonValue('text')
TEXT,
@JsonValue('image')
IMAGE,
@JsonValue('panel')
PANEL,
@JsonValue('panel_group')
PANEL_GROUP,
@JsonValue('table')
TABLE,
UNKNOWN,
}
/// KnowledgePanelElement is a single unit of KnowledgePanel that can be rendered on the client.
///
/// An Element could be one of [{@code ]KnowledgePanelElementType].
@JsonSerializable()
class KnowledgePanelElement extends JsonObject {
/// Type of the text description, Client may choose to display the description
/// depending upon the type.
@JsonKey(name: 'element_type')
final KnowledgePanelElementType elementType;
/// Text description of the Knowledge panel.
@JsonKey(name: 'text_element')
final KnowledgePanelTextElement? textElement;
/// Image element of the Knowledge panel.
@JsonKey(name: 'image_element')
final KnowledgePanelImageElement? imageElement;
/// Id of a KnowledgePanel embedded inside [this] KnowledgePanel.
@JsonKey(name: 'panel_element')
final KnowledgePanelPanelIdElement? panelElement;
@JsonKey(name: 'panel_group_element')
final KnowledgePanelPanelGroupElement? panelGroupElement;
/// Id of a KnowledgePanel embedded inside [this] KnowledgePanel.
@JsonKey(name: 'table_element')
final KnowledgePanelTableElement? tableElement;
const KnowledgePanelElement({
required this.elementType,
this.textElement,
this.imageElement,
this.panelElement,
this.panelGroupElement,
this.tableElement,
});
factory KnowledgePanelElement.fromJson(Map<String, dynamic> json) =>
_$KnowledgePanelElementFromJson(json);
@override
Map<String, dynamic> toJson() => _$KnowledgePanelElementToJson(this);
}