Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: copyWith methods for all classes #853

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/src/model/additives.dart
Expand Up @@ -35,4 +35,14 @@ class Additives {

return result;
}

Additives copyWith({
List<String>? ids,
List<String>? names,
}) {
return Additives(
ids ?? this.ids,
names ?? this.names,
);
}
}
8 changes: 8 additions & 0 deletions lib/src/model/agribalyse.dart
Expand Up @@ -15,4 +15,12 @@ class Agribalyse extends JsonObject {

@override
Map<String, dynamic> toJson() => _$AgribalyseToJson(this);

Agribalyse copyWith({
double? score,
}) {
return Agribalyse(
score: score ?? this.score,
);
}
}
10 changes: 10 additions & 0 deletions lib/src/model/allergens.dart
Expand Up @@ -68,4 +68,14 @@ class Allergens {

return result;
}

Allergens copyWith({
List<String>? ids,
List<String>? names,
}) {
return Allergens(
ids ?? this.ids,
names ?? this.names,
);
}
}
30 changes: 30 additions & 0 deletions lib/src/model/attribute.dart
Expand Up @@ -115,4 +115,34 @@ class Attribute extends JsonObject {

@override
String toString() => 'Attribute(${toJson()})';

Attribute copyWith({
String? id,
String? name,
String? title,
String? iconUrl,
String? defaultF,
String? settingNote,
String? settingName,
String? description,
String? descriptionShort,
double? match,
String? status,
String? panelId,
}) {
return Attribute(
id: id ?? this.id,
name: name ?? this.name,
title: title ?? this.title,
iconUrl: iconUrl ?? this.iconUrl,
defaultF: defaultF ?? this.defaultF,
settingNote: settingNote ?? this.settingNote,
settingName: settingName ?? this.settingName,
description: description ?? this.description,
descriptionShort: descriptionShort ?? this.descriptionShort,
match: match ?? this.match,
status: status ?? this.status,
panelId: panelId ?? this.panelId,
);
}
}
15 changes: 15 additions & 0 deletions lib/src/model/attribute_group.dart
Expand Up @@ -50,6 +50,21 @@ class AttributeGroup extends JsonObject {
return result;
}


AttributeGroup copyWith({
String? id,
String? name,
String? warning,
List<Attribute>? attributes,
}) {
return AttributeGroup(
id: id ?? this.id,
name: name ?? this.name,
warning: warning ?? this.warning,
attributes: attributes ?? this.attributes,
);
}

static const String ATTRIBUTE_GROUP_NUTRITIONAL_QUALITY =
'nutritional_quality';
static const String ATTRIBUTE_GROUP_PROCESSING = 'processing';
Expand Down
12 changes: 12 additions & 0 deletions lib/src/model/badge_base.dart
Expand Up @@ -32,4 +32,16 @@ class BadgeBase extends JsonObject {
', level: $level'
'${userId == null ? '' : ', userId: $userId'}'
')';

BadgeBase copyWith({
String? userId,
String? badgeName,
int? level,
}) {
return BadgeBase(
userId: userId ?? this.userId,
badgeName: badgeName ?? this.badgeName,
level: level ?? this.level,
);
}
}
10 changes: 10 additions & 0 deletions lib/src/model/ecoscore_adjustments.dart
Expand Up @@ -19,4 +19,14 @@ class EcoscoreAdjustments extends JsonObject {

@override
Map<String, dynamic> toJson() => _$EcoscoreAdjustmentsToJson(this);

EcoscoreAdjustments copyWith({
Packaging? packaging,
OriginsOfIngredients? originsOfIngredients,
}) {
return EcoscoreAdjustments(
packaging: packaging ?? this.packaging,
originsOfIngredients: originsOfIngredients ?? this.originsOfIngredients,
);
}
}
18 changes: 18 additions & 0 deletions lib/src/model/ecoscore_data.dart
Expand Up @@ -46,4 +46,22 @@ class EcoscoreData extends JsonObject {
Map<String, dynamic> toJson() => _$EcoscoreDataToJson(this);

static Map<String, dynamic>? toJsonHelper(EcoscoreData? d) => d?.toJson();

EcoscoreData copyWith({
String? grade,
double? score,
EcoscoreStatus? status,
Agribalyse? agribalyse,
EcoscoreAdjustments? adjustments,
bool? missingDataWarning,
}) {
return EcoscoreData(
grade: grade ?? this.grade,
score: score ?? this.score,
status: status ?? this.status,
agribalyse: agribalyse ?? this.agribalyse,
adjustments: adjustments ?? this.adjustments,
missingDataWarning: missingDataWarning ?? this.missingDataWarning,
);
}
}
16 changes: 16 additions & 0 deletions lib/src/model/events_base.dart
Expand Up @@ -43,4 +43,20 @@ class EventsBase extends JsonObject {
'${barcode == null ? '' : ', barcode: $barcode'}'
'${points == null ? '' : ', points: $points'}'
')';

EventsBase copyWith({
String? eventType,
DateTime? timestamp,
String? userId,
String? barcode,
int? points,
}) {
return EventsBase(
eventType: eventType ?? this.eventType,
timestamp: timestamp ?? this.timestamp,
userId: userId ?? this.userId,
barcode: barcode ?? this.barcode,
points: points ?? this.points,
);
}
}
26 changes: 26 additions & 0 deletions lib/src/model/ingredient.dart
Expand Up @@ -84,6 +84,32 @@ class Ingredient extends JsonObject {
'${bold == null ? '' : ',bold=$bold'}'
'${ingredients == null ? '' : ',ingredients=$ingredients'}'
')';

Ingredient copyWith({
int? rank,
String? id,
String? text,
double? percent,
double? percentEstimate,
IngredientSpecialPropertyStatus? vegan,
IngredientSpecialPropertyStatus? vegetarian,
IngredientSpecialPropertyStatus? fromPalmOil,
List<Ingredient>? ingredients,
bool? bold,
}) {
return Ingredient(
rank: rank ?? this.rank,
id: id ?? this.id,
text: text ?? this.text,
percent: percent ?? this.percent,
percentEstimate: percentEstimate ?? this.percentEstimate,
vegan: vegan ?? this.vegan,
vegetarian: vegetarian ?? this.vegetarian,
fromPalmOil: fromPalmOil ?? this.fromPalmOil,
ingredients: ingredients ?? this.ingredients,
bold: bold ?? this.bold,
);
}
}

