Skip to content

Commit

Permalink
Merge pull request #1 from Instabug/MOB-2891
Browse files Browse the repository at this point in the history
Mob 2891, MOB-2854
  • Loading branch information
alyezz authored Mar 4, 2019
2 parents 74ce3d7 + a2e472a commit 4c414b0
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The table below contains a list of APIs we're planning to implement for our 1.0

| API Method | Native Equivalent (Android/iOS) |
|------------|-----------------------------------------------------------------------------------------------------------------------------------------|
| | `new Instabug.Builder(this, "APP_TOKEN").build()`<br>`+ [Instabug startWithToken:invocationEvents:]` |
| `start(String token, List<InvocationEvent> invocationEvents)` | `new Instabug.Builder(this, "APP_TOKEN").build()`<br>`+ [Instabug startWithToken:invocationEvents:]` |
| | `Instabug.showWelcomeMessage(WelcomeMessage.State state)`<br>`+ [Instabug showWelcomeMessageWithMode:]` |
| | `Instabug.identifyUser(String username, String email)`<br>`+ [Instabug identifyUserWithEmail:name:]` |
| | `Instabug.logoutUser()`<br>`+ [Instabug logOut]` |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@
import com.instabug.library.Instabug;
import com.instabug.library.invocation.InstabugInvocationEvent;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

// import com.instabug.library.InstabugColorTheme;
// import com.instabug.library.InstabugCustomTextPlaceHolder;
Expand All @@ -23,8 +34,14 @@

/** InstabugFlutterPlugin */
public class InstabugFlutterPlugin implements MethodCallHandler {

private ArrayList<InstabugInvocationEvent> invocationEvents = new ArrayList<>();

final public static String INVOCATION_EVENT_NONE = "InvocationEvent.none";
final public static String INVOCATION_EVENT_SCREENSHOT = "InvocationEvent.screenshot";
final public static String INVOCATION_EVENT_TWO_FINGER_SWIPE_LEFT = "InvocationEvent.twoFingersSwipeLeft";
final public static String INVOCATION_EVENT_FLOATING_BUTTON = "InvocationEvent.floatingButton";
final public static String INVOCATION_EVENT_SHAKE = "InvocationEvent.shake";

private Map<String, Object> constants = getConstants();

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

@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("start")) {
result.success(null);
} else {
Method[] methods = this.getClass().getMethods();
boolean isImplemented = false;
String callMethod = call.method;
if (callMethod.contains(":")) {
callMethod = call.method.substring( 0, call.method.indexOf(":"));
}
for (Method method : methods) {
if (callMethod.equals(method.getName())) {
isImplemented = true;
ArrayList<Object> tempParamValues = new ArrayList<>();
HashMap map = (HashMap<String, String>)call.arguments;
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry)iterator.next();
tempParamValues.add(pair.getValue());
iterator.remove();
}
Object[] paramValues = tempParamValues.toArray();
try {
method.invoke(this, paramValues);
} catch (Exception e) {
e.printStackTrace();
result.notImplemented();
}
result.success(null);
break;
}
}
if (!isImplemented) {
result.notImplemented();
}
}
public void start(Application application, String token) {
new Instabug.Builder(application, token).build();

/**
* starts the SDK
* @param application the application Object
* @param token token The token that identifies the app, you can find
* it on your dashboard.
* @param invocationEvents invocationEvents The events that invoke
* the SDK's UI.
*/
public void start(Application application, String token, ArrayList<String> invocationEvents) {
InstabugInvocationEvent[] invocationEventsArray = new InstabugInvocationEvent[invocationEvents.size()];
for (int i = 0; i < invocationEvents.size(); i++) {
invocationEventsArray[i] = (InstabugInvocationEvent)constants.get(invocationEvents.get(i));
}
new Instabug.Builder(application, token).setInvocationEvents(invocationEventsArray).build();
}

public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put("InvocationEvent.none", InstabugInvocationEvent.NONE);
constants.put("InvocationEvent.screenshot", InstabugInvocationEvent.SCREENSHOT);
constants.put("InvocationEvent.twoFingersSwipeLeft", InstabugInvocationEvent.TWO_FINGER_SWIPE_LEFT);
constants.put("InvocationEvent.floatingButton", InstabugInvocationEvent.FLOATING_BUTTON);
constants.put("InvocationEvent.shake", InstabugInvocationEvent.SHAKE);
return constants;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import io.flutter.app.FlutterApplication;
import com.instabug.instabugflutter.InstabugFlutterPlugin;

import java.util.ArrayList;

public class CustomFlutterApplication extends FlutterApplication {
@Override
public void onCreate() {
super.onCreate();

new InstabugFlutterPlugin().start(CustomFlutterApplication.this, "9582e6cfe34e2b8897f48cfa3b617adb");
ArrayList<String> invocation_events = new ArrayList<>();
invocation_events.add(InstabugFlutterPlugin.INVOCATION_EVENT_FLOATING_BUTTON);
new InstabugFlutterPlugin().start(CustomFlutterApplication.this, "9582e6cfe34e2b8897f48cfa3b617adb", invocation_events);
}
}
7 changes: 4 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'dart:async';

import 'dart:io' show Platform;
import 'package:flutter/services.dart';
import 'package:instabug_flutter/instabug_flutter.dart';

Expand All @@ -25,11 +25,12 @@ class _MyAppState extends State<MyApp> {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
InstabugFlutter.start('9582e6cfe34e2b8897f48cfa3b617adb', [InvocationEvent.floatingButton, InvocationEvent.shake]);
if (Platform.isIOS) {
InstabugFlutter.start('9582e6cfe34e2b8897f48cfa3b617adb', [InvocationEvent.floatingButton, InvocationEvent.shake]);
}
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}

// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
Expand Down
64 changes: 46 additions & 18 deletions ios/Classes/InstabugFlutterPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
#import "Instabug.h"

@implementation InstabugFlutterPlugin

+ (NSDictionary *) constants {
return @{
@"InvocationEvent.shake": @(IBGInvocationEventShake),
@"InvocationEvent.screenshot": @(IBGInvocationEventScreenshot),
@"InvocationEvent.twoFingersSwipeLeft": @(IBGInvocationEventTwoFingersSwipeLeft),
@"InvocationEvent.floatingButton": @(IBGInvocationEventFloatingButton),
@"InvocationEvent.none": @(IBGInvocationEventNone),
};
};

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel
methodChannelWithName:@"instabug_flutter"
Expand All @@ -11,28 +22,45 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"startWithToken:invocationEvents" isEqualToString:call.method]) {
NSDictionary *invocationEventsMap = @{
@"InvocationEvent.shake": @(IBGInvocationEventShake),
@"InvocationEvent.screenshot": @(IBGInvocationEventScreenshot),
@"InvocationEvent.twoFingersSwipeLeft": @(IBGInvocationEventTwoFingersSwipeLeft),
@"InvocationEvent.rightEdgePan": @(IBGInvocationEventRightEdgePan),
@"InvocationEvent.floatingButton": @(IBGInvocationEventFloatingButton),
@"InvocationEvent.none": @(IBGInvocationEventNone),
};
BOOL isImplemented = NO;
SEL method = NSSelectorFromString(call.method);
if([[InstabugFlutterPlugin class] respondsToSelector:method]) {
isImplemented = YES;
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[[InstabugFlutterPlugin class] methodSignatureForSelector:method]];
[inv setSelector:method];
[inv setTarget:[InstabugFlutterPlugin class]];
/*
* Indices 0 and 1 indicate the hidden arguments self and _cmd,
* respectively; you should set these values directly with the target and selector properties.
* Use indices 2 and greater for the arguments normally passed in a message.
*/
NSInteger index = 2;
NSDictionary *argumentsDictionary = call.arguments;
for (id key in argumentsDictionary) {
NSObject *arg = [argumentsDictionary objectForKey:key];
[inv setArgument:&(arg) atIndex:index];
index++;
}
[inv invoke];
}
if (!isImplemented) {
result(FlutterMethodNotImplemented);
}
}

