Skip to content

Commit

Permalink
fix: merge ProductType extensions on a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
Valimp committed Nov 22, 2024
1 parent 0167f51 commit 39601a0
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 82 deletions.
2 changes: 1 addition & 1 deletion packages/smooth_app/lib/pages/offline_data_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/duration_constants.dart';
import 'package:smooth_app/helpers/app_helper.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/pages/product/product_type_extensions.dart';
import 'package:smooth_app/widgets/smooth_app_bar.dart';
import 'package:smooth_app/widgets/smooth_scaffold.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ import 'package:smooth_app/helpers/product_cards_helper.dart';
import 'package:smooth_app/pages/crop_parameters.dart';
import 'package:smooth_app/pages/image_crop_page.dart';
import 'package:smooth_app/pages/preferences/user_preferences_widgets.dart';
import 'package:smooth_app/pages/product/add_new_product/add_new_product_type.dart';
import 'package:smooth_app/pages/product/add_new_product/product_type_radio_list_tile.dart';
import 'package:smooth_app/pages/product/add_new_product_helper.dart';
import 'package:smooth_app/pages/product/common/product_dialog_helper.dart';
import 'package:smooth_app/pages/product/nutrition_page_loaded.dart';
import 'package:smooth_app/pages/product/product_field_editor.dart';
import 'package:smooth_app/pages/product/product_image_swipeable_view.dart';
import 'package:smooth_app/pages/product/product_type_extensions.dart';
import 'package:smooth_app/pages/product/simple_input_page_helpers.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/widgets/smooth_scaffold.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/pages/product/product_type_extensions.dart';
import 'package:smooth_app/resources/app_icons.dart' as icons;
import 'package:smooth_app/themes/smooth_theme_colors.dart';
import 'package:smooth_app/themes/theme_provider.dart';
Expand All @@ -20,10 +21,10 @@ class ProductTypeRadioListTile extends StatefulWidget {
final void Function(ProductType) onChanged;

@override
State<ProductTypeRadioListTile> createState() => _AddNewProductType();
State<ProductTypeRadioListTile> createState() => _ProductTypeRadioListTile();
}

class _AddNewProductType extends State<ProductTypeRadioListTile>
class _ProductTypeRadioListTile extends State<ProductTypeRadioListTile>
with TickerProviderStateMixin {
AnimationController? _controller;
late Animation<Color?> _colorAnimation;
Expand Down Expand Up @@ -195,32 +196,3 @@ class _AddNewProductType extends State<ProductTypeRadioListTile>
);
}
}