const Map<IngredientSpecialPropertyStatus, String> _MAP = {
Expand Down
30 changes: 30 additions & 0 deletions lib/src/model/insight.dart
Expand Up @@ -75,6 +75,16 @@ class InsightsResult extends JsonObject {

@override
Map<String, dynamic> toJson() => _$InsightsResultToJson(this);

InsightsResult copyWith({
String? status,
List<Insight>? insights,
}) {
return InsightsResult(
status: status ?? this.status,
insights: insights ?? this.insights,
);
}
}

class Insight {
Expand Down Expand Up @@ -137,4 +147,24 @@ class Insight {

return result;
}

Insight copyWith({
String? id,
InsightType? type,
String? barcode,
List<dynamic>? countries,
String? lang,
String? model,
double? confidence,
}) {
return Insight(
id: id ?? this.id,
type: type ?? this.type,
barcode: barcode ?? this.barcode,
countries: countries ?? this.countries,
lang: lang ?? this.lang,
model: model ?? this.model,
confidence: confidence ?? this.confidence,
);
}
}
12 changes: 12 additions & 0 deletions lib/src/model/key_stats.dart
Expand Up @@ -27,4 +27,16 @@ class KeyStats extends JsonObject {

@override
String toString() => toJson().toString();

KeyStats copyWith({
String? key,
int? count,
int? values,
}) {
return KeyStats(
key: key ?? this.key,
count: count ?? this.count,
values: values ?? this.values,
);
}
}
39 changes: 39 additions & 0 deletions lib/src/model/knowledge_panel.dart
Expand Up @@ -120,6 +120,26 @@ class KnowledgePanel extends JsonObject {

@override
Map<String, dynamic> toJson() => _$KnowledgePanelToJson(this);

KnowledgePanel copyWith({
TitleElement? titleElement,
Level? level,
bool? expanded,
List<KnowledgePanelElement>? elements,
List<String>? topics,
final Evaluation? evaluation,
final KnowledgePanelSize? size,
}) {
return KnowledgePanel(
titleElement: titleElement ?? this.titleElement,
level: level ?? this.level,
expanded: expanded ?? this.expanded,
elements: elements ?? this.elements,
topics: topics ?? this.topics,
evaluation: evaluation ?? this.evaluation,
size: size ?? this.size,
);
}
}

/// An element representing the title of the KnowledgePanel which could consist
Expand Down Expand Up @@ -167,4 +187,23 @@ class TitleElement extends JsonObject {

@override
Map<String, dynamic> toJson() => _$TitleElementToJson(this);

TitleElement copyWith({
String? title,
String? subtitle,
final Grade? grade,
final TitleElementType? type,
final String? iconUrl,
final bool? iconColorFromEvaluation,
}) {
return TitleElement(
title: title ?? this.title,
subtitle: subtitle ?? this.subtitle,
grade: grade ?? this.grade,
type: type ?? this.type,
iconUrl: iconUrl ?? this.iconUrl,
iconColorFromEvaluation:
iconColorFromEvaluation ?? this.iconColorFromEvaluation,
);
}
}