From cd3cb2092fc6c5b8785b117120bcae424117b375 Mon Sep 17 00:00:00 2001 From: hunghd Date: Mon, 12 Aug 2024 14:30:24 +0700 Subject: [PATCH 01/10] migrate to dart:js_interop --- lib/src/flutter_branch_sdk_web.dart | 119 +++++++++++++++------------- lib/src/web/branch_js.dart | 72 ++++++++--------- 2 files changed, 97 insertions(+), 94 deletions(-) diff --git a/lib/src/flutter_branch_sdk_web.dart b/lib/src/flutter_branch_sdk_web.dart index 95abd6df..f9257b42 100644 --- a/lib/src/flutter_branch_sdk_web.dart +++ b/lib/src/flutter_branch_sdk_web.dart @@ -4,8 +4,8 @@ // ignore: avoid_web_libraries_in_flutter import 'dart:async'; import 'dart:convert'; -import 'dart:js' as js; -import 'dart:js_util'; +import 'dart:js_interop'; +import 'dart:js_interop_unsafe' as js; import 'dart:typed_data'; import 'package:flutter/widgets.dart'; @@ -84,7 +84,7 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { final Completer> response = Completer(); try { - BranchJS.data(js.allowInterop((err, data) { + BranchJS.data((JSAny? err, JSAny? data) { if (err == null) { if (data != null) { var responseData = @@ -96,7 +96,7 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { } else { response.completeError(err); } - })); + }.toJS); } catch (e) { debugPrint('getLatestReferringParams() error: ${e.toString()}'); response.completeError(e); @@ -111,7 +111,7 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { Completer>(); try { - BranchJS.first(js.allowInterop((err, data) { + BranchJS.first((JSAny? err, JSAny? data) { if (err == null) { if (data != null) { var responseData = @@ -123,7 +123,7 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { } else { response.completeError(err); } - })); + }.toJS); } catch (e) { debugPrint('getFirstReferringParams() error: ${e.toString()}'); response.completeError(e); @@ -135,11 +135,13 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { @override void setIdentity(String userId) { try { - BranchJS.setIdentity(userId, js.allowInterop((error, data) { - if (error == null) { - _userIdentified = true; - } - })); + BranchJS.setIdentity( + userId, + (JSAny? error, JSAny? data) { + if (error == null) { + _userIdentified = true; + } + }.toJS); } catch (e) { debugPrint('setIdentity() error: ${e.toString()}'); } @@ -149,11 +151,11 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { @override void logout() { try { - BranchJS.logout(js.allowInterop((error) { + BranchJS.logout((JSAny? error) { if (error == null) { _userIdentified = false; } - })); + }.toJS); } catch (e) { debugPrint('logout() error: ${e.toString()}'); } @@ -185,15 +187,16 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { Completer responseCompleter = Completer(); try { - BranchJS.link(_dartObjectToJsObject(linkData), - js.allowInterop((err, url) { - if (err == null) { - responseCompleter.complete(BranchResponse.success(result: url)); - } else { - responseCompleter.completeError( - BranchResponse.error(errorCode: '-1', errorMessage: err)); - } - })); + BranchJS.link( + _dartObjectToJsObject(linkData), + (String? err, String url) { + if (err == null) { + responseCompleter.complete(BranchResponse.success(result: url)); + } else { + responseCompleter.completeError( + BranchResponse.error(errorCode: '-1', errorMessage: err)); + } + }.toJS); } catch (e) { debugPrint('getShortUrl() error: ${e.toString()}'); responseCompleter.completeError(BranchResponse.error( @@ -214,11 +217,11 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { await getShortUrl(buo: buo, linkProperties: linkProperties); if (response.success) { try { - await promiseToFuture(navigatorShare(_dartObjectToJsObject({ + await navigatorShare(_dartObjectToJsObject({ "title": messageText, "text": buo.title, "url": response.result - }))); + })).toDart; } catch (e) { browserPrompt(messageText, response.result); } @@ -231,7 +234,7 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { void trackContent( {required List buo, required BranchEvent branchEvent}) { - List contentItems = []; + List contentItems = []; for (var element in buo) { contentItems.add(_dartObjectToJsObject(element.toMap())); } @@ -241,11 +244,11 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { BranchJS.logEvent( branchEvent.eventName, _dartObjectToJsObject(branchEvent.toMap()), - contentItems, + contentItems.toJS, branchEvent.alias); } else { BranchJS.logEvent(branchEvent.eventName, - _dartObjectToJsObject(branchEvent.toMap()), contentItems); + _dartObjectToJsObject(branchEvent.toMap()), contentItems.toJS); } } catch (e) { debugPrint('trackContent() error: ${e.toString()}'); @@ -380,20 +383,21 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { Completer responseCompleter = Completer(); try { - BranchJS.lastAttributedTouchData(attributionWindow, - js.allowInterop((err, data) { - if (err == null) { - if (data != null) { - responseCompleter.complete( - BranchResponse.success(result: _jsObjectToDartObject(data))); - } else { - responseCompleter.complete(BranchResponse.success(result: {})); - } - } else { - responseCompleter.complete(BranchResponse.error( - errorCode: '999', errorMessage: err.toString())); - } - })); + BranchJS.lastAttributedTouchData( + attributionWindow?.toJS, + (JSAny? err, JSAny? data) { + if (err == null) { + if (data != null) { + responseCompleter.complete(BranchResponse.success( + result: _jsObjectToDartObject(data))); + } else { + responseCompleter.complete(BranchResponse.success(result: {})); + } + } else { + responseCompleter.complete(BranchResponse.error( + errorCode: '999', errorMessage: err.toString())); + } + }.toJS); } catch (e) { debugPrint('getLastAttributedTouchData() error: ${e.toString()}'); responseCompleter.complete(BranchResponse.error( @@ -418,22 +422,23 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { Map linkData = {...linkProperties.toMap(), 'data': data}; try { - BranchJS.qrCode(_dartObjectToJsObject(linkData), + BranchJS.qrCode( + _dartObjectToJsObject(linkData), _dartObjectToJsObject(qrCodeSettings.toMap()), - js.allowInterop((err, qrCode) { - if (err == null) { - if (qrCode != null) { - responseCompleter.complete( - BranchResponse.success(result: qrCode.rawBuffer.asUint8List())); - } else { - responseCompleter.complete(BranchResponse.error( - errorCode: '-1', errorMessage: 'Qrcode generate error')); - } - } else { - responseCompleter.complete(BranchResponse.error( - errorCode: '-1', errorMessage: err.toString())); - } - })); + (JSAny? err, QrCodeData? qrCode) { + if (err == null) { + if (qrCode != null) { + responseCompleter.complete( + BranchResponse.success(result: qrCode.rawBuffer.toDart)); + } else { + responseCompleter.complete(BranchResponse.error( + errorCode: '-1', errorMessage: 'Qrcode generate error')); + } + } else { + responseCompleter.complete(BranchResponse.error( + errorCode: '-1', errorMessage: err.toString())); + } + }.toJS); } catch (e) { responseCompleter.complete(BranchResponse.error( errorCode: '-1', errorMessage: 'qrCode generate error')); @@ -480,7 +485,7 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { ///Have Branch end the current deep link session and start a new session with the provided URL. @override void handleDeepLink(String url) { - js.context.callMethod('open', [url, '_self']); + globalContext.callMethodVarArgs('open'.toJS, [url.toJS, '_self'.toJS]); } /// Add a Partner Parameter for Facebook. diff --git a/lib/src/web/branch_js.dart b/lib/src/web/branch_js.dart index e9543dea..42d03e01 100644 --- a/lib/src/web/branch_js.dart +++ b/lib/src/web/branch_js.dart @@ -1,31 +1,29 @@ @JS() library branchjs; -import 'dart:typed_data'; - -import 'package:js/js.dart'; +import 'dart:js_interop'; @JS('JSON.stringify') -external String jsonStringify(Object obj); +external String jsonStringify(JSAny obj); @JS('JSON.parse') -external dynamic jsonParse(String str); +external JSAny jsonParse(String str); @JS('navigator.share') -external dynamic navigatorShare(Object data); +external JSPromise navigatorShare(JSAny data); @JS('prompt') -external dynamic browserPrompt(String message, [String data]); +external JSAny browserPrompt(String message, [String data]); @JS() @anonymous -class QrCodeData { - external ByteBuffer rawBuffer; - external Function base64(); +extension type QrCodeData._(JSObject _) implements JSObject { + external JSUint8Array rawBuffer; + external JSFunction base64(); } @JS('branch') -class BranchJS { +extension type BranchJS._(JSObject _) implements JSObject { /// addListener(event, listener) /// Parameters /// @@ -60,7 +58,7 @@ class BranchJS { /// didCloseJourney: Journey's close animation has completed and it is no longer visible to the user. /// didCallJourneyClose: Emitted when developer calls branch.closeJourney() to dismiss Journey. @JS('addListener') - external static void addListener([String event, Function listener]); + external static void addListener([String event, JSFunction listener]); // No documentation in full reference // @JS('banner') @@ -83,7 +81,7 @@ class BranchJS { /// /// branch.closeJourney(function(err) { console.log(err); }); @JS('closeJourney') - external static void closeJourney([Function callback]); + external static void closeJourney(JSFunction callback); /// data(callback) /// Parameters @@ -98,7 +96,7 @@ class BranchJS { /// If the Branch session has already been initialized, the callback will return /// immediately, otherwise, it will return once Branch has been initialized. @JS('data') - external static void data([Function callback]); + external static void data(JSFunction callback); /// deepview(data, options, callback) /// Parameters @@ -157,8 +155,8 @@ class BranchJS { /// "Error message" /// ); @JS('deepview') - external static void deepview(Object data, - [Object options, Function callback]); + external static void deepview(JSAny data, + [JSAny options, JSFunction callback]); /// deepviewCta() /// Perform the branch deepview CTA (call to action) on mobile after branch.deepview() call is @@ -213,7 +211,7 @@ class BranchJS { /// Warning: For a referral program, you should not use unique awards for custom events and redeem /// pre-identify call. This can allow users to cheat the system. @JS('deepviewCta') - external static void deepviewCta([Function errorCallback]); + external static void deepviewCta(JSFunction errorCallback); /// first(callback) /// Parameters @@ -228,7 +226,7 @@ class BranchJS { /// If the Branch session has already been initialized, the callback will return /// immediately, otherwise, it will return once Branch has been initialized. @JS('first') - external static void first([Function callback]); + external static void first(JSFunction callback); // No documentation on reference // @JS('getCode') @@ -295,7 +293,7 @@ class BranchJS { /// Note: Branch.init must be called prior to calling any other Branch functions. @JS('init') external static void init(String branchKey, - [Object? options, Function? callback]); + [JSAny? options, JSFunction? callback]); /// link(data, callback) /// Parameters @@ -377,7 +375,7 @@ class BranchJS { /// 'https://bnc.lt/l/3HZMytU-BW' // Branch deep linking URL /// ); @JS('link') - external static void link(Object data, Function callback); + external static void link(JSAny data, JSFunction callback); /// logout(callback) /// Parameters @@ -397,7 +395,7 @@ class BranchJS { /// "Error message" /// ); @JS('logout') - external static void logout([Function callback]); + external static void logout(JSFunction callback); /// removeListener(listener) /// Parameters @@ -410,7 +408,7 @@ class BranchJS { /// passed a referrence to the same function that was passed to branch.addListener(), not /// just an identical clone of the function. @JS('removeListener') - external static void removeListener(Function listener); + external static void removeListener(JSFunction listener); /// sendSMS(phone, linkData, options, callback) /// Parameters @@ -486,8 +484,8 @@ class BranchJS { /// /// callback("Error message"); @JS('sendSMS') - external static void sendSMS(String phone, Object linkData, - [Object options, Function callback]); + external static void sendSMS(String phone, JSAny linkData, + [JSAny options, JSFunction callback]); /// setBranchViewData(data) /// Parameters @@ -515,7 +513,7 @@ class BranchJS { /// } /// }); @JS('setBranchViewData') - external static void setBranchViewData(Object data); + external static void setBranchViewData(JSAny data); /// setIdentity(identity, callback) /// Parameters @@ -550,7 +548,7 @@ class BranchJS { /// } /// ); @JS('setIdentity') - external static void setIdentity(String identity, [Function callback]); + external static void setIdentity(String identity, [JSFunction callback]); /// track(event, metadata, callback) /// Parameters @@ -577,7 +575,7 @@ class BranchJS { /// callback("Error message"); @JS('track') external static void track(String event, - [Object metadata, Function callback]); + [JSAny metadata, JSFunction callback]); /// trackCommerceEvent(event, commerce_data, metadata, callback) /// Parameters @@ -629,8 +627,8 @@ class BranchJS { /// } /// }); @JS('trackCommerceEvent') - external static void trackCommerceEvent(String name, Object commerceData, - [Object metadata, Function callback]); + external static void trackCommerceEvent(String name, JSAny commerceData, + [JSAny metadata, JSFunction callback]); /// logEvent(event, event_data_and_custom_data, content_items, customer_event_alias, callback) /// Parameters @@ -754,10 +752,10 @@ class BranchJS { @JS('logEvent') external static void logEvent(String event, - [Object eventDataAndCustomData, - Object contentItems, + [JSAny eventDataAndCustomData, + JSArray contentItems, String customerEventAlias, - Function callback]); + JSFunction callback]); /// disableTracking(disableTracking) /// Parameters @@ -779,7 +777,7 @@ class BranchJS { /// information associated to them. You can change this behavior at any time, by calling the aforementioned function. /// The do-not-track mode state is persistent: it is saved for the user across browser sessions for the web site. @JS('disableTracking') - external static void disableTracking([bool disableTracking]); + external static void disableTracking(bool disableTracking); /// lastAttributedTouchData (number, callback) /// @@ -809,8 +807,8 @@ class BranchJS { /// '{}' /// ); @JS('lastAttributedTouchData') - external static void lastAttributedTouchData(attributionWindow, - [Function callback]); + external static void lastAttributedTouchData(JSAny? attributionWindow, + [JSFunction callback]); /// qrcode(data, callback) /// Parameters @@ -894,8 +892,8 @@ class BranchJS { /// ); @JS('qrCode') - external static void qrCode(Object qrCodeLinkData, Object qrCodeSettings, - Function(String? err, QrCodeData? qrCode) callback); + external static void qrCode( + JSAny qrCodeLinkData, JSAny qrCodeSettings, JSFunction callback); /// Sets the value of parameters required by Google Conversion APIs for DMA Compliance in EEA region. /// [eeaRegion] `true` If European regulations, including the DMA, apply to this user and conversion. From e7221018e6d156f779dea71faafcebd850606030 Mon Sep 17 00:00:00 2001 From: hunghd Date: Mon, 12 Aug 2024 14:31:08 +0700 Subject: [PATCH 02/10] update sdk constraints to support js_interop --- example/pubspec.lock | 34 +++++++++++++--------------------- example/pubspec.yaml | 2 +- pubspec.lock | 32 ++++++++++++-------------------- pubspec.yaml | 3 +-- 4 files changed, 27 insertions(+), 44 deletions(-) diff --git a/example/pubspec.lock b/example/pubspec.lock index b37ba1d0..91237be7 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -84,7 +84,7 @@ packages: path: ".." relative: true source: path - version: "8.0.4" + version: "8.1.0" flutter_lints: dependency: "direct dev" description: @@ -111,30 +111,22 @@ packages: url: "https://pub.dev" source: hosted version: "0.19.0" - js: - dependency: transitive - description: - name: js - sha256: c1b2e9b5ea78c45e1a0788d29606ba27dc5f71f019f32ca5140f61ef071838cf - url: "https://pub.dev" - source: hosted - version: "0.7.1" leak_tracker: dependency: transitive description: name: leak_tracker - sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a" + sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05" url: "https://pub.dev" source: hosted - version: "10.0.4" + version: "10.0.5" leak_tracker_flutter_testing: dependency: transitive description: name: leak_tracker_flutter_testing - sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8" + sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806" url: "https://pub.dev" source: hosted - version: "3.0.3" + version: "3.0.5" leak_tracker_testing: dependency: transitive description: @@ -163,18 +155,18 @@ packages: dependency: transitive description: name: material_color_utilities - sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" + sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec url: "https://pub.dev" source: hosted - version: "0.8.0" + version: "0.11.1" meta: dependency: transitive description: name: meta - sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136" + sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7 url: "https://pub.dev" source: hosted - version: "1.12.0" + version: "1.15.0" path: dependency: transitive description: @@ -248,10 +240,10 @@ packages: dependency: transitive description: name: test_api - sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f" + sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb" url: "https://pub.dev" source: hosted - version: "0.7.0" + version: "0.7.2" typed_data: dependency: transitive description: @@ -280,10 +272,10 @@ packages: dependency: transitive description: name: vm_service - sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec" + sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc url: "https://pub.dev" source: hosted - version: "14.2.1" + version: "14.2.4" sdks: dart: ">=3.3.0 <4.0.0" flutter: ">=3.18.0-18.0.pre.54" diff --git a/example/pubspec.yaml b/example/pubspec.yaml index cf66e382..7b2b0b3a 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -6,7 +6,7 @@ description: Demonstrates how to use the flutter_branch_sdk plugin. publish_to: 'none' # Remove this line if you wish to publish to pub.dev environment: - sdk: ">=2.18.0 <4.0.0" + sdk: ">=3.3.0 <4.0.0" # Dependencies specify other packages that your package needs in order to work. # To automatically upgrade your package dependencies to the latest versions diff --git a/pubspec.lock b/pubspec.lock index f3e8675f..6577114c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -72,30 +72,22 @@ packages: description: flutter source: sdk version: "0.0.0" - js: - dependency: "direct main" - description: - name: js - sha256: c1b2e9b5ea78c45e1a0788d29606ba27dc5f71f019f32ca5140f61ef071838cf - url: "https://pub.dev" - source: hosted - version: "0.7.1" leak_tracker: dependency: transitive description: name: leak_tracker - sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a" + sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05" url: "https://pub.dev" source: hosted - version: "10.0.4" + version: "10.0.5" leak_tracker_flutter_testing: dependency: transitive description: name: leak_tracker_flutter_testing - sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8" + sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806" url: "https://pub.dev" source: hosted - version: "3.0.3" + version: "3.0.5" leak_tracker_testing: dependency: transitive description: @@ -124,18 +116,18 @@ packages: dependency: transitive description: name: material_color_utilities - sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" + sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec url: "https://pub.dev" source: hosted - version: "0.8.0" + version: "0.11.1" meta: dependency: transitive description: name: meta - sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136" + sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7 url: "https://pub.dev" source: hosted - version: "1.12.0" + version: "1.15.0" path: dependency: transitive description: @@ -201,10 +193,10 @@ packages: dependency: transitive description: name: test_api - sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f" + sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb" url: "https://pub.dev" source: hosted - version: "0.7.0" + version: "0.7.2" vector_math: dependency: transitive description: @@ -217,10 +209,10 @@ packages: dependency: transitive description: name: vm_service - sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec" + sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc url: "https://pub.dev" source: hosted - version: "14.2.1" + version: "14.2.4" sdks: dart: ">=3.3.0 <4.0.0" flutter: ">=3.18.0-18.0.pre.54" diff --git a/pubspec.yaml b/pubspec.yaml index 2af82e14..0ab43d2e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ version: 8.1.0 homepage: https://github.com/RodrigoSMarques/flutter_branch_sdk environment: - sdk: ">=2.18.0 <4.0.0" + sdk: ">=3.3.0 <4.0.0" flutter: ">=3.3.0" dependencies: @@ -13,7 +13,6 @@ dependencies: flutter_web_plugins: sdk: flutter plugin_platform_interface: ^2.1.8 - js: ^0.7.1 dev_dependencies: flutter_test: From 118f6dbeb0f50dfd5d29b3f53e37e10c140a0f39 Mon Sep 17 00:00:00 2001 From: hunghd Date: Mon, 12 Aug 2024 14:31:53 +0700 Subject: [PATCH 03/10] udpate index.html to new template --- example/web/index.html | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/example/web/index.html b/example/web/index.html index 8fcb70f9..36bb8f22 100644 --- a/example/web/index.html +++ b/example/web/index.html @@ -32,34 +32,15 @@ Flutter Branch SDK Example - - - - - + From 1ca82bfac5782b54d0b872670aad9e0e99ed78b8 Mon Sep 17 00:00:00 2001 From: hunghd Date: Thu, 15 Aug 2024 14:05:20 +0700 Subject: [PATCH 04/10] correct data type --- lib/src/flutter_branch_sdk_web.dart | 4 ++-- lib/src/web/branch_js.dart | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/src/flutter_branch_sdk_web.dart b/lib/src/flutter_branch_sdk_web.dart index f9257b42..31236ef7 100644 --- a/lib/src/flutter_branch_sdk_web.dart +++ b/lib/src/flutter_branch_sdk_web.dart @@ -19,7 +19,7 @@ import 'web/branch_js.dart'; /// A workaround to deep-converting an object from JS to a Dart Object. dynamic _jsObjectToDartObject(data) => json.decode(jsonStringify(data)); -dynamic _dartObjectToJsObject(data) => jsonParse(json.encode(data)); +JSAny _dartObjectToJsObject(data) => jsonParse(json.encode(data)); /// A web implementation of the FlutterBranchSdkPlatform of the FlutterBranchSdk plugin. class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { @@ -234,7 +234,7 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { void trackContent( {required List buo, required BranchEvent branchEvent}) { - List contentItems = []; + List contentItems = []; for (var element in buo) { contentItems.add(_dartObjectToJsObject(element.toMap())); } diff --git a/lib/src/web/branch_js.dart b/lib/src/web/branch_js.dart index 42d03e01..e7144194 100644 --- a/lib/src/web/branch_js.dart +++ b/lib/src/web/branch_js.dart @@ -10,7 +10,7 @@ external String jsonStringify(JSAny obj); external JSAny jsonParse(String str); @JS('navigator.share') -external JSPromise navigatorShare(JSAny data); +external JSPromise navigatorShare(JSAny data); @JS('prompt') external JSAny browserPrompt(String message, [String data]); @@ -81,7 +81,7 @@ extension type BranchJS._(JSObject _) implements JSObject { /// /// branch.closeJourney(function(err) { console.log(err); }); @JS('closeJourney') - external static void closeJourney(JSFunction callback); + external static void closeJourney([JSFunction callback]); /// data(callback) /// Parameters @@ -96,7 +96,7 @@ extension type BranchJS._(JSObject _) implements JSObject { /// If the Branch session has already been initialized, the callback will return /// immediately, otherwise, it will return once Branch has been initialized. @JS('data') - external static void data(JSFunction callback); + external static void data([JSFunction callback]); /// deepview(data, options, callback) /// Parameters @@ -211,7 +211,7 @@ extension type BranchJS._(JSObject _) implements JSObject { /// Warning: For a referral program, you should not use unique awards for custom events and redeem /// pre-identify call. This can allow users to cheat the system. @JS('deepviewCta') - external static void deepviewCta(JSFunction errorCallback); + external static void deepviewCta([JSFunction errorCallback]); /// first(callback) /// Parameters @@ -226,7 +226,7 @@ extension type BranchJS._(JSObject _) implements JSObject { /// If the Branch session has already been initialized, the callback will return /// immediately, otherwise, it will return once Branch has been initialized. @JS('first') - external static void first(JSFunction callback); + external static void first([JSFunction callback]); // No documentation on reference // @JS('getCode') @@ -395,7 +395,7 @@ extension type BranchJS._(JSObject _) implements JSObject { /// "Error message" /// ); @JS('logout') - external static void logout(JSFunction callback); + external static void logout([JSFunction callback]); /// removeListener(listener) /// Parameters From 38b894c621e49cdcf36d294a1eb0dcf24ca855ae Mon Sep 17 00:00:00 2001 From: Rodrigo de Souza Marques Date: Tue, 27 Aug 2024 16:19:49 -0300 Subject: [PATCH 05/10] Release 8.2.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 8.2.0 ### ⚠️ BREAKING CHANGE * Minimum required Dart SDK version 3.3.0 (Flutter 3.19.0 - 15/02/2024) ### 🎉 Features * Issue #361: Migrate to dart:js_interop to support Webassamebly. Thanks @hnvn ### 🐛 Bug Fixes * Fix issue #368: "-118, Warning. Session initialization already happened" triggered in the listSession callback --- CHANGELOG.md | 10 ++ android/settings.gradle | 1 - .../FlutterBranchSdkPlugin.java | 94 +++++++++++-------- example/.metadata | 30 ++++++ example/ios/Podfile.lock | 4 +- example/pubspec.lock | 18 ++-- example/pubspec.yaml | 4 +- pubspec.lock | 4 +- pubspec.yaml | 3 +- 9 files changed, 109 insertions(+), 59 deletions(-) create mode 100644 example/.metadata diff --git a/CHANGELOG.md b/CHANGELOG.md index 82ccb54a..1203e751 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +## 8.2.0 +### ⚠️ BREAKING CHANGE +* Minimum required Dart SDK version 3.3.0 (Flutter 3.19.0 - 15/02/2024) + +### 🎉 Features +* Issue #361: Migrate to dart:js_interop to support Webassamebly. Thanks @hnvn + +### 🐛 Bug Fixes +* Fix issue #368: "-118, Warning. Session initialization already happened" triggered in the listSession callback + ## 8.1.0 ### 🔧 Native SDK Updates diff --git a/android/settings.gradle b/android/settings.gradle index 7d49f31d..989ef1a9 100644 --- a/android/settings.gradle +++ b/android/settings.gradle @@ -1,2 +1 @@ rootProject.name = 'flutter_branch_sdk' -android.defaults.buildfeatures.buildconfig=true diff --git a/android/src/main/java/br/com/rsmarques/flutter_branch_sdk/FlutterBranchSdkPlugin.java b/android/src/main/java/br/com/rsmarques/flutter_branch_sdk/FlutterBranchSdkPlugin.java index ea1a6bd1..a59b5083 100644 --- a/android/src/main/java/br/com/rsmarques/flutter_branch_sdk/FlutterBranchSdkPlugin.java +++ b/android/src/main/java/br/com/rsmarques/flutter_branch_sdk/FlutterBranchSdkPlugin.java @@ -10,6 +10,7 @@ import android.os.Looper; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import org.json.JSONException; import org.json.JSONObject; @@ -50,18 +51,63 @@ public class FlutterBranchSdkPlugin implements FlutterPlugin, MethodCallHandler, private static final String DEBUG_NAME = "FlutterBranchSDK"; private static final String MESSAGE_CHANNEL = "flutter_branch_sdk/message"; private static final String EVENT_CHANNEL = "flutter_branch_sdk/event"; - private Activity activity; - private Context context; - private ActivityPluginBinding activityPluginBinding; - private EventSink eventSink = null; - private Map sessionParams = null; - private BranchError initialError = null; private final FlutterBranchSdkHelper branchSdkHelper = new FlutterBranchSdkHelper(); private final JSONObject requestMetadata = new JSONObject(); private final JSONObject facebookParameters = new JSONObject(); private final JSONObject snapParameters = new JSONObject(); private final ArrayList preInstallParameters = new ArrayList(); private final ArrayList campaingParameters = new ArrayList(); + private Activity activity; + private Context context; + private ActivityPluginBinding activityPluginBinding; + private EventSink eventSink = null; + private Map sessionParams = null; + private BranchError initialError = null; + /** + * --------------------------------------------------------------------------------------------- + * Branch SDK Call Methods + * -------------------------------------------------------------------------------------------- + **/ + private final Branch.BranchReferralInitListener branchReferralInitListener = new + Branch.BranchReferralInitListener() { + @Override + public void onInitFinished(JSONObject params, BranchError error) { + LogUtils.debug(DEBUG_NAME, "triggered onInitFinished"); + if (error == null) { + LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener - params: " + params.toString()); + try { + sessionParams = branchSdkHelper.paramsToMap(params); + } catch (JSONException e) { + LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener - error to Map: " + e.getLocalizedMessage()); + return; + } + if (eventSink != null) { + eventSink.success(sessionParams); + sessionParams = null; + } + } else if (error.getErrorCode() == BranchError.ERR_BRANCH_ALREADY_INITIALIZED) { + LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener : " + error.getMessage()); + try { + sessionParams = branchSdkHelper.paramsToMap(Branch.getInstance().getLatestReferringParams()); + } catch (JSONException e) { + LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener - error to Map: " + e.getLocalizedMessage()); + return; + } + if (eventSink != null) { + eventSink.success(sessionParams); + sessionParams = null; + } + } else { + LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener - error: " + error); + if (eventSink != null) { + eventSink.error(String.valueOf(error.getErrorCode()), error.getMessage(), null); + initialError = null; + } else { + initialError = error; + } + } + } + }; private boolean isInitialized = false; /** @@ -74,6 +120,7 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { LogUtils.debug(DEBUG_NAME, "triggered onAttachedToEngine"); setupChannels(binding.getBinaryMessenger(), binding.getApplicationContext()); } + @Override public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { LogUtils.debug(DEBUG_NAME, "triggered onDetachedFromEngine"); @@ -342,41 +389,6 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result rawResult) { } } - /** - * --------------------------------------------------------------------------------------------- - * Branch SDK Call Methods - * -------------------------------------------------------------------------------------------- - **/ - private final Branch.BranchReferralInitListener branchReferralInitListener = new - Branch.BranchReferralInitListener() { - @Override - public void onInitFinished(JSONObject params, BranchError error) { - LogUtils.debug(DEBUG_NAME, "triggered onInitFinished"); - if (error == null) { - LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener - params: " + params.toString()); - - try { - sessionParams = branchSdkHelper.paramsToMap(params); - } catch (JSONException e) { - LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener - error to Map: " + e.getLocalizedMessage()); - return; - } - if (eventSink != null) { - eventSink.success(sessionParams); - sessionParams = null; - } - } else { - LogUtils.debug(DEBUG_NAME, "BranchReferralInitListener - error: " + error); - if (eventSink != null) { - eventSink.error(String.valueOf(error.getErrorCode()), error.getMessage(), null); - initialError = null; - } else { - initialError = error; - } - } - } - }; - private void setupBranch(MethodCall call, final Result result) { LogUtils.debug(DEBUG_NAME, "triggered setupBranch"); if (!(call.arguments instanceof Map)) { diff --git a/example/.metadata b/example/.metadata new file mode 100644 index 00000000..02e9c7bf --- /dev/null +++ b/example/.metadata @@ -0,0 +1,30 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: "80c2e84975bbd28ecf5f8d4bd4ca5a2490bfc819" + channel: "stable" + +project_type: app + +# Tracks metadata for the flutter migrate command +migration: + platforms: + - platform: root + create_revision: 80c2e84975bbd28ecf5f8d4bd4ca5a2490bfc819 + base_revision: 80c2e84975bbd28ecf5f8d4bd4ca5a2490bfc819 + - platform: web + create_revision: 80c2e84975bbd28ecf5f8d4bd4ca5a2490bfc819 + base_revision: 80c2e84975bbd28ecf5f8d4bd4ca5a2490bfc819 + + # User provided section + + # List of Local paths (relative to this file) that should be + # ignored by the migrate tool. + # + # Files that are not part of the templates will be ignored by default. + unmanaged_files: + - 'lib/main.dart' + - 'ios/Runner.xcodeproj/project.pbxproj' diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 09c110cf..40583893 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -1,7 +1,7 @@ PODS: - BranchSDK (3.6.0) - Flutter (1.0.0) - - flutter_branch_sdk (8.1.0): + - flutter_branch_sdk (8.2.0): - BranchSDK (~> 3.6.0) - Flutter @@ -22,7 +22,7 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: BranchSDK: 40ff1a13b1dc0729f79a9aae230418d25bf739a8 Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7 - flutter_branch_sdk: e5b94559acdc8596fe7b68598a706879602cb0e7 + flutter_branch_sdk: b3763b3d53a7cfbc87c51136bed66fcdc282bdf5 PODFILE CHECKSUM: 4e8f8b2be68aeea4c0d5beb6ff1e79fface1d048 diff --git a/example/pubspec.lock b/example/pubspec.lock index 91237be7..c9e16294 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -84,15 +84,15 @@ packages: path: ".." relative: true source: path - version: "8.1.0" + version: "8.2.0" flutter_lints: dependency: "direct dev" description: name: flutter_lints - sha256: "9e8c3858111da373efc5aa341de011d9bd23e2c5c5e0c62bccf32438e192d7b1" + sha256: "3f41d009ba7172d5ff9be5f6e6e6abb4300e263aab8866d2a0842ed2a70f8f0c" url: "https://pub.dev" source: hosted - version: "3.0.2" + version: "4.0.0" flutter_test: dependency: "direct dev" description: flutter @@ -139,10 +139,10 @@ packages: dependency: transitive description: name: lints - sha256: cbf8d4b858bb0134ef3ef87841abdf8d63bfc255c266b7bf6b39daa1085c4290 + sha256: "976c774dd944a42e83e2467f4cc670daef7eed6295b10b36ae8c85bcbf828235" url: "https://pub.dev" source: hosted - version: "3.0.0" + version: "4.0.0" matcher: dependency: transitive description: @@ -256,10 +256,10 @@ packages: dependency: "direct main" description: name: uuid - sha256: "814e9e88f21a176ae1359149021870e87f7cddaf633ab678a5d2b0bff7fd1ba8" + sha256: "83d37c7ad7aaf9aa8e275490669535c8080377cfa7a7004c24dfac53afffaa90" url: "https://pub.dev" source: hosted - version: "4.4.0" + version: "4.4.2" vector_math: dependency: transitive description: @@ -272,10 +272,10 @@ packages: dependency: transitive description: name: vm_service - sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc + sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d" url: "https://pub.dev" source: hosted - version: "14.2.4" + version: "14.2.5" sdks: dart: ">=3.3.0 <4.0.0" flutter: ">=3.18.0-18.0.pre.54" diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 7b2b0b3a..812e53fd 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -29,7 +29,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.8 - uuid: ^4.4.0 + uuid: ^4.4.2 intl: ^0.19.0 dev_dependencies: @@ -41,7 +41,7 @@ dev_dependencies: # activated in the `analysis_options.yaml` file located at the root of your # package. See that file for information about deactivating specific lint # rules and activating additional ones. - flutter_lints: ^3.0.1 + flutter_lints: ^4.0.0 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec diff --git a/pubspec.lock b/pubspec.lock index 6577114c..4ca6cb3f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -209,10 +209,10 @@ packages: dependency: transitive description: name: vm_service - sha256: f652077d0bdf60abe4c1f6377448e8655008eef28f128bc023f7b5e8dfeb48fc + sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d" url: "https://pub.dev" source: hosted - version: "14.2.4" + version: "14.2.5" sdks: dart: ">=3.3.0 <4.0.0" flutter: ">=3.18.0-18.0.pre.54" diff --git a/pubspec.yaml b/pubspec.yaml index 0ab43d2e..cde5bc38 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,11 +1,10 @@ name: flutter_branch_sdk description: Flutter Plugin for create deep link using Brach SDK (https://branch.io). This plugin provides a cross-platform (iOS, Android, Web). -version: 8.1.0 +version: 8.2.0 homepage: https://github.com/RodrigoSMarques/flutter_branch_sdk environment: sdk: ">=3.3.0 <4.0.0" - flutter: ">=3.3.0" dependencies: flutter: From ae6d884826f3140aa4b73115ab2b4d64fdb6b26e Mon Sep 17 00:00:00 2001 From: Rodrigo de Souza Marques Date: Tue, 27 Aug 2024 16:27:20 -0300 Subject: [PATCH 06/10] Update CHANGELOG.md --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1203e751..0d22d8fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,14 +5,13 @@ ### 🎉 Features * Issue #361: Migrate to dart:js_interop to support Webassamebly. Thanks @hnvn +## 8.1.1 ### 🐛 Bug Fixes * Fix issue #368: "-118, Warning. Session initialization already happened" triggered in the listSession callback ## 8.1.0 ### 🔧 Native SDK Updates - * Updated included iOS SDK to 3.6.0 - [iOS Version History](https://github.com/BranchMetrics/ios-branch-deep-linking-attribution/releases) - * Updated included Branch Android SDK to 5.12.2 - [Android Version History](https://github.com/BranchMetrics/android-branch-deep-linking-attribution/releases) ## 8.0.4 From 249c8df740057dad970fe292368ec2b253c159a9 Mon Sep 17 00:00:00 2001 From: Rodrigo de Souza Marques Date: Wed, 28 Aug 2024 06:48:51 -0300 Subject: [PATCH 07/10] Update build-web.sh --- tool/build-web.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tool/build-web.sh b/tool/build-web.sh index bd706302..96d3e50b 100644 --- a/tool/build-web.sh +++ b/tool/build-web.sh @@ -7,4 +7,5 @@ flutter config --no-analytics flutter pub get #flutter build web --source-maps #flutter build web --profile --source-maps --dart-define=Dart2jsOptimization=O0 -flutter build web \ No newline at end of file +#flutter build web +flutter build web --wasm \ No newline at end of file From f07ee9348ba76bbe76c146136242a27a4917f6f0 Mon Sep 17 00:00:00 2001 From: hunghd Date: Thu, 29 Aug 2024 00:00:26 +0700 Subject: [PATCH 08/10] correct qrcode data type --- lib/src/flutter_branch_sdk_web.dart | 2 +- lib/src/web/branch_js.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/flutter_branch_sdk_web.dart b/lib/src/flutter_branch_sdk_web.dart index 31236ef7..b5ecd111 100644 --- a/lib/src/flutter_branch_sdk_web.dart +++ b/lib/src/flutter_branch_sdk_web.dart @@ -429,7 +429,7 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { if (err == null) { if (qrCode != null) { responseCompleter.complete( - BranchResponse.success(result: qrCode.rawBuffer.toDart)); + BranchResponse.success(result: qrCode.rawBuffer.toDart.asUint8List())); } else { responseCompleter.complete(BranchResponse.error( errorCode: '-1', errorMessage: 'Qrcode generate error')); diff --git a/lib/src/web/branch_js.dart b/lib/src/web/branch_js.dart index e7144194..4ca60873 100644 --- a/lib/src/web/branch_js.dart +++ b/lib/src/web/branch_js.dart @@ -18,7 +18,7 @@ external JSAny browserPrompt(String message, [String data]); @JS() @anonymous extension type QrCodeData._(JSObject _) implements JSObject { - external JSUint8Array rawBuffer; + external JSArrayBuffer rawBuffer; external JSFunction base64(); } From 52d09fcd9efa07a62c997d15447ff3b8c1bae959 Mon Sep 17 00:00:00 2001 From: hunghd Date: Thu, 29 Aug 2024 07:42:43 +0700 Subject: [PATCH 09/10] correct code formating --- lib/src/flutter_branch_sdk_web.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/flutter_branch_sdk_web.dart b/lib/src/flutter_branch_sdk_web.dart index b5ecd111..f50ddeb3 100644 --- a/lib/src/flutter_branch_sdk_web.dart +++ b/lib/src/flutter_branch_sdk_web.dart @@ -428,8 +428,8 @@ class FlutterBranchSdkWeb extends FlutterBranchSdkPlatform { (JSAny? err, QrCodeData? qrCode) { if (err == null) { if (qrCode != null) { - responseCompleter.complete( - BranchResponse.success(result: qrCode.rawBuffer.toDart.asUint8List())); + responseCompleter.complete(BranchResponse.success( + result: qrCode.rawBuffer.toDart.asUint8List())); } else { responseCompleter.complete(BranchResponse.error( errorCode: '-1', errorMessage: 'Qrcode generate error')); From deec5897ecd7687a3f3a88c3d626966e8ce200ca Mon Sep 17 00:00:00 2001 From: Rodrigo de Souza Marques Date: Thu, 29 Aug 2024 19:51:39 -0300 Subject: [PATCH 10/10] Add Flutter version to pubscpec.yaml --- example/pubspec.lock | 2 +- pubspec.lock | 2 +- pubspec.yaml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/example/pubspec.lock b/example/pubspec.lock index c9e16294..951fd1c5 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -278,4 +278,4 @@ packages: version: "14.2.5" sdks: dart: ">=3.3.0 <4.0.0" - flutter: ">=3.18.0-18.0.pre.54" + flutter: ">=3.19.0" diff --git a/pubspec.lock b/pubspec.lock index 4ca6cb3f..8bc8ad43 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -215,4 +215,4 @@ packages: version: "14.2.5" sdks: dart: ">=3.3.0 <4.0.0" - flutter: ">=3.18.0-18.0.pre.54" + flutter: ">=3.19.0" diff --git a/pubspec.yaml b/pubspec.yaml index a83532e4..99eaa550 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,7 +5,7 @@ homepage: https://github.com/RodrigoSMarques/flutter_branch_sdk environment: sdk: ">=3.3.0 <4.0.0" - flutter: ">=3.3.0" + flutter: ">=3.19.0" dependencies: flutter: