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

pull/225/head
Jonathan Daniels 3 years ago committed by jonathandaniels-vgv
parent 47b74e3a9d
commit 687b3715b5

@ -1,3 +1,4 @@
library share_repository;
export 'src/models/models.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,28 @@
import 'package:share_repository/share_repository.dart';
/// {@template share_repository}
/// Repository to facilitate sharing scores.
/// {@endtemplate}
class ShareRepository {
/// {@macro share_repository}
const ShareRepository();
// TODO(jonathandaniels-vgv): Change to prod url.
static const _shareUrl = 'https://ashehwkdkdjruejdnensjsjdne.web.app/#/';
/// Returns a url to share the [shareText] on the given [platform]. The
/// [shareText] must have the score embedded.
String shareScore({
required String shareText,
required SharePlatform platform,
}) {
final encodedUrl = Uri.encodeComponent(_shareUrl);
final encodedShareText = Uri.encodeComponent(shareText);
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,43 @@ import 'package:test/test.dart';
void main() {
group('ShareRepository', () {
test('can be instantiated', () {
expect(ShareRepository(), isNotNull);
late ShareRepository shareRepository;
setUp(() {
shareRepository = ShareRepository();
});
group('constructor', () {
test('creates new ShareRepository instance', () {
expect(ShareRepository(), isNotNull);
});
});
group('shareScore', () {
const shareText = 'hello world!';
test('returns the correct share url for twitter', () async {
expect(
shareRepository.shareScore(
shareText: shareText,
platform: SharePlatform.twitter,
),
equals(
'https://twitter.com/intent/tweet?url=https%3A%2F%2Fashehwkdkdjruejdnensjsjdne.web.app%2F%23%2F&text=hello%20world!',
),
);
});
test('returns the correct share url for facebook', () async {
expect(
shareRepository.shareScore(
shareText: shareText,
platform: SharePlatform.facebook,
),
equals(
'https://www.facebook.com/sharer.php?u=https%3A%2F%2Fashehwkdkdjruejdnensjsjdne.web.app%2F%23%2F&quote=hello%20world!',
),
);
});
});
});
}

Loading…
Cancel
Save