From 78f5eee9fba17afae449b0d05e587f0becc4ed1f Mon Sep 17 00:00:00 2001 From: Edouard Marquez Date: Wed, 9 Aug 2023 12:00:14 +0200 Subject: [PATCH] Ensure options in users list modal work well (#4482) --- .../lib/pages/all_product_list_modal.dart | 153 ++++++++++++++++++ .../lib/pages/all_product_list_page.dart | 124 -------------- .../smooth_app/lib/pages/history_page.dart | 2 +- ...list_modal.dart => product_list_page.dart} | 2 +- .../lib/pages/product/new_product_page.dart | 2 +- 5 files changed, 156 insertions(+), 127 deletions(-) create mode 100644 packages/smooth_app/lib/pages/all_product_list_modal.dart delete mode 100644 packages/smooth_app/lib/pages/all_product_list_page.dart rename packages/smooth_app/lib/pages/product/common/{product_list_modal.dart => product_list_page.dart} (99%) diff --git a/packages/smooth_app/lib/pages/all_product_list_modal.dart b/packages/smooth_app/lib/pages/all_product_list_modal.dart new file mode 100644 index 00000000000..4b7b12c9010 --- /dev/null +++ b/packages/smooth_app/lib/pages/all_product_list_modal.dart @@ -0,0 +1,153 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +import 'package:provider/provider.dart'; +import 'package:smooth_app/data_models/product_list.dart'; +import 'package:smooth_app/database/dao_product_list.dart'; +import 'package:smooth_app/database/local_database.dart'; +import 'package:smooth_app/generic_lib/design_constants.dart'; +import 'package:smooth_app/pages/preferences/user_preferences_list_tile.dart'; +import 'package:smooth_app/pages/product/common/product_list_popup_items.dart'; +import 'package:smooth_app/pages/product/common/product_query_page_helper.dart'; + +/// Page that lists all product lists. +class AllProductListModal extends StatelessWidget { + AllProductListModal({ + required this.currentList, + }); + + final ProductList currentList; + + final List _hardcodedProductLists = [ + ProductList.scanSession(), + ProductList.scanHistory(), + ProductList.history(), + ]; + + @override + Widget build(BuildContext context) { + final LocalDatabase localDatabase = context.watch(); + final DaoProductList daoProductList = DaoProductList(localDatabase); + + final List userLists = daoProductList.getUserLists(); + final List productLists = + List.from(_hardcodedProductLists); + for (final String userList in userLists) { + productLists.add(ProductList.user(userList)); + } + + return SliverList( + delegate: SliverChildBuilderDelegate( + childCount: productLists.length, + (final BuildContext context, final int index) { + final ProductList productList = productLists[index]; + return Column( + children: [ + ConstrainedBox( + constraints: BoxConstraints( + minWidth: double.infinity, + minHeight: 80.0 * MediaQuery.of(context).textScaleFactor, + ), + child: FutureBuilder( + future: daoProductList.get(productList), + builder: + (BuildContext context, AsyncSnapshot snapshot) { + if (snapshot.connectionState != ConnectionState.done) { + return const Center( + child: CircularProgressIndicator.adaptive(), + ); + } + return _ModalProductListItem( + productList: productList, + selected: productList.listType == currentList.listType && + productList.parameters == currentList.parameters, + ); + }, + ), + ), + if (index < productLists.length - 1) const Divider(height: 1.0), + ], + ); + }, + ), + ); + } +} + +class _ModalProductListItem extends StatelessWidget { + const _ModalProductListItem({ + required this.productList, + required this.selected, + }); + + final ProductList productList; + final bool selected; + + @override + Widget build(BuildContext context) { + final LocalDatabase localDatabase = context.watch(); + + final AppLocalizations appLocalizations = AppLocalizations.of(context); + + final int productsLength = productList.barcodes.length; + final bool enableRename = productList.listType == ProductListType.USER; + final bool hasProducts = productsLength > 0; + + return UserPreferencesListTile( + title: Text( + ProductQueryPageHelper.getProductListLabel( + productList, + appLocalizations, + ), + ), + subtitle: Text( + appLocalizations.user_list_length(productsLength), + ), + trailing: (enableRename || hasProducts || productList.isEditable) + ? PopupMenuButton( + itemBuilder: (BuildContext context) { + final List list = [ + if (enableRename) ProductListPopupRename(), + if (hasProducts) ProductListPopupShare(), + if (hasProducts) ProductListPopupOpenInWeb(), + if (hasProducts) ProductListPopupClear(), + if (productList.isEditable) ProductListPopupDelete(), + ]; + final List> result = + >[]; + for (final ProductListPopupItem item in list) { + result.add( + PopupMenuItem( + value: item.getEntry(), + child: ListTile( + leading: Icon(item.getIconData()), + title: Text(item.getTitle(appLocalizations)), + contentPadding: EdgeInsets.zero, + onTap: () async { + Navigator.of(context).pop(); + await item.doSomething( + productList: productList, + localDatabase: localDatabase, + context: context, + ); + }, + ), + ), + ); + } + return result; + }, + icon: const Icon(Icons.more_vert), + ) + : null, + selected: selected, + selectedColor: Theme.of(context).primaryColor.withOpacity(0.2), + contentPadding: const EdgeInsetsDirectional.only( + start: VERY_LARGE_SPACE, + end: LARGE_SPACE, + top: VERY_SMALL_SPACE, + bottom: VERY_SMALL_SPACE, + ), + onTap: () => Navigator.of(context).pop(productList), + ); + } +} diff --git a/packages/smooth_app/lib/pages/all_product_list_page.dart b/packages/smooth_app/lib/pages/all_product_list_page.dart deleted file mode 100644 index da18d9e0cc7..00000000000 --- a/packages/smooth_app/lib/pages/all_product_list_page.dart +++ /dev/null @@ -1,124 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_gen/gen_l10n/app_localizations.dart'; -import 'package:provider/provider.dart'; -import 'package:smooth_app/data_models/product_list.dart'; -import 'package:smooth_app/database/dao_product_list.dart'; -import 'package:smooth_app/database/local_database.dart'; -import 'package:smooth_app/generic_lib/design_constants.dart'; -import 'package:smooth_app/pages/preferences/user_preferences_list_tile.dart'; -import 'package:smooth_app/pages/product/common/product_list_popup_items.dart'; -import 'package:smooth_app/pages/product/common/product_query_page_helper.dart'; - -/// Page that lists all product lists. -class AllProductListModal extends StatelessWidget { - AllProductListModal({ - required this.currentList, - }); - - final ProductList currentList; - - final List _hardcodedProductLists = [ - ProductList.scanSession(), - ProductList.scanHistory(), - ProductList.history(), - ]; - - @override - Widget build(BuildContext context) { - final LocalDatabase localDatabase = context.watch(); - final DaoProductList daoProductList = DaoProductList(localDatabase); - - final List userLists = daoProductList.getUserLists(); - final List productLists = - List.from(_hardcodedProductLists); - for (final String userList in userLists) { - productLists.add(ProductList.user(userList)); - } - final AppLocalizations appLocalizations = AppLocalizations.of(context); - - return SliverList( - delegate: SliverChildBuilderDelegate( - childCount: productLists.length, - (final BuildContext context, final int index) { - final ProductList productList = productLists[index]; - return Column( - children: [ - FutureBuilder( - future: daoProductList.getLength(productList), - builder: ( - final BuildContext context, - final AsyncSnapshot snapshot, - ) { - final int productsLength = snapshot.data ?? 0; - - return UserPreferencesListTile( - title: Text( - ProductQueryPageHelper.getProductListLabel( - productList, - appLocalizations, - ), - ), - subtitle: Text( - appLocalizations.user_list_length(productsLength), - ), - trailing: PopupMenuButton( - itemBuilder: (BuildContext context) { - final bool enableRename = - productList.listType == ProductListType.USER; - final List list = - [ - if (enableRename) ProductListPopupRename(), - ProductListPopupShare(), - ProductListPopupOpenInWeb(), - if (productsLength > 0) ProductListPopupClear(), - if (productList.isEditable) ProductListPopupDelete(), - ]; - final List> - result = - >[]; - for (final ProductListPopupItem item in list) { - result.add( - PopupMenuItem( - value: item.getEntry(), - child: ListTile( - leading: Icon(item.getIconData()), - title: Text(item.getTitle(appLocalizations)), - contentPadding: EdgeInsets.zero, - onTap: () async { - Navigator.of(context).pop(); - await item.doSomething( - productList: productList, - localDatabase: localDatabase, - context: context, - ); - }, - ), - ), - ); - } - return result; - }, - icon: const Icon(Icons.more_vert), - ), - selected: productList.listType == currentList.listType && - productList.parameters == currentList.parameters, - selectedColor: - Theme.of(context).primaryColor.withOpacity(0.2), - contentPadding: const EdgeInsetsDirectional.only( - start: VERY_LARGE_SPACE, - end: LARGE_SPACE, - top: VERY_SMALL_SPACE, - bottom: VERY_SMALL_SPACE, - ), - onTap: () => Navigator.of(context).pop(productList), - ); - }, - ), - if (index < productLists.length - 1) const Divider(height: 1.0), - ], - ); - }, - ), - ); - } -} diff --git a/packages/smooth_app/lib/pages/history_page.dart b/packages/smooth_app/lib/pages/history_page.dart index b4bb6a1361e..68be6bdf733 100644 --- a/packages/smooth_app/lib/pages/history_page.dart +++ b/packages/smooth_app/lib/pages/history_page.dart @@ -3,7 +3,7 @@ import 'package:provider/provider.dart'; import 'package:smooth_app/data_models/product_list.dart'; import 'package:smooth_app/database/dao_product_list.dart'; import 'package:smooth_app/database/local_database.dart'; -import 'package:smooth_app/pages/product/common/product_list_modal.dart'; +import 'package:smooth_app/pages/product/common/product_list_page.dart'; class HistoryPage extends StatefulWidget { const HistoryPage(); diff --git a/packages/smooth_app/lib/pages/product/common/product_list_modal.dart b/packages/smooth_app/lib/pages/product/common/product_list_page.dart similarity index 99% rename from packages/smooth_app/lib/pages/product/common/product_list_modal.dart rename to packages/smooth_app/lib/pages/product/common/product_list_page.dart index 33cead27246..980f6b8410c 100644 --- a/packages/smooth_app/lib/pages/product/common/product_list_modal.dart +++ b/packages/smooth_app/lib/pages/product/common/product_list_page.dart @@ -19,7 +19,7 @@ import 'package:smooth_app/generic_lib/loading_dialog.dart'; import 'package:smooth_app/generic_lib/widgets/smooth_responsive.dart'; import 'package:smooth_app/helpers/app_helper.dart'; import 'package:smooth_app/helpers/robotoff_insight_helper.dart'; -import 'package:smooth_app/pages/all_product_list_page.dart'; +import 'package:smooth_app/pages/all_product_list_modal.dart'; import 'package:smooth_app/pages/carousel_manager.dart'; import 'package:smooth_app/pages/personalized_ranking_page.dart'; import 'package:smooth_app/pages/product/common/product_list_item_simple.dart'; diff --git a/packages/smooth_app/lib/pages/product/new_product_page.dart b/packages/smooth_app/lib/pages/product/new_product_page.dart index df8bf9879bf..8b6ab418b86 100644 --- a/packages/smooth_app/lib/pages/product/new_product_page.dart +++ b/packages/smooth_app/lib/pages/product/new_product_page.dart @@ -24,7 +24,7 @@ import 'package:smooth_app/helpers/product_cards_helper.dart'; import 'package:smooth_app/knowledge_panel/knowledge_panels/knowledge_panel_product_cards.dart'; import 'package:smooth_app/knowledge_panel/knowledge_panels_builder.dart'; import 'package:smooth_app/pages/carousel_manager.dart'; -import 'package:smooth_app/pages/product/common/product_list_modal.dart'; +import 'package:smooth_app/pages/product/common/product_list_page.dart'; import 'package:smooth_app/pages/product/common/product_refresher.dart'; import 'package:smooth_app/pages/product/edit_product_page.dart'; import 'package:smooth_app/pages/product/product_questions_widget.dart';