Skip to content

Commit 08e9c89

Browse files
committed
fix: update network body max size handling and caching
- Changed the return type of `getNetworkBodyMaxSize` from `int?` to `double?` in the API and updated related method calls accordingly. - Renamed `registerW3CFlagsListener` to `registerFeatureFlagsListener` for consistency across the codebase. - Introduced a callback mechanism in `FeatureFlagsManager` to handle changes in network body max size, ensuring proper cache management in `NetworkManager`. - Updated tests to reflect the new method names and ensure functionality remains intact.
1 parent 5b06977 commit 08e9c89

File tree

6 files changed

+45
-9
lines changed

6 files changed

+45
-9
lines changed

android/src/main/java/com/instabug/flutter/modules/InstabugApi.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,13 +516,13 @@ public void setNetworkLogBodyEnabled(@NonNull Boolean isEnabled) {
516516
}
517517

518518
@Override
519-
public void getNetworkBodyMaxSize(@NonNull InstabugPigeon.Result<Long> result) {
519+
public void getNetworkBodyMaxSize(@NonNull InstabugPigeon.Result<Double> result) {
520520
ThreadManager.runOnMainThread(
521521
new Runnable() {
522522
@Override
523523
public void run() {
524524
try {
525-
long networkCharLimit = InternalCore.INSTANCE.get_networkLogCharLimit();
525+
double networkCharLimit = InternalCore.INSTANCE.get_networkLogCharLimit();
526526
result.success(networkCharLimit);
527527
} catch (Exception e) {
528528
e.printStackTrace();

lib/src/modules/instabug.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class Instabug {
191191
invocationEvents.mapToString(),
192192
debugLogsLevel.toString(),
193193
);
194-
return FeatureFlagsManager().registerW3CFlagsListener();
194+
return FeatureFlagsManager().registerFeatureFlagsListener();
195195
}
196196

197197
/// Sets a [callback] to be called wehenever a screen name is captured to mask

lib/src/utils/feature_flags_manager.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ typedef OnW3CFeatureFlagChange = void Function(
99
bool isW3cCaughtHeaderEnabled,
1010
);
1111

12+
typedef OnNetworkBodyMaxSizeChangeCallback = void Function();
13+
1214
class FeatureFlagsManager implements FeatureFlagsFlutterApi {
1315
// Access the singleton instance
1416
factory FeatureFlagsManager() {
@@ -23,6 +25,10 @@ class FeatureFlagsManager implements FeatureFlagsFlutterApi {
2325
// Host API instance
2426
static InstabugHostApi _host = InstabugHostApi();
2527

28+
// Callback for network body max size changes
29+
static OnNetworkBodyMaxSizeChangeCallback?
30+
_onNetworkBodyMaxSizeChangeCallback;
31+
2632
/// @nodoc
2733
@visibleForTesting
2834
// Setter for the host API
@@ -38,6 +44,13 @@ class FeatureFlagsManager implements FeatureFlagsFlutterApi {
3844
// since it breaks the singleton pattern
3945
}
4046

47+
/// Sets the callback for network body max size changes
48+
// ignore: avoid_setters_without_getters
49+
set onNetworkBodyMaxSizeChangeCallback(
50+
OnNetworkBodyMaxSizeChangeCallback callback) {
51+
_onNetworkBodyMaxSizeChangeCallback = callback;
52+
}
53+
4154
// Internal state flags
4255
bool _isAndroidW3CExternalTraceID = false;
4356
bool _isAndroidW3CExternalGeneratedHeader = false;
@@ -67,9 +80,10 @@ class FeatureFlagsManager implements FeatureFlagsFlutterApi {
6780
);
6881
}
6982

70-
Future<void> registerW3CFlagsListener() async {
83+
Future<void> registerFeatureFlagsListener() async {
7184
FeatureFlagsFlutterApi.setup(this); // Use 'this' instead of _instance
7285

86+
// W3C Feature Flags
7387
final featureFlags = await _host.isW3CFeatureFlagsEnabled();
7488
_isAndroidW3CCaughtHeader =
7589
featureFlags['isW3cCaughtHeaderEnabled'] ?? false;
@@ -78,6 +92,10 @@ class FeatureFlagsManager implements FeatureFlagsFlutterApi {
7892
_isAndroidW3CExternalGeneratedHeader =
7993
featureFlags['isW3cExternalGeneratedHeaderEnabled'] ?? false;
8094

95+
// Network Body Max Size
96+
final networkBodyMaxSize = await _host.getNetworkBodyMaxSize();
97+
_networkBodyMaxSize = networkBodyMaxSize?.toInt() ?? 0;
98+
8199
return _host.registerFeatureFlagChangeListener();
82100
}
83101

@@ -96,5 +114,6 @@ class FeatureFlagsManager implements FeatureFlagsFlutterApi {
96114
@override
97115
void onNetworkLogBodyMaxSizeChange(int networkBodyMaxSize) {
98116
_networkBodyMaxSize = networkBodyMaxSize;
117+
_onNetworkBodyMaxSizeChangeCallback?.call();
99118
}
100119
}

lib/src/utils/network_manager.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ class NetworkManager {
1818
int _defaultNetworkBodyMaxSize = 10240; // in bytes
1919
final _host = InstabugHostApi();
2020

21+
NetworkManager() {
22+
// Register for network body max size changes
23+
FeatureFlagsManager().onNetworkBodyMaxSizeChangeCallback =
24+
() {
25+
clearNetworkBodyMaxSizeCache();
26+
};
27+
}
28+
2129
// ignore: use_setters_to_change_properties
2230
void setObfuscateLogCallback(ObfuscateLogCallback callback) {
2331
_obfuscateLogCallback = callback;
@@ -109,20 +117,23 @@ class NetworkManager {
109117
/// Gets the network body max size from native SDK, with caching
110118
Future<int?> _getNetworkBodyMaxSize() async {
111119
if (_cachedNetworkBodyMaxSize != null) {
120+
print('[Kamal] _cachedNetworkBodyMaxSize: $_cachedNetworkBodyMaxSize');
112121
return _cachedNetworkBodyMaxSize;
113122
}
114123

115124
final ffmNetworkBodyLimit = FeatureFlagsManager().networkBodyMaxSize;
116125

117126
if (ffmNetworkBodyLimit > 0) {
118127
_cachedNetworkBodyMaxSize = ffmNetworkBodyLimit;
128+
print('[Kamal] ffmNetworkBodyLimit: $ffmNetworkBodyLimit');
119129
return ffmNetworkBodyLimit;
120130
}
121131

122132
try {
123133
final limit = await _host.getNetworkBodyMaxSize();
124-
_cachedNetworkBodyMaxSize = limit;
125-
return limit;
134+
_cachedNetworkBodyMaxSize = limit?.toInt();
135+
print('[Kamal] limit: $limit');
136+
return limit?.toInt();
126137
} catch (error) {
127138
InstabugLogger.I.e(
128139
'Failed to get network body max size from native API: $error'
@@ -131,7 +142,13 @@ class NetworkManager {
131142
tag: InstabugConstants.networkManagerTag,
132143
);
133144
_cachedNetworkBodyMaxSize = _defaultNetworkBodyMaxSize;
145+
print('[Kamal] _defaultNetworkBodyMaxSize: $_defaultNetworkBodyMaxSize');
134146
return _defaultNetworkBodyMaxSize;
135147
}
136148
}
149+
150+
/// Clears the cached network body max size
151+
void clearNetworkBodyMaxSizeCache() {
152+
_cachedNetworkBodyMaxSize = null;
153+
}
137154
}

pigeons/instabug.api.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,5 @@ abstract class InstabugHostApi {
8080
void setNetworkLogBodyEnabled(bool isEnabled);
8181

8282
@async
83-
int? getNetworkBodyMaxSize();
83+
double? getNetworkBodyMaxSize();
8484
}

test/feature_flags_manager_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void main() {
5454
"isW3cCaughtHeaderEnabled": true,
5555
}),
5656
);
57-
await FeatureFlagsManager().registerW3CFlagsListener();
57+
await FeatureFlagsManager().registerFeatureFlagsListener();
5858

5959
final isW3CExternalTraceID =
6060
await FeatureFlagsManager().getW3CFeatureFlagsHeader();
@@ -75,7 +75,7 @@ void main() {
7575
}),
7676
);
7777

78-
await FeatureFlagsManager().registerW3CFlagsListener();
78+
await FeatureFlagsManager().registerFeatureFlagsListener();
7979

8080
verify(
8181
mInstabugHost.registerFeatureFlagChangeListener(),

0 commit comments

Comments
 (0)