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 all commits
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 All @@ -21,6 +22,7 @@ import 'package:smooth_app/pages/user_management/login_page.dart';
import 'package:smooth_app/query/paged_product_query.dart';
import 'package:smooth_app/query/paged_to_be_completed_product_query.dart';
import 'package:smooth_app/query/paged_user_product_query.dart';
import 'package:smooth_app/query/product_query.dart';

class UserPreferencesAccount extends AbstractUserPreferences {
UserPreferencesAccount({
Expand Down Expand Up @@ -243,6 +245,7 @@ class _UserPreferencesPageState extends State<UserPreferencesSection> {
iconData: Icons.add_circle_outline,
context: context,
localDatabase: localDatabase,
type: UserProductSearchType.CONTRIBUTOR,
),
const UserPreferencesListItemDivider(),
_buildProductQueryTile(
Expand All @@ -254,6 +257,7 @@ class _UserPreferencesPageState extends State<UserPreferencesSection> {
iconData: Icons.edit,
context: context,
localDatabase: localDatabase,
type: UserProductSearchType.INFORMER,
),
const UserPreferencesListItemDivider(),
_buildProductQueryTile(
Expand All @@ -265,6 +269,7 @@ class _UserPreferencesPageState extends State<UserPreferencesSection> {
iconData: Icons.add_a_photo,
context: context,
localDatabase: localDatabase,
type: UserProductSearchType.PHOTOGRAPHER,
),
const UserPreferencesListItemDivider(),
_buildProductQueryTile(
Expand Down Expand Up @@ -361,12 +366,37 @@ class _UserPreferencesPageState extends State<UserPreferencesSection> {
return Column(children: result);
}

Future<int?> _getMyCount(
final UserProductSearchType type,
) async {
final UserProductSearchQueryConfiguration configuration =
UserProductSearchQueryConfiguration(
type: type,
userId: OpenFoodAPIConfiguration.globalUser!.userId,
pageSize: 1,
language: ProductQuery.getLanguage(),
fields: <ProductField>[],
);

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

Widget _buildProductQueryTile({
required final PagedProductQuery productQuery,
required final String title,
required final IconData iconData,
required final BuildContext context,
required final LocalDatabase localDatabase,
final UserProductSearchType? type,
}) =>
_getListTile(
title,
Expand All @@ -377,16 +407,34 @@ class _UserPreferencesPageState extends State<UserPreferencesSection> {
context: context,
),
iconData,
type: type,
);

Widget _getListTile(
final String title,
final VoidCallback onTap,
final IconData leading,
) =>
final IconData leading, {
final UserProductSearchType? type,
}) =>
UserPreferencesListTile(
title: Text(title),
onTap: onTap,
leading: UserPreferencesListTile.getTintedIcon(leading, context),
trailing: (type != null)
? FutureBuilder<int?>(
future: _getMyCount(type),
builder: (BuildContext context, AsyncSnapshot<int?> snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return const SizedBox(
height: LARGE_SPACE,
width: LARGE_SPACE,
child: CircularProgressIndicator.adaptive());
}
return Text(
snapshot.data == null ? '0' : snapshot.data.toString(),
);
},
)
: null,
);
}