Skip to content

Commit

Permalink
fix: Added pull to refresh to Product and Product list page (openfood…
Browse files Browse the repository at this point in the history
  • Loading branch information
AshAman999 authored Mar 28, 2022
1 parent 860709d commit 9729b3f
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 158 deletions.
154 changes: 78 additions & 76 deletions packages/smooth_app/lib/pages/product/common/product_list_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,6 @@ class _ProductListPageState extends State<ProductListPage> {
overflow: TextOverflow.fade,
),
),
if ((!_selectionMode) && products.isNotEmpty)
IconButton(
icon: const Icon(Icons.refresh),
onPressed: () async => _refreshListProducts(
products,
localDatabase,
),
),
if ((!_selectionMode) && products.isNotEmpty)
Flexible(
child: ElevatedButton(
Expand Down Expand Up @@ -150,80 +142,90 @@ class _ProductListPageState extends State<ProductListPage> {
)
],
)
: ListView.builder(
itemCount: products.length,
itemBuilder: (BuildContext context, int index) {
final Product product = products[index];
final String barcode = product.barcode!;
final bool selected = _selectedBarcodes.contains(barcode);
void onTap() => setState(
() {
if (selected) {
_selectedBarcodes.remove(barcode);
} else {
_selectedBarcodes.add(barcode);
: RefreshIndicator(
//if it is in selectmode then refresh indicator is not shown
notificationPredicate:
_selectionMode ? (_) => false : (_) => true,
onRefresh: () async => _refreshListProducts(
products,
localDatabase,
),
child: ListView.builder(
itemCount: products.length,
itemBuilder: (BuildContext context, int index) {
final Product product = products[index];
final String barcode = product.barcode!;
final bool selected = _selectedBarcodes.contains(barcode);
void onTap() => setState(
() {
if (selected) {
_selectedBarcodes.remove(barcode);
} else {
_selectedBarcodes.add(barcode);
}
},
);
final Widget child = GestureDetector(
onTap: _selectionMode ? onTap : null,
child: Container(
padding: EdgeInsets.symmetric(
horizontal: _selectionMode ? 0 : 12.0,
vertical: 8.0,
),
child: Row(
children: <Widget>[
if (_selectionMode)
Icon(
selected
? Icons.check_box
: Icons.check_box_outline_blank,
),
Expanded(
child: ProductListItemSimple(
product: product,
onTap: _selectionMode ? onTap : null,
onLongPress: !_selectionMode
? () => setState(() => _selectionMode = true)
: null,
),
),
],
),
),
);
if (dismissible) {
return Dismissible(
background: Container(color: colorScheme.background),
key: Key(product.barcode!),
onDismissed: (final DismissDirection direction) async {
final bool removed =
productList.remove(product.barcode!);
if (removed) {
await daoProductList.put(productList);
_selectedBarcodes.remove(product.barcode);
setState(() => products.removeAt(index));
}
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
removed
? appLocalizations.product_removed_history
: appLocalizations.product_could_not_remove,
),
duration: const Duration(seconds: 3),
),
);
// TODO(monsieurtanuki): add a snackbar ("put back the food")
},
child: child,
);
final Widget child = GestureDetector(
onTap: _selectionMode ? onTap : null,
child: Container(
padding: EdgeInsets.symmetric(
horizontal: _selectionMode ? 0 : 12.0,
vertical: 8.0,
),
child: Row(
children: <Widget>[
if (_selectionMode)
Icon(
selected
? Icons.check_box
: Icons.check_box_outline_blank,
),
Expanded(
child: ProductListItemSimple(
product: product,
onTap: _selectionMode ? onTap : null,
onLongPress: !_selectionMode
? () => setState(() => _selectionMode = true)
: null,
),
),
],
),
),
);
if (dismissible) {
return Dismissible(
background: Container(color: colorScheme.background),
}
return Container(
key: Key(product.barcode!),
onDismissed: (final DismissDirection direction) async {
final bool removed = productList.remove(product.barcode!);
if (removed) {
await daoProductList.put(productList);
_selectedBarcodes.remove(product.barcode);
setState(() => products.removeAt(index));
}
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
removed
? appLocalizations.product_removed_history
: appLocalizations.product_could_not_remove,
),
duration: const Duration(seconds: 3),
),
);
// TODO(monsieurtanuki): add a snackbar ("put back the food")
},
child: child,
);
}
return Container(
key: Key(product.barcode!),
child: child,
);
},
},
),
),
);
}
Expand Down
167 changes: 85 additions & 82 deletions packages/smooth_app/lib/pages/product/new_product_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,94 +183,97 @@ class _ProductPageState extends State<ProductPage> {
}

