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

Add Sentry crash-reporting opt-out #211

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 16 additions & 12 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -15,18 +15,22 @@ import 'package:sentry_flutter/sentry_flutter.dart';

Future<void> main() async {
usePathUrlStrategy();
await SentryFlutter.init(
(options) {
options.dsn = 'https://[email protected]/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://[email protected]/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
Expand Down
19 changes: 18 additions & 1 deletion lib/screens/SettingsScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,27 @@ class _SettingsScreenState extends State<SettingsScreen> {
)),
));

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)))
]));
Expand Down
19 changes: 18 additions & 1 deletion lib/services/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion lib/services/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class Storage {
// Read the file
return await file.readAsString();
} catch (e) {
// If encountering an error, return 0
return null;
}
}
Expand Down
Loading