mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-11-28 15:13:57 +01:00
feat(rewind): share a summary card
Sharing a Rewind only sent the visible slide. Share now previews a 1080 x 1920 card with the listening time, top artists, top songs and Mornye branding, tinted by the top song's cover (issue #828). The visible slide stays available as the second choice. The card ignores the device's text scale and pixel ratio, so every phone and computer makes the same image.
This commit is contained in:
1 parent
ff86c9465d
commit
f4ead34ede
5 files changed
+465
-2
No files matched your search
@@ -8668,6 +8668,8 @@
|
||||
"listeningRewindSlide": "Slide {index} of {count}",
|
||||
"@listeningRewindSlide": { "placeholders": { "index": { "type": "int" }, "count": { "type": "int" } } },
|
||||
"listeningRewindShare": "Share",
|
||||
"listeningRewindShareCard": "Share summary card",
|
||||
"listeningRewindShareSlide": "Share this slide",
|
||||
"listeningRewindShareError": "Could not share your Rewind",
|
||||
"listeningRewindNext": "Next",
|
||||
"listeningRewindPrevious": "Previous",
|
||||
|
||||
@@ -15,6 +15,7 @@ import 'package:mornye/providers/listening_rewind_provider.dart';
|
||||
import 'package:mornye/services/listening_rewind.dart';
|
||||
import 'package:mornye/theme/app_motion.dart';
|
||||
import 'package:mornye/theme/app_tokens.dart';
|
||||
import 'package:mornye/theme/cover_palette.dart';
|
||||
import 'package:mornye/theme/mornye_theme.dart';
|
||||
import 'package:mornye/utils/widget_capture.dart';
|
||||
import 'package:mornye/widgets/app_action_button.dart';
|
||||
@@ -31,6 +32,7 @@ import 'package:mornye/widgets/settled_blur.dart';
|
||||
|
||||
export 'package:mornye/providers/listening_rewind_provider.dart';
|
||||
|
||||
part 'listening_rewind_share_card.dart';
|
||||
part 'listening_rewind_story.dart';
|
||||
part 'listening_rewind_slides.dart';
|
||||
part 'listening_rewind_widgets.dart';
|
||||
|
||||
@@ -0,0 +1,339 @@
|
||||
part of 'listening_rewind_screen.dart';
|
||||
|
||||
/// The card's layout size. Its image is three times this: the 1080 x 1920
|
||||
/// story format.
|
||||
const rewindShareCardSize = Size(360, 640);
|
||||
const _shareCardPixelRatio = 3.0;
|
||||
const _shareCardRowHeight = 40.0;
|
||||
|
||||
/// A period's listening time, top artists and top songs on one image. Its
|
||||
/// layout ignores the device, so every phone and computer makes the same card.
|
||||
class RewindShareCard extends StatelessWidget {
|
||||
const RewindShareCard({
|
||||
super.key,
|
||||
required this.rewind,
|
||||
required this.period,
|
||||
this.coverColor,
|
||||
});
|
||||
|
||||
final ListeningRewind rewind;
|
||||
final ListeningRewindPeriod period;
|
||||
|
||||
/// The top song's cover color, which tints the background.
|
||||
final Color? coverColor;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = context.l10n;
|
||||
final textTheme = Theme.of(context).textTheme;
|
||||
final locale = Localizations.localeOf(context).toString();
|
||||
final number = NumberFormat.decimalPattern(locale);
|
||||
final artwork = rewind.summary.artistArtwork;
|
||||
final artists = rewind.topArtists.take(5).toList(growable: false);
|
||||
final tracks = rewind.summary.tracks.take(5).toList(growable: false);
|
||||
final tint = coverColor ?? Theme.of(context).colorScheme.primary;
|
||||
return MediaQuery(
|
||||
data: MediaQuery.of(context).copyWith(
|
||||
devicePixelRatio: _shareCardPixelRatio,
|
||||
textScaler: TextScaler.noScaling,
|
||||
),
|
||||
child: SizedBox.fromSize(
|
||||
size: rewindShareCardSize,
|
||||
child: DecoratedBox(
|
||||
// Both stops stay dark, so white text reads over any cover.
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [
|
||||
Color.lerp(tint, Colors.black, 0.5)!,
|
||||
Color.lerp(tint, Colors.black, 0.85)!,
|
||||
],
|
||||
),
|
||||
),
|
||||
child: DefaultTextStyle.merge(
|
||||
style: const TextStyle(color: Colors.white),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(24, 24, 24, 20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'${l10n.listeningRewindIntroTitle} · '
|
||||
'${listeningRewindPeriodLabel(context, period)}',
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: textTheme.labelLarge?.copyWith(
|
||||
color: Colors.white.withValues(alpha: 0.8),
|
||||
),
|
||||
),
|
||||
// One line, however long the number and its unit get.
|
||||
FittedBox(
|
||||
fit: BoxFit.scaleDown,
|
||||
alignment: AlignmentDirectional.centerStart,
|
||||
child: Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: number.format(rewind.minutes),
|
||||
style: textTheme.displaySmall?.copyWith(
|
||||
fontWeight: FontWeight.w800,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
TextSpan(
|
||||
text:
|
||||
' ${l10n.listeningRewindMinutesUnit(rewind.minutes)}',
|
||||
style: textTheme.titleMedium?.copyWith(
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
maxLines: 1,
|
||||
),
|
||||
),
|
||||
if (artists.isNotEmpty) ...[
|
||||
const SizedBox(height: 12),
|
||||
_ShareCardHeading(l10n.listeningStatsTopArtists),
|
||||
for (final (index, artist) in artists.indexed)
|
||||
_ShareCardRow(
|
||||
rank: index + 1,
|
||||
artwork: _Artwork(
|
||||
uri: artwork[artist.key],
|
||||
size: 32,
|
||||
artist: true,
|
||||
),
|
||||
title: artist.key,
|
||||
subtitle: l10n.listeningStatsMinutes(
|
||||
(artist.value / 60000).round(),
|
||||
),
|
||||
),
|
||||
],
|
||||
if (tracks.isNotEmpty) ...[
|
||||
const SizedBox(height: 12),
|
||||
_ShareCardHeading(l10n.listeningStatsTopTracks),
|
||||
for (final (index, total) in tracks.indexed)
|
||||
_ShareCardRow(
|
||||
rank: index + 1,
|
||||
artwork: _Artwork(uri: total.track.artwork, size: 32),
|
||||
title: total.track.title,
|
||||
subtitle: total.track.artist,
|
||||
),
|
||||
],
|
||||
const Spacer(),
|
||||
Row(
|
||||
children: [
|
||||
Image.asset(
|
||||
'assets/images/logo-transparent.png',
|
||||
width: 22,
|
||||
height: 22,
|
||||
color: Colors.white,
|
||||
errorBuilder: (_, _, _) => const SizedBox.shrink(),
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
l10n.appName,
|
||||
style: textTheme.labelLarge?.copyWith(
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ShareCardHeading extends StatelessWidget {
|
||||
const _ShareCardHeading(this.text);
|
||||
|
||||
final String text;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Padding(
|
||||
padding: const EdgeInsets.only(bottom: 4),
|
||||
child: Text(
|
||||
text,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: Theme.of(context).textTheme.titleSmall?.copyWith(
|
||||
fontWeight: FontWeight.w700,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class _ShareCardRow extends StatelessWidget {
|
||||
const _ShareCardRow({
|
||||
required this.rank,
|
||||
required this.artwork,
|
||||
required this.title,
|
||||
required this.subtitle,
|
||||
});
|
||||
|
||||
final int rank;
|
||||
final Widget artwork;
|
||||
final String title;
|
||||
final String subtitle;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final textTheme = Theme.of(context).textTheme;
|
||||
final muted = Colors.white.withValues(alpha: 0.75);
|
||||
return SizedBox(
|
||||
height: _shareCardRowHeight,
|
||||
child: Row(
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 20,
|
||||
child: Text(
|
||||
'$rank',
|
||||
style: textTheme.labelLarge?.copyWith(color: muted),
|
||||
),
|
||||
),
|
||||
artwork,
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
title,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: textTheme.labelLarge?.copyWith(
|
||||
fontWeight: FontWeight.w700,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
subtitle,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: textTheme.labelSmall?.copyWith(color: muted),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Previews the card and shares it. Pops with true to share the visible slide
|
||||
/// instead, which the story captures itself.
|
||||
class _RewindShareSheet extends StatefulWidget {
|
||||
const _RewindShareSheet({required this.rewind, required this.period});
|
||||
|
||||
final ListeningRewind rewind;
|
||||
final ListeningRewindPeriod period;
|
||||
|
||||
@override
|
||||
State<_RewindShareSheet> createState() => _RewindShareSheetState();
|
||||
}
|
||||
|
||||
class _RewindShareSheetState extends State<_RewindShareSheet> {
|
||||
final _cardKey = GlobalKey();
|
||||
final _shareButtonKey = GlobalKey();
|
||||
var _sharing = false;
|
||||
|
||||
Future<void> _share() async {
|
||||
if (_sharing) return;
|
||||
final l10n = context.l10n;
|
||||
final messenger = ScaffoldMessenger.of(context);
|
||||
final button = _shareButtonKey.currentContext?.findRenderObject();
|
||||
final origin = button is RenderBox && button.hasSize
|
||||
? button.localToGlobal(Offset.zero) & button.size
|
||||
: null;
|
||||
setState(() => _sharing = true);
|
||||
try {
|
||||
// A cover color can still be waiting for paint when Share is tapped.
|
||||
await WidgetsBinding.instance.endOfFrame;
|
||||
if (!mounted) return;
|
||||
final file = await captureBoundaryToPngFile(
|
||||
_cardKey,
|
||||
pixelRatio: _shareCardPixelRatio,
|
||||
fileName: 'mornye_rewind_card.png',
|
||||
);
|
||||
if (file == null) throw StateError('Card could not be captured');
|
||||
await SharePlus.instance.share(
|
||||
ShareParams(
|
||||
files: [XFile(file.path, mimeType: 'image/png')],
|
||||
text: l10n.listeningRewindIntroTitle,
|
||||
sharePositionOrigin: origin,
|
||||
),
|
||||
);
|
||||
} catch (_) {
|
||||
messenger.showSnackBar(
|
||||
SnackBar(content: Text(l10n.listeningRewindShareError)),
|
||||
);
|
||||
} finally {
|
||||
if (mounted) setState(() => _sharing = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = context.l10n;
|
||||
final previewHeight = math.min(
|
||||
MediaQuery.sizeOf(context).height * 0.6,
|
||||
rewindShareCardSize.height,
|
||||
);
|
||||
return SafeArea(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
SizedBox(
|
||||
height: previewHeight,
|
||||
width: previewHeight * rewindShareCardSize.aspectRatio,
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
child: FittedBox(
|
||||
child: CoverSourceColorBuilder(
|
||||
imageSource:
|
||||
widget.rewind.summary.tracks.firstOrNull?.track.artwork,
|
||||
builder: (context, coverColor) => RepaintBoundary(
|
||||
key: _cardKey,
|
||||
child: RewindShareCard(
|
||||
rewind: widget.rewind,
|
||||
period: widget.period,
|
||||
coverColor: coverColor,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
AppActionButton(
|
||||
key: _shareButtonKey,
|
||||
onPressed: _sharing ? null : _share,
|
||||
icon: const Icon(Icons.share),
|
||||
label: Text(l10n.listeningRewindShareCard),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
AppActionButton(
|
||||
tonal: true,
|
||||
onPressed: _sharing
|
||||
? null
|
||||
: () => Navigator.of(context).pop(true),
|
||||
icon: const Icon(Icons.image_outlined),
|
||||
label: Text(l10n.listeningRewindShareSlide),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -180,6 +180,17 @@ class _RewindStoryState extends State<_RewindStory>
|
||||
}
|
||||
}
|
||||
|
||||
/// Offers the summary card, with the visible slide as the alternative.
|
||||
Future<void> _chooseShare() async {
|
||||
final shareSlide = await showAppModalBottomSheet<bool>(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
builder: (context) =>
|
||||
_RewindShareSheet(rewind: widget.rewind, period: widget.period),
|
||||
);
|
||||
if (shareSlide == true && mounted) await _share();
|
||||
}
|
||||
|
||||
/// Shares the visible slide, without the story controls, as an image.
|
||||
Future<void> _share() async {
|
||||
if (_sharing) return;
|
||||
@@ -303,7 +314,7 @@ class _RewindStoryState extends State<_RewindStory>
|
||||
child: _Reveal(
|
||||
order: 4,
|
||||
child: AppActionButton(
|
||||
onPressed: _sharing ? null : _share,
|
||||
onPressed: _sharing ? null : _chooseShare,
|
||||
icon: const Icon(Icons.share),
|
||||
label: Text(l10n.listeningRewindShare),
|
||||
),
|
||||
|
||||
@@ -488,7 +488,10 @@ void main() {
|
||||
await tester.pumpAndSettle();
|
||||
expect(blur.debugImage, isNotNull);
|
||||
|
||||
await tester.runAsync(() => tester.tap(find.text('Share')));
|
||||
await tester.tap(find.text('Share'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(RewindShareCard), findsOneWidget);
|
||||
await tester.runAsync(() => tester.tap(find.text('Share this slide')));
|
||||
await tester.pump();
|
||||
for (var attempt = 0; attempt < 100 && !shared.isCompleted; attempt++) {
|
||||
await tester.runAsync(
|
||||
@@ -514,4 +517,110 @@ void main() {
|
||||
expect(blur.debugImage, isNotNull);
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
|
||||
for (final (choice, fileName, width, height) in [
|
||||
('Share summary card', 'mornye_rewind_card.png', 1080, 1920),
|
||||
('Share this slide', 'mornye_rewind.png', _size.width, _size.height),
|
||||
]) {
|
||||
testWidgets('$choice shares $fileName at $width x $height', (tester) async {
|
||||
reduced.value = true;
|
||||
await open(tester, mornye: false);
|
||||
final temporary = Directory('${directory.path}/share')..createSync();
|
||||
final messenger =
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
||||
const paths = MethodChannel('plugins.flutter.io/path_provider');
|
||||
const share = MethodChannel('dev.fluttercommunity.plus/share');
|
||||
final shared = Completer<String>();
|
||||
messenger.setMockMethodCallHandler(paths, (_) async => temporary.path);
|
||||
messenger.setMockMethodCallHandler(share, (call) async {
|
||||
final arguments = call.arguments as Map;
|
||||
shared.complete((arguments['paths'] as List).single as String);
|
||||
return 'dev.fluttercommunity.plus/share/success';
|
||||
});
|
||||
addTearDown(() {
|
||||
messenger.setMockMethodCallHandler(paths, null);
|
||||
messenger.setMockMethodCallHandler(share, null);
|
||||
});
|
||||
|
||||
await tester.pumpAndSettle();
|
||||
final story = tester.getRect(find.byType(PageView));
|
||||
while (find.text('Share').evaluate().isEmpty) {
|
||||
await tester.tapAt(Offset(story.right - 40, story.center.dy));
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
await tester.tap(find.text('Share'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(RewindShareCard), findsOneWidget);
|
||||
await tester.runAsync(() => tester.tap(find.text(choice)));
|
||||
await tester.pump();
|
||||
for (var attempt = 0; attempt < 100 && !shared.isCompleted; attempt++) {
|
||||
await tester.runAsync(
|
||||
() => Future<void>.delayed(const Duration(milliseconds: 10)),
|
||||
);
|
||||
await tester.pump();
|
||||
}
|
||||
expect(shared.isCompleted, isTrue);
|
||||
final file = await shared.future;
|
||||
expect(file, endsWith(fileName));
|
||||
final image = await tester.runAsync(() async {
|
||||
final codec = await ui.instantiateImageCodec(
|
||||
await File(file).readAsBytes(),
|
||||
);
|
||||
return (await codec.getNextFrame()).image;
|
||||
});
|
||||
expect(image!.width, width);
|
||||
expect(image.height, height);
|
||||
expect(tester.takeException(), isNull);
|
||||
// The cover color's sample read gives up after five seconds.
|
||||
await tester.pump(const Duration(seconds: 6));
|
||||
});
|
||||
}
|
||||
|
||||
for (final mornye in [true, false]) {
|
||||
testWidgets('the summary card holds five long names (Mornye: $mornye)', (
|
||||
tester,
|
||||
) async {
|
||||
final long = 'A very long name that cannot fit on one line ' * 3;
|
||||
final rows = [
|
||||
for (var i = 0; i < 6; i++)
|
||||
_row(
|
||||
'2026-10-0${i + 1}',
|
||||
'$long$i',
|
||||
artist: '$long$i',
|
||||
minutes: 400000 - i,
|
||||
),
|
||||
];
|
||||
final rewind = ListeningRewind.compute(
|
||||
range: ListeningRewindRange.of(
|
||||
ListeningRewindPeriod.year,
|
||||
DateTime(2026, 10, 14, 21),
|
||||
),
|
||||
summary: ListeningSummary.fromRows(rows),
|
||||
previousMilliseconds: 0,
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
theme: mornye ? MornyeTheme.build(Brightness.dark) : ThemeData(),
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: Center(
|
||||
child: FittedBox(
|
||||
child: MediaQuery(
|
||||
data: const MediaQueryData(textScaler: TextScaler.linear(2)),
|
||||
child: RewindShareCard(
|
||||
rewind: rewind,
|
||||
period: ListeningRewindPeriod.year,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(tester.getSize(find.byType(RewindShareCard)), rewindShareCardSize);
|
||||
expect(find.text('5'), findsNWidgets(2));
|
||||
expect(find.text('6'), findsNothing);
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user