Widget _buildProductBody(BuildContext context) {
return ListView(children: <Widget>[
Align(
heightFactor: 0.7,
alignment: Alignment.topLeft,
child: ProductImageCarousel(
_product,
height: 200,
onUpload: _refreshProduct,
),
),
Padding(
padding: const EdgeInsets.symmetric(
horizontal: SMALL_SPACE,
),
child: Hero(
tag: _product.barcode ?? '',
child: SummaryCard(
return RefreshIndicator(
onRefresh: () => _refreshProduct(context),
child: ListView(children: <Widget>[
Align(
heightFactor: 0.7,
alignment: Alignment.topLeft,
child: ProductImageCarousel(
_product,
_productPreferences,
isFullVersion: true,
showUnansweredQuestions: true,
refreshProductCallback: _refreshProduct,
height: 200,
onUpload: _refreshProduct,
),
),
),
_buildKnowledgePanelCards(),
Padding(
padding: const EdgeInsets.all(SMALL_SPACE),
child: SmoothActionButton(
text: 'Edit product', // TODO(monsieurtanuki): translations
onPressed: () async {
final bool? refreshed = await Navigator.push<bool>(
context,
MaterialPageRoute<bool>(
builder: (BuildContext context) => EditProductPage(_product),
),
);
if (refreshed ?? false) {
setState(() {});
}
},
Padding(
padding: const EdgeInsets.symmetric(
horizontal: SMALL_SPACE,
),
child: Hero(
tag: _product.barcode ?? '',
child: SummaryCard(
_product,
_productPreferences,
isFullVersion: true,
showUnansweredQuestions: true,
refreshProductCallback: _refreshProduct,
),
),
),
),
if (context.read<UserPreferences>().getFlag(
UserPreferencesDevMode.userPreferencesFlagAdditionalButton) ??
false)
ElevatedButton(
onPressed: () async {
if (_product.categoriesTags == null) {
// TODO(monsieurtanuki): that's another story: how to set an initial category?
return;
}
if (_product.categoriesTags!.length < 2) {
// TODO(monsieurtanuki): no father, we need to do something with roots
return;
}
final String currentTag =
_product.categoriesTags![_product.categoriesTags!.length - 1];
final String fatherTag =
_product.categoriesTags![_product.categoriesTags!.length - 2];
final CategoryCache categoryCache =
CategoryCache(ProductQuery.getLanguage()!);
final Map<String, TaxonomyCategory>? siblingsData =
await categoryCache.getCategorySiblingsAndFather(
fatherTag: fatherTag,
);
if (siblingsData == null) {
// TODO(monsieurtanuki): what shall we do?
return;
}
final String? newTag = await Navigator.push<String>(
context,
MaterialPageRoute<String>(
builder: (BuildContext context) => CategoryPickerPage(
barcode: _product.barcode!,
initialMap: siblingsData,
initialTree: _product.categoriesTags!,
categoryCache: categoryCache,
_buildKnowledgePanelCards(),
Padding(
padding: const EdgeInsets.all(SMALL_SPACE),
child: SmoothActionButton(
text: 'Edit product', // TODO(monsieurtanuki): translations
onPressed: () async {
final bool? refreshed = await Navigator.push<bool>(
context,
MaterialPageRoute<bool>(
builder: (BuildContext context) => EditProductPage(_product),
),
),
);
if (newTag != null && newTag != currentTag) {
setState(() {});
}
},
child: const Text('Additional Button'),
);
if (refreshed ?? false) {
setState(() {});
}
},
),
),
]);
if (context.read<UserPreferences>().getFlag(
UserPreferencesDevMode.userPreferencesFlagAdditionalButton) ??
false)
ElevatedButton(
onPressed: () async {
if (_product.categoriesTags == null) {
// TODO(monsieurtanuki): that's another story: how to set an initial category?
return;
}
if (_product.categoriesTags!.length < 2) {
// TODO(monsieurtanuki): no father, we need to do something with roots
return;
}
final String currentTag =
_product.categoriesTags![_product.categoriesTags!.length - 1];
final String fatherTag =
_product.categoriesTags![_product.categoriesTags!.length - 2];
final CategoryCache categoryCache =
CategoryCache(ProductQuery.getLanguage()!);
final Map<String, TaxonomyCategory>? siblingsData =
await categoryCache.getCategorySiblingsAndFather(
fatherTag: fatherTag,
);
if (siblingsData == null) {
// TODO(monsieurtanuki): what shall we do?
return;
}
final String? newTag = await Navigator.push<String>(
context,
MaterialPageRoute<String>(
builder: (BuildContext context) => CategoryPickerPage(
barcode: _product.barcode!,
initialMap: siblingsData,
initialTree: _product.categoriesTags!,
categoryCache: categoryCache,
),
),
);
if (newTag != null && newTag != currentTag) {
setState(() {});
}
},
child: const Text('Additional Button'),
),
]),
);
}

FutureBuilder<KnowledgePanels> _buildKnowledgePanelCards() {
Expand Down

0 comments on commit 9729b3f

Please sign in to comment.