Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 14 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
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
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,43 @@ 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!';
}
if (fetchAndRefreshed.failedPingedHost != null) {
return 'Server down (${fetchAndRefreshed.failedPingedHost})';
}
return 'Server error (${fetchAndRefreshed.exceptionString})';
}
}

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

Future<_MetaProductRefresher> _fetchAndRefresh(
Future<FetchedProduct> _fetchAndRefresh(
final LocalDatabase localDatabase,
final String barcode,
) async {
Expand All @@ -177,12 +185,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 +238,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