From 3d250f4e41e29d9d8f00344a45ff68f37d0b9568 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Fri, 20 Dec 2024 12:36:51 -0500 Subject: [PATCH] Add Sentry crash-reporting opt-out (#211) * Add Sentry opt-out * [chore] Remove inaccurate comment * Prevent line-wrap in iOS --- lib/main.dart | 28 ++++++++++++++++------------ lib/screens/SettingsScreen.dart | 19 ++++++++++++++++++- lib/services/settings.dart | 19 ++++++++++++++++++- lib/services/storage.dart | 1 - 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index d6364b4..adeaf3d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,7 +2,7 @@ import 'dart:async'; import 'package:flutter/cupertino.dart' show CupertinoThemeData, DefaultCupertinoLocalizations; import 'package:flutter/material.dart' - show BottomSheetThemeData, Colors, DefaultMaterialLocalizations, ThemeData, ThemeMode, MaterialApp, Scaffold; + show BottomSheetThemeData, Colors, DefaultMaterialLocalizations, ThemeData, ThemeMode; import 'package:flutter/scheduler.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; @@ -15,18 +15,22 @@ import 'package:sentry_flutter/sentry_flutter.dart'; Future main() async { usePathUrlStrategy(); - await SentryFlutter.init( - (options) { - options.dsn = 'https://96106df405ade3f013187dfc8e4200e7@o920269.ingest.us.sentry.io/4508132321001472'; - // Capture all traces. May need to adjust if overwhelming - options.tracesSampleRate = 1.0; - // For each trace, capture all profiles - options.profilesSampleRate = 1.0; - }, - appRunner: () => runApp(Main()), - ); - // or define SENTRY_DSN via Dart environment variable (--dart-define) + var settings = Settings(); + if (settings.trackErrors) { + await SentryFlutter.init( + (options) { + options.dsn = 'https://96106df405ade3f013187dfc8e4200e7@o920269.ingest.us.sentry.io/4508132321001472'; + // Capture all traces. May need to adjust if overwhelming + options.tracesSampleRate = 1.0; + // For each trace, capture all profiles + options.profilesSampleRate = 1.0; + }, + appRunner: () => runApp(Main()), + ); + } else { + runApp(Main()); + } } //TODO: EventChannel might be better than the stream controller we are using now diff --git a/lib/screens/SettingsScreen.dart b/lib/screens/SettingsScreen.dart index dd35bcd..db01e69 100644 --- a/lib/screens/SettingsScreen.dart +++ b/lib/screens/SettingsScreen.dart @@ -85,10 +85,27 @@ class _SettingsScreenState extends State { )), )); + items.add(ConfigSection(children: [ + ConfigItem( + label: Text('Report errors automatically'), + labelWidth: 250, + content: Align( + alignment: Alignment.centerRight, + child: Switch.adaptive( + materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, + value: settings.trackErrors, + onChanged: (value) { + setState(() { + settings.trackErrors = value; + }); + }, + ))), + ])); + items.add(ConfigSection(children: [ ConfigPageItem( label: Text('Enroll with Managed Nebula'), - labelWidth: 200, + labelWidth: 250, onPressed: () => Utils.openPage(context, (context) => EnrollmentScreen(stream: widget.stream, allowCodeEntry: true))) ])); diff --git a/lib/services/settings.dart b/lib/services/settings.dart index d4d4742..67c802d 100644 --- a/lib/services/settings.dart +++ b/lib/services/settings.dart @@ -3,6 +3,10 @@ import 'dart:convert'; import 'package:flutter/cupertino.dart'; import 'package:flutter/scheduler.dart'; import 'package:mobile_nebula/services/storage.dart'; +import 'package:sentry_flutter/sentry_flutter.dart'; + +bool DEFAULT_LOG_WRAP = false; +bool DEFAULT_TRACK_ERRORS = true; class Settings { final _storage = Storage(); @@ -30,13 +34,26 @@ class Settings { } bool get logWrap { - return _getBool('logWrap', false); + return _getBool('logWrap', DEFAULT_LOG_WRAP); } set logWrap(bool enabled) { _set('logWrap', enabled); } + bool get trackErrors { + return _getBool('trackErrors', DEFAULT_TRACK_ERRORS); + } + + set trackErrors(bool enabled) { + _set('trackErrors', enabled); + + // Side-effect: Disable Sentry immediately + if (!enabled) { + Sentry.close(); + } + } + String _getString(String key, String defaultValue) { final val = _settings[key]; if (val is String) { diff --git a/lib/services/storage.dart b/lib/services/storage.dart index af8dcc6..a3451c3 100644 --- a/lib/services/storage.dart +++ b/lib/services/storage.dart @@ -42,7 +42,6 @@ class Storage { // Read the file return await file.readAsString(); } catch (e) { - // If encountering an error, return 0 return null; } }