extension ProductTypeExtension on ProductType {
String getTitle(AppLocalizations appLocalizations) {
return switch (this) {
ProductType.food => appLocalizations.product_type_label_food,
ProductType.beauty => appLocalizations.product_type_label_beauty,
ProductType.petFood => appLocalizations.product_type_label_pet_food,
ProductType.product => appLocalizations.product_type_label_product,
};
}

String getSubtitle(AppLocalizations appLocalizations) {
return switch (this) {
ProductType.food => appLocalizations.product_type_subtitle_food,
ProductType.beauty => appLocalizations.product_type_subtitle_beauty,
ProductType.petFood => appLocalizations.product_type_subtitle_pet_food,
ProductType.product => appLocalizations.product_type_subtitle_product,
};
}

String getIllustration() {
return switch (this) {
ProductType.food => 'assets/misc/logo_off_half.svg',
ProductType.beauty => 'assets/misc/logo_obf_half.svg',
ProductType.petFood => 'assets/misc/logo_opff_half.svg',
ProductType.product => 'assets/misc/logo_opf_half.svg',
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/helpers/analytics_helper.dart';
import 'package:smooth_app/pages/product/add_new_product/add_new_product_page.dart';
import 'package:smooth_app/pages/product/product_field_editor.dart';
import 'package:smooth_app/pages/product/product_type_extensions.dart';
import 'package:smooth_app/pages/product/simple_input_page_helpers.dart';
import 'package:smooth_app/query/product_query.dart';

/// "Incomplete product!" card to be displayed in product summary, if relevant.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:smooth_app/pages/product/common/product_query_page_helper.dart';
import 'package:smooth_app/pages/product/edit_product_page.dart';
import 'package:smooth_app/pages/product/product_list_helper.dart';
import 'package:smooth_app/pages/product/product_page/new_product_page.dart';
import 'package:smooth_app/pages/product/product_type_extensions.dart';
import 'package:smooth_app/query/category_product_query.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/resources/app_icons.dart' as icons;
Expand Down
75 changes: 75 additions & 0 deletions packages/smooth_app/lib/pages/product/product_type_extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';

extension ProductTypeExtension on ProductType {
String getTitle(AppLocalizations appLocalizations) {
return switch (this) {
ProductType.food => appLocalizations.product_type_label_food,
ProductType.beauty => appLocalizations.product_type_label_beauty,
ProductType.petFood => appLocalizations.product_type_label_pet_food,
ProductType.product => appLocalizations.product_type_label_product,
};
}

String getSubtitle(AppLocalizations appLocalizations) {
return switch (this) {
ProductType.food => appLocalizations.product_type_subtitle_food,
ProductType.beauty => appLocalizations.product_type_subtitle_beauty,
ProductType.petFood => appLocalizations.product_type_subtitle_pet_food,
ProductType.product => appLocalizations.product_type_subtitle_product,
};
}

String getIllustration() {
return switch (this) {
ProductType.food => 'assets/misc/logo_off_half.svg',
ProductType.beauty => 'assets/misc/logo_obf_half.svg',
ProductType.petFood => 'assets/misc/logo_opff_half.svg',
ProductType.product => 'assets/misc/logo_opf_half.svg',
};
}

String getDomain() => switch (this) {
ProductType.food => 'openfoodfacts',
ProductType.beauty => 'openbeautyfacts',
ProductType.petFood => 'openpetfoodfacts',
ProductType.product => 'openproductsfacts',
};

String getLabel(final AppLocalizations appLocalizations) => switch (this) {
ProductType.food => appLocalizations.product_type_label_food,
ProductType.beauty => appLocalizations.product_type_label_beauty,
ProductType.petFood => appLocalizations.product_type_label_pet_food,
ProductType.product => appLocalizations.product_type_label_product,
};

String getRoadToScoreLabel(final AppLocalizations appLocalizations) =>
switch (this) {
ProductType.food => appLocalizations.hey_incomplete_product_message,
ProductType.beauty =>
appLocalizations.hey_incomplete_product_message_beauty,
ProductType.petFood =>
appLocalizations.hey_incomplete_product_message_pet_food,
ProductType.product =>
appLocalizations.hey_incomplete_product_message_product,
};

String getShareProductLabel(
final AppLocalizations appLocalizations,
final String url,
) =>
switch (this) {
ProductType.food => appLocalizations.share_product_text(
url,
),
ProductType.beauty => appLocalizations.share_product_text_beauty(
url,
),
ProductType.petFood => appLocalizations.share_product_text_pet_food(
url,
),
ProductType.product => appLocalizations.share_product_text_product(
url,
),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import 'package:smooth_app/pages/navigator/app_navigator.dart';
import 'package:smooth_app/pages/product/common/product_dialog_helper.dart';
import 'package:smooth_app/pages/product/common/product_query_page_helper.dart';
import 'package:smooth_app/pages/product/common/search_helper.dart';
import 'package:smooth_app/pages/product/product_type_extensions.dart';
import 'package:smooth_app/query/keywords_product_query.dart';
import 'package:smooth_app/query/product_query.dart';

/// Search helper dedicated to product search.
class SearchProductHelper extends SearchHelper {
Expand Down
48 changes: 1 addition & 47 deletions packages/smooth_app/lib/query/product_query.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/database/dao_string.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/helpers/analytics_helper.dart';
import 'package:smooth_app/pages/preferences/user_preferences_dev_mode.dart';
import 'package:smooth_app/pages/product/product_type_extensions.dart';
import 'package:uuid/uuid.dart';

// ignore: avoid_classes_with_only_static_members
Expand Down Expand Up @@ -295,49 +295,3 @@ abstract class ProductQuery {
ProductField.OWNER_FIELDS,
];
}

extension ProductTypeExtension on ProductType {
String getDomain() => switch (this) {
ProductType.food => 'openfoodfacts',
ProductType.beauty => 'openbeautyfacts',
ProductType.petFood => 'openpetfoodfacts',
ProductType.product => 'openproductsfacts',
};

String getLabel(final AppLocalizations appLocalizations) => switch (this) {
ProductType.food => appLocalizations.product_type_label_food,
ProductType.beauty => appLocalizations.product_type_label_beauty,
ProductType.petFood => appLocalizations.product_type_label_pet_food,
ProductType.product => appLocalizations.product_type_label_product,
};

String getRoadToScoreLabel(final AppLocalizations appLocalizations) =>
switch (this) {
ProductType.food => appLocalizations.hey_incomplete_product_message,
ProductType.beauty =>
appLocalizations.hey_incomplete_product_message_beauty,
ProductType.petFood =>
appLocalizations.hey_incomplete_product_message_pet_food,
ProductType.product =>
appLocalizations.hey_incomplete_product_message_product,
};

String getShareProductLabel(
final AppLocalizations appLocalizations,
final String url,
) =>
switch (this) {
ProductType.food => appLocalizations.share_product_text(
url,
),
ProductType.beauty => appLocalizations.share_product_text_beauty(
url,
),
ProductType.petFood => appLocalizations.share_product_text_pet_food(
url,
),
ProductType.product => appLocalizations.share_product_text_product(
url,
),
};
}

0 comments on commit 39601a0

Please sign in to comment.