Skip to content

Commit cb85eda

Browse files
feat: support app variant (#585)
* feat: app variant * feat: app variant * feat: app variant * feat: app variant
1 parent dd69dcc commit cb85eda

File tree

10 files changed

+119
-23
lines changed

10 files changed

+119
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Added
66

7+
- Add support for App variant. ([#585](https://github.com/Instabug/Instabug-Flutter/pull/585))
8+
79
- Add support for xCode 16. ([#574](https://github.com/Instabug/Instabug-Flutter/pull/574))
810

911
- Add support for BugReporting user consents. ([#573](https://github.com/Instabug/Instabug-Flutter/pull/573))

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import android.graphics.BitmapFactory;
77
import android.net.Uri;
88
import android.util.Log;
9+
910
import androidx.annotation.NonNull;
1011
import androidx.annotation.Nullable;
1112
import androidx.annotation.VisibleForTesting;
13+
1214
import com.instabug.flutter.generated.InstabugPigeon;
1315
import com.instabug.flutter.util.ArgsRegistry;
1416
import com.instabug.flutter.util.Reflection;
@@ -30,9 +32,11 @@
3032
import com.instabug.library.invocation.InstabugInvocationEvent;
3133
import com.instabug.library.model.NetworkLog;
3234
import com.instabug.library.ui.onboarding.WelcomeMessage;
35+
3336
import io.flutter.FlutterInjector;
3437
import io.flutter.embedding.engine.loader.FlutterLoader;
3538
import io.flutter.plugin.common.BinaryMessenger;
39+
3640
import org.jetbrains.annotations.NotNull;
3741
import org.json.JSONObject;
3842

@@ -102,10 +106,12 @@ public Boolean isEnabled() {
102106

103107
@NotNull
104108
@Override
105-
public Boolean isBuilt() { return Instabug.isBuilt(); }
109+
public Boolean isBuilt() {
110+
return Instabug.isBuilt();
111+
}
106112

107113
@Override
108-
public void init(@NonNull String token, @NonNull List<String> invocationEvents, @NonNull String debugLogsLevel) {
114+
public void init(@NonNull String token, @NonNull List<String> invocationEvents, @NonNull String debugLogsLevel, @Nullable String appVariant) {
109115
setCurrentPlatform();
110116

111117
InstabugInvocationEvent[] invocationEventsArray = new InstabugInvocationEvent[invocationEvents.size()];
@@ -116,11 +122,14 @@ public void init(@NonNull String token, @NonNull List<String> invocationEvents,
116122

117123
final Application application = (Application) context;
118124
final int parsedLogLevel = ArgsRegistry.sdkLogLevels.get(debugLogsLevel);
119-
120-
new Instabug.Builder(application, token)
125+
Instabug.Builder builder = new Instabug.Builder(application, token)
121126
.setInvocationEvents(invocationEventsArray)
122-
.setSdkDebugLogsLevel(parsedLogLevel)
123-
.build();
127+
.setSdkDebugLogsLevel(parsedLogLevel);
128+
if (appVariant != null) {
129+
builder.setAppVariant(appVariant);
130+
}
131+
132+
builder.build();
124133

125134
Instabug.setScreenshotProvider(screenshotProvider);
126135
}
@@ -146,6 +155,17 @@ public void setUserData(@NonNull String data) {
146155
Instabug.setUserData(data);
147156
}
148157

158+
@Override
159+
public void setAppVariant(@NonNull String appVariant) {
160+
try {
161+
Instabug.setAppVariant(appVariant);
162+
163+
} catch (Exception e) {
164+
e.printStackTrace();
165+
}
166+
167+
}
168+
149169
@Override
150170
public void logUserEvent(@NonNull String name) {
151171
Instabug.logUserEvent(name);
@@ -500,13 +520,13 @@ public void willRedirectToStore() {
500520
Instabug.willRedirectToStore();
501521
}
502522

503-
523+
504524
@Override
505525
public void setNetworkLogBodyEnabled(@NonNull Boolean isEnabled) {
506-
try {
507-
Instabug.setNetworkLogBodyEnabled(isEnabled);
508-
} catch (Exception e) {
509-
e.printStackTrace();
510-
}
526+
try {
527+
Instabug.setNetworkLogBodyEnabled(isEnabled);
528+
} catch (Exception e) {
529+
e.printStackTrace();
530+
}
511531
}
512532
}

android/src/test/java/com/instabug/flutter/InstabugApiTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ public void testSetCurrentPlatform() {
132132
@Test
133133
public void testSdkInit() {
134134
String token = "app-token";
135+
String appVariant = "app-variant";
136+
135137
List<String> invocationEvents = Collections.singletonList("InvocationEvent.floatingButton");
136138
String logLevel = "LogLevel.error";
137139

@@ -143,7 +145,7 @@ public void testSdkInit() {
143145
when(mock.setSdkDebugLogsLevel(anyInt())).thenReturn(mock);
144146
});
145147

146-
api.init(token, invocationEvents, logLevel);
148+
api.init(token, invocationEvents, logLevel,appVariant);
147149

148150
Instabug.Builder builder = mInstabugBuilder.constructed().get(0);
149151

@@ -155,6 +157,8 @@ public void testSdkInit() {
155157
);
156158
verify(builder).setInvocationEvents(InstabugInvocationEvent.FLOATING_BUTTON);
157159
verify(builder).setSdkDebugLogsLevel(LogLevel.ERROR);
160+
verify(builder).setAppVariant(appVariant);
161+
158162
verify(builder).build();
159163

160164
// Sets screenshot provider
@@ -652,6 +656,14 @@ public void testSetNetworkLogBodyEnabled() {
652656
mInstabug.verify(() -> Instabug.setNetworkLogBodyEnabled(true));
653657
}
654658

659+
@Test
660+
public void testSetAppVariant() {
661+
String appVariant = "app-variant";
662+
api.setAppVariant(appVariant);
663+
664+
mInstabug.verify(() -> Instabug.setAppVariant(appVariant));
665+
}
666+
655667
@Test
656668
public void testSetNetworkLogBodyDisabled() {
657669
api.setNetworkLogBodyEnabled(false);

example/ios/InstabugTests/InstabugApiTests.m

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,22 @@ - (void)testSetEnabled {
3838

3939
- (void)testInit {
4040
NSString *token = @"app-token";
41+
NSString *appVariant = @"app-variant";
42+
4143
NSArray<NSString *> *invocationEvents = @[@"InvocationEvent.floatingButton", @"InvocationEvent.screenshot"];
4244
NSString *logLevel = @"LogLevel.error";
4345
FlutterError *error;
4446

45-
[self.api initToken:token invocationEvents:invocationEvents debugLogsLevel:logLevel error:&error];
47+
[self.api initToken:token invocationEvents:invocationEvents debugLogsLevel:logLevel appVariant:appVariant error:&error];
4648

4749
OCMVerify([self.mInstabug setCurrentPlatform:IBGPlatformFlutter]);
50+
4851
OCMVerify([self.mInstabug setSdkDebugLogsLevel:IBGSDKDebugLogsLevelError]);
52+
4953
OCMVerify([self.mInstabug startWithToken:token invocationEvents:(IBGInvocationEventFloatingButton | IBGInvocationEventScreenshot)]);
54+
55+
XCTAssertEqual(Instabug.appVariant, appVariant);
56+
5057
}
5158

5259
- (void)testShow {

example/ios/Podfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ target 'Runner' do
3030

3131
use_frameworks!
3232
use_modular_headers!
33-
3433
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
3534
end
3635

example/lib/main.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ void main() {
4848
WidgetsFlutterBinding.ensureInitialized();
4949

5050
Instabug.init(
51-
token: 'ed6f659591566da19b67857e1b9d40ab',
52-
invocationEvents: [InvocationEvent.floatingButton],
53-
debugLogsLevel: LogLevel.verbose,
54-
);
51+
token: 'ed6f659591566da19b67857e1b9d40ab',
52+
invocationEvents: [InvocationEvent.floatingButton],
53+
debugLogsLevel: LogLevel.verbose,
54+
appVariant: 'variant 1');
5555

5656
FlutterError.onError = (FlutterErrorDetails details) {
5757
Zone.current.handleUncaughtError(details.exception, details.stack!);

ios/Classes/Modules/InstabugApi.m

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ - (nullable NSNumber *)isEnabledWithError:(FlutterError * _Nullable __autoreleas
2929
return @(Instabug.enabled);
3030
}
3131

32-
- (void)initToken:(NSString *)token invocationEvents:(NSArray<NSString *> *)invocationEvents debugLogsLevel:(NSString *)debugLogsLevel error:(FlutterError *_Nullable *_Nonnull)error {
32+
- (void)initToken:(nonnull NSString *)token invocationEvents:(nonnull NSArray<NSString *> *)invocationEvents debugLogsLevel:(nonnull NSString *)debugLogsLevel appVariant:(nullable NSString *)appVariant error:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
33+
34+
if(appVariant != nil){
35+
Instabug.appVariant = appVariant;
36+
}
37+
38+
3339
SEL setPrivateApiSEL = NSSelectorFromString(@"setCurrentPlatform:");
3440
if ([[Instabug class] respondsToSelector:setPrivateApiSEL]) {
3541
NSInteger *platformID = IBGPlatformFlutter;
@@ -45,7 +51,8 @@ - (void)initToken:(NSString *)token invocationEvents:(NSArray<NSString *> *)invo
4551
[IBGNetworkLogger disableAutomaticCapturingOfNetworkLogs];
4652

4753
IBGInvocationEvent resolvedEvents = 0;
48-
54+
55+
4956
for (NSString *event in invocationEvents) {
5057
resolvedEvents |= (ArgsRegistry.invocationEvents[event]).integerValue;
5158
}
@@ -396,4 +403,12 @@ - (void)setNetworkLogBodyEnabledIsEnabled:(NSNumber *)isEnabled
396403
IBGNetworkLogger.logBodyEnabled = [isEnabled boolValue];
397404
}
398405

406+
407+
- (void)setAppVariantAppVariant:(nonnull NSString *)appVariant error:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
408+
409+
Instabug.appVariant = appVariant;
410+
411+
}
412+
413+
399414
@end

lib/src/modules/instabug.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'dart:typed_data';
1111
import 'dart:ui';
1212

1313
import 'package:flutter/material.dart';
14+
1415
// to maintain supported versions prior to Flutter 3.3
1516
// ignore: unused_import
1617
import 'package:flutter/services.dart';
@@ -179,17 +180,20 @@ class Instabug {
179180
/// The [token] that identifies the app, you can find it on your dashboard.
180181
/// The [invocationEvents] are the events that invoke the SDK's UI.
181182
/// The [debugLogsLevel] used to debug Instabug's SDK.
183+
/// The [appVariant] used to set current App variant name.
182184
static Future<void> init({
183185
required String token,
184186
required List<InvocationEvent> invocationEvents,
185187
LogLevel debugLogsLevel = LogLevel.error,
188+
String? appVariant,
186189
}) async {
187190
$setup();
188191
InstabugLogger.I.logLevel = debugLogsLevel;
189192
await _host.init(
190193
token,
191194
invocationEvents.mapToString(),
192195
debugLogsLevel.toString(),
196+
appVariant,
193197
);
194198
return FeatureFlagsManager().registerW3CFlagsListener();
195199
}
@@ -482,4 +486,11 @@ class Instabug {
482486
static Future<void> willRedirectToStore() async {
483487
return _host.willRedirectToStore();
484488
}
489+
490+
/// This property sets the `appVariant` string to be included in all network requests.
491+
/// It should be set before calling [init] method.
492+
/// [appVariant] used to set current App variant name
493+
static Future<void> setAppVariant(String appVariant) async {
494+
return _host.setAppVariant(appVariant);
495+
}
485496
}

pigeons/instabug.api.dart

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,65 @@ abstract class FeatureFlagsFlutterApi {
1212
@HostApi()
1313
abstract class InstabugHostApi {
1414
void setEnabled(bool isEnabled);
15+
1516
bool isEnabled();
17+
1618
bool isBuilt();
17-
void init(String token, List<String> invocationEvents, String debugLogsLevel);
19+
20+
void init(
21+
String token,
22+
List<String> invocationEvents,
23+
String debugLogsLevel,
24+
String? appVariant,
25+
);
1826

1927
void show();
28+
2029
void showWelcomeMessageWithMode(String mode);
2130

2231
void identifyUser(String email, String? name, String? userId);
32+
2333
void setUserData(String data);
34+
35+
void setAppVariant(String appVariant);
36+
2437
void logUserEvent(String name);
38+
2539
void logOut();
2640

2741
void setLocale(String locale);
42+
2843
void setColorTheme(String theme);
44+
2945
void setWelcomeMessageMode(String mode);
46+
3047
void setPrimaryColor(int color);
48+
3149
void setSessionProfilerEnabled(bool enabled);
50+
3251
void setValueForStringWithKey(String value, String key);
3352

3453
void appendTags(List<String> tags);
54+
3555
void resetTags();
3656

3757
@async
3858
List<String>? getTags();
3959

4060
void addExperiments(List<String> experiments);
61+
4162
void removeExperiments(List<String> experiments);
63+
4264
void clearAllExperiments();
65+
4366
void addFeatureFlags(Map<String, String> featureFlagsMap);
67+
4468
void removeFeatureFlags(List<String> featureFlags);
69+
4570
void removeAllFeatureFlags();
4671

4772
void setUserAttribute(String value, String key);
73+
4874
void removeUserAttribute(String key);
4975

5076
@async
@@ -58,13 +84,17 @@ abstract class InstabugHostApi {
5884
String? crashMode,
5985
String? sessionReplayMode,
6086
);
87+
6188
void reportScreenChange(String screenName);
6289

6390
void setCustomBrandingImage(String light, String dark);
91+
6492
void setFont(String font);
6593

6694
void addFileAttachmentWithURL(String filePath, String fileName);
95+
6796
void addFileAttachmentWithData(Uint8List data, String fileName);
97+
6898
void clearFileAttachments();
6999

70100
void networkLog(Map<String, Object> data);

test/instabug_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void main() {
8484
);
8585

8686
verify(
87-
mHost.init(token, events.mapToString(), LogLevel.error.toString()),
87+
mHost.init(token, events.mapToString(), LogLevel.error.toString(), null),
8888
).called(1);
8989
});
9090

0 commit comments

Comments
 (0)