Skip to content

Commit

Permalink
feat: add branch deep link actions
Browse files Browse the repository at this point in the history
  • Loading branch information
vinothvino42 committed Dec 19, 2023
1 parent dee89c0 commit 8ee1c1d
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 3 deletions.
142 changes: 142 additions & 0 deletions lib/action/branch_link_action.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import 'package:ensemble/branch_link_manager.dart';
import 'package:ensemble/framework/action.dart';
import 'package:ensemble/framework/error_handling.dart';
import 'package:ensemble/framework/event.dart';
import 'package:ensemble/framework/scope.dart';
import 'package:ensemble/screen_controller.dart';
import 'package:ensemble/util/utils.dart';
import 'package:flutter/cupertino.dart';

class BranchLinkInitAction extends EnsembleAction {
BranchLinkInitAction({
super.initiator,
required this.onSuccess,
this.onError,
this.options,
});

Map<String, dynamic>? options;
EnsembleAction? onSuccess;
EnsembleAction? onError;

factory BranchLinkInitAction.fromMap({dynamic payload}) {
if (payload is Map) {
EnsembleAction? successAction =
EnsembleAction.fromYaml(payload['onSuccess']);
if (successAction == null) {
throw LanguageError(
"onSuccess() is required for branchLinkInit action");
}

return BranchLinkInitAction(
onSuccess: successAction,
onError: EnsembleAction.fromYaml(payload['onError']),
options: Utils.getMap(payload['options']),
);
}
throw LanguageError('Missing inputs for branchLinkInit');
}

@override
Future execute(BuildContext context, ScopeManager scopeManager) async {
try {
final useTestKey = Utils.getBool(options?['useTestKey'], fallback: false);
final enableLog = Utils.getBool(options?['enableLog'], fallback: false);
final disableTrack =
Utils.getBool(options?['disableTrack'], fallback: false);

await BranchLinkManager().init(
useTestKey: useTestKey,
enableLog: enableLog,
disableTrack: disableTrack);
return ScreenController()
.executeAction(context, onSuccess!, event: EnsembleEvent(initiator));
} catch (e) {
return ScreenController().executeAction(context, onError!,
event: EnsembleEvent(initiator,
error: 'Unable to initialize the Branch SDK: $e'));
}
}
}

class BranchLinkValidateAction extends EnsembleAction {
BranchLinkValidateAction({
super.initiator,
required this.onSuccess,
this.onError,
});

EnsembleAction? onSuccess;
EnsembleAction? onError;

factory BranchLinkValidateAction.fromMap({dynamic payload}) {
if (payload is Map) {
EnsembleAction? successAction =
EnsembleAction.fromYaml(payload['onSuccess']);
if (successAction == null) {
throw LanguageError(
"onSuccess() is required for branchLinkValidate action");
}

return BranchLinkValidateAction(
onSuccess: successAction,
onError: EnsembleAction.fromYaml(payload['onError']),
);
}
throw LanguageError('Missing inputs for branchLinkValidate');
}

@override
Future execute(BuildContext context, ScopeManager scopeManager) async {
try {
BranchLinkManager().validate();
return ScreenController()
.executeAction(context, onSuccess!, event: EnsembleEvent(initiator));
} catch (e) {
return ScreenController().executeAction(context, onError!,
event: EnsembleEvent(initiator,
error: 'Unable to validate the Branch SDK: $e'));
}
}
}

