From 5163db593f89631e922c3c2085809ef60b3a42d9 Mon Sep 17 00:00:00 2001 From: jasmeet0817 Date: Tue, 16 Nov 2021 16:50:41 +0100 Subject: [PATCH] fix: Update to latest KnowledgePanel API changes (#294) --- lib/model/KnowledgePanel.dart | 67 ++++++++++--------------- lib/model/KnowledgePanel.g.dart | 69 +++++++++++++------------- lib/model/KnowledgePanelElement.dart | 23 ++++++--- lib/model/KnowledgePanelElement.g.dart | 24 +++++---- 4 files changed, 90 insertions(+), 93 deletions(-) diff --git a/lib/model/KnowledgePanel.dart b/lib/model/KnowledgePanel.dart index 4c583d30ba..e8d8635dda 100644 --- a/lib/model/KnowledgePanel.dart +++ b/lib/model/KnowledgePanel.dart @@ -5,26 +5,6 @@ import 'KnowledgePanelElement.dart'; part 'KnowledgePanel.g.dart'; -/// Type of the KnowledgePanel. -enum KnowledgePanelType { - /// Do you know types informative Knowledge Panels. - @JsonValue('doyouknow') - DO_YOU_KNOW, - - /// Knowledge Panel with ecoscore information. - @JsonValue('score') - SCORE, - - /// Knowledge Panel with ecoscore LCA. - @JsonValue('ecoscore_lca') - ECOSCORE_LCA, - - /// Knowledge Panel which is rendered as a card on the UI. - @JsonValue('card') - CARD, - UNKNOWN, -} - /// Level of information conveyed by this KnowledgePanel. /// /// Client may choose to display the panel based on the level. @@ -62,11 +42,21 @@ enum Evaluation { GOOD, @JsonValue('neutral') NEUTRAL, + @JsonValue('average') + AVERAGE, @JsonValue('bad') BAD, UNKNOWN, } +/// Type of title element. +enum TitleElementType { + // Title Element depicts a grade like 'Ecoscore' or 'Nutriscore'. + @JsonValue('grade') + GRADE, + UNKNOWN, +} + /// KnowledgePanels are a standardized and generic units of information that /// the client can display on the product page. /// @@ -74,18 +64,14 @@ enum Evaluation { // NOTE: This is WIP, do not use and expect changes. @JsonSerializable() class KnowledgePanel extends JsonObject { - /// Panel id of the parent panel. - @JsonKey(name: 'parent_panel_id') - final String parentPanelId; - /// Title of the KnowledgePanel. @JsonKey(name: 'title_element') - final TitleElement titleElement; + final TitleElement? titleElement; /// Level of this KnowledgePanel. Client may choose to display the panel based /// on the level. @JsonKey(unknownEnumValue: Level.UNKNOWN) - final Level level; + final Level? level; final bool? expanded; @@ -93,32 +79,20 @@ class KnowledgePanel extends JsonObject { /// rendered on the client. final List? elements; - final KnowledgePanelType? type; - /// The topics discussed in this knowledge panel, example: 'Environment'. final List? topics; - /// Grade of the panel, depicting the level of impact the product has for the - /// corresponding topics. Client can choose to color code the panel depending - /// on how good/bad the grade is. - /// Scale: 'A' -> 'E' - @JsonKey(unknownEnumValue: Grade.UNKNOWN) - final Grade? grade; - /// Evaluation of the panel, depicting whether the content of the panel is /// good/bad/neutral for the topic to which the panel applies. @JsonKey(unknownEnumValue: Evaluation.UNKNOWN) final Evaluation? evaluation; const KnowledgePanel({ - required this.parentPanelId, - required this.titleElement, - required this.level, + this.titleElement, + this.level, this.expanded, this.elements, - this.type, this.topics, - this.grade, this.evaluation, }); @@ -139,6 +113,17 @@ class TitleElement extends JsonObject { /// Subtitle of the panel. Example - 'High environmental impact'. final String? subtitle; + /// Grade of the panel, depicting the level of impact the product has for the + /// corresponding topics. Client can choose to color code the panel depending + /// on how good/bad the grade is. + /// Scale: 'A' -> 'E' + @JsonKey(unknownEnumValue: Grade.UNKNOWN) + final Grade? grade; + + /// Type of the TitleElement. + @JsonKey(unknownEnumValue: TitleElementType.UNKNOWN) + final TitleElementType? type; + /// URL of an icon representing the Panel. @JsonKey(name: 'icon_url') final String? iconUrl; @@ -152,6 +137,8 @@ class TitleElement extends JsonObject { const TitleElement({ required this.title, this.subtitle, + this.grade, + this.type, this.iconUrl, this.iconColorFromEvaluation, }); diff --git a/lib/model/KnowledgePanel.g.dart b/lib/model/KnowledgePanel.g.dart index 05ae4fc250..b19d4c2721 100644 --- a/lib/model/KnowledgePanel.g.dart +++ b/lib/model/KnowledgePanel.g.dart @@ -8,35 +8,30 @@ part of 'KnowledgePanel.dart'; KnowledgePanel _$KnowledgePanelFromJson(Map json) => KnowledgePanel( - parentPanelId: json['parent_panel_id'] as String, - titleElement: - TitleElement.fromJson(json['title_element'] as Map), - level: _$enumDecode(_$LevelEnumMap, json['level'], + titleElement: json['title_element'] == null + ? null + : TitleElement.fromJson( + json['title_element'] as Map), + level: _$enumDecodeNullable(_$LevelEnumMap, json['level'], unknownValue: Level.UNKNOWN), expanded: json['expanded'] as bool?, elements: (json['elements'] as List?) ?.map( (e) => KnowledgePanelElement.fromJson(e as Map)) .toList(), - type: _$enumDecodeNullable(_$KnowledgePanelTypeEnumMap, json['type']), topics: (json['topics'] as List?)?.map((e) => e as String).toList(), - grade: _$enumDecodeNullable(_$GradeEnumMap, json['grade'], - unknownValue: Grade.UNKNOWN), evaluation: _$enumDecodeNullable(_$EvaluationEnumMap, json['evaluation'], unknownValue: Evaluation.UNKNOWN), ); Map _$KnowledgePanelToJson(KnowledgePanel instance) => { - 'parent_panel_id': instance.parentPanelId, 'title_element': instance.titleElement, 'level': _$LevelEnumMap[instance.level], 'expanded': instance.expanded, 'elements': instance.elements, - 'type': _$KnowledgePanelTypeEnumMap[instance.type], 'topics': instance.topics, - 'grade': _$GradeEnumMap[instance.grade], 'evaluation': _$EvaluationEnumMap[instance.evaluation], }; @@ -66,15 +61,6 @@ K _$enumDecode( ).key; } -const _$LevelEnumMap = { - Level.TRIVIA: 'trivia', - Level.INFO: 'info', - Level.HELPFUL: 'helpful', - Level.WARNING: 'warning', - Level.ALERT: 'alert', - Level.UNKNOWN: 'UNKNOWN', -}; - K? _$enumDecodeNullable( Map enumValues, dynamic source, { @@ -86,26 +72,19 @@ K? _$enumDecodeNullable( return _$enumDecode(enumValues, source, unknownValue: unknownValue); } -const _$KnowledgePanelTypeEnumMap = { - KnowledgePanelType.DO_YOU_KNOW: 'doyouknow', - KnowledgePanelType.SCORE: 'score', - KnowledgePanelType.ECOSCORE_LCA: 'ecoscore_lca', - KnowledgePanelType.CARD: 'card', - KnowledgePanelType.UNKNOWN: 'UNKNOWN', -}; - -const _$GradeEnumMap = { - Grade.A: 'A', - Grade.B: 'B', - Grade.C: 'C', - Grade.D: 'D', - Grade.E: 'E', - Grade.UNKNOWN: 'UNKNOWN', +const _$LevelEnumMap = { + Level.TRIVIA: 'trivia', + Level.INFO: 'info', + Level.HELPFUL: 'helpful', + Level.WARNING: 'warning', + Level.ALERT: 'alert', + Level.UNKNOWN: 'UNKNOWN', }; const _$EvaluationEnumMap = { Evaluation.GOOD: 'good', - Evaluation.NEUTRAL: 'neutral', + Evaluation.NEUTRAL: 'average', + Evaluation.AVERAGE: 'average', Evaluation.BAD: 'bad', Evaluation.UNKNOWN: 'UNKNOWN', }; @@ -113,6 +92,10 @@ const _$EvaluationEnumMap = { TitleElement _$TitleElementFromJson(Map json) => TitleElement( title: json['title'] as String, subtitle: json['subtitle'] as String?, + grade: _$enumDecodeNullable(_$GradeEnumMap, json['grade'], + unknownValue: Grade.UNKNOWN), + type: _$enumDecodeNullable(_$TitleElementTypeEnumMap, json['type'], + unknownValue: TitleElementType.UNKNOWN), iconUrl: json['icon_url'] as String?, iconColorFromEvaluation: json['icon_color_from_evaluation'] as bool? ?? false, @@ -122,6 +105,22 @@ Map _$TitleElementToJson(TitleElement instance) => { 'title': instance.title, 'subtitle': instance.subtitle, + 'grade': _$GradeEnumMap[instance.grade], + 'type': _$TitleElementTypeEnumMap[instance.type], 'icon_url': instance.iconUrl, 'icon_color_from_evaluation': instance.iconColorFromEvaluation, }; + +const _$GradeEnumMap = { + Grade.A: 'A', + Grade.B: 'B', + Grade.C: 'C', + Grade.D: 'D', + Grade.E: 'E', + Grade.UNKNOWN: 'UNKNOWN', +}; + +const _$TitleElementTypeEnumMap = { + TitleElementType.GRADE: 'grade', + TitleElementType.UNKNOWN: 'UNKNOWN', +}; diff --git a/lib/model/KnowledgePanelElement.dart b/lib/model/KnowledgePanelElement.dart index c8e91eb946..5784a53941 100644 --- a/lib/model/KnowledgePanelElement.dart +++ b/lib/model/KnowledgePanelElement.dart @@ -20,7 +20,17 @@ enum KnowledgePanelTextElementType { /// Default type of the text element, this is just a normal description. @JsonValue('notes') DEFAULT, - UNKNOWN, +} + +/// 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. @@ -33,7 +43,7 @@ class KnowledgePanelTextElement extends JsonObject { /// depending upon the type. @JsonKey( name: 'text_type', - unknownEnumValue: KnowledgePanelTextElementType.UNKNOWN) + unknownEnumValue: KnowledgePanelTextElementType.DEFAULT) final KnowledgePanelTextElementType? type; const KnowledgePanelTextElement({required this.html, this.type}); @@ -158,7 +168,8 @@ class KnowledgePanelTableRowElement extends JsonObject { class KnowledgePanelTableColumn extends JsonObject { final String text; - final String type; + @JsonKey(unknownEnumValue: KnowledgePanelColumnType.TEXT) + final KnowledgePanelColumnType? type; const KnowledgePanelTableColumn({required this.text, required this.type}); @@ -174,9 +185,6 @@ class KnowledgePanelTableColumn extends JsonObject { class KnowledgePanelTableElement extends JsonObject { final String id; - @JsonKey(name: 'type') - final String type; - final String title; @JsonKey(name: 'columns') @@ -186,7 +194,6 @@ class KnowledgePanelTableElement extends JsonObject { const KnowledgePanelTableElement({ required this.id, - required this.type, required this.title, required this.columns, required this.rows, @@ -241,7 +248,7 @@ class KnowledgePanelElement extends JsonObject { @JsonKey(name: 'panel_element') final KnowledgePanelPanelIdElement? panelElement; - @JsonKey(name: 'panel_group') + @JsonKey(name: 'panel_group_element') final KnowledgePanelPanelGroupElement? panelGroupElement; /// Id of a KnowledgePanel embedded inside [this] KnowledgePanel. diff --git a/lib/model/KnowledgePanelElement.g.dart b/lib/model/KnowledgePanelElement.g.dart index a4e24e758d..56599817b6 100644 --- a/lib/model/KnowledgePanelElement.g.dart +++ b/lib/model/KnowledgePanelElement.g.dart @@ -12,7 +12,7 @@ KnowledgePanelTextElement _$KnowledgePanelTextElementFromJson( html: json['html'] as String, type: _$enumDecodeNullable( _$KnowledgePanelTextElementTypeEnumMap, json['text_type'], - unknownValue: KnowledgePanelTextElementType.UNKNOWN), + unknownValue: KnowledgePanelTextElementType.DEFAULT), ); Map _$KnowledgePanelTextElementToJson( @@ -64,7 +64,6 @@ const _$KnowledgePanelTextElementTypeEnumMap = { KnowledgePanelTextElementType.WARNING: 'warning', KnowledgePanelTextElementType.NOTES: 'notes', KnowledgePanelTextElementType.DEFAULT: 'notes', - KnowledgePanelTextElementType.UNKNOWN: 'UNKNOWN', }; KnowledgePanelImageElement _$KnowledgePanelImageElementFromJson( @@ -133,7 +132,7 @@ Map _$KnowledgePanelTableCellToJson( const _$EvaluationEnumMap = { Evaluation.GOOD: 'good', - Evaluation.NEUTRAL: 'neutral', + Evaluation.AVERAGE: 'average', Evaluation.BAD: 'bad', Evaluation.UNKNOWN: 'UNKNOWN', }; @@ -157,21 +156,27 @@ KnowledgePanelTableColumn _$KnowledgePanelTableColumnFromJson( Map json) => KnowledgePanelTableColumn( text: json['text'] as String, - type: json['type'] as String, + type: _$enumDecodeNullable( + _$KnowledgePanelColumnTypeEnumMap, json['type'], + unknownValue: KnowledgePanelColumnType.TEXT), ); Map _$KnowledgePanelTableColumnToJson( KnowledgePanelTableColumn instance) => { 'text': instance.text, - 'type': instance.type, + 'type': _$KnowledgePanelColumnTypeEnumMap[instance.type], }; +const _$KnowledgePanelColumnTypeEnumMap = { + KnowledgePanelColumnType.TEXT: 'text', + KnowledgePanelColumnType.PERCENT: 'percent', +}; + KnowledgePanelTableElement _$KnowledgePanelTableElementFromJson( Map json) => KnowledgePanelTableElement( id: json['id'] as String, - type: json['type'] as String, title: json['title'] as String, columns: (json['columns'] as List) .map((e) => @@ -187,7 +192,6 @@ Map _$KnowledgePanelTableElementToJson( KnowledgePanelTableElement instance) => { 'id': instance.id, - 'type': instance.type, 'title': instance.title, 'columns': instance.columns, 'rows': instance.rows, @@ -210,10 +214,10 @@ KnowledgePanelElement _$KnowledgePanelElementFromJson( ? null : KnowledgePanelPanelIdElement.fromJson( json['panel_element'] as Map), - panelGroupElement: json['panel_group'] == null + panelGroupElement: json['panel_group_element'] == null ? null : KnowledgePanelPanelGroupElement.fromJson( - json['panel_group'] as Map), + json['panel_group_element'] as Map), tableElement: json['table_element'] == null ? null : KnowledgePanelTableElement.fromJson( @@ -227,7 +231,7 @@ Map _$KnowledgePanelElementToJson( 'text_element': instance.textElement, 'image_element': instance.imageElement, 'panel_element': instance.panelElement, - 'panel_group': instance.panelGroupElement, + 'panel_group_element': instance.panelGroupElement, 'table_element': instance.tableElement, };