Skip to content

Commit c2ef51e

Browse files
authored
Merge pull request #16 from GabrielRozendo/fix-lifecycle
Changing the "SystemChannels.lifecycle" to "AppLifecycleListener"
2 parents 7bd460d + 2a20596 commit c2ef51e

1 file changed

Lines changed: 9 additions & 16 deletions

File tree

lib/aptabase_flutter.dart

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import "dart:developer" as developer;
88

99
import "package:aptabase_flutter/sys_info.dart";
1010
import "package:flutter/foundation.dart";
11+
import "package:flutter/widgets.dart";
1112
import "package:universal_io/io.dart";
1213

1314
import "package:aptabase_flutter/init_options.dart";
1415
import "package:aptabase_flutter/random_string.dart";
1516
import "package:aptabase_flutter/storage_manager.dart";
1617
import "package:aptabase_flutter/storage_manager_hive.dart";
17-
import "package:flutter/scheduler.dart";
18-
import "package:flutter/services.dart";
1918

2019
export "package:aptabase_flutter/init_options.dart";
2120

@@ -48,9 +47,8 @@ class Aptabase {
4847
static Timer? _timer;
4948
static var _isTimerRunning = false;
5049
static late final StorageManager _storage;
50+
static AppLifecycleListener? _listener;
5151

52-
static final _inactiveState = AppLifecycleState.inactive.toString();
53-
static final _pausedState = AppLifecycleState.paused.toString();
5452
static final instance = Aptabase._();
5553

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

97-
SystemChannels.lifecycle.setMessageHandler(_handleLifeCycle);
95+
_listener = AppLifecycleListener(
96+
onInactive: () => _tick("lifecycle onInactive"),
97+
onResume: _startTimer,
98+
);
9899

99100
await _tick("init");
100101
_startTimer();
@@ -106,6 +107,9 @@ class Aptabase {
106107
_timer?.cancel();
107108
_timer = null;
108109
_isTimerRunning = false;
110+
111+
_listener?.dispose();
112+
_listener = null;
109113
}
110114

111115
static void _startTimer() {
@@ -115,17 +119,6 @@ class Aptabase {
115119
);
116120
}
117121

118-
static Future<String?> _handleLifeCycle(String? msg) async {
119-
if (msg == _inactiveState || msg == _pausedState) {
120-
await _tick("lifecycle $msg");
121-
_dispose();
122-
} else {
123-
_startTimer();
124-
}
125-
126-
return msg;
127-
}
128-
129122
static Future<void> _tick(String reason) async {
130123
_logDebug("Checking events ($reason)");
131124

0 commit comments

Comments
 (0)