Skip to content

Commit

Permalink
feat: add create branch link with share sheet action
Browse files Browse the repository at this point in the history
  • Loading branch information
vinothvino42 committed Dec 19, 2023
1 parent 749a66e commit 4eadad1
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 8 deletions.
89 changes: 89 additions & 0 deletions lib/action/branch_link_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,92 @@ class BranchLinkCreateDeeplinkAction extends EnsembleAction {
}
}
}

class BranchLinkCreateDeeplinkWithShareSheetAction extends EnsembleAction {
BranchLinkCreateDeeplinkWithShareSheetAction({
super.initiator,
required this.onSuccess,
required this.messageText,
this.messageTitle,
this.sharingTitle,
this.onError,
this.universalProps,
this.linkProps,
});

EnsembleAction? onSuccess;
EnsembleAction? onError;
String messageText;
String? messageTitle;
String? sharingTitle;
Map<String, dynamic>? universalProps;
Map<String, dynamic>? linkProps;

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

final universalPropsData = Utils.getMap(payload['universalProps']);
final linkPropsData = Utils.getMap(payload['linkProps']);

if (universalPropsData == null) {
throw LanguageError(
'universalProps is required for branchLinkCreateDeepLinkWithShareSheet action');
}

if (linkPropsData == null) {
throw LanguageError(
'linkProps is required for branchLinkCreateDeepLinkWithShareSheet action');
}

final messageTextData = Utils.optionalString(payload['messageText']);
if (messageTextData == null) {
throw LanguageError(
'messageText is required for branchLinkCreateDeepLinkWithShareSheet action');
}

return BranchLinkCreateDeeplinkWithShareSheetAction(
onSuccess: successAction,
onError: EnsembleAction.fromYaml(payload['onError']),
messageText: messageTextData,
messageTitle: payload['messageTitle'],
sharingTitle: payload['sharingTitle'],
universalProps: universalPropsData,
linkProps: linkPropsData,
);
}
throw LanguageError(
'Missing inputs for branchLinkCreateDeepLinkWithShareSheet');
}

@override
Future execute(BuildContext context, ScopeManager scopeManager) async {
try {
final response = await BranchLinkManager().createDeeplinkWithShareSheet(
messageText: messageText,
messageTitle: Utils.getString(messageTitle, fallback: ''),
sharingTitle: Utils.getString(sharingTitle, fallback: ''),
universalProps: universalProps!,
linkProps: linkProps!);
if (response != null && response.success) {
return ScreenController().executeAction(context, onSuccess!,
event: EnsembleEvent(initiator, data: {'result': response.result}));
} else {
return ScreenController().executeAction(context, onError!,
event: EnsembleEvent(initiator,
error:
'Branch SDK: Unable to create deeplink - Reason: ${response?.errorMessage}'));
}
} catch (e) {
return ScreenController().executeAction(context, onError!,
event: EnsembleEvent(initiator,
error: 'Branch SDK: Unable to create deeplink - Reason: $e'));
}
}
}
47 changes: 39 additions & 8 deletions lib/branch_link_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,54 @@ class BranchLinkManager {
Future<BranchResponse<dynamic>?> createDeepLink(
Map<String, dynamic> universalProps,
Map<String, dynamic> linkProps) async {
final buo = _getUniversalObject(universalProps);
final blp = _getLinkProperties(linkProps);

try {
BranchResponse response =
await FlutterBranchSdk.getShortUrl(buo: buo, linkProperties: blp);
return response;
} catch (e) {
return null;
}
}

Future<BranchResponse> createDeeplinkWithShareSheet({
required String messageText,
required Map<String, dynamic> universalProps,
required Map<String, dynamic> linkProps,
String messageTitle = '',
String sharingTitle = '',
}) async {
final buo = _getUniversalObject(universalProps);
final blp = _getLinkProperties(linkProps);

BranchResponse response = await FlutterBranchSdk.showShareSheet(
buo: buo,
linkProperties: blp,
messageText: messageText,
androidMessageTitle: messageTitle,
androidSharingTitle: sharingTitle,
);

return response;
}

BranchUniversalObject _getUniversalObject(
Map<String, dynamic> universalProps) {
final contentMetadata = BranchContentMetaData();
contentMetadata.contentSchema = universalProps['contentSchema'];

final buo = BranchUniversalObject(
return BranchUniversalObject(
canonicalIdentifier: universalProps['id'],
title: universalProps['title'],
imageUrl: universalProps['imageUrl'],
contentDescription: universalProps['title'],
contentMetadata: contentMetadata,
);
}

BranchLinkProperties _getLinkProperties(Map<String, dynamic> linkProps) {
final blp = BranchLinkProperties(
channel: linkProps['channel'],
feature: linkProps['feature'],
Expand All @@ -52,12 +89,6 @@ class BranchLinkManager {
});
}

try {
BranchResponse response =
await FlutterBranchSdk.getShortUrl(buo: buo, linkProperties: blp);
return response;
} catch (e) {
return null;
}
return blp;
}
}
4 changes: 4 additions & 0 deletions lib/framework/action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,7 @@ enum ActionType {
branchLinkInit,
branchLinkValidate,
branchLinkCreateDeeplink,
branchLinkCreateDeeplinkWithShareSheet,
}

enum ToastType { success, error, warning, info }
Expand Down Expand Up @@ -1095,6 +1096,9 @@ abstract class EnsembleAction {
return BranchLinkValidateAction.fromMap(payload: payload);
} else if (actionType == ActionType.branchLinkCreateDeeplink) {
return BranchLinkCreateDeeplinkAction.fromMap(payload: payload);
} else if (actionType == ActionType.branchLinkCreateDeeplink) {
return BranchLinkCreateDeeplinkWithShareSheetAction.fromMap(
payload: payload);
}

throw LanguageError("Invalid action.",
Expand Down

0 comments on commit 4eadad1

Please sign in to comment.