mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-07-01 12:23:44 +08:00
Analytics: Move firebase lab detection into analytics package
This way the app.dart file has minimal logic.
This commit is contained in:
@ -1,4 +1,7 @@
|
|||||||
|
import 'package:flutter/foundation.dart' as foundation;
|
||||||
|
|
||||||
import 'package:fixnum/fixnum.dart';
|
import 'package:fixnum/fixnum.dart';
|
||||||
|
import 'package:flutter_runtime_env/flutter_runtime_env.dart';
|
||||||
import 'package:function_types/function_types.dart';
|
import 'package:function_types/function_types.dart';
|
||||||
import 'package:package_info_plus/package_info_plus.dart';
|
import 'package:package_info_plus/package_info_plus.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
@ -15,11 +18,8 @@ import 'storage.dart';
|
|||||||
|
|
||||||
export 'events.dart';
|
export 'events.dart';
|
||||||
|
|
||||||
const defaultAnalyticsEnabled = true;
|
|
||||||
|
|
||||||
class Analytics {
|
class Analytics {
|
||||||
bool enabled = defaultAnalyticsEnabled;
|
late bool enabled;
|
||||||
bool collectUsageStatistics = defaultAnalyticsEnabled;
|
|
||||||
|
|
||||||
final Func2<String, Map<String, String>, void> analyticsCallback;
|
final Func2<String, Map<String, String>, void> analyticsCallback;
|
||||||
final AnalyticsStorage storage;
|
final AnalyticsStorage storage;
|
||||||
@ -32,57 +32,48 @@ class Analytics {
|
|||||||
required this.enabled,
|
required this.enabled,
|
||||||
required this.pref,
|
required this.pref,
|
||||||
required this.pseudoId,
|
required this.pseudoId,
|
||||||
}) : config = AnalyticsConfig("", pref) {
|
required this.config,
|
||||||
collectUsageStatistics =
|
}) {
|
||||||
pref.getBool("collectUsageStatistics") ?? collectUsageStatistics;
|
|
||||||
|
|
||||||
_sessionId = DateTime.now().millisecondsSinceEpoch.toRadixString(16);
|
_sessionId = DateTime.now().millisecondsSinceEpoch.toRadixString(16);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Analytics? _global;
|
static Analytics? _global;
|
||||||
static Analytics init({
|
static Future<Analytics> init({
|
||||||
required bool enable,
|
|
||||||
required SharedPreferences pref,
|
required SharedPreferences pref,
|
||||||
required Func2<String, Map<String, String>, void> analyticsCallback,
|
required Func2<String, Map<String, String>, void> analyticsCallback,
|
||||||
required String storagePath,
|
required String storagePath,
|
||||||
}) {
|
}) async {
|
||||||
|
bool inFireBaseTestLab = await inFirebaseTestLab();
|
||||||
|
bool canBeEnabled = !foundation.kDebugMode && !inFireBaseTestLab;
|
||||||
|
|
||||||
var pseudoId = pref.getString("pseudoId");
|
var pseudoId = pref.getString("pseudoId");
|
||||||
if (pseudoId == null) {
|
if (pseudoId == null) {
|
||||||
pseudoId = const Uuid().v4();
|
pseudoId = const Uuid().v4();
|
||||||
pref.setString("pseudoId", pseudoId);
|
pref.setString("pseudoId", pseudoId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var config = AnalyticsConfig("", pref);
|
||||||
|
config.load(pref);
|
||||||
|
|
||||||
|
var enabled = canBeEnabled && config.enabled;
|
||||||
|
|
||||||
_global = Analytics._(
|
_global = Analytics._(
|
||||||
analyticsCallback: analyticsCallback,
|
analyticsCallback: analyticsCallback,
|
||||||
storage: AnalyticsStorage(storagePath),
|
storage: AnalyticsStorage(storagePath),
|
||||||
enabled: enable,
|
enabled: enabled,
|
||||||
pseudoId: pseudoId,
|
pseudoId: pseudoId,
|
||||||
pref: pref,
|
pref: pref,
|
||||||
|
config: config,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Log.d("Analytics Collection: $enabled");
|
||||||
|
Log.d("Analytics Storage: $storagePath");
|
||||||
|
|
||||||
_global!._sendAppUpdateEvent();
|
_global!._sendAppUpdateEvent();
|
||||||
|
|
||||||
return _global!;
|
return _global!;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> save() async {
|
|
||||||
_setBool(pref, "collectUsageStatistics", collectUsageStatistics,
|
|
||||||
defaultAnalyticsEnabled);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _setBool(
|
|
||||||
SharedPreferences pref,
|
|
||||||
String key,
|
|
||||||
bool value,
|
|
||||||
bool defaultValue,
|
|
||||||
) async {
|
|
||||||
if (value == defaultValue) {
|
|
||||||
await pref.remove(key);
|
|
||||||
} else {
|
|
||||||
await pref.setBool(key, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static Analytics? get instance => _global;
|
static Analytics? get instance => _global;
|
||||||
|
|
||||||
late String _sessionId;
|
late String _sessionId;
|
||||||
|
@ -14,22 +14,18 @@ class AnalyticsConfig extends ChangeNotifier with SettingsSharedPref {
|
|||||||
final SharedPreferences pref;
|
final SharedPreferences pref;
|
||||||
|
|
||||||
var appVersion = "";
|
var appVersion = "";
|
||||||
|
var enabled = true;
|
||||||
|
|
||||||
void load(SharedPreferences pref) {
|
void load(SharedPreferences pref) {
|
||||||
appVersion = pref.getString("appVersion") ?? "";
|
appVersion = pref.getString("appVersion") ?? "";
|
||||||
|
|
||||||
|
enabled = pref.getBool("collectUsageStatistics") ?? enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> save() async {
|
Future<void> save() async {
|
||||||
// var def = AnalyticsConfig(id, pref);
|
var def = AnalyticsConfig(id, pref);
|
||||||
|
|
||||||
pref.setString("appVersion", appVersion);
|
await setBool("collectUsageStatistics", enabled, def.enabled);
|
||||||
|
await pref.setString("appVersion", appVersion);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO
|
|
||||||
// 1. Config
|
|
||||||
// 2. Move all the logic from app to here (firebase)
|
|
||||||
// 3. Move the controlling logic over here
|
|
||||||
// 4. Backend stuff
|
|
||||||
// 5. Simple event log - UI
|
|
||||||
|
10
lib/app.dart
10
lib/app.dart
@ -1,11 +1,9 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart' as foundation;
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
import 'package:easy_localization/easy_localization.dart';
|
import 'package:easy_localization/easy_localization.dart';
|
||||||
import 'package:easy_localization_loader/easy_localization_loader.dart';
|
import 'package:easy_localization_loader/easy_localization_loader.dart';
|
||||||
import 'package:flutter_runtime_env/flutter_runtime_env.dart';
|
|
||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
@ -99,17 +97,11 @@ class JournalApp extends StatefulWidget {
|
|||||||
AppSettings appSettings,
|
AppSettings appSettings,
|
||||||
SharedPreferences pref,
|
SharedPreferences pref,
|
||||||
) async {
|
) async {
|
||||||
bool inFireBaseTestLab = await inFirebaseTestLab();
|
|
||||||
bool enabled = !foundation.kDebugMode && !inFireBaseTestLab;
|
|
||||||
|
|
||||||
var supportDir = await getApplicationSupportDirectory();
|
var supportDir = await getApplicationSupportDirectory();
|
||||||
var analyticsStorage = p.join(supportDir.path, 'analytics');
|
var analyticsStorage = p.join(supportDir.path, 'analytics');
|
||||||
await Directory(analyticsStorage).create(recursive: true);
|
await Directory(analyticsStorage).create(recursive: true);
|
||||||
|
|
||||||
Log.d("Analytics Collection: $enabled");
|
var analytics = await Analytics.init(
|
||||||
Log.d("Analytics Storage: $analyticsStorage");
|
|
||||||
var analytics = Analytics.init(
|
|
||||||
enable: enabled,
|
|
||||||
pref: pref,
|
pref: pref,
|
||||||
analyticsCallback: captureErrorBreadcrumb,
|
analyticsCallback: captureErrorBreadcrumb,
|
||||||
storagePath: analyticsStorage,
|
storagePath: analyticsStorage,
|
||||||
|
@ -466,11 +466,11 @@ class SettingsListState extends State<SettingsList> {
|
|||||||
if (Analytics.instance != null)
|
if (Analytics.instance != null)
|
||||||
SwitchListTile(
|
SwitchListTile(
|
||||||
title: Text(tr('settings.usageStats')),
|
title: Text(tr('settings.usageStats')),
|
||||||
value: Analytics.instance!.collectUsageStatistics,
|
value: Analytics.instance!.config.enabled,
|
||||||
onChanged: (bool val) {
|
onChanged: (bool val) {
|
||||||
Analytics.instance!.collectUsageStatistics = val;
|
Analytics.instance!.config.enabled = val;
|
||||||
Analytics.instance!.save();
|
Analytics.instance!.config.save();
|
||||||
setState(() {});
|
setState(() {}); // Remove this once Analytics.instace is not used
|
||||||
|
|
||||||
// FIXME: This also should go in the ananlytics package
|
// FIXME: This also should go in the ananlytics package
|
||||||
logEvent(
|
logEvent(
|
||||||
|
Reference in New Issue
Block a user