Skip to content

Commit

Permalink
feat: 4423 - specific "Not connected to internet" displayed error
Browse files Browse the repository at this point in the history
Impacted files:
* `barcode_product_query.dart`: removed useless `try` as already `catch`'ed
* `continuous_scan_model.dart`: removed the `codeInvalid` case that could never happen
* `fetched_product.dart`: refactored with explicit constructors and additional exception and connectivity fields; removed the `codeInvalid` case that could never happen
* `new_product_page.dart`: minor refactoring
* `product_dialog_helper.dart`: removed the `codeInvalid` case that could never happen; minor refactoting
* `product_list_item_simple.dart`: removed the `codeInvalid` case that could never happen
* `product_loader_page.dart`: removed useless `try` as already `catch`'ed
* `product_refresher.dart`: added a specific "You're not connected to the internet" error message; refactored using more `FetchedProduct`; removed useless method
* `pubspec.lock`: wtf
* `pubspec.yaml`: added package `connectivity_plus`
* `question_card.dart`: refactored using `FetchedProduct`
  • Loading branch information
monsieurtanuki committed Aug 5, 2023
1 parent 292939b commit 2bdc0cd
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,6 @@ class ContinuousScanModel with ChangeNotifier {
case FetchedProductStatus.internetError:
_setBarcodeState(barcode, ScannedProductState.ERROR_INTERNET);
return;
case FetchedProductStatus.codeInvalid:
_setBarcodeState(barcode, ScannedProductState.ERROR_INVALID_CODE);
return;
case FetchedProductStatus.userCancelled:
// we do nothing
return;
Expand All @@ -247,9 +244,6 @@ class ContinuousScanModel with ChangeNotifier {
case FetchedProductStatus.internetError:
_setBarcodeState(barcode, ScannedProductState.ERROR_INTERNET);
return;
case FetchedProductStatus.codeInvalid:
_setBarcodeState(barcode, ScannedProductState.ERROR_INVALID_CODE);
return;
case FetchedProductStatus.userCancelled:
// we do nothing
return;
Expand Down
42 changes: 34 additions & 8 deletions packages/smooth_app/lib/data_models/fetched_product.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,54 @@
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:openfoodfacts/openfoodfacts.dart';

/// Status of a "fetch [Product]" operation
enum FetchedProductStatus {
// found locally or from internet
ok,
internetNotFound,
internetError,
userCancelled,
codeInvalid,
// TODO(monsieurtanuki): time-out
}

/// A [Product] that we tried to fetch, but was it successful?..
class FetchedProduct {
const FetchedProduct._({
required this.status,
this.product,
this.connectivityResult,
this.exceptionString,
});

// The reason behind the "ignore": I want to force "product" to be not null
FetchedProduct(final Product product)
const FetchedProduct.found(final Product product)
// ignore: prefer_initializing_formals
: product = product,
status = FetchedProductStatus.ok;
: this._(
status: FetchedProductStatus.ok,
product: product,
);

/// The internet Product search said it couldn't find the product.
const FetchedProduct.internetNotFound()
: this._(status: FetchedProductStatus.internetNotFound);

/// The user cancelled the operation.
const FetchedProduct.userCancelled()
: this._(status: FetchedProductStatus.userCancelled);

/// When the "fetch product" operation didn't go well (no status "ok" here)
FetchedProduct.error(this.status)
: product = null,
assert(status != FetchedProductStatus.ok);
/// When the "fetch product" operation had an internet error.
const FetchedProduct.error({
required final String exceptionString,
required final ConnectivityResult connectivityResult,
}) : this._(
status: FetchedProductStatus.internetError,
connectivityResult: connectivityResult,
exceptionString: exceptionString,
);

final Product? product;
final FetchedProductStatus status;
final ConnectivityResult? connectivityResult;
final String? exceptionString;
// TODO(monsieurtanuki): add a "ping" action in order to check if the server is alive?
}
13 changes: 7 additions & 6 deletions packages/smooth_app/lib/pages/hunger_games/question_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:shimmer/shimmer.dart';
import 'package:smooth_app/cards/product_cards/product_title_card.dart';
import 'package:smooth_app/data_models/fetched_product.dart';
import 'package:smooth_app/database/dao_product.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
Expand All @@ -24,22 +25,22 @@ class QuestionCard extends StatelessWidget {

@override
Widget build(BuildContext context) {
final Future<Product?> productFuture = _getProduct(
final Future<FetchedProduct> productFuture = _getProduct(
question.barcode!,
context.read<LocalDatabase>(),
);

final Size screenSize = MediaQuery.of(context).size;

return FutureBuilder<Product?>(
return FutureBuilder<FetchedProduct>(
future: productFuture,
builder: (
BuildContext context,
AsyncSnapshot<Product?> snapshot,
AsyncSnapshot<FetchedProduct> snapshot,
) {
Product? product;
if (snapshot.connectionState == ConnectionState.done) {
product = snapshot.data;
product = snapshot.data?.product;
// TODO(monsieurtanuki): do something aggressive if product is null here and we don't have a fallback value - like an error widget
}
// fallback version
Expand Down Expand Up @@ -131,13 +132,13 @@ class QuestionCard extends StatelessWidget {
);
}

Future<Product?> _getProduct(
Future<FetchedProduct> _getProduct(
final String barcode,
final LocalDatabase localDatabase,
) async {
final Product? result = await DaoProduct(localDatabase).get(barcode);
if (result != null) {
return result;
return FetchedProduct.found(result);
}
return ProductRefresher().silentFetchAndRefresh(
barcode: question.barcode!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ProductDialogHelper {
Future<FetchedProduct> openBestChoice() async {
final Product? product = await DaoProduct(localDatabase).get(barcode);
if (product != null) {
return FetchedProduct(product);
return FetchedProduct.found(product);
}
return openUniqueProductSearch();
}
Expand All @@ -52,7 +52,7 @@ class ProductDialogHelper {
isScanned: false,
).getFetchedProduct(),
title: '${AppLocalizations.of(context).looking_for}: $barcode') ??
FetchedProduct.error(FetchedProductStatus.userCancelled);
const FetchedProduct.userCancelled();

void _openProductNotFoundDialog() => showDialog<Widget>(
context: context,
Expand Down Expand Up @@ -175,9 +175,6 @@ class ProductDialogHelper {
case FetchedProductStatus.internetNotFound:
_openProductNotFoundDialog();
return;
case FetchedProductStatus.codeInvalid:
_openErrorMessage(appLocalizations.barcode_invalid_error);
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ class _ProductListItemSimpleState extends State<ProductListItemSimple> {
switch (_model.downloadingStatus) {
case null:
break;
case FetchedProductStatus.codeInvalid:
return appLocalizations.barcode_invalid_error;
case FetchedProductStatus.internetNotFound:
return appLocalizations.product_internet_error;
default:
Expand Down
78 changes: 39 additions & 39 deletions packages/smooth_app/lib/pages/product/common/product_refresher.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_svg/svg.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/fetched_product.dart';
import 'package:smooth_app/database/dao_product.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/dialogs/smooth_alert_dialog.dart';
Expand Down Expand Up @@ -93,14 +95,11 @@ class ProductRefresher {
/// Fetches the product from the server and refreshes the local database.
///
/// Silent version.
Future<Product?> silentFetchAndRefresh({
Future<FetchedProduct> silentFetchAndRefresh({
required final String barcode,
required final LocalDatabase localDatabase,
}) async {
final _MetaProductRefresher meta =
await _fetchAndRefresh(localDatabase, barcode);
return meta.product;
}
}) async =>
_fetchAndRefresh(localDatabase, barcode);

/// Fetches the products from the server and refreshes the local database.
///
Expand All @@ -111,23 +110,6 @@ class ProductRefresher {
}) async =>
_fetchAndRefreshList(localDatabase, barcodes);

/// Fetches the product from the server and refreshes the local database.
/// In the case of an error, it will be send throw an [Exception]
/// Silent version.
Future<Product?> silentFetchAndRefreshWithException({
required final String barcode,
required final LocalDatabase localDatabase,
}) async {
final _MetaProductRefresher meta =
await _fetchAndRefresh(localDatabase, barcode);

if (meta.error != null) {
throw Exception(meta.error);
}

return meta.product;
}

/// Fetches the product from the server and refreshes the local database.
///
/// With a waiting dialog.
Expand All @@ -139,18 +121,40 @@ class ProductRefresher {
final LocalDatabase localDatabase = widget.context.read<LocalDatabase>();
final AppLocalizations appLocalizations =
AppLocalizations.of(widget.context);
final _MetaProductRefresher? fetchAndRefreshed =
await LoadingDialog.run<_MetaProductRefresher>(
final FetchedProduct? fetchAndRefreshed =
await LoadingDialog.run<FetchedProduct>(
future: _fetchAndRefresh(localDatabase, barcode),
context: widget.context,
title: appLocalizations.refreshing_product,
);
if (fetchAndRefreshed == null) {
// the user probably cancelled
return false;
}
if (fetchAndRefreshed.product == null) {
if (widget.mounted) {
await LoadingDialog.error(context: widget.context);
String getTitle(final FetchedProduct fetchedProduct) {
switch (fetchAndRefreshed.status) {
// TODO(monsieurtanuki): refine and localize
case FetchedProductStatus.ok:
return 'Not supposed to happen...';
case FetchedProductStatus.userCancelled:
return 'Not supposed to happen either...';
case FetchedProductStatus.internetNotFound:
return 'Product not found';
case FetchedProductStatus.internetError:
if (fetchAndRefreshed.connectivityResult ==
ConnectivityResult.none) {
return 'You are not connected to the internet!';
}
return 'Server error (${fetchAndRefreshed.exceptionString})';
}
}

await LoadingDialog.error(
context: widget.context,
title: getTitle(fetchAndRefreshed),
);
}
return false;
}
Expand All @@ -165,7 +169,7 @@ class ProductRefresher {
return true;
}

Future<_MetaProductRefresher> _fetchAndRefresh(
Future<FetchedProduct> _fetchAndRefresh(
final LocalDatabase localDatabase,
final String barcode,
) async {
Expand All @@ -177,12 +181,17 @@ class ProductRefresher {
await DaoProduct(localDatabase).put(result.product!);
localDatabase.upToDate.setLatestDownloadedProduct(result.product!);
localDatabase.notifyListeners();
return _MetaProductRefresher.product(result.product);
return FetchedProduct.found(result.product!);
}
return const _MetaProductRefresher.error(null);
return const FetchedProduct.internetNotFound();
} catch (e) {
Logs.e('Refresh from server error', ex: e);
return _MetaProductRefresher.error(e.toString());
final ConnectivityResult connectivityResult =
await Connectivity().checkConnectivity();
return FetchedProduct.error(
exceptionString: e.toString(),
connectivityResult: connectivityResult,
);
}
}

Expand Down Expand Up @@ -212,12 +221,3 @@ class ProductRefresher {
}
}
}

class _MetaProductRefresher {
const _MetaProductRefresher.error(this.error) : product = null;

const _MetaProductRefresher.product(this.product) : error = null;

final String? error;
final Product? product;
}
4 changes: 2 additions & 2 deletions packages/smooth_app/lib/pages/product/new_product_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class _ProductPageState extends State<ProductPage>
final LocalDatabase localDatabase = context.read<LocalDatabase>();
final DaoProductList daoProductList = DaoProductList(localDatabase);
return RefreshIndicator(
onRefresh: () => ProductRefresher().fetchAndRefresh(
onRefresh: () async => ProductRefresher().fetchAndRefresh(
barcode: barcode,
widget: this,
),
Expand Down Expand Up @@ -266,7 +266,7 @@ class _ProductPageState extends State<ProductPage>
context,
<String>{widget.product.barcode!},
);
if (refreshed != null && refreshed) {
if (refreshed == true) {
setState(() {});
}
}
Expand Down
24 changes: 14 additions & 10 deletions packages/smooth_app/lib/pages/product/product_loader_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_svg/svg.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/fetched_product.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/buttons/smooth_large_button_with_icon.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
Expand Down Expand Up @@ -42,28 +43,31 @@ class _ProductLoaderPageState extends State<ProductLoaderPage> {
_state = _ProductLoaderState.loading;
});

try {
final Product? product =
await ProductRefresher().silentFetchAndRefreshWithException(
barcode: widget.barcode,
localDatabase: context.read<LocalDatabase>(),
);
final FetchedProduct fetchedProduct =
await ProductRefresher().silentFetchAndRefresh(
barcode: widget.barcode,
localDatabase: context.read<LocalDatabase>(),
);

if (product != null && mounted) {
if (mounted) {
if (fetchedProduct.product != null) {
navigator.pushReplacement(
AppRoutes.PRODUCT(
widget.barcode,
heroTag: 'product_${widget.barcode}',
),
extra: product,
extra: fetchedProduct.product,
);
} else {
return;
}
if (fetchedProduct.status == FetchedProductStatus.internetNotFound) {
setState(() {
_state = _ProductLoaderState.productNotFound;
});
return;
}
} catch (err) {
setState(() {
// TODO(monsieurtanuki): put more details from FetchedProduct?
_state = _ProductLoaderState.serverError;
});
}
Expand Down
Loading

0 comments on commit 2bdc0cd

Please sign in to comment.