From 0656a92dc74a8874119c67c21fef37cf1ff449a1 Mon Sep 17 00:00:00 2001 From: Frederik Benoist Date: Thu, 1 Jun 2023 23:06:56 +0200 Subject: [PATCH] refactor:plausible --- lib/main.dart | 23 ++++++++++++++--------- lib/service/plausible.dart | 4 ++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index c2570c4..923f00a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,3 @@ -// ignore_for_file: prefer_const_constructors - import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -8,12 +6,10 @@ import 'package:flutter_localizations/flutter_localizations.dart'; import 'objectbox.dart'; import 'package:wakelock/wakelock.dart'; - import 'package:event_bus_plus/event_bus_plus.dart'; import 'package:mobdr/service/logger_util.dart'; import 'package:mobdr/config/constant.dart'; -import 'package:mobdr/core/routes/plausible_tracker.dart'; import 'package:mobdr/cubit/language/language_cubit.dart'; import 'package:mobdr/cubit/language/app_localizations.dart'; import 'package:mobdr/cubit/language/initial_language.dart'; @@ -26,7 +22,6 @@ import 'package:mobdr/db/db_log.dart'; /// Provides access to the ObjectBox Store throughout the app. late ObjectBox objectbox; -late PlausibleTracker plausible; final EventBus eventBus = EventBus(); @@ -96,19 +91,29 @@ class MyCustomScrollBehavior extends MaterialScrollBehavior { class MyApp extends StatelessWidget with WidgetsBindingObserver { @override void didChangeAppLifecycleState(AppLifecycleState state) async { - if (state == AppLifecycleState.resumed) { - LoggerUtil.logNStackInfo("The application is in foreground"); + if (state == AppLifecycleState.paused) { + LoggerUtil.logNStackInfo("The application is paused"); + } else if (state == AppLifecycleState.resumed) { + LoggerUtil.logNStackInfo("The application is resumed"); // check if plausible is UP - plausible.enabled = await plausible.hello(); + await PlausibleUtil.hello(); - plausible.event( + PlausibleUtil.addEvent( name: 'access', page: 'access', referrer: 'referrerPage', props: { 'name': SharedPrefs().login, }); + } else if (state == AppLifecycleState.inactive) { + LoggerUtil.logNStackInfo("The application is inactive"); + } else if (state == AppLifecycleState.detached) { + LoggerUtil.logNStackInfo("The application is detached"); + eventBus.dispose(); + } + + if (state == AppLifecycleState.resumed) { } else if (state == AppLifecycleState.inactive) { LoggerUtil.logNStackInfo("The application runs in the background"); } diff --git a/lib/service/plausible.dart b/lib/service/plausible.dart index d48a740..1cdc376 100644 --- a/lib/service/plausible.dart +++ b/lib/service/plausible.dart @@ -19,6 +19,10 @@ class PlausibleUtil { plausible!.enabled = await plausible!.hello(); } + static Future hello() async { + plausible!.enabled = await plausible!.hello(); + } + static void addEvent({ required String name, required String page,