mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-07-01 04:07:53 +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:flutter_runtime_env/flutter_runtime_env.dart';
|
||||
import 'package:function_types/function_types.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
@ -15,11 +18,8 @@ import 'storage.dart';
|
||||
|
||||
export 'events.dart';
|
||||
|
||||
const defaultAnalyticsEnabled = true;
|
||||
|
||||
class Analytics {
|
||||
bool enabled = defaultAnalyticsEnabled;
|
||||
bool collectUsageStatistics = defaultAnalyticsEnabled;
|
||||
late bool enabled;
|
||||
|
||||
final Func2<String, Map<String, String>, void> analyticsCallback;
|
||||
final AnalyticsStorage storage;
|
||||
@ -32,57 +32,48 @@ class Analytics {
|
||||
required this.enabled,
|
||||
required this.pref,
|
||||
required this.pseudoId,
|
||||
}) : config = AnalyticsConfig("", pref) {
|
||||
collectUsageStatistics =
|
||||
pref.getBool("collectUsageStatistics") ?? collectUsageStatistics;
|
||||
|
||||
required this.config,
|
||||
}) {
|
||||
_sessionId = DateTime.now().millisecondsSinceEpoch.toRadixString(16);
|
||||
}
|
||||
|
||||
static Analytics? _global;
|
||||
static Analytics init({
|
||||
required bool enable,
|
||||
static Future<Analytics> init({
|
||||
required SharedPreferences pref,
|
||||
required Func2<String, Map<String, String>, void> analyticsCallback,
|
||||
required String storagePath,
|
||||
}) {
|
||||
}) async {
|
||||
bool inFireBaseTestLab = await inFirebaseTestLab();
|
||||
bool canBeEnabled = !foundation.kDebugMode && !inFireBaseTestLab;
|
||||
|
||||
var pseudoId = pref.getString("pseudoId");
|
||||
if (pseudoId == null) {
|
||||
pseudoId = const Uuid().v4();
|
||||
pref.setString("pseudoId", pseudoId);
|
||||
}
|
||||
|
||||
var config = AnalyticsConfig("", pref);
|
||||
config.load(pref);
|
||||
|
||||
var enabled = canBeEnabled && config.enabled;
|
||||
|
||||
_global = Analytics._(
|
||||
analyticsCallback: analyticsCallback,
|
||||
storage: AnalyticsStorage(storagePath),
|
||||
enabled: enable,
|
||||
enabled: enabled,
|
||||
pseudoId: pseudoId,
|
||||
pref: pref,
|
||||
config: config,
|
||||
);
|
||||
|
||||
Log.d("Analytics Collection: $enabled");
|
||||
Log.d("Analytics Storage: $storagePath");
|
||||
|
||||
_global!._sendAppUpdateEvent();
|
||||
|
||||
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;
|
||||
|
||||
late String _sessionId;
|
||||
|
@ -14,22 +14,18 @@ class AnalyticsConfig extends ChangeNotifier with SettingsSharedPref {
|
||||
final SharedPreferences pref;
|
||||
|
||||
var appVersion = "";
|
||||
var enabled = true;
|
||||
|
||||
void load(SharedPreferences pref) {
|
||||
appVersion = pref.getString("appVersion") ?? "";
|
||||
|
||||
enabled = pref.getBool("collectUsageStatistics") ?? enabled;
|
||||
}
|
||||
|
||||
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 'package:flutter/foundation.dart' as foundation;
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:easy_localization/easy_localization.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_provider/path_provider.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
@ -99,17 +97,11 @@ class JournalApp extends StatefulWidget {
|
||||
AppSettings appSettings,
|
||||
SharedPreferences pref,
|
||||
) async {
|
||||
bool inFireBaseTestLab = await inFirebaseTestLab();
|
||||
bool enabled = !foundation.kDebugMode && !inFireBaseTestLab;
|
||||
|
||||
var supportDir = await getApplicationSupportDirectory();
|
||||
var analyticsStorage = p.join(supportDir.path, 'analytics');
|
||||
await Directory(analyticsStorage).create(recursive: true);
|
||||
|
||||
Log.d("Analytics Collection: $enabled");
|
||||
Log.d("Analytics Storage: $analyticsStorage");
|
||||
var analytics = Analytics.init(
|
||||
enable: enabled,
|
||||
var analytics = await Analytics.init(
|
||||
pref: pref,
|
||||
analyticsCallback: captureErrorBreadcrumb,
|
||||
storagePath: analyticsStorage,
|
||||
|
@ -466,11 +466,11 @@ class SettingsListState extends State<SettingsList> {
|
||||
if (Analytics.instance != null)
|
||||
SwitchListTile(
|
||||
title: Text(tr('settings.usageStats')),
|
||||
value: Analytics.instance!.collectUsageStatistics,
|
||||
value: Analytics.instance!.config.enabled,
|
||||
onChanged: (bool val) {
|
||||
Analytics.instance!.collectUsageStatistics = val;
|
||||
Analytics.instance!.save();
|
||||
setState(() {});
|
||||
Analytics.instance!.config.enabled = val;
|
||||
Analytics.instance!.config.save();
|
||||
setState(() {}); // Remove this once Analytics.instace is not used
|
||||
|
||||
// FIXME: This also should go in the ananlytics package
|
||||
logEvent(
|
||||
|
Reference in New Issue
Block a user