Skip to content

Commit

Permalink
Ensure options in users list modal work well (#4482)
Browse files Browse the repository at this point in the history
  • Loading branch information
g123k authored Aug 9, 2023
1 parent aa8fbb4 commit 78f5eee
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 127 deletions.
153 changes: 153 additions & 0 deletions packages/smooth_app/lib/pages/all_product_list_modal.dart
Original file line number Diff line number Diff line change
@@ -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<ProductList> _hardcodedProductLists = <ProductList>[
ProductList.scanSession(),
ProductList.scanHistory(),
ProductList.history(),
];

@override
Widget build(BuildContext context) {
final LocalDatabase localDatabase = context.watch<LocalDatabase>();
final DaoProductList daoProductList = DaoProductList(localDatabase);

final List<String> userLists = daoProductList.getUserLists();
final List<ProductList> productLists =
List<ProductList>.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: <Widget>[
ConstrainedBox(
constraints: BoxConstraints(
minWidth: double.infinity,
minHeight: 80.0 * MediaQuery.of(context).textScaleFactor,
),
child: FutureBuilder<void>(
future: daoProductList.get(productList),
builder:
(BuildContext context, AsyncSnapshot<void> 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<LocalDatabase>();

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<ProductListPopupMenuEntry>(
itemBuilder: (BuildContext context) {
final List<ProductListPopupItem> list = <ProductListPopupItem>[
if (enableRename) ProductListPopupRename(),
if (hasProducts) ProductListPopupShare(),
if (hasProducts) ProductListPopupOpenInWeb(),
if (hasProducts) ProductListPopupClear(),
if (productList.isEditable) ProductListPopupDelete(),
];
final List<PopupMenuEntry<ProductListPopupMenuEntry>> result =
<PopupMenuEntry<ProductListPopupMenuEntry>>[];
for (final ProductListPopupItem item in list) {
result.add(
PopupMenuItem<ProductListPopupMenuEntry>(
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),
);
}
}
124 changes: 0 additions & 124 deletions packages/smooth_app/lib/pages/all_product_list_page.dart

This file was deleted.

2 changes: 1 addition & 1 deletion packages/smooth_app/lib/pages/history_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 78f5eee

Please sign in to comment.