Skip to content

Commit

Permalink
Merge pull request #71 from RodrigoSMarques/bugfix
Browse files Browse the repository at this point in the history
Fix: Methods executed on the main thread (iOS/Android)
  • Loading branch information
RodrigoSMarques authored Jan 4, 2021
2 parents e1ef714 + 7c04813 commit ae188bc
Show file tree
Hide file tree
Showing 12 changed files with 526 additions and 394 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.3.1
* Android: fixed assertion failures due to reply messages that were sent on the wrong thread.

# 1.3.0
* iOS Native SDK Update 0.36.0 - [iOS Version History](https://help.branch.io/developers-hub/docs/ios-version-history)

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Branch.io helps mobile apps grow with deep links that power referral systems, sh

Supports both Android and iOS.
* Android - Branch SDK Version >= 5.0.3 [Android Version History](https://help.branch.io/developers-hub/docs/android-version-history)
* iOS - Branch SDK Version >= 0.35.0 [iOS Version History](https://help.branch.io/developers-hub/docs/ios-version-history)
* iOS - Branch SDK Version >= 0.36.0 [iOS Version History](https://help.branch.io/developers-hub/docs/ios-version-history)

Implemented functions in plugin:

Expand All @@ -22,6 +22,9 @@ Implemented functions in plugin:
* Init Branch Session and Deep Link
* Referral rewards


Note: **This plugin not work with FlutterFragmentActivity**

## Getting Started
### Configure Branch Dashboard
* Register Your App
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -136,7 +138,7 @@ public void onReattachedToActivityForConfigChanges(ActivityPluginBinding activit
--------------------------------------------------------------------------------------------**/
@Override
public void onListen(Object o, EventChannel.EventSink eventSink) {
this.eventSink = eventSink;
this.eventSink = new MainThreadEventSink(eventSink);
if (initialParams != null) {
eventSink.success(initialParams);
initialParams = null;
Expand All @@ -150,7 +152,7 @@ public void onListen(Object o, EventChannel.EventSink eventSink) {

@Override
public void onCancel(Object o) {
this.eventSink = null;
this.eventSink = new MainThreadEventSink(null);;
initialError = null;
initialParams = null;
}
Expand All @@ -159,8 +161,7 @@ public void onCancel(Object o) {
ActivityLifecycleCallbacks Interface Methods
--------------------------------------------------------------------------------------------**/
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
}
public void onActivityCreated(Activity activity, Bundle bundle) {}

@Override
public void onActivityStarted(Activity activity) {
Expand Down Expand Up @@ -202,7 +203,8 @@ public boolean onNewIntent(Intent intent) {
MethodCallHandler Interface Methods
--------------------------------------------------------------------------------------------**/
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
public void onMethodCall(@NonNull MethodCall call, @NonNull Result rawResult) {
Result result = new MethodResultWrapper(rawResult);
switch (call.method) {
case "getShortUrl":
getShortUrl(call, result);
Expand Down Expand Up @@ -611,4 +613,91 @@ public void onReceivingResponse(JSONArray list, BranchError error) {
private void isUserIdentified(Result result) {
result.success(Branch.getInstance(context).isUserIdentified());
}

// MethodChannel.Result wrapper that responds on the platform thread.
private static class MethodResultWrapper implements Result {
private Result methodResult;
private Handler handler;

MethodResultWrapper(Result result) {
methodResult = result;
handler = new Handler(Looper.getMainLooper());
}

@Override
public void success(final Object result) {
handler.post(
new Runnable() {
@Override
public void run() {
methodResult.success(result);
}
});
}

@Override
public void error(
final String errorCode, final String errorMessage, final Object errorDetails) {
handler.post(
new Runnable() {
@Override
public void run() {
methodResult.error(errorCode, errorMessage, errorDetails);
}
});
}

@Override
public void notImplemented() {
handler.post(
new Runnable() {
@Override
public void run() {
methodResult.notImplemented();
}
});
}
}

private static class MainThreadEventSink implements EventChannel.EventSink {
private EventChannel.EventSink eventSink;
private Handler handler;

MainThreadEventSink(EventChannel.EventSink eventSink) {
this.eventSink = eventSink;
handler = new Handler(Looper.getMainLooper());
}

@Override
public void success(final Object o) {
handler.post(new Runnable() {
@Override
public void run() {
eventSink.success(o);
}
});
}

@Override
public void error(final String s, final String s1, final Object o) {
handler.post(new Runnable() {
@Override
public void run() {
eventSink.error(s, s1, o);
}
});
}

@Override
public void endOfStream() {
handler.post(new Runnable() {
@Override
public void run() {
eventSink.endOfStream();
}
});
}
}
}


2 changes: 1 addition & 1 deletion example/.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_branch_sdk","path":"/Volumes/SANDISK/Development/Github/flutter_branch_sdk/","dependencies":[]}],"android":[{"name":"flutter_branch_sdk","path":"/Volumes/SANDISK/Development/Github/flutter_branch_sdk/","dependencies":[]}],"macos":[],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"flutter_branch_sdk","dependencies":[]}],"date_created":"2020-11-17 23:06:51.480842","version":"1.22.4"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"flutter_branch_sdk","path":"/Volumes/SANDISK/Development/Github/flutter_branch_sdk/","dependencies":[]}],"android":[{"name":"flutter_branch_sdk","path":"/Volumes/SANDISK/Development/Github/flutter_branch_sdk/","dependencies":[]}],"macos":[],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"flutter_branch_sdk","dependencies":[]}],"date_created":"2021-01-03 23:28:34.205495","version":"1.22.5"}
2 changes: 1 addition & 1 deletion example/ios/Flutter/.last_build_id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ae2416522934f67f7462f7625054791b
5b19e5cb9a3de52bf49e88d43e6aeaf8
10 changes: 5 additions & 5 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PODS:
- Branch (0.35.0)
- Branch (0.36.0)
- Flutter (1.0.0)
- flutter_branch_sdk (1.2.0):
- Branch (~> 0.35.0)
- Branch (~> 0.36.0)
- Flutter

DEPENDENCIES:
Expand All @@ -20,10 +20,10 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/flutter_branch_sdk/ios"

SPEC CHECKSUMS:
Branch: 9a37f707974a128c37829033c49018b79c7e7a2d
Branch: 8aebcb7a6465f19f9f4d315acced369ae078d3f2
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
flutter_branch_sdk: 4b6cda04245cdff4cc36e04c41fb0dc98ffb3c71
flutter_branch_sdk: e112bc8cc622f6682099823ce4bc1520fb8d55db

PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c

COCOAPODS: 1.9.3
COCOAPODS: 1.10.0
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
Expand All @@ -38,11 +36,11 @@
ReferencedContainer = "container:Runner.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
Expand All @@ -61,8 +59,6 @@
ReferencedContainer = "container:Runner.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Profile"
Expand Down
Loading

0 comments on commit ae188bc

Please sign in to comment.