From fdf8b2c318265074f701a175c55df62e0847584e Mon Sep 17 00:00:00 2001 From: Matias Pequeno Date: Wed, 2 Aug 2023 11:42:51 -0300 Subject: [PATCH] Initialize native sdk as a hook --- .../lib/src/hooks/native_hook.dart | 19 +++++++++++++ rollbar_flutter/lib/src/method_channel.dart | 18 +++++++++++++ rollbar_flutter/lib/src/rollbar.dart | 27 +++++++++---------- 3 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 rollbar_flutter/lib/src/hooks/native_hook.dart create mode 100644 rollbar_flutter/lib/src/method_channel.dart diff --git a/rollbar_flutter/lib/src/hooks/native_hook.dart b/rollbar_flutter/lib/src/hooks/native_hook.dart new file mode 100644 index 0000000..b404b02 --- /dev/null +++ b/rollbar_flutter/lib/src/hooks/native_hook.dart @@ -0,0 +1,19 @@ +import 'package:flutter/services.dart'; +import 'package:rollbar_dart/rollbar.dart'; + +import '../method_channel.dart'; +import 'hook.dart'; + +class NativeHook implements Hook { + static const _platform = MethodChannel('com.rollbar.flutter'); + + @override + Future install(final Config config) async { + await _platform.initialize(config: config); + } + + @override + Future uninstall() async { + await _platform.close(); + } +} diff --git a/rollbar_flutter/lib/src/method_channel.dart b/rollbar_flutter/lib/src/method_channel.dart new file mode 100644 index 0000000..e87334d --- /dev/null +++ b/rollbar_flutter/lib/src/method_channel.dart @@ -0,0 +1,18 @@ +import 'package:flutter/services.dart'; +import 'package:rollbar_flutter/rollbar_flutter.dart'; + +extension RollbarMethodChannel on MethodChannel { + /// The platform-specific path where we can persist data if needed. + Future get persistencePath async => + await invokeMethod('persistencePath'); + + /// Initializes the native Apple/Android SDK Rollbar notifier + /// using the given configuration. + Future initialize({required Config config}) async => + await invokeMethod('initialize', config.toMap()); + + /// Unwinds the native Apple/Android SDK Rollbar notifier. + /// + /// This is a no-op at the moment. + Future close() async => await invokeMethod('close'); +} diff --git a/rollbar_flutter/lib/src/rollbar.dart b/rollbar_flutter/lib/src/rollbar.dart index 625e201..2ae2e45 100644 --- a/rollbar_flutter/lib/src/rollbar.dart +++ b/rollbar_flutter/lib/src/rollbar.dart @@ -10,16 +10,9 @@ import 'package:rollbar_dart/rollbar.dart'; import 'hooks/hook.dart'; import 'hooks/flutter_hook.dart'; import 'hooks/platform_hook.dart'; +import 'hooks/native_hook.dart'; import 'platform_transformer.dart'; - -extension _Methods on MethodChannel { - Future initialize({required Config config}) async => - await invokeMethod('initialize', config.toMap()); - - /// The platform-specific path where we can persist data if needed. - Future get persistencePath async => - await invokeMethod('persistencePath'); -} +import 'method_channel.dart'; typedef RollbarClosure = FutureOr Function(); @@ -35,13 +28,20 @@ class RollbarFlutter { RollbarClosure appRunner, ) async { if (!config.handleUncaughtErrors) { - await _run(config, appRunner); - } else if (requiresCustomZone) { + await _run(config, appRunner, [NativeHook()]); + } else if (!PlatformHook.isAvailable) { await runZonedGuarded( - () async => await _run(config, appRunner, [FlutterHook()]), + () async => await _run(config, appRunner, [ + FlutterHook(), + NativeHook(), + ]), Rollbar.error); } else { - await _run(config, appRunner, [FlutterHook(), PlatformHook()]); + await _run(config, appRunner, [ + FlutterHook(), + PlatformHook(), + NativeHook(), + ]); } } @@ -62,7 +62,6 @@ class RollbarFlutter { await hook.install(config); } - await _platform.initialize(config: config); await appRunner(); }