Skip to content

Commit

Permalink
[firebase_dynamic_links] Add support of expanding from short links (f…
Browse files Browse the repository at this point in the history
  • Loading branch information
imzyy authored and mehmetf committed Nov 15, 2019
1 parent 4900968 commit b522b2b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 13 deletions.
4 changes: 4 additions & 0 deletions packages/firebase_dynamic_links/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.0+7

* Add `getDynamicLink` to support expanding from short links.

## 0.5.0+6

* Updated README instructions for contributing for consistency with other Flutterfire plugins.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public void onMethodCall(MethodCall call, Result result) {
builder.setLongLink(url);
buildShortDynamicLink(builder, call, createShortLinkListener(result));
break;
case "FirebaseDynamicLinks#getDynamicLink":
handleGetDynamicLink(result, Uri.parse((String) call.argument("url")));
break;
case "FirebaseDynamicLinks#getInitialLink":
handleGetInitialDynamicLink(result);
break;
Expand All @@ -114,17 +117,8 @@ private Map<String, Object> getMapFromPendingDynamicLinkData(
return dynamicLink;
}

private void handleGetInitialDynamicLink(final Result result) {
// If there's no activity, then there's no initial dynamic link.
if (registrar.activity() == null) {
result.success(null);
return;
}

FirebaseDynamicLinks.getInstance()
.getDynamicLink(registrar.activity().getIntent())
.addOnSuccessListener(
registrar.activity(),
private void addDynamicLinkListener(Task<PendingDynamicLinkData> task, final Result result) {
task.addOnSuccessListener(
new OnSuccessListener<PendingDynamicLinkData>() {
@Override
public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
Expand All @@ -138,7 +132,6 @@ public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
}
})
.addOnFailureListener(
registrar.activity(),
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Expand All @@ -147,6 +140,22 @@ public void onFailure(@NonNull Exception e) {
});
}

private void handleGetDynamicLink(final Result result, Uri uri) {
addDynamicLinkListener(FirebaseDynamicLinks.getInstance().getDynamicLink(uri), result);
}

private void handleGetInitialDynamicLink(final Result result) {
// If there's no activity, then there's no initial dynamic link.
if (registrar.activity() == null) {
result.success(null);
return;
}

addDynamicLinkListener(
FirebaseDynamicLinks.getInstance().getDynamicLink(registrar.activity().getIntent()),
result);
}

private OnCompleteListener<ShortDynamicLink> createShortLinkListener(final Result result) {
return new OnCompleteListener<ShortDynamicLink>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
} else {
result(dict);
}
} else if ([@"FirebaseDynamicLinks#getDynamicLink" isEqualToString:call.method]) {
NSURL *shortLink = [NSURL URLWithString:call.arguments[@"url"]];
FIRDynamicLinkUniversalLinkHandler completion =
^(FIRDynamicLink *_Nullable dynamicLink, NSError *_Nullable error) {
if (error) {
result(getFlutterError(error));
} else {
result(getDictionaryFromDynamicLink(dynamicLink));
}
};
[[FIRDynamicLinks dynamicLinks] handleUniversalLink:shortLink completion:completion];
} else {
result(FlutterMethodNotImplemented);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class FirebaseDynamicLinks {
return getPendingDynamicLinkDataFromMap(linkData);
}

Future<PendingDynamicLinkData> getDynamicLink(Uri url) async {
final Map<String, dynamic> linkData = await FirebaseDynamicLinks.channel
.invokeMapMethod<String, dynamic>('FirebaseDynamicLinks#getDynamicLink',
<String, dynamic>{'url': url.toString()});
return getPendingDynamicLinkDataFromMap(linkData);
}

PendingDynamicLinkData getPendingDynamicLinkDataFromMap(
Map<dynamic, dynamic> linkData) {
if (linkData == null) return null;
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase_dynamic_links/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: firebase_dynamic_links
description: Flutter plugin for Google Dynamic Links for Firebase, an app solution for creating
and handling links across multiple platforms.
version: 0.5.0+6
version: 0.5.0+7

author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_dynamic_links
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ void main() {
'minimumVersion': 'Version 12',
},
};
case 'FirebaseDynamicLinks#getDynamicLink':
return <dynamic, dynamic>{
'link': 'https://google.com',
};
default:
return null;
}
Expand All @@ -65,6 +69,21 @@ void main() {
]);
});

test('getDynamicLink', () async {
final Uri argument = Uri.parse('short-link');
final PendingDynamicLinkData data =
await FirebaseDynamicLinks.instance.getDynamicLink(argument);

expect(data.link.host, 'google.com');

expect(log, <Matcher>[
isMethodCall('FirebaseDynamicLinks#getDynamicLink',
arguments: <String, dynamic>{
'url': argument.toString(),
})
]);
});

group('$DynamicLinkParameters', () {
test('shortenUrl', () async {
final Uri url = Uri.parse('google.com');
Expand Down

0 comments on commit b522b2b

Please sign in to comment.