-
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: prices - top contributors now within the app (#5383)
* feat: prices - top contributors now locally New files: * `price_user_button.dart`: Widget that displays a user, for Prices. * `prices_users_page.dart`: Page that displays the top prices users. Impacted files: * `app_en.arb`: added 2 label for "Contributor prices" * `app_fr.arb`: added 2 label for "Contributor prices" * `price_count_widget.dart`: minor refactoring * `price_data_widget.dart`: minor refactoring using new class `PriceUserButton` * `user_preferences_account.dart`: local display of top users; minor refactoring using new class `PriceUserButton` * Added comment.
- Loading branch information
1 parent
9f7d9d4
commit 7809854
Showing
7 changed files
with
264 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/smooth_app/lib/pages/prices/price_user_button.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||
import 'package:openfoodfacts/openfoodfacts.dart'; | ||
import 'package:smooth_app/pages/prices/get_prices_model.dart'; | ||
import 'package:smooth_app/pages/prices/price_button.dart'; | ||
import 'package:smooth_app/pages/prices/prices_page.dart'; | ||
import 'package:smooth_app/query/product_query.dart'; | ||
|
||
/// Widget that displays a user, for Prices. | ||
class PriceUserButton extends StatelessWidget { | ||
const PriceUserButton(this.user); | ||
|
||
final String user; | ||
|
||
static String showUserTitle({ | ||
required final String user, | ||
required final BuildContext context, | ||
}) => | ||
user == ProductQuery.getWriteUser().userId | ||
? AppLocalizations.of(context).user_search_prices_title | ||
: AppLocalizations.of(context).user_any_search_prices_title; | ||
|
||
static Future<void> showUserPrices({ | ||
required final String user, | ||
required final BuildContext context, | ||
}) async => | ||
Navigator.of(context).push( | ||
MaterialPageRoute<void>( | ||
builder: (BuildContext context) => PricesPage( | ||
GetPricesModel( | ||
parameters: GetPricesParameters() | ||
..owner = user | ||
..orderBy = <OrderBy<GetPricesOrderField>>[ | ||
const OrderBy<GetPricesOrderField>( | ||
field: GetPricesOrderField.created, | ||
ascending: false, | ||
), | ||
] | ||
..pageSize = GetPricesModel.pageSize | ||
..pageNumber = 1, | ||
displayOwner: false, | ||
displayProduct: true, | ||
uri: OpenPricesAPIClient.getUri( | ||
path: 'app/users/$user', | ||
uriHelper: ProductQuery.uriProductHelper, | ||
), | ||
title: showUserTitle(user: user, context: context), | ||
subtitle: user, | ||
), | ||
), | ||
), | ||
); | ||
|
||
@override | ||
Widget build(BuildContext context) => PriceButton( | ||
title: user, | ||
iconData: Icons.account_box, | ||
onPressed: () async => showUserPrices( | ||
user: user, | ||
context: context, | ||
), | ||
); | ||
} |
143 changes: 143 additions & 0 deletions
143
packages/smooth_app/lib/pages/prices/prices_users_page.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||
import 'package:openfoodfacts/openfoodfacts.dart'; | ||
import 'package:smooth_app/generic_lib/design_constants.dart'; | ||
import 'package:smooth_app/generic_lib/widgets/smooth_back_button.dart'; | ||
import 'package:smooth_app/generic_lib/widgets/smooth_card.dart'; | ||
import 'package:smooth_app/helpers/launch_url_helper.dart'; | ||
import 'package:smooth_app/pages/prices/price_button.dart'; | ||
import 'package:smooth_app/pages/prices/price_count_widget.dart'; | ||
import 'package:smooth_app/pages/prices/price_user_button.dart'; | ||
import 'package:smooth_app/query/product_query.dart'; | ||
import 'package:smooth_app/widgets/smooth_app_bar.dart'; | ||
import 'package:smooth_app/widgets/smooth_scaffold.dart'; | ||
|
||
/// Page that displays the top prices users. | ||
class PricesUsersPage extends StatefulWidget { | ||
const PricesUsersPage(); | ||
|
||
@override | ||
State<PricesUsersPage> createState() => _PricesUsersPageState(); | ||
} | ||
|
||
class _PricesUsersPageState extends State<PricesUsersPage> { | ||
late final Future<MaybeError<GetUsersResult>> _users = _showTopUsers(); | ||
|
||
// In this specific page, let's never try to go beyond the top 10. | ||
// cf. https://github.com/openfoodfacts/smooth-app/pull/5383#issuecomment-2171117141 | ||
static const int _pageSize = 10; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final AppLocalizations appLocalizations = AppLocalizations.of(context); | ||
return SmoothScaffold( | ||
appBar: SmoothAppBar( | ||
centerTitle: false, | ||
leading: const SmoothBackButton(), | ||
title: Text( | ||
appLocalizations.all_search_prices_top_user_title, | ||
), | ||
actions: <Widget>[ | ||
IconButton( | ||
tooltip: appLocalizations.prices_app_button, | ||
icon: const Icon(Icons.open_in_new), | ||
onPressed: () async => LaunchUrlHelper.launchURL( | ||
OpenPricesAPIClient.getUri( | ||
path: 'app/users', | ||
uriHelper: ProductQuery.uriProductHelper, | ||
).toString(), | ||
), | ||
), | ||
], | ||
), | ||
body: FutureBuilder<MaybeError<GetUsersResult>>( | ||
future: _users, | ||
builder: ( | ||
final BuildContext context, | ||
final AsyncSnapshot<MaybeError<GetUsersResult>> snapshot, | ||
) { | ||
if (snapshot.connectionState != ConnectionState.done) { | ||
return const CircularProgressIndicator(); | ||
} | ||
if (snapshot.hasError) { | ||
return Text(snapshot.error!.toString()); | ||
} | ||
// highly improbable | ||
if (!snapshot.hasData) { | ||
return const Text('no data'); | ||
} | ||
if (snapshot.data!.isError) { | ||
return Text(snapshot.data!.error!); | ||
} | ||
final GetUsersResult result = snapshot.data!.value; | ||
// highly improbable | ||
if (result.items == null) { | ||
return const Text('empty list'); | ||
} | ||
final List<Widget> children = <Widget>[]; | ||
|
||
for (final PriceUser item in result.items!) { | ||
children.add( | ||
SmoothCard( | ||
child: Wrap( | ||
spacing: VERY_SMALL_SPACE, | ||
children: <Widget>[ | ||
PriceUserButton(item.userId), | ||
PriceButton( | ||
onPressed: () async => PriceUserButton.showUserPrices( | ||
user: item.userId, | ||
context: context, | ||
), | ||
iconData: Icons.label, | ||
title: '${item.priceCount}', | ||
buttonStyle: ElevatedButton.styleFrom( | ||
foregroundColor: PriceCountWidget.getForegroundColor( | ||
item.priceCount, | ||
), | ||
backgroundColor: PriceCountWidget.getBackgroundColor( | ||
item.priceCount, | ||
), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
final AppLocalizations appLocalizations = | ||
AppLocalizations.of(context); | ||
final String title = | ||
appLocalizations.prices_users_list_length_many_pages( | ||
_pageSize, | ||
result.total!, | ||
); | ||
children.insert( | ||
0, | ||
SmoothCard(child: ListTile(title: Text(title))), | ||
); | ||
// so that the last content gets not hidden by the FAB | ||
children.add( | ||
const SizedBox(height: 2 * MINIMUM_TOUCH_SIZE), | ||
); | ||
return ListView( | ||
children: children, | ||
); | ||
}, | ||
), | ||
); | ||
} | ||
|
||
static Future<MaybeError<GetUsersResult>> _showTopUsers() async => | ||
OpenPricesAPIClient.getUsers( | ||
GetUsersParameters() | ||
..orderBy = <OrderBy<GetUsersOrderField>>[ | ||
const OrderBy<GetUsersOrderField>( | ||
field: GetUsersOrderField.priceCount, | ||
ascending: false, | ||
), | ||
] | ||
..pageSize = _pageSize | ||
..pageNumber = 1, | ||
uriHelper: ProductQuery.uriProductHelper, | ||
); | ||
} |