Skip to content

Commit

Permalink
Merge pull request #5 from RodrigoSMarques/bugfix
Browse files Browse the repository at this point in the history
Version 0.1.2
  • Loading branch information
RodrigoSMarques authored Jan 8, 2020
2 parents b81a9bf + d75e103 commit 2139671
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 73 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
* Pubspec.yaml Update
## 0.1.1
* Minor adjustments and fix initial deep link data loss
## 0.1.2
* Compatibility with apps built on earlier versions of Flutter 1.12
* Improved error handling in initSession

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ public class FlutterBranchSdkInit {
private static final String DEBUG_NAME = "FlutterBranchSDK";

public static void init(Context context) {
Log.i(DEBUG_NAME, " FlutterBranchSdkInit");
if (BuildConfig.DEBUG) {
Log.i(DEBUG_NAME, " DebugMode");
Log.i(DEBUG_NAME, "Branch SDK in DebugMode");
Branch.enableDebugMode();
}
// Branch object initialization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@
import io.flutter.plugin.common.PluginRegistry.NewIntentListener;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/**
* FlutterBranchSdkPlugin
*/
public class FlutterBranchSdkPlugin implements FlutterPlugin, MethodCallHandler, StreamHandler, NewIntentListener, ActivityAware,
Application.ActivityLifecycleCallbacks {
private static final String DEBUG_NAME = "FlutterBranchSDK";
Expand All @@ -63,15 +60,23 @@ public class FlutterBranchSdkPlugin implements FlutterPlugin, MethodCallHandler,
private static final String MESSAGE_CHANNEL = "flutter_branch_sdk/message";
private static final String EVENT_CHANNEL = "flutter_branch_sdk/event";
private EventSink eventSink = null;
private Map<String, Object> initialData = null;
private Map<String, Object> initialParams = null;
private BranchError initialError = null;

/**
* Plugin registration.
*/
/**---------------------------------------------------------------------------------------------
Plugin registry
--------------------------------------------------------------------------------------------**/

public static void registerWith(Registrar registrar) {
if (registrar.activity() == null) {
// When a background flutter view tries to register the plugin, the registrar has no activity.
// We stop the registration process as this plugin is foreground only.
return;
}
FlutterBranchSdkPlugin plugin = new FlutterBranchSdkPlugin();
plugin.setupChannels(registrar.messenger(), registrar.context());
plugin.setupChannels(registrar.messenger(), registrar.activity().getApplicationContext());
plugin.setActivity(registrar.activity());
registrar.addNewIntentListener(plugin);
}

@Override
Expand All @@ -86,15 +91,19 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {

private void setupChannels(BinaryMessenger messenger, Context context) {
this.context = context;
this.activity = null;

methodChannel = new MethodChannel(messenger, MESSAGE_CHANNEL);
eventChannel = new EventChannel(messenger, EVENT_CHANNEL);

methodChannel.setMethodCallHandler(this);
eventChannel.setStreamHandler(this);

FlutterBranchSdkInit.init(this.context);
FlutterBranchSdkInit.init(context);
}

private void setActivity(Activity activity) {
this.activity = activity;
activity.getApplication().registerActivityLifecycleCallbacks(this);
}

private void teardownChannels() {
Expand All @@ -103,13 +112,14 @@ private void teardownChannels() {
this.context = null;
}

/**---------------------------------------------------------------------------------------------
ActivityAware Interface Methods
--------------------------------------------------------------------------------------------**/
@Override
public void onAttachedToActivity(ActivityPluginBinding binding) {
this.activity = binding.getActivity();
this.activityPluginBinding = binding;

activity.getApplication().registerActivityLifecycleCallbacks(this);
binding.addOnNewIntentListener(this);
public void onAttachedToActivity(ActivityPluginBinding activityPluginBinding) {
this.activityPluginBinding = activityPluginBinding;
setActivity(activityPluginBinding.getActivity());
activityPluginBinding.addOnNewIntentListener(this);
}

@Override
Expand All @@ -124,25 +134,37 @@ public void onDetachedFromActivityForConfigChanges() {
}

@Override
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
onAttachedToActivity(binding);
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding activityPluginBinding) {
onAttachedToActivity(activityPluginBinding);
}

/**---------------------------------------------------------------------------------------------
StreamHandler Interface Methods
--------------------------------------------------------------------------------------------**/
@Override
public void onListen(Object o, EventChannel.EventSink eventSink) {
this.eventSink = eventSink;
if (initialData != null) {
eventSink.success(initialData);
initialData = null;
if (initialParams != null) {
eventSink.success(initialParams);
initialParams = null;
initialError = null;
} else if (initialError != null) {
eventSink.error(String.valueOf(initialError.getErrorCode()), initialError.getMessage(),null);
initialParams = null;
initialError = null;
}
}

@Override
public void onCancel(Object o) {
this.eventSink = null;
initialData = null;
initialError = null;
initialParams = null;
}

/**---------------------------------------------------------------------------------------------
ActivityLifecycleCallbacks Interface Methods
--------------------------------------------------------------------------------------------**/
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
}
Expand Down Expand Up @@ -171,6 +193,9 @@ public void onActivityDestroyed(Activity activity) {
}
}

/**---------------------------------------------------------------------------------------------
NewIntentListener Interface Methods
--------------------------------------------------------------------------------------------**/
@Override
public boolean onNewIntent(Intent intent) {
if (this.activity != null) {
Expand All @@ -180,35 +205,10 @@ public boolean onNewIntent(Intent intent) {
}
return true;
}
//----------------------------------------------------------------------------------------------

private Branch.BranchReferralInitListener branchReferralInitListener = new
Branch.BranchReferralInitListener() {
@Override
public void onInitFinished(JSONObject params, BranchError error) {
if (error == null) {
Log.d(DEBUG_NAME, "branchReferralInitListener" + params.toString());
if (eventSink == null) {
try {
initialData = paramsToMap(params);
} catch (JSONException e) {
e.printStackTrace();
}
return;
}
try {
eventSink.success(paramsToMap(params));
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.d(DEBUG_NAME, "branchReferralInitListener - error: " + error.toString());
}
}
};

//----------------------------------------------------------------------------------------------

/**---------------------------------------------------------------------------------------------
MethodCallHandler Interface Methods
--------------------------------------------------------------------------------------------**/
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
switch (call.method) {
Expand Down Expand Up @@ -253,10 +253,39 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
}
}

//----------------------------------------------------------------------------------------------
/**---------------------------------------------------------------------------------------------
Branch SDK Call Methods
--------------------------------------------------------------------------------------------**/
private Branch.BranchReferralInitListener branchReferralInitListener = new
Branch.BranchReferralInitListener() {
@Override
public void onInitFinished(JSONObject params, BranchError error) {
if (error == null) {
Log.d(DEBUG_NAME, "BranchReferralInitListener - params: " + params.toString());
try {
initialParams = paramsToMap(params);
} catch (JSONException e) {
Log.d(DEBUG_NAME, "BranchReferralInitListener - error to Map: " + e.getLocalizedMessage());
return;
}
if (eventSink != null) {
eventSink.success(initialParams);
initialParams = null;
}
} else {
Log.d(DEBUG_NAME, "BranchReferralInitListener - error: " + error.toString());
if (eventSink != null) {
eventSink.error(String.valueOf(error.getErrorCode()), error.getMessage(),null);
initialError = null;
} else {
initialError = error;
}
}
}
};

private void validateSDKIntegration() {
IntegrationValidator.validate(activity);
IntegrationValidator.validate(context);
}

private void getShortUrl(MethodCall call, final Result result) {
Expand Down Expand Up @@ -434,6 +463,9 @@ private void setTrackingDisabled(MethodCall call) {
Branch.getInstance().disableTracking(value);
}

/**---------------------------------------------------------------------------------------------
Object Conversion Functions
--------------------------------------------------------------------------------------------**/
private BranchUniversalObject convertToBUO(HashMap<String, Object> argsMap) {

BranchUniversalObject buo = new BranchUniversalObject();
Expand Down
33 changes: 31 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_branch_sdk/flutter_branch_sdk.dart';

void main() => runApp(MyApp());
Expand All @@ -21,6 +22,7 @@ class _MyAppState extends State<MyApp> {
var scaffoldKey = new GlobalKey<ScaffoldState>();
StreamSubscription<Map> streamSubscription;
StreamController<String> controllerData = StreamController<String>();
StreamController<String> controllerInitSession = StreamController<String>();
StreamController<String> controllerUrl = StreamController<String>();

@override
Expand All @@ -33,7 +35,6 @@ class _MyAppState extends State<MyApp> {

void listenDynamicLinks() async {
streamSubscription = FlutterBranchSdk.initSession().listen((data) {
print('initSession');
print('listenDynamicLinks - DeepLink Data: $data');
controllerData.sink.add((data.toString()));
if (data.containsKey('+clicked_branch_link') &&
Expand All @@ -51,7 +52,11 @@ class _MyAppState extends State<MyApp> {
duration: 10);
}
}, onError: (error) {
print(error);
PlatformException platformException = error as PlatformException;
print(
'InitSession error: ${platformException.code} - ${platformException.message}');
controllerInitSession.add(
'InitSession error: ${platformException.code} - ${platformException.message}');
});
}

Expand Down Expand Up @@ -159,6 +164,29 @@ class _MyAppState extends State<MyApp> {
body: ListView(
padding: EdgeInsets.all(10),
children: <Widget>[
StreamBuilder<String>(
stream: controllerInitSession.stream,
initialData: '',
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data.isNotEmpty) {
return Column(
children: <Widget>[
Center(
child: Text(
snapshot.data,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Colors.red),
))
],
);
} else {
return Container();
}
},
),
RaisedButton(
child: Text('Validate SDK Integration'),
onPressed: () {
Expand Down Expand Up @@ -403,6 +431,7 @@ class _MyAppState extends State<MyApp> {
super.dispose();
controllerData.close();
controllerUrl.close();
controllerInitSession.close();
streamSubscription.cancel();
}
}
7 changes: 7 additions & 0 deletions ios/Classes/FlutterBranchIoSdkUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import Foundation
import Branch

//---------------------------------------------------------------------------------------------
// Object Conversion Functions
// --------------------------------------------------------------------------------------------
func convertToBUO(dict: [String: Any?]) -> BranchUniversalObject? {
guard let canonicalIdentifier = dict["canonicalIdentifier"] as? String? else {
return nil
Expand Down Expand Up @@ -207,6 +210,10 @@ func convertToAdType(adType: String) -> BranchEventAdType {
return BranchEventAdType.none
}
}
//---------------------------------------------------------------------------------------------
// Extension
// --------------------------------------------------------------------------------------------

extension Date {
var millisecondsSince1970:Int64 {
return Int64((self.timeIntervalSince1970 * 1000.0).rounded())
Expand Down
Loading

0 comments on commit 2139671

Please sign in to comment.