From dc9aa7bc1d3e7efe2933a64f4b28dd5339d0f120 Mon Sep 17 00:00:00 2001 From: Vadim <44135514+vodemn@users.noreply.github.com> Date: Sat, 27 Jan 2024 21:33:10 +0100 Subject: [PATCH] run app with `runZonedGuarded` --- .vscode/launch.json | 4 ++-- lib/data/analytics/analytics.dart | 16 ++++++++++---- lib/firebase.dart | 22 ------------------- lib/main_prod.dart | 19 ++--------------- lib/main_release.dart | 19 ----------------- lib/runner.dart | 35 +++++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 64 deletions(-) delete mode 100644 lib/firebase.dart delete mode 100644 lib/main_release.dart create mode 100644 lib/runner.dart diff --git a/.vscode/launch.json b/.vscode/launch.json index a222516..76ad313 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -41,7 +41,7 @@ "--dart-define", "cameraPreviewAspectRatio=240/320", ], - "program": "${workspaceFolder}/lib/main_release.dart", + "program": "${workspaceFolder}/lib/main_prod.dart", }, { "name": "prod-profile (android)", @@ -54,7 +54,7 @@ "--dart-define", "cameraPreviewAspectRatio=240/320", ], - "program": "${workspaceFolder}/lib/main_release.dart", + "program": "${workspaceFolder}/lib/main_prod.dart", }, { "name": "dev-debug (ios)", diff --git a/lib/data/analytics/analytics.dart b/lib/data/analytics/analytics.dart index 9920311..1231a05 100644 --- a/lib/data/analytics/analytics.dart +++ b/lib/data/analytics/analytics.dart @@ -9,11 +9,19 @@ class LightmeterAnalytics { const LightmeterAnalytics({required ILightmeterAnalyticsApi api}) : _api = api; + void init() { + FlutterError.onError = (details) => logCrash(details.exception, details.stack); + PlatformDispatcher.instance.onError = (error, stack) { + logCrash(error, stack); + return true; + }; + } + Future logEvent( String eventName, { Map? parameters, }) async { - if (kDebugMode) { + if (!kReleaseMode) { log(' logEvent: $eventName / $parameters'); return; } @@ -26,17 +34,17 @@ class LightmeterAnalytics { Future logCrash( dynamic exception, - StackTrace? stack, { + StackTrace? stackTrace, { dynamic reason, Iterable information = const [], }) async { - if (kDebugMode) { + if (!kReleaseMode) { return; } return _api.logCrash( exception, - stack, + stackTrace, reason: reason, information: information, ); diff --git a/lib/firebase.dart b/lib/firebase.dart deleted file mode 100644 index 0a0e6b0..0000000 --- a/lib/firebase.dart +++ /dev/null @@ -1,22 +0,0 @@ -import 'dart:developer'; - -import 'package:firebase_core/firebase_core.dart'; -import 'package:firebase_crashlytics/firebase_crashlytics.dart'; -import 'package:flutter/foundation.dart'; - -import 'package:lightmeter/firebase_options.dart'; - -Future initializeFirebase({required bool handleErrors}) async { - try { - await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); - if (handleErrors) { - FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError; - PlatformDispatcher.instance.onError = (error, stack) { - FirebaseCrashlytics.instance.recordError(error, stack, fatal: true); - return true; - }; - } - } catch (e) { - log(e.toString()); - } -} diff --git a/lib/main_prod.dart b/lib/main_prod.dart index 3460f32..30e0ce1 100644 --- a/lib/main_prod.dart +++ b/lib/main_prod.dart @@ -1,19 +1,4 @@ -import 'package:flutter/material.dart'; -import 'package:lightmeter/application.dart'; -import 'package:lightmeter/application_wrapper.dart'; import 'package:lightmeter/environment.dart'; -import 'package:lightmeter/firebase.dart'; -import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart'; +import 'package:lightmeter/runner.dart'; -Future main() async { - WidgetsFlutterBinding.ensureInitialized(); - await initializeFirebase(handleErrors: true); - runApp( - const IAPProductsProvider( - child: ApplicationWrapper( - Environment.prod(), - child: Application(), - ), - ), - ); -} +Future main() => runLightmeterApp(const Environment.prod()); diff --git a/lib/main_release.dart b/lib/main_release.dart deleted file mode 100644 index eea83e2..0000000 --- a/lib/main_release.dart +++ /dev/null @@ -1,19 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:lightmeter/application.dart'; -import 'package:lightmeter/application_wrapper.dart'; -import 'package:lightmeter/environment.dart'; -import 'package:lightmeter/firebase.dart'; -import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart'; - -Future main() async { - WidgetsFlutterBinding.ensureInitialized(); - await initializeFirebase(handleErrors: false); - runApp( - const IAPProductsProvider( - child: ApplicationWrapper( - Environment.prod(), - child: Application(), - ), - ), - ); -} diff --git a/lib/runner.dart b/lib/runner.dart new file mode 100644 index 0000000..04d4321 --- /dev/null +++ b/lib/runner.dart @@ -0,0 +1,35 @@ +import 'dart:async'; + +import 'package:firebase_core/firebase_core.dart'; +import 'package:flutter/widgets.dart'; +import 'package:lightmeter/application.dart'; +import 'package:lightmeter/application_wrapper.dart'; +import 'package:lightmeter/data/analytics/analytics.dart'; +import 'package:lightmeter/data/analytics/api/analytics_firebase.dart'; +import 'package:lightmeter/environment.dart'; +import 'package:lightmeter/firebase_options.dart'; +import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart'; + +const _errorsLogger = LightmeterAnalytics(api: LightmeterAnalyticsFirebase()); + +Future runLightmeterApp(Environment env) async { + runZonedGuarded( + () async { + WidgetsFlutterBinding.ensureInitialized(); + if (env.buildType == BuildType.prod) { + await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); + } + _errorsLogger.init(); + final application = ApplicationWrapper(env, child: const Application()); + runApp( + env.buildType == BuildType.dev + ? IAPProducts( + products: [IAPProduct(storeId: IAPProductType.paidFeatures.storeId)], + child: application, + ) + : IAPProductsProvider(child: application), + ); + }, + _errorsLogger.logCrash, + ); +}