Skip to content

Commit

Permalink
feat: 4423 - new "server down" message after a ping attempt
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
monsieurtanuki committed Aug 6, 2023
1 parent e656cd3 commit e6776cf
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 5 deletions.
11 changes: 10 additions & 1 deletion packages/smooth_app/lib/data_models/fetched_product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class FetchedProduct {
this.product,
this.connectivityResult,
this.exceptionString,
this.failedPingedHost,
});

// The reason behind the "ignore": I want to force "product" to be not null
Expand All @@ -40,15 +41,23 @@ class FetchedProduct {
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;
// TODO(monsieurtanuki): add a "ping" action in order to check if the server is alive?

/// 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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';
Expand Down Expand Up @@ -147,6 +148,9 @@ class ProductRefresher {
ConnectivityResult.none) {
return 'You are not connected to the internet!';
}
if (fetchAndRefreshed.failedPingedHost != null) {
return 'Server down (${fetchAndRefreshed.failedPingedHost})';
}
return 'Server error (${fetchAndRefreshed.exceptionString})';
}
}
Expand Down Expand Up @@ -188,9 +192,22 @@ class ProductRefresher {
Logs.e('Refresh from server error', ex: e);
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import FlutterMacOS
import Foundation

import audioplayers_darwin
import connectivity_plus
import device_info_plus
import file_selector_macos
import flutter_secure_storage_macos
Expand All @@ -22,6 +23,7 @@ import url_launcher_macos

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin"))
ConnectivityPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlugin"))
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
FlutterSecureStorageMacosPlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStorageMacosPlugin"))
Expand Down
24 changes: 24 additions & 0 deletions packages/smooth_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.5"
dart_ping:
dependency: "direct main"
description:
name: dart_ping
sha256: dd3a93d9b986565cb2fadd0c9277cf9880298634ccc9588e353e63c6f736a386
url: "https://pub.dev"
source: hosted
version: "9.0.0"
dart_ping_ios:
dependency: "direct main"
description:
name: dart_ping_ios
sha256: ba60bcd1ef8f13d564e9490197fb32c34d38fd1c10a890143a52f5b71d82ea95
url: "https://pub.dev"
source: hosted
version: "4.0.0"
dart_style:
dependency: transitive
description:
Expand Down Expand Up @@ -541,6 +557,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.0.1"
flutter_icmp_ping:
dependency: transitive
description:
name: flutter_icmp_ping
sha256: a06c2255a857c8f9d1b0a68f546b113557e48e7a543f91e38bd66aeab296f3a6
url: "https://pub.dev"
source: hosted
version: "3.1.2"
flutter_image_compress:
dependency: "direct main"
description:
Expand Down
7 changes: 3 additions & 4 deletions packages/smooth_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ dependencies:
webview_flutter: 3.0.4
flutter_custom_tabs: ^1.0.4
flutter_image_compress: 2.0.4
connectivity_plus: ^4.0.2
dart_ping: 9.0.0
dart_ping_ios: 4.0.0

# According to the build variant, only one "app store" implementation must be added when building a release
# Call "flutter pub remove xxxx" to remove unused dependencies
Expand All @@ -96,11 +99,7 @@ dependencies:
scanner_zxing:
path: ../scanner/zxing




openfoodfacts: 2.7.4
connectivity_plus: ^4.0.2
# openfoodfacts:
# path: ../../../openfoodfacts-dart

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "generated_plugin_registrant.h"

#include <audioplayers_windows/audioplayers_windows_plugin.h>
#include <connectivity_plus/connectivity_plus_windows_plugin.h>
#include <file_selector_windows/file_selector_windows.h>
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>
#include <permission_handler_windows/permission_handler_windows_plugin.h>
Expand All @@ -18,6 +19,8 @@
void RegisterPlugins(flutter::PluginRegistry* registry) {
AudioplayersWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("AudioplayersWindowsPlugin"));
ConnectivityPlusWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("ConnectivityPlusWindowsPlugin"));
FileSelectorWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FileSelectorWindows"));
FlutterSecureStorageWindowsPluginRegisterWithRegistrar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

list(APPEND FLUTTER_PLUGIN_LIST
audioplayers_windows
connectivity_plus
file_selector_windows
flutter_secure_storage_windows
permission_handler_windows
Expand Down

0 comments on commit e6776cf

Please sign in to comment.