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: adding contribution count #3267

Merged
merged 7 commits into from
Nov 7, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_email_sender/flutter_email_sender.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart';
import 'package:openfoodfacts/utils/UserProductSearchQueryConfiguration.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -216,6 +217,33 @@ class _UserPreferencesPageState extends State<UserPreferencesSection> {
);
}

Future<int?> _getMyCount(
prathamsoni11 marked this conversation as resolved.
Show resolved Hide resolved
final String userId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful between userId and email: to be tested if it works in both cases.

final UserProductSearchType type,
final OpenFoodFactsLanguage language,
) async {
final UserProductSearchQueryConfiguration configuration =
UserProductSearchQueryConfiguration(
type: type,
userId: userId,
prathamsoni11 marked this conversation as resolved.
Show resolved Hide resolved
pageSize: 1,
language: language,
prathamsoni11 marked this conversation as resolved.
Show resolved Hide resolved
fields: [],
prathamsoni11 marked this conversation as resolved.
Show resolved Hide resolved
);

final SearchResult result;
try {
result = await OpenFoodAPIClient.searchProducts(
OpenFoodAPIConfiguration.globalUser,
configuration,
queryType: OpenFoodAPIConfiguration.globalQueryType,
);
} catch (e) {
return null;
}
return result.count;
}

@override
Widget build(BuildContext context) {
// We need to listen to reflect login's from outside of the preferences page
Expand All @@ -227,6 +255,11 @@ class _UserPreferencesPageState extends State<UserPreferencesSection> {
final AppLocalizations appLocalizations = AppLocalizations.of(context);
final Size size = MediaQuery.of(context).size;

final String currentLanguageCode =
Localizations.localeOf(context).toString();
final OpenFoodFactsLanguage language =
LanguageHelper.fromJson(currentLanguageCode);
prathamsoni11 marked this conversation as resolved.
Show resolved Hide resolved

final List<Widget> result;

if (OpenFoodAPIConfiguration.globalUser != null) {
Expand All @@ -243,6 +276,9 @@ class _UserPreferencesPageState extends State<UserPreferencesSection> {
iconData: Icons.add_circle_outline,
context: context,
localDatabase: localDatabase,
userId: userId,
type: UserProductSearchType.CONTRIBUTOR,
language: language,
),
const UserPreferencesListItemDivider(),
_buildProductQueryTile(
Expand All @@ -254,6 +290,9 @@ class _UserPreferencesPageState extends State<UserPreferencesSection> {
iconData: Icons.edit,
context: context,
localDatabase: localDatabase,
userId: userId,
type: UserProductSearchType.INFORMER,
language: language,
),
const UserPreferencesListItemDivider(),
_buildProductQueryTile(
Expand All @@ -265,6 +304,9 @@ class _UserPreferencesPageState extends State<UserPreferencesSection> {
iconData: Icons.add_a_photo,
context: context,
localDatabase: localDatabase,
userId: userId,
type: UserProductSearchType.PHOTOGRAPHER,
language: language,
),
const UserPreferencesListItemDivider(),
_buildProductQueryTile(
Expand Down Expand Up @@ -367,6 +409,9 @@ class _UserPreferencesPageState extends State<UserPreferencesSection> {
required final IconData iconData,
required final BuildContext context,
required final LocalDatabase localDatabase,
final String? userId,
final UserProductSearchType? type,
final OpenFoodFactsLanguage? language,
}) =>
_getListTile(
title,
Expand All @@ -377,16 +422,32 @@ class _UserPreferencesPageState extends State<UserPreferencesSection> {
context: context,
),
iconData,
userId: userId,
type: type,
language: language,
);

Widget _getListTile(
final String title,
final VoidCallback onTap,
final IconData leading,
) =>
final IconData leading, {
final String? userId,
final UserProductSearchType? type,
final OpenFoodFactsLanguage? language,
}) =>
UserPreferencesListTile(
title: Text(title),
onTap: onTap,
leading: UserPreferencesListTile.getTintedIcon(leading, context),
trailing: (userId != null && type != null && language != null)
? FutureBuilder<int?>(
future: _getMyCount(userId, type, language),
builder: (BuildContext context, AsyncSnapshot<int?> snapshot) {
return Text(
prathamsoni11 marked this conversation as resolved.
Show resolved Hide resolved
snapshot.data == null ? '0' : snapshot.data.toString(),
);
},
)
: null,
);
}