Skip to content

Commit

Permalink
feat: adds share social launch url
Browse files Browse the repository at this point in the history
  • Loading branch information
AyadLaouissi committed Apr 24, 2024
1 parent 438b5ff commit 16d7d69
Show file tree
Hide file tree
Showing 12 changed files with 518 additions and 56 deletions.
1 change: 1 addition & 0 deletions lib/app/view/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class App extends StatelessWidget {
providers: [
Provider.value(value: apiClient.crosswordResource),
Provider.value(value: apiClient.leaderboardResource),
Provider.value(value: apiClient.shareResource),
Provider.value(value: apiClient.hintResource),
Provider.value(value: user),
Provider.value(value: crosswordRepository),
Expand Down
6 changes: 6 additions & 0 deletions lib/share/view/share_score_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:api_client/api_client.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:io_crossword/l10n/l10n.dart';
import 'package:io_crossword/player/player.dart';
import 'package:io_crossword/share/share.dart';
Expand All @@ -12,10 +14,14 @@ class ShareScorePage extends StatelessWidget {
context: context,
builder: (context) {
final l10n = context.l10n;
final shareResource = context.read<ShareResource>();

return ShareDialog(
title: l10n.shareYourScore,
content: const ShareScorePage(),
facebookShareUrl: shareResource.facebookShareBaseUrl(),
linkedInShareUrl: shareResource.linkedinShareBaseUrl(),
twitterShareUrl: shareResource.twitterShareBaseUrl(),
);
},
);
Expand Down
6 changes: 6 additions & 0 deletions lib/share/view/share_word_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:api_client/api_client.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:game_domain/game_domain.dart';
import 'package:io_crossword/l10n/l10n.dart';
import 'package:io_crossword/share/share.dart';
Expand All @@ -14,12 +16,16 @@ class ShareWordPage extends StatelessWidget {
context: context,
builder: (context) {
final l10n = context.l10n;
final shareResource = context.read<ShareResource>();

return ShareDialog(
title: l10n.shareThisWord,
content: ShareWordPage(
word: word,
),
facebookShareUrl: shareResource.facebookShareBaseUrl(),
linkedInShareUrl: shareResource.linkedinShareBaseUrl(),
twitterShareUrl: shareResource.twitterShareBaseUrl(),
);
},
);
Expand Down
31 changes: 20 additions & 11 deletions lib/share/widgets/share_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import 'package:flutter/material.dart';
import 'package:io_crossword/extensions/context_ext.dart';
import 'package:io_crossword/l10n/l10n.dart';
import 'package:io_crossword_ui/io_crossword_ui.dart';

class ShareDialog extends StatelessWidget {
const ShareDialog({
required this.title,
required this.content,
required this.facebookShareUrl,
required this.twitterShareUrl,
required this.linkedInShareUrl,
super.key,
});

final Widget content;
final String title;
final String facebookShareUrl;
final String twitterShareUrl;
final String linkedInShareUrl;

@override
Widget build(BuildContext context) {
Expand All @@ -35,24 +42,26 @@ class ShareDialog extends StatelessWidget {
l10n.shareOn,
),
const SizedBox(height: IoCrosswordSpacing.xlgsm),
const Row(
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
onPressed: null,
icon: Icon(IoIcons.linkedin),
onPressed: () {
context.launchUrl(linkedInShareUrl);
},
icon: const Icon(IoIcons.linkedin),
),
IconButton(
onPressed: null,
icon: Icon(IoIcons.instagram),
onPressed: () {
context.launchUrl(twitterShareUrl);
},
icon: const Icon(IoIcons.twitter),
),
IconButton(
onPressed: null,
icon: Icon(IoIcons.twitter),
),
IconButton(
onPressed: null,
icon: Icon(IoIcons.facebook),
onPressed: () {
context.launchUrl(facebookShareUrl);
},
icon: const Icon(IoIcons.facebook),
),
],
),
Expand Down
8 changes: 8 additions & 0 deletions packages/api_client/lib/src/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class ApiClient {
late final CrosswordResource crosswordResource =
CrosswordResource(apiClient: this);

/// {@macro share_resource}
late final ShareResource shareResource = ShareResource(apiClient: this);

/// {@macro hint_resource}
late final HintResource hintResource = HintResource(apiClient: this);

Expand Down Expand Up @@ -100,6 +103,11 @@ class ApiClient {
});
}

/// Returns the base url.
String get baseUrl {
return _base.toString();
}

/// Returns the score share url for the score for the specified [userId].
String shareScoreUrl(String userId) {
return _base.replace(
Expand Down
27 changes: 25 additions & 2 deletions packages/api_client/lib/src/resources/share_resource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class ShareResource {

final ApiClient _apiClient;

final _tweetContent = 'Check out my score at IOCrossword #GoogleIO!';
final _tweetScoreContent = 'Check out my score at IOCrossword #GoogleIO!';

final _tweetContent = 'Check out IOCrossword #GoogleIO!';

String _twitterShareUrl(String text) =>
'https://twitter.com/intent/tweet?text=$text';
Expand Down Expand Up @@ -40,7 +42,7 @@ class ShareResource {
String twitterShareScoreUrl(String userId) {
final shareUrl = _apiClient.shareScoreUrl(userId);
final content = [
_tweetContent,
_tweetScoreContent,
shareUrl,
];
return _twitterShareUrl(_encode(content));
Expand Down Expand Up @@ -85,4 +87,25 @@ class ShareResource {
final shareUrl = _apiClient.shareWordUrl(sectionId, wordId);
return _linkedinShareUrl(shareUrl);
}

/// Returns the url to share a facebook post.
String facebookShareBaseUrl() {
return _facebookShareUrl(_apiClient.baseUrl);
}

/// Returns the url to share a twitter post.
String twitterShareBaseUrl() {
final shareUrl = _apiClient.baseUrl;
final content = [
_tweetContent,
shareUrl,
];
return _twitterShareUrl(_encode(content));
}

/// Returns the url to share a linkedin post.
String linkedinShareBaseUrl() {
final shareUrl = _apiClient.baseUrl;
return _linkedinShareUrl(shareUrl);
}
}
9 changes: 9 additions & 0 deletions packages/api_client/test/src/api_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ void main() {
});
});

group('shareScoreUrl', () {
test('returns the correct url', () {
expect(
subject.baseUrl,
equals('http://baseurl.com'),
);
});
});

group('shareScoreUrl', () {
test('returns the correct url', () {
expect(
Expand Down
33 changes: 33 additions & 0 deletions packages/api_client/test/src/resources/share_resource_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,38 @@ void main() {
contains('wordUrl'),
);
});

test('facebookShareBaseUrl returns the correct url', () {
when(() => apiClient.baseUrl).thenReturn('https://baseurl');

expect(
resource.facebookShareBaseUrl(),
equals('https://www.facebook.com/sharer.php?u=https://baseurl'),
);
});

test('twitterShareBaseUrl returns the correct url', () {
when(() => apiClient.baseUrl).thenReturn('https://baseurl');

expect(
resource.twitterShareBaseUrl(),
equals(
'https://twitter.com/intent/tweet?text=Check%20out'
'%20IOCrossword%20%23GoogleIO!%0ahttps://baseurl',
),
);
});

test('linkedinShareBaseUrl returns the correct url', () {
when(() => apiClient.baseUrl).thenReturn('https://baseurl');

expect(
resource.linkedinShareBaseUrl(),
equals(
'https://www.linkedin.com/sharing/share-offsite/'
'?url=https://baseurl',
),
);
});
});
}
21 changes: 21 additions & 0 deletions test/helpers/pump_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ class _MockUser extends Mock implements User {
String get id => '';
}

class _MockShareResource extends Mock implements ShareResource {
@override
String facebookShareBaseUrl() {
return 'https://facebook';
}

@override
String linkedinShareBaseUrl() {
return 'https://linkedin';
}

@override
String twitterShareBaseUrl() {
return 'https://twitter';
}
}

extension PumpApp on WidgetTester {
Future<void> pumpApp(
Widget widget, {
Expand All @@ -47,6 +64,7 @@ extension PumpApp on WidgetTester {
LeaderboardRepository? leaderboardRepository,
CrosswordResource? crosswordResource,
LeaderboardResource? leaderboardResource,
ShareResource? shareResource,
HintResource? hintResource,
CrosswordBloc? crosswordBloc,
PlayerBloc? playerBloc,
Expand Down Expand Up @@ -77,6 +95,9 @@ extension PumpApp on WidgetTester {
Provider.value(
value: crosswordResource ?? mockedCrosswordResource,
),
Provider.value(
value: shareResource ?? _MockShareResource(),
),
Provider.value(
value: crosswordRepository ?? mockedCrosswordRepository,
),
Expand Down
Loading

0 comments on commit 16d7d69

Please sign in to comment.