Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing the "SystemChannels.lifecycle" to "AppLifecycleListener" #16

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions lib/aptabase_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import "dart:developer" as developer;

import "package:aptabase_flutter/sys_info.dart";
import "package:flutter/foundation.dart";
import "package:flutter/widgets.dart";
import "package:universal_io/io.dart";

import "package:aptabase_flutter/init_options.dart";
import "package:aptabase_flutter/random_string.dart";
import "package:aptabase_flutter/storage_manager.dart";
import "package:aptabase_flutter/storage_manager_hive.dart";
import "package:flutter/scheduler.dart";
import "package:flutter/services.dart";

export "package:aptabase_flutter/init_options.dart";

Expand Down Expand Up @@ -48,9 +47,8 @@ class Aptabase {
static Timer? _timer;
static var _isTimerRunning = false;
static late final StorageManager _storage;
static AppLifecycleListener? _listener;

static final _inactiveState = AppLifecycleState.inactive.toString();
static final _pausedState = AppLifecycleState.paused.toString();
static final instance = Aptabase._();

/// Initializes the Aptabase SDK with the given appKey.
Expand Down Expand Up @@ -94,7 +92,10 @@ class Aptabase {
await _storage.init();
_logDebug("Storage initialized");

SystemChannels.lifecycle.setMessageHandler(_handleLifeCycle);
_listener = AppLifecycleListener(
onInactive: () => _tick("lifecycle onInactive"),
onResume: _startTimer,
);

await _tick("init");
_startTimer();
Expand All @@ -106,6 +107,9 @@ class Aptabase {
_timer?.cancel();
_timer = null;
_isTimerRunning = false;

_listener?.dispose();
_listener = null;
}

static void _startTimer() {
Expand All @@ -115,17 +119,6 @@ class Aptabase {
);
}

static Future<String?> _handleLifeCycle(String? msg) async {
if (msg == _inactiveState || msg == _pausedState) {
await _tick("lifecycle $msg");
_dispose();
} else {
_startTimer();
}

return msg;
}

static Future<void> _tick(String reason) async {
_logDebug("Checking events ($reason)");

Expand Down