Allow Analytics collection and Crash report collection to be disabled

As the developer I really don't want users to disable these, but as
a User, I always want the option, so I should include it.
This commit is contained in:
Vishesh Handa
2019-10-06 12:40:49 +02:00
parent 6c21f82d3a
commit 8d717cbaeb
4 changed files with 54 additions and 9 deletions

View File

@ -19,15 +19,15 @@ import 'screens/githostsetup_screens.dart';
import 'screens/onboarding_screens.dart';
class JournalApp extends StatelessWidget {
static Future main() async {
static Future main(SharedPreferences pref) async {
Fimber.plantTree(DebugTree.elapsed(useColors: true));
var pref = await SharedPreferences.getInstance();
var appState = AppState(pref);
appState.dumpToLog();
_enableAnalyticsIfPossible();
if (Settings.instance.collectUsageStatistics) {
_enableAnalyticsIfPossible();
}
if (appState.localGitRepoConfigured == false) {
// FIXME: What about exceptions!
@ -41,8 +41,6 @@ class JournalApp extends StatelessWidget {
var dir = await getGitBaseDirectory();
appState.gitBaseDirectory = dir.path;
Settings.instance.load(pref);
runApp(StateContainer(
appState: appState,
child: JournalApp(),

View File

@ -2,23 +2,32 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_crashlytics/flutter_crashlytics.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:journal/app.dart';
import 'package:journal/settings.dart';
void main() async {
var pref = await SharedPreferences.getInstance();
Settings.instance.load(pref);
var reportCrashes =
!JournalApp.isInDebugMode && Settings.instance.collectCrashReports;
FlutterError.onError = (FlutterErrorDetails details) {
if (JournalApp.isInDebugMode) {
if (!reportCrashes) {
FlutterError.dumpErrorToConsole(details);
} else {
Zone.current.handleUncaughtError(details.exception, details.stack);
}
};
if (!JournalApp.isInDebugMode) {
if (reportCrashes) {
await FlutterCrashlytics().initialize();
}
runZoned<Future<void>>(() async {
await JournalApp.main();
await JournalApp.main(pref);
}, onError: (Object error, StackTrace stackTrace) async {
print("Uncaught Exception: " + error.toString());
print(stackTrace);

View File

@ -197,6 +197,34 @@ class SettingsListState extends State<SettingsList> {
Settings.instance.save();
},
),
SizedBox(height: 16.0),
PreferenceTitle("Analytics"),
CheckboxPreference(
"Collect Anonymous Usage Statistics",
"usage_stats",
defaultVal: Settings.instance.collectUsageStatistics,
onEnable: () {
Settings.instance.collectUsageStatistics = true;
Settings.instance.save();
},
onDisable: () {
Settings.instance.collectUsageStatistics = false;
Settings.instance.save();
},
),
CheckboxPreference(
"Collect Anonymous Crash Reports",
"crash_reports",
defaultVal: Settings.instance.collectCrashReports,
onEnable: () {
Settings.instance.collectCrashReports = true;
Settings.instance.save();
},
onDisable: () {
Settings.instance.collectCrashReports = false;
Settings.instance.save();
},
),
VersionNumberTile(),
]);
}

View File

@ -24,6 +24,9 @@ class Settings {
NoteViewerFontSize noteViewerFontSize = NoteViewerFontSize.Normal;
bool collectUsageStatistics = true;
bool collectCrashReports = true;
void load(SharedPreferences pref) {
gitAuthor = pref.getString("gitAuthor") ?? gitAuthor;
gitAuthorEmail = pref.getString("gitAuthorEmail") ?? gitAuthorEmail;
@ -36,6 +39,11 @@ class Settings {
str = pref.getString("noteFileNameFormat") ?? noteFileNameFormat.toString();
noteFileNameFormat =
NoteFileNameFormat.values.firstWhere((e) => e.toString() == str);
collectUsageStatistics =
pref.getBool("collectCrashReports") ?? collectUsageStatistics;
collectCrashReports =
pref.getBool("collectCrashReports") ?? collectCrashReports;
}
Future save() async {
@ -44,6 +52,8 @@ class Settings {
pref.setString("gitAuthorEmail", gitAuthorEmail);
pref.setString("noteViewerFontSize", noteViewerFontSize.toString());
pref.setString("noteFileNameFormat", noteFileNameFormat.toString());
pref.setBool("collectUsageStatistics", collectUsageStatistics);
pref.setBool("collectCrashReports", collectCrashReports);
// Shouldn't we check if something has actually changed?
for (var f in changeObservers) {