NSString *token = call.arguments[@"token"];

/**
* starts the SDK
* @param {token} token The token that identifies the app
* @param {invocationEvents} invocationEvents The events that invoke
* the SDK's UI.
*/
+ (void)startWithToken:(NSString *)token invocationEvents:(NSArray*)invocationEventsArray {
NSDictionary *constants = [self constants];
NSInteger invocationEvents = IBGInvocationEventNone;
for (NSString * invocationEvent in call.arguments[@"invocationEvents"]) {
invocationEvents |= ((NSNumber *) invocationEventsMap[invocationEvent]).integerValue;
for (NSString * invocationEvent in invocationEventsArray) {
invocationEvents |= ((NSNumber *) constants[invocationEvent]).integerValue;
}

[Instabug startWithToken:token invocationEvents:invocationEvents];
result(nil);
} else {
result(FlutterMethodNotImplemented);
}
}

@end
14 changes: 12 additions & 2 deletions lib/instabug_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:async';

import 'package:flutter/services.dart';

enum InvocationEvent { shake, screenshot, twoFingersSwipeLeft, rightEdgePan, floatingButton, none }
enum InvocationEvent { shake, screenshot, twoFingersSwipeLeft, floatingButton, none }

class InstabugFlutter {
static const MethodChannel _channel =
Expand All @@ -13,12 +13,22 @@ class InstabugFlutter {
return version;
}

/*
* Starts the SDK.
* This is the main SDK method that does all the magic. This is the only
* method that SHOULD be called.
* @param {string} token The token that identifies the app, you can find
* it on your dashboard.
* @param {List<InvocationEvent>} invocationEvents The events that invoke
* the SDK's UI.
*/
static void start(String token, List<InvocationEvent> invocationEvents) async {
List<String> invocationEventsStrings = new List<String>();
invocationEvents.forEach((e) {
invocationEventsStrings.add(e.toString());
});
Map params = {'token': token, 'invocationEvents': invocationEventsStrings};
await _channel.invokeMethod('startWithToken:invocationEvents', params);
await _channel.invokeMethod('startWithToken:invocationEvents:', params);
}

}

0 comments on commit 4c414b0

Please sign in to comment.