Skip to content

Commit c385b62

Browse files
committed
fix: dynamically enable or disable PostHogWidget
1 parent 3cf9ab8 commit c385b62

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

lib/src/posthog.dart

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'package:meta/meta.dart';
1+
import 'package:flutter/foundation.dart';
22

33
import 'package:posthog_flutter/src/error_tracking/posthog_error_tracking_autocapture_integration.dart';
44
import 'package:posthog_flutter/src/error_tracking/posthog_exception.dart';
@@ -14,6 +14,9 @@ class Posthog {
1414

1515
PostHogConfig? _config;
1616

17+
@internal
18+
final sessionRecordingActive = ValueNotifier<bool>(false);
19+
1720
factory Posthog() {
1821
return _instance;
1922
}
@@ -42,6 +45,10 @@ class Posthog {
4245
Future<void> setup(PostHogConfig config) {
4346
_config = config; // Store the config
4447

48+
if (config.sessionReplay) {
49+
sessionRecordingActive.value = true;
50+
}
51+
4552
_installFlutterIntegrations(config);
4653

4754
return _posthog.setup(config);
@@ -200,6 +207,7 @@ class Posthog {
200207
Future<void> close() {
201208
_config = null;
202209
_currentScreen = null;
210+
sessionRecordingActive.value = false;
203211
PosthogObserver.clearCurrentContext();
204212

205213
// Uninstall Flutter integrations
@@ -217,13 +225,18 @@ class Posthog {
217225
///
218226
/// [resumeCurrent] - If true (default), resumes recording of the current session.
219227
/// If false, starts a new session and begins recording.
220-
Future<void> startSessionRecording({bool resumeCurrent = true}) =>
221-
_posthog.startSessionRecording(resumeCurrent: resumeCurrent);
228+
Future<void> startSessionRecording({bool resumeCurrent = true}) async {
229+
await _posthog.startSessionRecording(resumeCurrent: resumeCurrent);
230+
sessionRecordingActive.value = true;
231+
}
222232

223233
/// Stops the current session recording if one is in progress.
224234
///
225235
/// This method will have no effect if PostHog is not enabled.
226-
Future<void> stopSessionRecording() => _posthog.stopSessionRecording();
236+
Future<void> stopSessionRecording() async {
237+
await _posthog.stopSessionRecording();
238+
sessionRecordingActive.value = false;
239+
}
227240

228241
/// Returns whether session replay is currently active.
229242
Future<bool> isSessionReplayActive() => _posthog.isSessionReplayActive();

lib/src/posthog_widget.dart

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,51 @@ class PostHogWidgetState extends State<PostHogWidget> {
3131
super.initState();
3232

3333
final config = Posthog().config;
34-
if (config == null || !config.sessionReplay) {
34+
if (config == null) {
3535
return;
3636
}
3737

38-
_throttleDuration = config.sessionReplayConfig.throttleDelay;
38+
if (config.sessionReplay) {
39+
_initComponents(config);
40+
_changeDetector?.start();
41+
}
42+
43+
// start listening for session recording toggles
44+
Posthog().sessionRecordingActive.addListener(_onSessionRecordingChanged);
45+
}
3946

47+
void _initComponents(PostHogConfig config) {
48+
_throttleDuration = config.sessionReplayConfig.throttleDelay;
4049
_screenshotCapturer = ScreenshotCapturer(config);
4150
_nativeCommunicator = NativeCommunicator();
42-
4351
_changeDetector = ChangeDetector(_onChangeDetected);
52+
}
53+
54+
void _onSessionRecordingChanged() {
55+
if (Posthog().sessionRecordingActive.value) {
56+
_startRecording();
57+
} else {
58+
_stopRecording();
59+
}
60+
}
61+
62+
void _startRecording() {
63+
final config = Posthog().config;
64+
if (config == null) {
65+
return;
66+
}
67+
68+
if (_changeDetector == null) {
69+
_initComponents(config);
70+
}
71+
4472
_changeDetector?.start();
4573
}
4674

75+
void _stopRecording() {
76+
_changeDetector?.stop();
77+
}
78+
4779
// This works as onRootViewsChangedListeners
4880
void _onChangeDetected() {
4981
if (_isThrottling) {
@@ -97,6 +129,8 @@ class PostHogWidgetState extends State<PostHogWidget> {
97129

98130
@override
99131
void dispose() {
132+
Posthog().sessionRecordingActive.removeListener(_onSessionRecordingChanged);
133+
100134
_throttleTimer?.cancel();
101135
_throttleTimer = null;
102136
_changeDetector?.stop();

lib/src/replay/change_detector.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'package:flutter/widgets.dart';
2-
import 'package:posthog_flutter/posthog_flutter.dart';
32

43
/// A class that detects changes in the UI and executes a callback when changes occur.
54
///
@@ -51,11 +50,6 @@ class ChangeDetector {
5150
/// Executes the [onChange] callback and schedules itself for the next frame
5251
/// if the change detector is still running.
5352
void _onFrameRendered() {
54-
final config = Posthog().config;
55-
if (config == null || !config.sessionReplay) {
56-
return;
57-
}
58-
5953
if (!_isRunning) {
6054
return;
6155
}

0 commit comments

Comments
 (0)