Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Dec 4, 2024
1 parent 4969b1a commit 05b4b35
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
33 changes: 1 addition & 32 deletions lib/src/posthog_widget_widget.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import 'dart:async';
import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:posthog_flutter/posthog_flutter.dart';
import 'package:posthog_flutter/src/replay/mask/posthog_mask_controller.dart';
import 'package:posthog_flutter/src/replay/vendor/equality.dart';
import 'package:posthog_flutter/src/util/logging.dart';

import 'replay/change_detector.dart';
Expand All @@ -27,7 +24,6 @@ class PostHogWidgetState extends State<PostHogWidget> {
NativeCommunicator? _nativeCommunicator;

Timer? _debounceTimer;
Uint8List? _lastSnapshot;
Duration _debounceDuration = const Duration(milliseconds: 1000);

@override
Expand Down Expand Up @@ -77,33 +73,7 @@ class PostHogWidgetState extends State<PostHogWidget> {
screen: Posthog().currentScreen);
}

// using png because its compressed, the native SDKs will decompress it
// and transform to jpeg if needed (soon webp)
// https://github.com/brendan-duncan/image does not have webp encoding
final ByteData? byteData =
await imageInfo.image.toByteData(format: ui.ImageByteFormat.png);
if (byteData == null) {
printIfDebug('Error: Failed to convert image to byte data.');
imageInfo.image.dispose();
return;
}

Uint8List pngBytes = byteData.buffer.asUint8List();
imageInfo.image.dispose();

if (pngBytes.isEmpty) {
printIfDebug('Error: Failed to convert image byte data to Uint8List.');
return;
}

if (const PHListEquality().equals(pngBytes, _lastSnapshot)) {
printIfDebug('Error: Snapshot is the same as the last one.');
return;
}

_lastSnapshot = pngBytes;

await _nativeCommunicator?.sendFullSnapshot(pngBytes,
await _nativeCommunicator?.sendFullSnapshot(imageInfo.imageBytes,
id: imageInfo.id, x: imageInfo.x, y: imageInfo.y);
}

Expand All @@ -127,7 +97,6 @@ class PostHogWidgetState extends State<PostHogWidget> {
_changeDetector = null;
_screenshotCapturer = null;
_nativeCommunicator = null;
_lastSnapshot = null;

super.dispose();
}
Expand Down
40 changes: 36 additions & 4 deletions lib/src/replay/screenshot/screenshot_capturer.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'dart:math';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/rendering.dart';
import 'package:posthog_flutter/posthog_flutter.dart';
import 'package:posthog_flutter/src/replay/mask/image_mask_painter.dart';
import 'package:posthog_flutter/src/replay/mask/posthog_mask_controller.dart';
import 'package:posthog_flutter/src/replay/vendor/equality.dart';
import 'package:posthog_flutter/src/util/logging.dart';

class ImageInfo {
Expand All @@ -14,13 +16,15 @@ class ImageInfo {
final int width;
final int height;
final bool shouldSendMetaEvent;
Uint8List imageBytes;

ImageInfo(this.image, this.id, this.x, this.y, this.width, this.height,
this.shouldSendMetaEvent);
this.shouldSendMetaEvent, this.imageBytes);
}

class ViewTreeSnapshotStatus {
bool sentMetaEvent = false;
Uint8List? imageBytes;
ViewTreeSnapshotStatus(this.sentMetaEvent);
}

Expand Down Expand Up @@ -48,8 +52,8 @@ class ScreenshotCapturer {
ViewTreeSnapshotStatus statusView) {
if (shouldSendMetaEvent) {
statusView.sentMetaEvent = true;
_views[renderObject] = statusView;
}
_views[renderObject] = statusView;
}

Future<ImageInfo?> captureScreenshot() async {
Expand Down Expand Up @@ -83,6 +87,32 @@ class ScreenshotCapturer {

final replayConfig = _config.sessionReplayConfig;

// using png because its compressed, the native SDKs will decompress it
// and transform to jpeg if needed (soon webp)
// https://github.com/brendan-duncan/image does not have webp encoding
final ByteData? byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
if (byteData == null) {
printIfDebug('Error: Failed to convert image to byte data.');
image.dispose();
return null;
}

Uint8List pngBytes = byteData.buffer.asUint8List();
image.dispose();

if (pngBytes.isEmpty) {
printIfDebug('Error: Failed to convert image byte data to Uint8List.');
return null;
}

if (const PHListEquality().equals(pngBytes, statusView.imageBytes)) {
printIfDebug('Snapshot is the same as the last one.');
return null;
}

statusView.imageBytes = pngBytes;

if (replayConfig.maskAllTexts || replayConfig.maskAllImages) {
final screenElementsRects =
await PostHogMaskController.instance.getCurrentScreenRects();
Expand All @@ -97,7 +127,8 @@ class ScreenshotCapturer {
globalPosition.dy.toInt(),
srcWidth.toInt(),
srcHeight.toInt(),
shouldSendMetaEvent);
shouldSendMetaEvent,
pngBytes);
_updateStatusView(shouldSendMetaEvent, renderObject, statusView);
return imageInfo;
}
Expand All @@ -110,7 +141,8 @@ class ScreenshotCapturer {
globalPosition.dy.toInt(),
srcWidth.toInt(),
srcHeight.toInt(),
shouldSendMetaEvent);
shouldSendMetaEvent,
pngBytes);
_updateStatusView(shouldSendMetaEvent, renderObject, statusView);
return imageInfo;
} catch (e) {
Expand Down

0 comments on commit 05b4b35

Please sign in to comment.