class BranchLinkCreateDeeplinkAction extends EnsembleAction {
BranchLinkCreateDeeplinkAction({
super.initiator,
required this.onSuccess,
this.onError,
});

EnsembleAction? onSuccess;
EnsembleAction? onError;

factory BranchLinkCreateDeeplinkAction.fromMap({dynamic payload}) {
if (payload is Map) {
EnsembleAction? successAction =
EnsembleAction.fromYaml(payload['onSuccess']);
if (successAction == null) {
throw LanguageError(
'onSuccess() is required for branchLinkCreateDeepLink action');
}

return BranchLinkCreateDeeplinkAction(
onSuccess: successAction,
onError: EnsembleAction.fromYaml(payload['onError']),
);
}
throw LanguageError('Missing inputs for branchLinkCreateDeepLink');
}

@override
Future execute(BuildContext context, ScopeManager scopeManager) async {
try {
BranchLinkManager().validate();
return ScreenController()
.executeAction(context, onSuccess!, event: EnsembleEvent(initiator));
} catch (e) {
return ScreenController().executeAction(context, onError!,
event: EnsembleEvent(initiator,
error: 'Unable to create deeplink using Branch SDK: $e'));
}
}
}
25 changes: 25 additions & 0 deletions lib/branch_link_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter_branch_sdk/flutter_branch_sdk.dart';

class BranchLinkManager {
static final BranchLinkManager _instance = BranchLinkManager._internal();

BranchLinkManager._internal();

factory BranchLinkManager() {
return _instance;
}

Future<void> init(
{bool useTestKey = false,
bool enableLog = false,
bool disableTrack = false}) async {
FlutterBranchSdk.init(
useTestKey: useTestKey,
enableLogging: enableLog,
disableTracking: disableTrack);
}

void validate() {
FlutterBranchSdk.validateSDKIntegration();
}
}
13 changes: 12 additions & 1 deletion lib/framework/action.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:app_settings/app_settings.dart';
import 'package:ensemble/action/badge_action.dart';
import 'package:ensemble/action/bottom_modal_action.dart';
import 'package:ensemble/action/branch_link_action.dart';
import 'package:ensemble/action/call_external_method.dart';
import 'package:ensemble/action/haptic_action.dart';
import 'package:ensemble/action/call_native_method.dart';
Expand Down Expand Up @@ -76,7 +77,8 @@ class ShowDialogAction extends EnsembleAction {
options: Utils.getMap(payload['options']),
onDialogDismiss: payload['onDialogDismiss'] == null
? null
: EnsembleAction.fromYaml(Utils.maybeYamlMap(payload['onDialogDismiss'])),
: EnsembleAction.fromYaml(
Utils.maybeYamlMap(payload['onDialogDismiss'])),
);
}
}
Expand Down Expand Up @@ -937,6 +939,9 @@ enum ActionType {
callExternalMethod,
invokeHaptic,
callNativeMethod,
branchLinkInit,
branchLinkValidate,
branchLinkCreateDeeplink,
}

enum ToastType { success, error, warning, info }
Expand Down Expand Up @@ -1084,6 +1089,12 @@ abstract class EnsembleAction {
return ClearKeychain.fromYaml(payload: payload);
} else if (actionType == ActionType.invokeHaptic) {
return HapticAction.from(payload);
} else if (actionType == ActionType.branchLinkInit) {
return BranchLinkInitAction.fromMap(payload: payload);
} else if (actionType == ActionType.branchLinkValidate) {
return BranchLinkValidateAction.fromMap(payload: payload);
} else if (actionType == ActionType.branchLinkCreateDeeplink) {
return BranchLinkCreateDeeplinkAction.fromMap(payload: payload);
}

throw LanguageError("Invalid action.",
Expand Down
5 changes: 3 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ dependencies:
app_links: ^3.4.3
share_plus: ^6.2.0
rate_my_app: ^2.0.0
table_calendar:
table_calendar:
git:
url: https://github.com/snehmehta/table_calendar
ref: master

flutter_app_badger: ^1.5.0
webview_flutter_wkwebview: ^3.9.2
webview_flutter_android: ^3.12.0
Expand All @@ -126,6 +126,7 @@ dependencies:
url: https://github.com/AyushmanG26/receive_sharing_intent.git
ref: abedc3d6d5b80df396e9c300fd1a052cbab08827
change_case: ^1.1.0
flutter_branch_sdk: ^7.0.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 8ee1c1d

Please sign in to comment.