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

Add textScale(r) value to Flutter context #1886

Merged
merged 27 commits into from Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a2d3c75
entrich event contexts app with text scale
denrase Feb 20, 2024
7da4423
read textScale from sentry widget event processor
denrase Feb 20, 2024
50bd207
revert change
denrase Feb 20, 2024
1e48b4b
revert change
denrase Feb 20, 2024
0fcf836
update changelog
denrase Feb 20, 2024
48881f2
Merge branch 'main' into feat/text-scale-value
denrase Feb 20, 2024
d3c0f35
Merge branch 'main' into feat/text-scale-value
denrase Mar 5, 2024
f6f14d5
update changelog
denrase Mar 5, 2024
4468fb8
Use scale method instead of deprecated `textScaleFactor`
denrase Mar 5, 2024
f49cced
Merge branch 'main' into feat/text-scale-value
denrase Mar 12, 2024
58a6940
fix changelog
denrase Mar 12, 2024
c3852ee
Merge branch 'main' into feat/text-scale-value
denrase Mar 12, 2024
b898149
fix changelog
denrase Mar 12, 2024
99cc06a
Merge branch 'main' into feat/text-scale-value
denrase Mar 19, 2024
f049f6b
use maybeTextScaleFactorOf method
denrase Mar 19, 2024
09c7edb
fix changelog
denrase Mar 19, 2024
0fd568b
Merge branch 'main' into feat/text-scale-value
denrase Mar 25, 2024
4e1cc5a
fix changelog
denrase Mar 25, 2024
62ccb79
Merge branch 'main' into feat/text-scale-value
denrase Apr 8, 2024
17b4845
use textScaleFactorOf
denrase Apr 8, 2024
9e67403
Merge branch 'feat/text-scale-value' of github.com:getsentry/sentry-d…
denrase Apr 8, 2024
d0d6baa
fix tests
denrase Apr 8, 2024
630bfad
update doc
denrase Apr 8, 2024
21fee1c
Merge branch 'main' into feat/text-scale-value
denrase Apr 9, 2024
ed83106
Merge branch 'main' into feat/text-scale-value
buenaflor Apr 10, 2024
27ede51
Update CHANGELOG.md
buenaflor Apr 10, 2024
4e2ed6b
Merge branch 'main' into feat/text-scale-value
buenaflor Apr 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features

- Add textScale(r) value to Flutter context ([#1886](https://github.com/getsentry/sentry-dart/pull/1886))

## 7.18.0

### Features
Expand Down
9 changes: 9 additions & 0 deletions dart/lib/src/protocol/sentry_app.dart
Expand Up @@ -19,6 +19,7 @@ class SentryApp {
this.appMemory,
this.inForeground,
this.viewNames,
this.textScale,
});

/// Human readable application name, as it appears on the platform.
Expand Down Expand Up @@ -52,6 +53,9 @@ class SentryApp {
/// The names of the currently visible views.
final List<String>? viewNames;

/// The text scale of the [SentryWidget] using fontSize == 1
final double? textScale;

/// Deserializes a [SentryApp] from JSON [Map].
factory SentryApp.fromJson(Map<String, dynamic> data) {
final viewNamesJson = data['view_names'] as List<dynamic>?;
Expand All @@ -68,6 +72,7 @@ class SentryApp {
appMemory: data['app_memory'],
inForeground: data['in_foreground'],
viewNames: viewNamesJson?.map((e) => e as String).toList(),
textScale: data['text_scale'],
);
}

Expand All @@ -84,6 +89,7 @@ class SentryApp {
if (appMemory != null) 'app_memory': appMemory!,
if (inForeground != null) 'in_foreground': inForeground!,
if (viewNames != null && viewNames!.isNotEmpty) 'view_names': viewNames!,
if (textScale != null) 'text_scale': textScale!,
};
}

Expand All @@ -98,6 +104,7 @@ class SentryApp {
appMemory: appMemory,
inForeground: inForeground,
viewNames: viewNames,
textScale: textScale,
);

SentryApp copyWith({
Expand All @@ -111,6 +118,7 @@ class SentryApp {
int? appMemory,
bool? inForeground,
List<String>? viewNames,
double? textScale,
}) =>
SentryApp(
name: name ?? this.name,
Expand All @@ -123,5 +131,6 @@ class SentryApp {
appMemory: appMemory ?? this.appMemory,
inForeground: inForeground ?? this.inForeground,
viewNames: viewNames ?? this.viewNames,
textScale: textScale ?? this.textScale,
);
}
6 changes: 6 additions & 0 deletions dart/test/protocol/sentry_app_test.dart
Expand Up @@ -15,6 +15,7 @@
deviceAppHash: 'fixture-deviceAppHash',
inForeground: true,
viewNames: ['fixture-viewName', 'fixture-viewName2'],
textScale: 'fixture-textScale',

Check failure on line 18 in dart/test/protocol/sentry_app_test.dart

View workflow job for this annotation

GitHub Actions / analyze / analyze

The argument type 'String' can't be assigned to the parameter type 'double?'.

See https://dart.dev/diagnostics/argument_type_not_assignable to learn more about this problem.
);

final sentryAppJson = <String, dynamic>{
Expand All @@ -27,6 +28,7 @@
'device_app_hash': 'fixture-deviceAppHash',
'in_foreground': true,
'view_names': ['fixture-viewName', 'fixture-viewName2'],
'text_scale': 'fixture-textScale',
};

group('json', () {
Expand All @@ -42,6 +44,7 @@
expect(json['device_app_hash'], 'fixture-deviceAppHash');
expect(json['in_foreground'], true);
expect(json['view_names'], ['fixture-viewName', 'fixture-viewName2']);
expect(json['text_scale'], 'fixture-textScale');
});
test('fromJson', () {
final sentryApp = SentryApp.fromJson(sentryAppJson);
Expand All @@ -56,6 +59,7 @@
expect(json['device_app_hash'], 'fixture-deviceAppHash');
expect(json['in_foreground'], true);
expect(json['view_names'], ['fixture-viewName', 'fixture-viewName2']);
expect(json['text_scale'], 'fixture-textScale');
});
});

