Skip to content

Commit 4c414b0

Browse files
authored
Merge pull request #1 from Instabug/MOB-2891
Mob 2891, MOB-2854
2 parents 74ce3d7 + a2e472a commit 4c414b0

File tree

6 files changed

+140
-33
lines changed

6 files changed

+140
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The table below contains a list of APIs we're planning to implement for our 1.0
2626

2727
| API Method | Native Equivalent (Android/iOS) |
2828
|------------|-----------------------------------------------------------------------------------------------------------------------------------------|
29-
| | `new Instabug.Builder(this, "APP_TOKEN").build()`<br>`+ [Instabug startWithToken:invocationEvents:]` |
29+
| `start(String token, List<InvocationEvent> invocationEvents)` | `new Instabug.Builder(this, "APP_TOKEN").build()`<br>`+ [Instabug startWithToken:invocationEvents:]` |
3030
| | `Instabug.showWelcomeMessage(WelcomeMessage.State state)`<br>`+ [Instabug showWelcomeMessageWithMode:]` |
3131
| | `Instabug.identifyUser(String username, String email)`<br>`+ [Instabug identifyUserWithEmail:name:]` |
3232
| | `Instabug.logoutUser()`<br>`+ [Instabug logOut]` |

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

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,21 @@
1111
import com.instabug.library.Instabug;
1212
import com.instabug.library.invocation.InstabugInvocationEvent;
1313

14+
import java.lang.reflect.Array;
15+
import java.lang.reflect.InvocationTargetException;
16+
import java.lang.reflect.Method;
17+
import java.lang.reflect.Parameter;
1418
import java.util.ArrayList;
1519
import java.util.Arrays;
1620
import java.util.Collections;
21+
import java.util.HashMap;
22+
import java.util.Iterator;
1723
import java.util.List;
24+
import java.util.Map;
25+
26+
import android.os.Handler;
27+
import android.os.Looper;
28+
import android.util.Log;
1829

1930
// import com.instabug.library.InstabugColorTheme;
2031
// import com.instabug.library.InstabugCustomTextPlaceHolder;
@@ -23,8 +34,14 @@
2334

