Skip to content

Commit 4cc25f0

Browse files
authored
[Mob 3193] - [Mob 3195] (#57)
* Android API Mappings * IOS API Mapping * Flutter API Mapping * Adds tests for new API * updates Readme * updated Changelog ## Mapped APIs: * setOnDismissCallback * setOnInvokeCallback * BugReporting.setEnabled
1 parent df1ba40 commit 4cc25f0

File tree

7 files changed

+245
-11
lines changed

7 files changed

+245
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Master
22

3+
* Adds setOnDismissCallback API mapping.
4+
* Adds setOnInvokeCallback API mapping.
5+
* Adds BugReporting.setEnabled API mapping.
36
* Adds setWelcomeMessageMode API mapping.
47
* Adds addFileAttachmentWithURL, addFileAttachmentWithData, clearFileAttachments API mapping.
58
* Adds setUserData API mapping.

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ The section below contains the APIs we're planning to implement for our 1.0 rele
5555

5656
| API Method | Native Equivalent (Android/iOS) |
5757
|---------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------|
58-
| `invokeWithMode(InvocationMode invocationMode, [List<InvocationOption> invocationOptions])` | `invoke(InvocationMode mode, @InvocationOption int... options)`<br>`+ invokeWithMode:options:` |
59-
| | `setState(Feature.State state)`<br>`enabled` |
60-
| | `setOnInvokeCallback(OnInvokeCallback onInvokeCallback)`<br>`willInvokeHandler` |
61-
| | `setOnDismissCallback(OnSdkDismissCallback onSdkDismissedCallback)`<br>`didDismissHandler` |
62-
| | `setInvocationEvents(InstabugInvocationEvent... invocationEvents)`<br>`invocationEvents` |
63-
| | `setAttachmentTypesEnabled(boolean initial, boolean extra, boolean gallery, boolean recording)`<br>`enabledAttachmentTypes` |
64-
| | `setReportTypes(@BugReporting.ReportType int... types)`<br>`promptOptionsEnabledReportTypes` |
65-
| | `setExtendedBugReportState(ExtendedBugReport.State state)`<br>`extendedBugReportMode` |
58+
| `invokeWithMode(InvocationMode invocationMode, [List<InvocationOption> invocationOptions])` | `invoke(InvocationMode mode, @InvocationOption int... options)`<br>`+invokeWithMode:options:`|
59+
| `setEnabled(bool isEnabled)` | `setState(Feature.State state)`<br>`enabled` |
60+
| `setOnInvokeCallback(Function function)` | `setOnInvokeCallback(OnInvokeCallback onInvokeCallback)`<br>`willInvokeHandler` |
61+
| `setOnDismissCallback(Function function)` | `setOnDismissCallback(OnSdkDismissCallback onSdkDismissedCallback)`<br>`didDismissHandler` |
62+
| | `setInvocationEvents(InstabugInvocationEvent... invocationEvents)`<br>`invocationEvents` |
63+
| | `setAttachmentTypesEnabled(boolean initial, boolean extra, boolean gallery, boolean recording`<br>`enabledAttachmentTypes` |
64+
| | `setReportTypes(@BugReporting.ReportType int... types)`<br>`promptOptionsEnabledReportTypes` |
65+
| | `setExtendedBugReportState(ExtendedBugReport.State state)`<br>`extendedBugReportMode` |
6666
| | `setOptions(@Option int... options)`<br>`bugReportingOptions`
6767
| | `show(@BugReporting.ReportType int type)`<br>`+ showWithReportType:options:`
6868

android/src/main/java/com/instabug/instabugflutter/InstabugFlutterPlugin.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import com.instabug.library.Instabug;
1515
import com.instabug.library.InstabugColorTheme;
1616
import com.instabug.library.InstabugCustomTextPlaceHolder;
17+
import com.instabug.library.OnSdkDismissCallback;
1718
import com.instabug.library.invocation.InstabugInvocationEvent;
19+
import com.instabug.library.invocation.OnInvokeCallback;
1820
import com.instabug.library.logging.InstabugLog;
1921
import com.instabug.library.ui.onboarding.WelcomeMessage;
2022

@@ -47,11 +49,12 @@ public class InstabugFlutterPlugin implements MethodCallHandler {
4749

4850
private InstabugCustomTextPlaceHolder placeHolder = new InstabugCustomTextPlaceHolder();
4951

52+
static MethodChannel channel;
5053
/**
5154
* Plugin registration.
5255
*/
5356
public static void registerWith(Registrar registrar) {
54-
final MethodChannel channel = new MethodChannel(registrar.messenger(), "instabug_flutter");
57+
channel = new MethodChannel(registrar.messenger(), "instabug_flutter");
5558
channel.setMethodCallHandler(new InstabugFlutterPlugin());
5659
}
5760

@@ -472,4 +475,51 @@ public void setWelcomeMessageMode(String welcomeMessageMode) {
472475
Instabug.setWelcomeMessageState(resolvedWelcomeMessageMode);
473476
}
474477

478+
/**
479+
* Enables and disables manual invocation and prompt options for bug and feedback.
480+
* @param {boolean} isEnabled
481+
*/
482+
public void setBugReportingEnabled(final boolean isEnabled) {
483+
new Handler(Looper.getMainLooper()).post(new Runnable() {
484+
@Override
485+
public void run() {
486+
if (isEnabled) {
487+
BugReporting.setState(Feature.State.ENABLED);
488+
} else {
489+
BugReporting.setState(Feature.State.DISABLED);
490+
}
491+
}
492+
});
493+
}
494+
495+
/**
496+
* Sets a block of code to be executed just before the SDK's UI is presented.
497+
* This block is executed on the UI thread. Could be used for performing any
498+
* UI changes before the SDK's UI is shown.
499+
*/
500+
public void setOnInvokeCallback() {
501+
BugReporting.setOnInvokeCallback(new OnInvokeCallback() {
502+
@Override
503+
public void onInvoke() {
504+
channel.invokeMethod("onInvokeCallback", "a");
505+
}
506+
});
507+
}
508+
509+
/**
510+
* Sets a block of code to be executed right after the SDK's UI is dismissed.
511+
* This block is executed on the UI thread. Could be used for performing any
512+
* UI changes after the SDK's UI is dismissed.
513+
*/
514+
public void setOnDismissCallback() {
515+
BugReporting.setOnDismissCallback(new OnSdkDismissCallback() {
516+
@Override
517+
public void call(DismissType dismissType, ReportType reportType) {
518+
HashMap<String, String> params = new HashMap<>();
519+
params.put("dismissType", dismissType.toString());
520+
params.put("reportType", reportType.toString());
521+
channel.invokeMethod("onDismissCallback", params);
522+
}
523+
});
524+
}
475525
}

example/lib/main.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class _MyAppState extends State<MyApp> {
6262
Instabug.addFileAttachmentWithData(list, "My File");
6363
Instabug.clearFileAttachments();
6464
//Instabug.clearFileAttachments();
65+
//BugReporting.setEnabled(false);
66+
BugReporting.setOnInvokeCallback(sdkInvoked);
67+
BugReporting.setOnDismissCallback(sdkDismissed);
6568
} on PlatformException {
6669
platformVersion = 'Failed to get platform version.';
6770
}
@@ -79,6 +82,15 @@ class _MyAppState extends State<MyApp> {
7982
Instabug.resetTags();
8083
}
8184

85+
void sdkInvoked() {
86+
debugPrint("I am called before invocation");
87+
}
88+
89+
void sdkDismissed(DismissType dismissType, ReportType reportType) {
90+
debugPrint('SDK Dismissed DismissType: ' + dismissType.toString());
91+
debugPrint('SDK Dismissed ReportType: ' + reportType.toString());
92+
}
93+
8294
void show() {
8395
Instabug.show();
8496
}

ios/Classes/InstabugFlutterPlugin.m

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
@implementation InstabugFlutterPlugin
77

88

9+
FlutterMethodChannel* channel;
910
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
10-
FlutterMethodChannel* channel = [FlutterMethodChannel
11+
channel = [FlutterMethodChannel
1112
methodChannelWithName:@"instabug_flutter"
1213
binaryMessenger:[registrar messenger]];
1314
InstabugFlutterPlugin* instance = [[InstabugFlutterPlugin alloc] init];
@@ -359,6 +360,57 @@ + (void)setWelcomeMessageMode:(NSString *)welcomeMessageMode {
359360
[Instabug setWelcomeMessageMode:welcomeMode];
360361
}
361362

363+
/**
364+
* Enables and disables manual invocation and prompt options for bug and feedback.
365+
* @param {boolean} isEnabled
366+
*/
367+
+ (void)setBugReportingEnabled:(NSNumber *)isEnabled {
368+
BOOL boolValue = [isEnabled boolValue];
369+
IBGBugReporting.enabled = boolValue;
370+
}
371+
372+
/**
373+
* Sets a block of code to be executed just before the SDK's UI is presented.
374+
* This block is executed on the UI thread. Could be used for performing any
375+
* UI changes before the SDK's UI is shown.
376+
*/
377+
+ (void)setOnInvokeCallback {
378+
IBGBugReporting.willInvokeHandler = ^{
379+
[channel invokeMethod:@"onInvokeCallback" arguments:nil];
380+
};
381+
}
382+
383+
/**
384+
* Sets a block of code to be executed right after the SDK's UI is dismissed.
385+
* This block is executed on the UI thread. Could be used for performing any
386+
* UI changes after the SDK's UI is dismissed.
387+
*/
388+
+ (void)setOnDismissCallback {
389+
IBGBugReporting.didDismissHandler = ^(IBGDismissType dismissType, IBGReportType reportType) {
390+
//parse dismiss type enum
391+
NSString* dismissTypeString;
392+
if (dismissType == IBGDismissTypeCancel) {
393+
dismissTypeString = @"CANCEL";
394+
} else if (dismissType == IBGDismissTypeSubmit) {
395+
dismissTypeString = @"SUBMIT";
396+
} else if (dismissType == IBGDismissTypeAddAttachment) {
397+
dismissTypeString = @"ADD_ATTACHMENT";
398+
}
399+
//parse report type enum
400+
NSString* reportTypeString;
401+
if (reportType == IBGReportTypeBug) {
402+
reportTypeString = @"bug";
403+
} else if (reportType == IBGReportTypeFeedback) {
404+
reportTypeString = @"feedback";
405+
} else {
406+
reportTypeString = @"other";
407+
}
408+
NSDictionary *result = @{ @"dismissType": dismissTypeString,
409+
@"reportType": reportTypeString};
410+
[channel invokeMethod:@"onDismissCallback" arguments:result];
411+
};
412+
}
413+
362414
+ (NSDictionary *)constants {
363415
return @{
364416
@"InvocationEvent.shake": @(IBGInvocationEventShake),

lib/BugReporting.dart

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'package:flutter/foundation.dart';
23
import 'package:flutter/services.dart';
34
import 'package:instabug_flutter/Instabug.dart';
45

@@ -9,13 +10,71 @@ enum InvocationOption {
910
EMAIL_FIELD_OPTIONAL
1011
}
1112

13+
enum DismissType {
14+
CANCEL,
15+
SUBMIT,
16+
ADD_ATTACHMENT
17+
}
18+
19+
enum ReportType {
20+
BUG,
21+
FEEDBACK,
22+
OTHER
23+
}
24+
1225
class BugReporting {
26+
27+
static Function onInvokeCallback;
28+
static Function onDismissCallback;
1329
static const MethodChannel _channel = MethodChannel('instabug_flutter');
1430

1531
static Future<String> get platformVersion async {
1632
final String version = await _channel.invokeMethod('getPlatformVersion');
1733
return version;
1834
}
35+
36+
static Future<dynamic> _handleMethod(MethodCall call) async {
37+
switch(call.method) {
38+
case 'onInvokeCallback':
39+
onInvokeCallback();
40+
return ;
41+
case 'onDismissCallback':
42+
Map<dynamic, dynamic> map = call.arguments;
43+
DismissType dismissType;
44+
ReportType reportType;
45+
final String dismissTypeString = map['dismissType'].toUpperCase();
46+
switch(dismissTypeString) {
47+
case 'CANCEL':
48+
dismissType = DismissType.CANCEL;
49+
break;
50+
case 'SUBMIT':
51+
dismissType = DismissType.SUBMIT;
52+
break;
53+
case 'ADD_ATTACHMENT':
54+
dismissType = DismissType.ADD_ATTACHMENT;
55+
break;
56+
}
57+
final String reportTypeString = map['reportType'].toUpperCase();
58+
switch(reportTypeString) {
59+
case 'BUG':
60+
reportType = ReportType.BUG;
61+
break;
62+
case 'FEEDBACK':
63+
reportType = ReportType.FEEDBACK;
64+
break;
65+
case 'OTHER':
66+
reportType = ReportType.OTHER;
67+
break;
68+
}
69+
try {
70+
onDismissCallback(dismissType,reportType);
71+
}
72+
catch(exception) {
73+
onDismissCallback();
74+
}
75+
return ;
76+
}
77+
}
1978
/// invoke sdk manually with desire invocation mode
2079
/// [invocationMode] the invocation mode
2180
/// [invocationOptions] the array of invocation options
@@ -29,4 +88,31 @@ class BugReporting {
2988
final List<dynamic> params = <dynamic>[invocationMode.toString(), invocationOptionsStrings];
3089
await _channel.invokeMethod<Object>('invokeWithMode:options:',params);
3190
}
32-
}
91+
92+
///Enables and disables manual invocation and prompt options for bug and feedback.
93+
/// [boolean] isEnabled
94+
static void setEnabled(bool isEnabled) async {
95+
final List<dynamic> params = <dynamic>[isEnabled];
96+
await _channel.invokeMethod<Object>('setBugReportingEnabled:', params);
97+
}
98+
99+
/// Sets a block of code to be executed just before the SDK's UI is presented.
100+
/// This block is executed on the UI thread. Could be used for performing any
101+
/// UI changes before the SDK's UI is shown.
102+
/// [function] A callback that gets executed before invoking the SDK
103+
static void setOnInvokeCallback(Function function) async {
104+
_channel.setMethodCallHandler(_handleMethod);
105+
onInvokeCallback = function;
106+
await _channel.invokeMethod<Object>('setOnInvokeCallback');
107+
}
108+
109+
/// Sets a block of code to be executed just before the SDK's UI is presented.
110+
/// This block is executed on the UI thread. Could be used for performing any
111+
/// UI changes before the SDK's UI is shown.
112+
/// [function] A callback that gets executed before invoking the SDK
113+
static void setOnDismissCallback(Function function) async {
114+
_channel.setMethodCallHandler(_handleMethod);
115+
onDismissCallback = function;
116+
await _channel.invokeMethod<Object>('setOnDismissCallback');
117+
}
118+
}

test/instabug_flutter_test.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,37 @@ test('startWithToken:invocationEvents: Test', () async {
361361
]);
362362
});
363363

364+
test('setBugReportingEnabled: Test', () async {
365+
bool isEnabled = false;
366+
final List<dynamic> args = <dynamic>[isEnabled];
367+
BugReporting.setEnabled(isEnabled);
368+
expect(log, <Matcher>[
369+
isMethodCall('setBugReportingEnabled:',
370+
arguments: args,
371+
)
372+
]);
373+
});
374+
375+
test('setOnInvokeCallback Test', () async {
376+
BugReporting.setOnInvokeCallback(()=> (){});
377+
expect(log, <Matcher>[
378+
isMethodCall('setOnInvokeCallback',
379+
arguments: null,
380+
)
381+
]);
382+
});
383+
384+
test('setOnDismissCallback Test', () async {
385+
BugReporting.setOnDismissCallback(()=> (){});
386+
expect(log, <Matcher>[
387+
isMethodCall('setOnDismissCallback',
388+
arguments: null,
389+
)
390+
]);
391+
});
392+
393+
394+
364395
}
365396

366397

0 commit comments

Comments
 (0)