Skip to content

Commit

Permalink
feat: 4423 - specific "Not connected to internet" displayed error (#4455
Browse files Browse the repository at this point in the history
)

* feat: 4423 - specific "Not connected to internet" displayed error

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`

* feat: 4423 - fixed pubspec.yaml

* feat: 4423 - new "server down" message after a ping attempt

Impacted files:
* `fetched_product.dart`: added field `failedPingedHost` where we store the host that we couldn't ping
* `generated_plugin_registrant.cc`: wtf
* `generated_plugins.cmake`: wtf
* `GeneratedPluginRegistrant.swift`: wtf
* `main.dart`: registered `DartPingIOS`
* `product_refresher.dart`: now trying to ping the server if exception and connection
* `pubspec.lock`: wtf
* `pubspec.yaml: added packages `dart_ping` and `dart_ping_ios`

* feat: 4423 - stupid lint check part 1

* feat: 4423 - stupid lint check part 2

* feat: 4423 - stupid lint check part 3

* feat: 4423 - stupid lint check part 5

* feat: 4423 - localizations

Impacted files:
* `app_en.arb`: 4 new labels when we couldn't retrieve a product (not found, no internet, server down, server error)
* `product_refresher.dart`: used the new labels
  • Loading branch information
monsieurtanuki committed Aug 16, 2023
1 parent ff10b44 commit f79bae6
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 103 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
51 changes: 43 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,63 @@
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,
this.failedPingedHost,
});

// 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,
final String? failedPingedHost,
}) : this._(
status: FetchedProductStatus.internetError,
connectivityResult: connectivityResult,
exceptionString: exceptionString,
failedPingedHost: failedPingedHost,
);

final Product? product;
final FetchedProductStatus status;

/// When relevant, result of the connectivity check.
final ConnectivityResult? connectivityResult;

/// When relevant, string of the exception.
final String? exceptionString;

/// When relevant, host of the query that we couldn't even ping.
final String? failedPingedHost;
}
26 changes: 26 additions & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -2272,6 +2272,32 @@
"@contrast_low": {
"description": "Low Contrast Text Color"
},
"product_refresher_internet_not_found": "Product not found!",
"@product_refresher_internet_not_found": {
"description": "When refreshing a product that does not exist on the server. Label is the body of a dialog."
},
"product_refresher_internet_not_connected": "You are not connected to internet!",
"@product_refresher_internet_not_connected": {
"description": "When refreshing a product and you're not even connected to internet. Label is the body of a dialog."
},
"product_refresher_internet_no_ping": "Server down ({host})",
"@product_refresher_internet_no_ping": {
"description": "When refreshing a product and you cannot even ping the server. Label is the body of a dialog.",
"placeholders": {
"host": {
"type": "String?"
}
}
},
"product_refresher_internet_error": "Server error ({exception})",
"@product_refresher_internet_error": {
"description": "When refreshing a product and the server returned an exception. Label is the body of a dialog.",
"placeholders": {
"exception": {
"type": "String?"
}
}
},
"product_loader_not_found_title": "Product not found!",
"@product_loader_not_found_title": {
"description": "When fetching a product opened via a link and it doesn't exist"
Expand Down
2 changes: 2 additions & 0 deletions packages/smooth_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:io';

import 'package:app_store_shared/app_store_shared.dart';
import 'package:dart_ping_ios/dart_ping_ios.dart';
import 'package:device_preview/device_preview.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -120,6 +121,7 @@ Future<bool> _init1() async {
return false;
}

DartPingIOS.register();
await SmoothServices().init(GlobalVars.appStore);
await setupAppNetworkConfig();
await UserManagementProvider.mountCredentials();
Expand Down
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
97 changes: 58 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,11 @@
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:dart_ping/dart_ping.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 +96,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 +111,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 +122,45 @@ 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) {
case FetchedProductStatus.ok:
return 'Not supposed to happen...';
case FetchedProductStatus.userCancelled:
return 'Not supposed to happen either...';
case FetchedProductStatus.internetNotFound:
return appLocalizations.product_refresher_internet_not_found;
case FetchedProductStatus.internetError:
if (fetchAndRefreshed.connectivityResult ==
ConnectivityResult.none) {
return appLocalizations
.product_refresher_internet_not_connected;
}
if (fetchAndRefreshed.failedPingedHost != null) {
return appLocalizations.product_refresher_internet_no_ping(
fetchAndRefreshed.failedPingedHost);
}
return appLocalizations.product_refresher_internet_no_ping(
fetchAndRefreshed.exceptionString);
}
}

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

Future<_MetaProductRefresher> _fetchAndRefresh(
Future<FetchedProduct> _fetchAndRefresh(
final LocalDatabase localDatabase,
final String barcode,
) async {
Expand All @@ -177,12 +187,30 @@ 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();
if (connectivityResult == ConnectivityResult.none) {
return FetchedProduct.error(
exceptionString: e.toString(),
connectivityResult: connectivityResult,
);
}
// TODO(monsieurtanuki): make things cleaner with off-dart
final String host =
OpenFoodAPIConfiguration.globalQueryType == QueryType.PROD
? OpenFoodAPIConfiguration.uriProdHost
: OpenFoodAPIConfiguration.uriTestHost;
final PingData result = await Ping(host, count: 1).stream.first;
return FetchedProduct.error(
exceptionString: e.toString(),
connectivityResult: connectivityResult,
failedPingedHost: result.error == null ? null : host,
);
}
}

Expand Down Expand Up @@ -212,12 +240,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;
}
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
Loading

0 comments on commit f79bae6

Please sign in to comment.