mirror of https://github.com/flutter/pinball.git
feat: social networks `ShareDisplay` (#406)
* feat: share screen and tests * chore: assets gen * feat: added share repository * feature: injecting sharerepository * feat: share url * feat: state for showing ShareDisplay at Backbox * test: tests for sharing links * test: backbox tests * Update lib/game/components/backbox/displays/share_display.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * Update lib/game/components/backbox/displays/share_display.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * Update lib/game/components/backbox/displays/share_display.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * Update test/game/components/backbox/backbox_test.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * Update test/game/components/backbox/displays/share_display_test.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * Update test/game/components/backbox/displays/share_display_test.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * Update test/game/components/backbox/backbox_test.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * Update test/game/components/backbox/backbox_test.dart Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> * refactor: moved down social networks icons * refactor: url and text for share Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com>pull/414/head
parent
c3094850fd
commit
80da24b43c
@ -0,0 +1,189 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame/input.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pinball/l10n/l10n.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
import 'package:pinball_ui/pinball_ui.dart';
|
||||||
|
import 'package:share_repository/share_repository.dart';
|
||||||
|
|
||||||
|
/// Signature for the callback called when the user tries to share their score
|
||||||
|
/// on the [ShareDisplay].
|
||||||
|
typedef OnSocialShareTap = void Function(SharePlatform);
|
||||||
|
|
||||||
|
final _descriptionTextPaint = TextPaint(
|
||||||
|
style: const TextStyle(
|
||||||
|
fontSize: 1.6,
|
||||||
|
color: PinballColors.white,
|
||||||
|
fontFamily: PinballFonts.pixeloidSans,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
/// {@template share_display}
|
||||||
|
/// Display that allows users to share their score to social networks.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class ShareDisplay extends Component with HasGameRef {
|
||||||
|
/// {@macro share_display}
|
||||||
|
ShareDisplay({
|
||||||
|
OnSocialShareTap? onShare,
|
||||||
|
}) : super(
|
||||||
|
children: [
|
||||||
|
_ShareInstructionsComponent(
|
||||||
|
onShare: onShare,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ShareInstructionsComponent extends PositionComponent with HasGameRef {
|
||||||
|
_ShareInstructionsComponent({
|
||||||
|
OnSocialShareTap? onShare,
|
||||||
|
}) : super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2(0, -25),
|
||||||
|
children: [
|
||||||
|
_DescriptionComponent(),
|
||||||
|
_SocialNetworksComponent(
|
||||||
|
onShare: onShare,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
class _DescriptionComponent extends PositionComponent with HasGameRef {
|
||||||
|
_DescriptionComponent()
|
||||||
|
: super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2.zero(),
|
||||||
|
children: [
|
||||||
|
_LetEveryoneTextComponent(),
|
||||||
|
_SharingYourScoreTextComponent(),
|
||||||
|
_SocialMediaTextComponent(),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
class _LetEveryoneTextComponent extends TextComponent with HasGameRef {
|
||||||
|
_LetEveryoneTextComponent()
|
||||||
|
: super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2.zero(),
|
||||||
|
textRenderer: _descriptionTextPaint,
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
text = readProvider<AppLocalizations>().letEveryone;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _SharingYourScoreTextComponent extends TextComponent with HasGameRef {
|
||||||
|
_SharingYourScoreTextComponent()
|
||||||
|
: super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2(0, 2.5),
|
||||||
|
textRenderer: _descriptionTextPaint,
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
text = readProvider<AppLocalizations>().bySharingYourScore;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _SocialMediaTextComponent extends TextComponent with HasGameRef {
|
||||||
|
_SocialMediaTextComponent()
|
||||||
|
: super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2(0, 5),
|
||||||
|
textRenderer: _descriptionTextPaint,
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
text = readProvider<AppLocalizations>().socialMediaAccount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _SocialNetworksComponent extends PositionComponent with HasGameRef {
|
||||||
|
_SocialNetworksComponent({
|
||||||
|
OnSocialShareTap? onShare,
|
||||||
|
}) : super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2(0, 12),
|
||||||
|
children: [
|
||||||
|
FacebookButtonComponent(onTap: onShare),
|
||||||
|
TwitterButtonComponent(onTap: onShare),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template facebook_button_component}
|
||||||
|
/// Button for sharing on Facebook.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class FacebookButtonComponent extends SpriteComponent
|
||||||
|
with HasGameRef, Tappable {
|
||||||
|
/// {@macro facebook_button_component}
|
||||||
|
FacebookButtonComponent({
|
||||||
|
OnSocialShareTap? onTap,
|
||||||
|
}) : _onTap = onTap,
|
||||||
|
super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2(-5, 0),
|
||||||
|
);
|
||||||
|
|
||||||
|
final OnSocialShareTap? _onTap;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool onTapDown(TapDownInfo info) {
|
||||||
|
_onTap?.call(SharePlatform.facebook);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
final sprite = Sprite(
|
||||||
|
gameRef.images.fromCache(Assets.images.backbox.button.facebook.keyName),
|
||||||
|
);
|
||||||
|
this.sprite = sprite;
|
||||||
|
size = sprite.originalSize / 25;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// {@template twitter_button_component}
|
||||||
|
/// Button for sharing on Twitter.
|
||||||
|
/// {@endtemplate}
|
||||||
|
class TwitterButtonComponent extends SpriteComponent with HasGameRef, Tappable {
|
||||||
|
/// {@macro twitter_button_component}
|
||||||
|
TwitterButtonComponent({
|
||||||
|
OnSocialShareTap? onTap,
|
||||||
|
}) : _onTap = onTap,
|
||||||
|
super(
|
||||||
|
anchor: Anchor.center,
|
||||||
|
position: Vector2(5, 0),
|
||||||
|
);
|
||||||
|
|
||||||
|
final OnSocialShareTap? _onTap;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool onTapDown(TapDownInfo info) {
|
||||||
|
_onTap?.call(SharePlatform.twitter);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
final sprite = Sprite(
|
||||||
|
gameRef.images.fromCache(Assets.images.backbox.button.twitter.keyName),
|
||||||
|
);
|
||||||
|
this.sprite = sprite;
|
||||||
|
size = sprite.originalSize / 25;
|
||||||
|
}
|
||||||
|
}
|
After Width: | Height: | Size: 3.1 KiB |
After Width: | Height: | Size: 3.5 KiB |
@ -0,0 +1,112 @@
|
|||||||
|
// ignore_for_file: cascade_invocations
|
||||||
|
|
||||||
|
import 'package:flame/game.dart';
|
||||||
|
import 'package:flame/input.dart';
|
||||||
|
import 'package:flame_bloc/flame_bloc.dart';
|
||||||
|
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||||
|
import 'package:flame_test/flame_test.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:pinball/game/bloc/game_bloc.dart';
|
||||||
|
import 'package:pinball/game/components/backbox/displays/share_display.dart';
|
||||||
|
import 'package:pinball/l10n/l10n.dart';
|
||||||
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
|
class _TestGame extends Forge2DGame with HasTappables {
|
||||||
|
@override
|
||||||
|
Future<void> onLoad() async {
|
||||||
|
await super.onLoad();
|
||||||
|
images.prefix = '';
|
||||||
|
await images.loadAll(
|
||||||
|
[
|
||||||
|
Assets.images.backbox.button.facebook.keyName,
|
||||||
|
Assets.images.backbox.button.twitter.keyName,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> pump(ShareDisplay component) {
|
||||||
|
return ensureAdd(
|
||||||
|
FlameBlocProvider<GameBloc, GameState>.value(
|
||||||
|
value: GameBloc(),
|
||||||
|
children: [
|
||||||
|
FlameProvider.value(
|
||||||
|
_MockAppLocalizations(),
|
||||||
|
children: [component],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _MockAppLocalizations extends Mock implements AppLocalizations {
|
||||||
|
@override
|
||||||
|
String get letEveryone => '';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get bySharingYourScore => '';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get socialMediaAccount => '';
|
||||||
|
}
|
||||||
|
|
||||||
|
class _MockTapDownInfo extends Mock implements TapDownInfo {}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
final flameTester = FlameTester(_TestGame.new);
|
||||||
|
|
||||||
|
group('ShareDisplay', () {
|
||||||
|
flameTester.test(
|
||||||
|
'loads correctly',
|
||||||
|
(game) async {
|
||||||
|
final component = ShareDisplay();
|
||||||
|
await game.pump(component);
|
||||||
|
expect(game.descendants(), contains(component));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'calls onShare when Facebook button is tapped',
|
||||||
|
(game) async {
|
||||||
|
var tapped = false;
|
||||||
|
|
||||||
|
final tapDownInfo = _MockTapDownInfo();
|
||||||
|
final component = ShareDisplay(
|
||||||
|
onShare: (_) => tapped = true,
|
||||||
|
);
|
||||||
|
await game.pump(component);
|
||||||
|
|
||||||
|
final facebookButton =
|
||||||
|
component.descendants().whereType<FacebookButtonComponent>().first;
|
||||||
|
|
||||||
|
facebookButton.onTapDown(tapDownInfo);
|
||||||
|
|
||||||
|
expect(tapped, isTrue);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
flameTester.test(
|
||||||
|
'calls onShare when Twitter button is tapped',
|
||||||
|
(game) async {
|
||||||
|
var tapped = false;
|
||||||
|
|
||||||
|
final tapDownInfo = _MockTapDownInfo();
|
||||||
|
final component = ShareDisplay(
|
||||||
|
onShare: (_) => tapped = true,
|
||||||
|
);
|
||||||
|
await game.pump(component);
|
||||||
|
|
||||||
|
final twitterButton =
|
||||||
|
component.descendants().whereType<TwitterButtonComponent>().first;
|
||||||
|
|
||||||
|
twitterButton.onTapDown(tapDownInfo);
|
||||||
|
|
||||||
|
expect(tapped, isTrue);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in new issue