mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-18 14:23:42 +00:00
fix(web): wait for the saved analytics preference (#1656)
This commit is contained in:
1 parent
a34383a7e2
commit
392ee36137
3 files changed
+20
-2
No files matched your search
@@ -53,6 +53,7 @@ class AppRuntimeState {
|
|||||||
class AppRuntimeController extends AsyncNotifier<AppRuntimeState> {
|
class AppRuntimeController extends AsyncNotifier<AppRuntimeState> {
|
||||||
static StartConfig? _defaultStartConfig;
|
static StartConfig? _defaultStartConfig;
|
||||||
Future<void> _preferenceWrites = Future<void>.value();
|
Future<void> _preferenceWrites = Future<void>.value();
|
||||||
|
bool _analyticsStarted = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<AppRuntimeState> build() async {
|
Future<AppRuntimeState> build() async {
|
||||||
@@ -73,6 +74,7 @@ class AppRuntimeController extends AsyncNotifier<AppRuntimeState> {
|
|||||||
startConfig
|
startConfig
|
||||||
..apiEnable = true
|
..apiEnable = true
|
||||||
..mcpEnable = config.api.mcpEnable;
|
..mcpEnable = config.api.mcpEnable;
|
||||||
|
await _initAnalytics(config);
|
||||||
} else {
|
} else {
|
||||||
startConfig
|
startConfig
|
||||||
..apiEnable = config.api.enable
|
..apiEnable = config.api.enable
|
||||||
@@ -253,7 +255,8 @@ class AppRuntimeController extends AsyncNotifier<AppRuntimeState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _initAnalytics(DownloaderConfig config) async {
|
Future<void> _initAnalytics(DownloaderConfig config) async {
|
||||||
if (!AnalyticsConfig.isConfigured || !config.extra.analyticsEnabled) return;
|
if (_analyticsStarted || !AnalyticsConfig.isConfigured || !config.extra.analyticsEnabled) return;
|
||||||
|
_analyticsStarted = true;
|
||||||
try {
|
try {
|
||||||
final generatedClientId = await Analytics.instance.init(fallbackClientId: config.extra.deviceId);
|
final generatedClientId = await Analytics.instance.init(fallbackClientId: config.extra.deviceId);
|
||||||
if (generatedClientId != null) {
|
if (generatedClientId != null) {
|
||||||
@@ -262,6 +265,7 @@ class AppRuntimeController extends AsyncNotifier<AppRuntimeState> {
|
|||||||
}
|
}
|
||||||
unawaited(Analytics.instance.logAppOpen());
|
unawaited(Analytics.instance.logAppOpen());
|
||||||
} catch (error, stackTrace) {
|
} catch (error, stackTrace) {
|
||||||
|
_analyticsStarted = false;
|
||||||
logger.w('GA4 init failed', error, stackTrace);
|
logger.w('GA4 init failed', error, stackTrace);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ Future<DownloaderConfig> loadDownloaderConfig({
|
|||||||
config = await load();
|
config = await load();
|
||||||
} catch (error, stackTrace) {
|
} catch (error, stackTrace) {
|
||||||
onLoadError?.call(error, stackTrace);
|
onLoadError?.call(error, stackTrace);
|
||||||
config = DownloaderConfig();
|
config = DownloaderConfig()..extra.analyticsEnabled = false;
|
||||||
loaded = false;
|
loaded = false;
|
||||||
}
|
}
|
||||||
final changed = await initDownloaderConfig(config, defaultDownloadDir: defaultDownloadDir);
|
final changed = await initDownloaderConfig(config, defaultDownloadDir: defaultDownloadDir);
|
||||||
|
|||||||
@@ -32,8 +32,22 @@ void main() {
|
|||||||
// The in-memory fallback still gets usable defaults.
|
// The in-memory fallback still gets usable defaults.
|
||||||
expect(config.downloadDir, '/downloads');
|
expect(config.downloadDir, '/downloads');
|
||||||
expect(config.categories, hasLength(4));
|
expect(config.categories, hasLength(4));
|
||||||
|
expect(config.extra.analyticsEnabled, isFalse);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
for (final analyticsEnabled in [false, true]) {
|
||||||
|
test('preserves the saved analytics preference ($analyticsEnabled)', () async {
|
||||||
|
final stored = configWith()..extra.analyticsEnabled = analyticsEnabled;
|
||||||
|
final config = await loadDownloaderConfig(
|
||||||
|
load: () async => stored,
|
||||||
|
save: (_) async {},
|
||||||
|
defaultDownloadDir: () async => '/downloads',
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(config.extra.analyticsEnabled, analyticsEnabled);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
test('skips persist when init changed nothing', () async {
|
test('skips persist when init changed nothing', () async {
|
||||||
var saved = 0;
|
var saved = 0;
|
||||||
await loadDownloaderConfig(
|
await loadDownloaderConfig(
|
||||||
|
|||||||
Reference in new issue
Block a user