Skip to content

Commit

Permalink
feat: Add retry button in case of Internet Error (#1105)
Browse files Browse the repository at this point in the history
* feat: add string for retry button

* feat: add method to reload barcode information on internet error

* feat: update localizations and add retry button

* chore: make setBarcodeState private
  • Loading branch information
danagbemava authored Feb 11, 2022
1 parent f1777e8 commit a1e8221
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
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/continuous_scan_model.dart';
import 'package:smooth_app/pages/product/common/product_dialog_helper.dart';

/// Product Card when an exception is caught
Expand All @@ -10,6 +13,8 @@ class SmoothProductCardError extends StatelessWidget {

@override
Widget build(BuildContext context) {
final AppLocalizations appLocalizations = AppLocalizations.of(context)!;

return Container(
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
Expand All @@ -30,7 +35,18 @@ class SmoothProductCardError extends StatelessWidget {
height: 12.0,
),
ProductDialogHelper.getErrorMessage(
AppLocalizations.of(context)!.product_internet_error,
appLocalizations.product_internet_error,
),
const SizedBox(
height: 12.0,
),
ElevatedButton(
onPressed: () async {
await context
.read<ContinuousScanModel>()
.retryBarcodeFetch(barcode);
},
child: Text(appLocalizations.retry_button_label),
),
],
),
Expand Down
19 changes: 12 additions & 7 deletions packages/smooth_app/lib/data_models/continuous_scan_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class ContinuousScanModel with ChangeNotifier {

Future<void> refreshProductList() async => _daoProductList.get(_productList);

void setBarcodeState(
void _setBarcodeState(
final String barcode,
final ScannedProductState state,
) {
Expand Down Expand Up @@ -103,11 +103,16 @@ class ContinuousScanModel with ChangeNotifier {
_addBarcode(code);
}

Future<void> retryBarcodeFetch(String barcode) async {
_setBarcodeState(barcode, ScannedProductState.LOADING);
await _updateBarcode(barcode);
}

Future<bool> _addBarcode(final String barcode) async {
final ScannedProductState? state = getBarcodeState(barcode);
if (state == null) {
_barcodes.add(barcode);
setBarcodeState(barcode, ScannedProductState.LOADING);
_setBarcodeState(barcode, ScannedProductState.LOADING);
_cacheOrLoadBarcode(barcode);
return true;
}
Expand Down Expand Up @@ -159,10 +164,10 @@ class ContinuousScanModel with ChangeNotifier {
_addProduct(fetchedProduct.product!, ScannedProductState.FOUND);
return;
case FetchedProductStatus.internetNotFound:
setBarcodeState(barcode, ScannedProductState.NOT_FOUND);
_setBarcodeState(barcode, ScannedProductState.NOT_FOUND);
return;
case FetchedProductStatus.internetError:
setBarcodeState(barcode, ScannedProductState.ERROR);
_setBarcodeState(barcode, ScannedProductState.ERROR);
return;
case FetchedProductStatus.userCancelled:
// we do nothing
Expand All @@ -179,10 +184,10 @@ class ContinuousScanModel with ChangeNotifier {
_addProduct(fetchedProduct.product!, ScannedProductState.FOUND);
return;
case FetchedProductStatus.internetNotFound:
setBarcodeState(barcode, ScannedProductState.NOT_FOUND);
_setBarcodeState(barcode, ScannedProductState.NOT_FOUND);
return;
case FetchedProductStatus.internetError:
setBarcodeState(barcode, ScannedProductState.ERROR);
_setBarcodeState(barcode, ScannedProductState.ERROR);
return;
case FetchedProductStatus.userCancelled:
// we do nothing
Expand All @@ -200,7 +205,7 @@ class ContinuousScanModel with ChangeNotifier {
await _daoProductList.push(productList, _latestFoundBarcode!);
await _daoProductList.push(_history, _latestFoundBarcode!);
}
setBarcodeState(product.barcode!, state);
_setBarcodeState(product.barcode!, state);
}

Future<void> clearScanSession() async {
Expand Down
5 changes: 3 additions & 2 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -572,5 +572,6 @@
"compare_products_mode": "Compare Mode",
"@compare_products_mode": {
"description": "Button to switch to 'compare products mode'"
}
}
},
"retry_button_label": "Retry"
}

0 comments on commit a1e8221

Please sign in to comment.