feat(share_repository): added ability to fetch share url for facebook and twitter (#225)

* feat(share_repository): added ability to fetch share url for facebook and twitter

* doc(share_repository): mention to open share links with url_launcher

* chore: addressed review feedback

* chore: update repository to take in app url

* test: changed test url to a fake one

* chore: rename method signature

* chore: renamed variables for new method name

Co-authored-by: RuiAlonso <rui.alonso@verygood.ventures>
pull/237/head
jonathandaniels-vgv 3 years ago committed by GitHub
parent 47b74e3a9d
commit 36c1afe80d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,3 +1,4 @@
library share_repository; library share_repository;
export 'src/models/models.dart';
export 'src/share_repository.dart'; export 'src/share_repository.dart';

@ -0,0 +1,8 @@
/// The platform that is being used to share a score.
enum SharePlatform {
/// Twitter platform.
twitter,
/// Facebook platform.
facebook,
}

@ -1,7 +1,30 @@
import 'package:share_repository/share_repository.dart';
/// {@template share_repository} /// {@template share_repository}
/// Repository to facilitate sharing scores. /// Repository to facilitate sharing scores.
/// {@endtemplate} /// {@endtemplate}
class ShareRepository { class ShareRepository {
/// {@macro share_repository} /// {@macro share_repository}
const ShareRepository(); const ShareRepository({
required String appUrl,
}) : _appUrl = appUrl;
final String _appUrl;
/// Returns a url to share the [value] on the given [platform].
///
/// The returned url can be opened using the [url_launcher](https://pub.dev/packages/url_launcher) package.
String shareText({
required String value,
required SharePlatform platform,
}) {
final encodedUrl = Uri.encodeComponent(_appUrl);
final encodedShareText = Uri.encodeComponent(value);
switch (platform) {
case SharePlatform.twitter:
return 'https://twitter.com/intent/tweet?url=$encodedUrl&text=$encodedShareText';
case SharePlatform.facebook:
return 'https://www.facebook.com/sharer.php?u=$encodedUrl&quote=$encodedShareText';
}
}
} }

@ -4,8 +4,38 @@ import 'package:test/test.dart';
void main() { void main() {
group('ShareRepository', () { group('ShareRepository', () {
const appUrl = 'https://fakeurl.com/';
late ShareRepository shareRepository;
setUp(() {
shareRepository = ShareRepository(appUrl: appUrl);
});
test('can be instantiated', () { test('can be instantiated', () {
expect(ShareRepository(), isNotNull); expect(ShareRepository(appUrl: appUrl), isNotNull);
});
group('shareText', () {
const value = 'hello world!';
test('returns the correct share url for twitter', () async {
const shareTextUrl =
'https://twitter.com/intent/tweet?url=https%3A%2F%2Ffakeurl.com%2F&text=hello%20world!';
final shareTextResult = shareRepository.shareText(
value: value,
platform: SharePlatform.twitter,
);
expect(shareTextResult, equals(shareTextUrl));
});
test('returns the correct share url for facebook', () async {
const shareTextUrl =
'https://www.facebook.com/sharer.php?u=https%3A%2F%2Ffakeurl.com%2F&quote=hello%20world!';
final shareTextResult = shareRepository.shareText(
value: value,
platform: SharePlatform.facebook,
);
expect(shareTextResult, equals(shareTextUrl));
});
}); });
}); });
} }

Loading…
Cancel
Save