Expand Down Expand Up @@ -86,6 +90,7 @@
deviceAppHash: 'hash1',
inForeground: true,
viewNames: ['screen1'],
textScale: 'textScale1',

Check failure on line 93 in dart/test/protocol/sentry_app_test.dart

View workflow job for this annotation

GitHub Actions / analyze / analyze

The argument type 'String' can't be assigned to the parameter type 'double?'.

See https://dart.dev/diagnostics/argument_type_not_assignable to learn more about this problem.
);

expect('name1', copy.name);
Expand All @@ -97,6 +102,7 @@
expect('hash1', copy.deviceAppHash);
expect(true, copy.inForeground);
expect(['screen1'], copy.viewNames);
expect('textScale1', copy.textScale);
});
});
}
1 change: 1 addition & 0 deletions flutter/example/lib/main.dart
Expand Up @@ -86,6 +86,7 @@ Future<void> setupSentry(

options.maxRequestBodySize = MaxRequestBodySize.always;
options.maxResponseBodySize = MaxResponseBodySize.always;
options.navigatorKey = navigatorKey;

_isIntegrationTest = isIntegrationTest;
if (_isIntegrationTest) {
Expand Down
34 changes: 34 additions & 0 deletions flutter/lib/src/event_processor/widget_event_processor.dart
@@ -0,0 +1,34 @@
import 'dart:async';

import 'package:flutter/widgets.dart';

import '../../sentry_flutter.dart';

class WidgetEventProcessor implements EventProcessor {
@override
FutureOr<SentryEvent?> apply(SentryEvent event, {Hint? hint}) {
if (event is SentryTransaction) {
return event;
}
if (event.exceptions == null && event.throwable == null) {
return event;
}
final context = sentryWidgetGlobalKey.currentContext;
if (context == null) {
return event;
}

// ignore: deprecated_member_use
final textScale = MediaQuery.maybeTextScaleFactorOf(context);
if (textScale == null) {
return event;
}
return event.copyWith(
contexts: event.contexts.copyWith(
app: event.contexts.app?.copyWith(
textScale: textScale,
),
),
);
}
}
10 changes: 6 additions & 4 deletions flutter/lib/src/sentry_flutter.dart
Expand Up @@ -8,6 +8,7 @@ import '../sentry_flutter.dart';
import 'event_processor/android_platform_exception_event_processor.dart';
import 'event_processor/flutter_exception_event_processor.dart';
import 'event_processor/platform_exception_event_processor.dart';
import 'event_processor/widget_event_processor.dart';
import 'frame_callback_handler.dart';
import 'integrations/connectivity/connectivity_integration.dart';
import 'integrations/screenshot_integration.dart';
Expand Down Expand Up @@ -110,12 +111,13 @@ mixin SentryFlutter {
options.addScopeObserver(NativeScopeObserver(_native!));
}

var flutterEventProcessor = FlutterEnricherEventProcessor(options);
options.addEventProcessor(flutterEventProcessor);
options.addEventProcessor(FlutterEnricherEventProcessor(options));
options.addEventProcessor(WidgetEventProcessor());

if (options.platformChecker.platform.isAndroid) {
options
.addEventProcessor(AndroidPlatformExceptionEventProcessor(options));
options.addEventProcessor(
AndroidPlatformExceptionEventProcessor(options),
);
}

options.addEventProcessor(PlatformExceptionEventProcessor());
Expand Down
10 changes: 9 additions & 1 deletion flutter/lib/src/sentry_widget.dart
@@ -1,6 +1,11 @@
import 'package:flutter/cupertino.dart';
import 'package:meta/meta.dart';
import '../sentry_flutter.dart';

/// Key which is used to identify the [SentryWidget]
@internal
final sentryWidgetGlobalKey = GlobalKey(debugLabel: 'sentry_widget');

/// This widget serves as a wrapper to include Sentry widgets such
/// as [SentryScreenshotWidget] and [SentryUserInteractionWidget].
class SentryWidget extends StatefulWidget {
Expand All @@ -18,6 +23,9 @@ class _SentryWidgetState extends State<SentryWidget> {
Widget content = widget.child;
content = SentryScreenshotWidget(child: content);
content = SentryUserInteractionWidget(child: content);
return content;
return Container(
key: sentryWidgetGlobalKey,
child: content,
);
}
}
44 changes: 44 additions & 0 deletions flutter/test/event_processor/widget_event_processor_test.dart
@@ -0,0 +1,44 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:sentry_flutter/src/event_processor/widget_event_processor.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late Fixture fixture;

setUp(() {
fixture = Fixture();
});

testWidgets('adds screenshot attachment dart:io', (tester) async {
await tester.runAsync(() async {
final sut = fixture.getSut();
await tester.pumpWidget(
SentryWidget(
child: Text(
'Catching Pokémon is a snap!',
textDirection: TextDirection.ltr,
),
),
);

final throwable = Exception();
SentryEvent? event = SentryEvent(throwable: throwable);
event = event.copyWith(
contexts: event.contexts.copyWith(
app: SentryApp(),
),
);
event = await sut.apply(event);

expect(event?.contexts.app?.textScale, 1.0);
});
});
}

class Fixture {
WidgetEventProcessor getSut() {
return WidgetEventProcessor();
}
}