Skip to content

Commit

Permalink
fix: #1427 scanning invalid code (#2336)
Browse files Browse the repository at this point in the history
* fix: #1427 scan invalid code

* fix: #1427 fix presubmit test
  • Loading branch information
cli1005 authored Jun 22, 2022
1 parent e3602b4 commit 5715cf7
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import 'package:smooth_app/pages/product/common/product_dialog_helper.dart';

/// Product Card when an exception is caught
class SmoothProductCardError extends StatelessWidget {
const SmoothProductCardError({required this.barcode});
const SmoothProductCardError({
required this.barcode,
required this.errorType,
});

final String barcode;
final ScannedProductState errorType;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -46,7 +50,10 @@ class SmoothProductCardError extends StatelessWidget {
height: 12.0,
),
ProductDialogHelper.getErrorMessage(
appLocalizations.product_internet_error,
_getErrorMessage(
errorType,
appLocalizations,
),
),
const SizedBox(
height: 12.0,
Expand All @@ -63,4 +70,18 @@ class SmoothProductCardError extends StatelessWidget {
),
);
}

String _getErrorMessage(
ScannedProductState errorType,
AppLocalizations appLocalizations,
) {
switch (errorType) {
case ScannedProductState.ERROR_INVALID_CODE:
return appLocalizations.barcode_invalid_error;
case ScannedProductState.ERROR_INTERNET:
return appLocalizations.product_internet_error;
default:
return appLocalizations.error_occurred;
}
}
}
13 changes: 10 additions & 3 deletions packages/smooth_app/lib/data_models/continuous_scan_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ enum ScannedProductState {
LOADING,
THANKS,
CACHED,
ERROR,
ERROR_INTERNET,
ERROR_INVALID_CODE,
}

class ContinuousScanModel with ChangeNotifier {
Expand Down Expand Up @@ -193,7 +194,10 @@ class ContinuousScanModel with ChangeNotifier {
_setBarcodeState(barcode, ScannedProductState.NOT_FOUND);
return;
case FetchedProductStatus.internetError:
_setBarcodeState(barcode, ScannedProductState.ERROR);
_setBarcodeState(barcode, ScannedProductState.ERROR_INTERNET);
return;
case FetchedProductStatus.codeInvalid:
_setBarcodeState(barcode, ScannedProductState.ERROR_INVALID_CODE);
return;
case FetchedProductStatus.userCancelled:
// we do nothing
Expand All @@ -213,7 +217,10 @@ class ContinuousScanModel with ChangeNotifier {
_setBarcodeState(barcode, ScannedProductState.NOT_FOUND);
return;
case FetchedProductStatus.internetError:
_setBarcodeState(barcode, ScannedProductState.ERROR);
_setBarcodeState(barcode, ScannedProductState.ERROR_INTERNET);
return;
case FetchedProductStatus.codeInvalid:
_setBarcodeState(barcode, ScannedProductState.ERROR_INVALID_CODE);
return;
case FetchedProductStatus.userCancelled:
// we do nothing
Expand Down
1 change: 1 addition & 0 deletions packages/smooth_app/lib/data_models/fetched_product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum FetchedProductStatus {
internetNotFound,
internetError,
userCancelled,
codeInvalid,
// TODO(monsieurtanuki): time-out
}

Expand Down
4 changes: 4 additions & 0 deletions packages/smooth_app/lib/database/barcode_product_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class BarcodeProductQuery {
return FetchedProduct(product);
}
}
if (barcode.trim().isNotEmpty &&
(result.barcode == null || result.barcode!.isEmpty)) {
return FetchedProduct.error(FetchedProductStatus.codeInvalid);
}
return FetchedProduct.error(FetchedProductStatus.internetNotFound);
}
}
1 change: 1 addition & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,7 @@
}
}
},
"barcode_invalid_error": "Invalid barcode",
"basic_details_add_success": "Basic details added succesfully",
"basic_details_add_error": "Unable to add basic details. Please try again after some time",
"@basic_details_add_error": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class ProductDialogHelper {
case FetchedProductStatus.internetNotFound:
_openProductNotFoundDialog();
return;
case FetchedProductStatus.codeInvalid:
_openErrorMessage(appLocalizations.barcode_invalid_error);
return;
}
}
}
12 changes: 10 additions & 2 deletions packages/smooth_app/lib/widgets/smooth_product_carousel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,16 @@ class _SmoothProductCarouselState extends State<SmoothProductCarousel> {
);
case ScannedProductState.THANKS:
return const SmoothProductCardThanks();
case ScannedProductState.ERROR:
return SmoothProductCardError(barcode: barcode);
case ScannedProductState.ERROR_INTERNET:
return SmoothProductCardError(
barcode: barcode,
errorType: ScannedProductState.ERROR_INTERNET,
);
case ScannedProductState.ERROR_INVALID_CODE:
return SmoothProductCardError(
barcode: barcode,
errorType: ScannedProductState.ERROR_INVALID_CODE,
);
}
}
}
Expand Down

0 comments on commit 5715cf7

Please sign in to comment.