mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 18:38:36 +08:00
Analytics: simplify code
This commit is contained in:
@ -7,8 +7,8 @@ import 'package:package_info_plus/package_info_plus.dart';
|
|||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:uuid/uuid.dart';
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
import 'package:gitjournal/analytics/config.dart';
|
|
||||||
import 'package:gitjournal/logger/logger.dart';
|
import 'package:gitjournal/logger/logger.dart';
|
||||||
|
import 'config.dart';
|
||||||
import 'device_info.dart';
|
import 'device_info.dart';
|
||||||
import 'events.dart';
|
import 'events.dart';
|
||||||
import 'generated/analytics.pb.dart' as pb;
|
import 'generated/analytics.pb.dart' as pb;
|
||||||
@ -19,7 +19,7 @@ import 'storage.dart';
|
|||||||
export 'events.dart';
|
export 'events.dart';
|
||||||
|
|
||||||
class Analytics {
|
class Analytics {
|
||||||
late bool enabled;
|
final bool canBeEnabled;
|
||||||
|
|
||||||
final Func2<String, Map<String, String>, void> analyticsCallback;
|
final Func2<String, Map<String, String>, void> analyticsCallback;
|
||||||
final AnalyticsStorage storage;
|
final AnalyticsStorage storage;
|
||||||
@ -29,7 +29,7 @@ class Analytics {
|
|||||||
Analytics._({
|
Analytics._({
|
||||||
required this.storage,
|
required this.storage,
|
||||||
required this.analyticsCallback,
|
required this.analyticsCallback,
|
||||||
required this.enabled,
|
required this.canBeEnabled,
|
||||||
required this.pref,
|
required this.pref,
|
||||||
required this.pseudoId,
|
required this.pseudoId,
|
||||||
required this.config,
|
required this.config,
|
||||||
@ -55,18 +55,16 @@ class Analytics {
|
|||||||
var config = AnalyticsConfig("", pref);
|
var config = AnalyticsConfig("", pref);
|
||||||
config.load(pref);
|
config.load(pref);
|
||||||
|
|
||||||
var enabled = canBeEnabled && config.enabled;
|
|
||||||
|
|
||||||
_global = Analytics._(
|
_global = Analytics._(
|
||||||
analyticsCallback: analyticsCallback,
|
analyticsCallback: analyticsCallback,
|
||||||
storage: AnalyticsStorage(storagePath),
|
storage: AnalyticsStorage(storagePath),
|
||||||
enabled: enabled,
|
canBeEnabled: canBeEnabled,
|
||||||
pseudoId: pseudoId,
|
pseudoId: pseudoId,
|
||||||
pref: pref,
|
pref: pref,
|
||||||
config: config,
|
config: config,
|
||||||
);
|
);
|
||||||
|
|
||||||
Log.d("Analytics Collection: $enabled");
|
Log.d("Analytics Collection: ${_global!.enabled}");
|
||||||
Log.d("Analytics Storage: $storagePath");
|
Log.d("Analytics Storage: $storagePath");
|
||||||
|
|
||||||
_global!._sendAppUpdateEvent();
|
_global!._sendAppUpdateEvent();
|
||||||
@ -74,6 +72,22 @@ class Analytics {
|
|||||||
return _global!;
|
return _global!;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool get enabled {
|
||||||
|
return canBeEnabled && config.enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
set enabled(bool newVal) {
|
||||||
|
if (enabled != newVal) {
|
||||||
|
config.enabled = newVal;
|
||||||
|
config.save();
|
||||||
|
|
||||||
|
logEvent(
|
||||||
|
Event.AnalyticsLevelChanged,
|
||||||
|
parameters: {"state": newVal.toString()},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static Analytics? get instance => _global;
|
static Analytics? get instance => _global;
|
||||||
|
|
||||||
late String _sessionId;
|
late String _sessionId;
|
||||||
@ -115,6 +129,7 @@ class Analytics {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: Send the backlog events when disabled
|
||||||
Future<void> _sendAnalytics() async {
|
Future<void> _sendAnalytics() async {
|
||||||
if (!enabled) {
|
if (!enabled) {
|
||||||
return;
|
return;
|
||||||
@ -171,8 +186,3 @@ class Analytics {
|
|||||||
config.save();
|
config.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// FIXME: Discard the old analytics, if there are way too many!
|
|
||||||
// TODO: Take network connectivity into account
|
|
||||||
// TODO: Take connection type (wifi vs mobile) into account
|
|
||||||
|
@ -468,15 +468,8 @@ class SettingsListState extends State<SettingsList> {
|
|||||||
title: Text(tr('settings.usageStats')),
|
title: Text(tr('settings.usageStats')),
|
||||||
value: Analytics.instance!.config.enabled,
|
value: Analytics.instance!.config.enabled,
|
||||||
onChanged: (bool val) {
|
onChanged: (bool val) {
|
||||||
Analytics.instance!.config.enabled = val;
|
Analytics.instance!.enabled = val;
|
||||||
Analytics.instance!.config.save();
|
|
||||||
setState(() {}); // Remove this once Analytics.instace is not used
|
setState(() {}); // Remove this once Analytics.instace is not used
|
||||||
|
|
||||||
// FIXME: This also should go in the ananlytics package
|
|
||||||
logEvent(
|
|
||||||
Event.AnalyticsLevelChanged,
|
|
||||||
parameters: {"state": val.toString()},
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
SwitchListTile(
|
SwitchListTile(
|
||||||
|
Reference in New Issue
Block a user