2435
/** InstabugFlutterPlugin */
2536
public class InstabugFlutterPlugin implements MethodCallHandler {
26-
27-
private ArrayList<InstabugInvocationEvent> invocationEvents = new ArrayList<>();
37+
38+
final public static String INVOCATION_EVENT_NONE = "InvocationEvent.none";
39+
final public static String INVOCATION_EVENT_SCREENSHOT = "InvocationEvent.screenshot";
40+
final public static String INVOCATION_EVENT_TWO_FINGER_SWIPE_LEFT = "InvocationEvent.twoFingersSwipeLeft";
41+
final public static String INVOCATION_EVENT_FLOATING_BUTTON = "InvocationEvent.floatingButton";
42+
final public static String INVOCATION_EVENT_SHAKE = "InvocationEvent.shake";
43+
44+
private Map<String, Object> constants = getConstants();
2845

2946
/** Plugin registration. */
3047
public static void registerWith(Registrar registrar) {
@@ -34,14 +51,62 @@ public static void registerWith(Registrar registrar) {
3451

3552
@Override
3653
public void onMethodCall(MethodCall call, Result result) {
37-
if (call.method.equals("start")) {
38-
result.success(null);
39-
} else {
54+
Method[] methods = this.getClass().getMethods();
55+
boolean isImplemented = false;
56+
String callMethod = call.method;
57+
if (callMethod.contains(":")) {
58+
callMethod = call.method.substring( 0, call.method.indexOf(":"));
59+
}
60+
for (Method method : methods) {
61+
if (callMethod.equals(method.getName())) {
62+
isImplemented = true;
63+
ArrayList<Object> tempParamValues = new ArrayList<>();
64+
HashMap map = (HashMap<String, String>)call.arguments;
65+
Iterator iterator = map.entrySet().iterator();
66+
while (iterator.hasNext()) {
67+
Map.Entry pair = (Map.Entry)iterator.next();
68+
tempParamValues.add(pair.getValue());
69+
iterator.remove();
70+
}
71+
Object[] paramValues = tempParamValues.toArray();
72+
try {
73+
method.invoke(this, paramValues);
74+
} catch (Exception e) {
75+
e.printStackTrace();
76+
result.notImplemented();
77+
}
78+
result.success(null);
79+
break;
80+
}
81+
}
82+
if (!isImplemented) {
4083
result.notImplemented();
4184
}
4285
}
43-
public void start(Application application, String token) {
44-
new Instabug.Builder(application, token).build();
86+
87+
/**
88+
* starts the SDK
89+
* @param application the application Object
90+
* @param token token The token that identifies the app, you can find
91+
* it on your dashboard.
92+
* @param invocationEvents invocationEvents The events that invoke
93+
* the SDK's UI.
94+
*/
95+
public void start(Application application, String token, ArrayList<String> invocationEvents) {
96+
InstabugInvocationEvent[] invocationEventsArray = new InstabugInvocationEvent[invocationEvents.size()];
97+
for (int i = 0; i < invocationEvents.size(); i++) {
98+
invocationEventsArray[i] = (InstabugInvocationEvent)constants.get(invocationEvents.get(i));
99+
}
100+
new Instabug.Builder(application, token).setInvocationEvents(invocationEventsArray).build();
45101
}
46102

103+
public Map<String, Object> getConstants() {
104+
final Map<String, Object> constants = new HashMap<>();
105+
constants.put("InvocationEvent.none", InstabugInvocationEvent.NONE);
106+
constants.put("InvocationEvent.screenshot", InstabugInvocationEvent.SCREENSHOT);
107+
constants.put("InvocationEvent.twoFingersSwipeLeft", InstabugInvocationEvent.TWO_FINGER_SWIPE_LEFT);
108+
constants.put("InvocationEvent.floatingButton", InstabugInvocationEvent.FLOATING_BUTTON);
109+
constants.put("InvocationEvent.shake", InstabugInvocationEvent.SHAKE);
110+
return constants;
111+
}
47112
}

example/android/app/src/main/java/com/instabug/instabugflutterexample/CustomFlutterApplication.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import io.flutter.app.FlutterApplication;
44
import com.instabug.instabugflutter.InstabugFlutterPlugin;
55

6+
import java.util.ArrayList;
7+
68
public class CustomFlutterApplication extends FlutterApplication {
79
@Override
810
public void onCreate() {
911
super.onCreate();
10-
11-
new InstabugFlutterPlugin().start(CustomFlutterApplication.this, "9582e6cfe34e2b8897f48cfa3b617adb");
12+
ArrayList<String> invocation_events = new ArrayList<>();
13+
invocation_events.add(InstabugFlutterPlugin.INVOCATION_EVENT_FLOATING_BUTTON);
14+
new InstabugFlutterPlugin().start(CustomFlutterApplication.this, "9582e6cfe34e2b8897f48cfa3b617adb", invocation_events);
1215
}
1316
}

example/lib/main.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'dart:async';
3-
3+
import 'dart:io' show Platform;
44
import 'package:flutter/services.dart';
55
import 'package:instabug_flutter/instabug_flutter.dart';
66

@@ -25,11 +25,12 @@ class _MyAppState extends State<MyApp> {
2525
String platformVersion;
2626
// Platform messages may fail, so we use a try/catch PlatformException.
2727
try {
28-
InstabugFlutter.start('9582e6cfe34e2b8897f48cfa3b617adb', [InvocationEvent.floatingButton, InvocationEvent.shake]);
28+
if (Platform.isIOS) {
29+
InstabugFlutter.start('9582e6cfe34e2b8897f48cfa3b617adb', [InvocationEvent.floatingButton, InvocationEvent.shake]);
30+
}
2931
} on PlatformException {
3032
platformVersion = 'Failed to get platform version.';
3133
}
32-
3334
// If the widget was removed from the tree while the asynchronous platform
3435
// message was in flight, we want to discard the reply rather than calling
3536
// setState to update our non-existent appearance.

ios/Classes/InstabugFlutterPlugin.m

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
#import "Instabug.h"
33

44
@implementation InstabugFlutterPlugin
5+
6+
+ (NSDictionary *) constants {
7+
return @{
8+
@"InvocationEvent.shake": @(IBGInvocationEventShake),
9+
@"InvocationEvent.screenshot": @(IBGInvocationEventScreenshot),
10+
@"InvocationEvent.twoFingersSwipeLeft": @(IBGInvocationEventTwoFingersSwipeLeft),
11+
@"InvocationEvent.floatingButton": @(IBGInvocationEventFloatingButton),
12+
@"InvocationEvent.none": @(IBGInvocationEventNone),
13+
};
14+
};
15+
516
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
617
FlutterMethodChannel* channel = [FlutterMethodChannel
718
methodChannelWithName:@"instabug_flutter"
@@ -11,28 +22,45 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
1122
}
1223

1324
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
14-
if ([@"startWithToken:invocationEvents" isEqualToString:call.method]) {
15-
NSDictionary *invocationEventsMap = @{
16-
@"InvocationEvent.shake": @(IBGInvocationEventShake),
17-
@"InvocationEvent.screenshot": @(IBGInvocationEventScreenshot),
18-
@"InvocationEvent.twoFingersSwipeLeft": @(IBGInvocationEventTwoFingersSwipeLeft),
19-
@"InvocationEvent.rightEdgePan": @(IBGInvocationEventRightEdgePan),
20-
@"InvocationEvent.floatingButton": @(IBGInvocationEventFloatingButton),
21-
@"InvocationEvent.none": @(IBGInvocationEventNone),
22-
};
25+
BOOL isImplemented = NO;
26+
SEL method = NSSelectorFromString(call.method);
27+
if([[InstabugFlutterPlugin class] respondsToSelector:method]) {
28+
isImplemented = YES;
29+
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[[InstabugFlutterPlugin class] methodSignatureForSelector:method]];
30+
[inv setSelector:method];
31+
[inv setTarget:[InstabugFlutterPlugin class]];
32+
/*
33+
* Indices 0 and 1 indicate the hidden arguments self and _cmd,
34+
* respectively; you should set these values directly with the target and selector properties.
35+
* Use indices 2 and greater for the arguments normally passed in a message.
36+
*/
37+
NSInteger index = 2;
38+
NSDictionary *argumentsDictionary = call.arguments;
39+
for (id key in argumentsDictionary) {
40+
NSObject *arg = [argumentsDictionary objectForKey:key];
41+
[inv setArgument:&(arg) atIndex:index];
42+
index++;
43+
}
44+
[inv invoke];
45+
}
46+
if (!isImplemented) {
47+
result(FlutterMethodNotImplemented);
48+
}
49+
}
2350

24-
NSString *token = call.arguments[@"token"];
25-
51+
/**
52+
* starts the SDK
53+
* @param {token} token The token that identifies the app
54+
* @param {invocationEvents} invocationEvents The events that invoke
55+
* the SDK's UI.
56+
*/
57+
+ (void)startWithToken:(NSString *)token invocationEvents:(NSArray*)invocationEventsArray {
58+
NSDictionary *constants = [self constants];
2659
NSInteger invocationEvents = IBGInvocationEventNone;
27-
for (NSString * invocationEvent in call.arguments[@"invocationEvents"]) {
28-
invocationEvents |= ((NSNumber *) invocationEventsMap[invocationEvent]).integerValue;
60+
for (NSString * invocationEvent in invocationEventsArray) {
61+
invocationEvents |= ((NSNumber *) constants[invocationEvent]).integerValue;
2962
}
30-
3163
[Instabug startWithToken:token invocationEvents:invocationEvents];
32-
result(nil);
33-
} else {
34-
result(FlutterMethodNotImplemented);
35-
}
3664
}
3765

3866
@end

lib/instabug_flutter.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'dart:async';
22

33
import 'package:flutter/services.dart';
44

5-
enum InvocationEvent { shake, screenshot, twoFingersSwipeLeft, rightEdgePan, floatingButton, none }
5+
enum InvocationEvent { shake, screenshot, twoFingersSwipeLeft, floatingButton, none }
66

77
class InstabugFlutter {
88
static const MethodChannel _channel =
@@ -13,12 +13,22 @@ class InstabugFlutter {
1313
return version;
1414
}
1515

16+
/*
17+
* Starts the SDK.
18+
* This is the main SDK method that does all the magic. This is the only
19+
* method that SHOULD be called.
20+
* @param {string} token The token that identifies the app, you can find
21+
* it on your dashboard.
22+
* @param {List<InvocationEvent>} invocationEvents The events that invoke
23+
* the SDK's UI.
24+
*/
1625
static void start(String token, List<InvocationEvent> invocationEvents) async {
1726
List<String> invocationEventsStrings = new List<String>();
1827
invocationEvents.forEach((e) {
1928
invocationEventsStrings.add(e.toString());
2029
});
2130
Map params = {'token': token, 'invocationEvents': invocationEventsStrings};
22-
await _channel.invokeMethod('startWithToken:invocationEvents', params);
31+
await _channel.invokeMethod('startWithToken:invocationEvents:', params);
2332
}
33+
2434
}

0 commit comments

Comments
 (0)