mirror of https://github.com/flutter/pinball.git
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
parent
47b74e3a9d
commit
36c1afe80d
@ -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 @@
|
|||||||
|
export 'share_platform.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"e=$encodedShareText';
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue