Skip to content

Commit

Permalink
feat: Allow to share products (openfoodfacts#2287)
Browse files Browse the repository at this point in the history
* feat: Allow to send products

* Use getProductUri instead + matomo
  • Loading branch information
M123-dev authored Jun 15, 2022
1 parent e288524 commit 347c39c
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 16 deletions.
9 changes: 9 additions & 0 deletions packages/smooth_app/lib/helpers/analytics_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class AnalyticsHelper {
static const String _scanAction = 'scanned product';
static const String _productPageAction = 'opened product page';
static const String _personalizedRankingAction = 'personalized ranking';
static const String _shareProductActionn = 'shared product';
static const String _loginAction = 'logged in';
static const String _registerAction = 'register';
static const String _userManagmentCategory = 'user management';
Expand Down Expand Up @@ -138,6 +139,14 @@ class AnalyticsHelper {
static void trackOpenLink({required String url}) =>
MatomoTracker.instance.trackOutlink(url);

static void trackShareProduct({required String barcode}) =>
MatomoTracker.instance.trackEvent(
eventName: _shareProductActionn,
action: 'shared',
eventCategory: 'product page',
eventValue: int.tryParse(barcode),
);

static void trackLogin() => MatomoTracker.instance
.trackEvent(action: _loginAction, eventCategory: _userManagmentCategory);

Expand Down
13 changes: 13 additions & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1343,5 +1343,18 @@
"add_photo_button_label": "Add photo",
"@add_photo_button_label": {
"description": "Label for the add photo button"
},
"share": "Share",
"@share": {
"description": "Button label for sharing something on another app. For example sharing the link to a product via Email"
},
"share_product_text": "Have a look at this product on Open Food Facts: {url}",
"@share_product_text": {
"description": "The content which is send, when sharing a product",
"placeholders": {
"url": {
"type": "String"
}
}
}
}
58 changes: 42 additions & 16 deletions packages/smooth_app/lib/pages/product/new_product_page.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:matomo_tracker/matomo_tracker.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:share_plus/share_plus.dart';
import 'package:smooth_app/cards/product_cards/product_image_carousel.dart';
import 'package:smooth_app/data_models/fetched_product.dart';
import 'package:smooth_app/data_models/product_list.dart';
Expand Down Expand Up @@ -293,6 +295,23 @@ class _ProductPageState extends State<ProductPage> with TraceableClientMixin {
}
}

Future<void> _shareProduct() async {
AnalyticsHelper.trackShareProduct(barcode: widget.product.barcode!);
final AppLocalizations appLocalizations = AppLocalizations.of(context);
// We need to provide a sharePositionOrigin to make the plugin work on ipad
final RenderBox? box = context.findRenderObject() as RenderBox?;
final String url = OpenFoodAPIClient.getProductUri(
widget.product.barcode!,
replaceSubdomain: true,
country: ProductQuery.getCountry(),
).toString();

Share.share(
appLocalizations.share_product_text(url),
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
);
}

Widget _buildActionBar(final AppLocalizations appLocalizations) => Padding(
padding: const EdgeInsets.all(SMALL_SPACE),
child: Row(
Expand Down Expand Up @@ -323,6 +342,11 @@ class _ProductPageState extends State<ProductPage> with TraceableClientMixin {
}
},
),
_buildActionBarItem(
ConstantIcons.instance.getShareIcon(),
appLocalizations.share,
_shareProduct,
),
],
),
);
Expand All @@ -334,23 +358,25 @@ class _ProductPageState extends State<ProductPage> with TraceableClientMixin {
) {
final ThemeData themeData = Theme.of(context);
final ColorScheme colorScheme = themeData.colorScheme;
return Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
padding: const EdgeInsets.all(
18), // TODO(monsieurtanuki): cf. FloatingActionButton
primary: colorScheme.primary,
return Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
padding: const EdgeInsets.all(
18), // TODO(monsieurtanuki): cf. FloatingActionButton
primary: colorScheme.primary,
),
child: Icon(iconData, color: colorScheme.onPrimary),
),
child: Icon(iconData, color: colorScheme.onPrimary),
),
const SizedBox(height: VERY_SMALL_SPACE),
Text(label),
],
const SizedBox(height: VERY_SMALL_SPACE),
AutoSizeText(label, textAlign: TextAlign.center),
],
),
);
}

Expand Down
49 changes: 49 additions & 0 deletions packages/smooth_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
mime:
dependency: transitive
description:
name: mime
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
mockito:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -1012,6 +1019,48 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "6.5.1"
share_plus:
dependency: "direct main"
description:
name: share_plus
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.8"
share_plus_linux:
dependency: transitive
description:
name: share_plus_linux
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0"
share_plus_macos:
dependency: transitive
description:
name: share_plus_macos
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
share_plus_platform_interface:
dependency: transitive
description:
name: share_plus_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.3"
share_plus_web:
dependency: transitive
description:
name: share_plus_web
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
share_plus_windows:
dependency: transitive
description:
name: share_plus_windows
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
shared_preferences:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions packages/smooth_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ dependencies:
path: ../data_importer_shared
data_importer:
path: ../data_importer
share_plus: ^4.0.8

dev_dependencies:
integration_test:
Expand Down

0 comments on commit 347c39c

Please sign in to comment.