Files
SpotiFLAC-Mobile/lib/widgets/preview_button.dart
T
zarzet 5694a9a188 fix(preview): read a preview link again when it no longer plays
A preview link is a snapshot of the moment a song was read. One service
signs its links for 15 minutes, while the Recommended shelf on Home keeps
its songs for a day, and an extension without previews of its own can
carry such a borrowed link on some songs. Pressing the button then waited
on a dead link twice, up to the player's 30 second prepare wait each
time, and ended in "Preview unavailable".

- When a link fails to start, the song's own extension is searched for
  the song and the link it has now is played instead. The button keeps
  owning the preview under the link it was built with, and the next press
  starts from the link that worked.
- A link that still does not play although its extension answered, or
  that its extension resolves to nothing, is not offered again while the
  app runs: its button hides instead of failing on every press.
- Starting a preview gives up after 10 seconds instead of the player's
  own wait.
2026-11-22 03:22:23 +07:00

196 lines
6.2 KiB
Dart

import 'package:audio_service/audio_service.dart';
import 'package:material_ui/material_ui.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mornye/l10n/l10n.dart';
import 'package:mornye/models/track.dart';
import 'package:mornye/providers/music_player_provider.dart';
import 'package:mornye/providers/preview_player_provider.dart';
import 'package:mornye/providers/settings_provider.dart';
import 'package:mornye/theme/mornye_theme.dart';
import 'package:mornye/utils/desktop_platform.dart';
import 'package:mornye/widgets/app_icon_button.dart';
import 'package:mornye/widgets/app_loading_indicator.dart';
class PreviewButton extends ConsumerWidget {
final Track track;
final double size;
/// A 32dp pointer target for desktop table rows.
final bool dense;
/// On a cover, where a bare outline gets lost in the artwork: Material
/// shows its filled icon button, in every state.
final bool overArtwork;
const PreviewButton({
super.key,
required this.track,
this.size = 24,
this.dense = false,
this.overArtwork = false,
});
BoxConstraints get _constraints => dense
? const BoxConstraints(minWidth: 32, minHeight: 32)
: const BoxConstraints(minWidth: 48, minHeight: 48);
ButtonStyle? get _style => dense
? IconButton.styleFrom(tapTargetSize: MaterialTapTargetSize.shrinkWrap)
: null;
Future<void> _onPressed(BuildContext context, WidgetRef ref) async {
try {
await ref
.read(previewPlayerProvider.notifier)
.toggle(
track.previewUrl,
refresh: () => ref.read(previewLinkRefreshProvider)(track),
);
} catch (_) {
if (!context.mounted) return;
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text(context.l10n.previewUnavailable)));
}
}
/// Loosely matches the built-in player's current item to this track so the
/// per-track button stays in sync with the mini player instead of showing a
/// conflicting preview state.
bool _isCurrentMainTrack(MediaItem? item) {
if (item == null) return false;
String norm(String? s) => (s ?? '').toLowerCase().trim();
return norm(item.title) == norm(track.name) &&
norm(item.artist) == norm(track.artistName);
}
@override
Widget build(BuildContext context, WidgetRef ref) {
final colorScheme = Theme.of(context).colorScheme;
final filled = overArtwork && !context.isMornye;
// When the built-in player is currently on this track, mirror and control
// it (consistent with the mini player) rather than the preview snippet.
final isCurrentMainTrack = ref.watch(
currentMediaItemProvider.select(
(item) => _isCurrentMainTrack(item.value),
),
);
if (isCurrentMainTrack) {
final isPlaying = ref.watch(playbackPlayingProvider);
if (filled) {
return AppIconButton.filled(
icon: Icon(isPlaying ? Icons.pause : Icons.play_arrow),
tooltip: isPlaying
? context.l10n.previewStop
: context.l10n.previewPlay,
onPressed: () => ref
.read(musicPlayerControllerProvider)
.togglePlayPause(isPlaying),
);
}
return AppIconButton(
iconSize: size,
padding: EdgeInsets.zero,
constraints: _constraints,
style: _style,
icon: Icon(
isPlaying
? Icons.pause_circle_filled_rounded
: Icons.play_circle_fill_rounded,
color: colorScheme.primary,
),
tooltip: isPlaying
? context.l10n.previewStop
: context.l10n.previewPlay,
onPressed: () =>
ref.read(musicPlayerControllerProvider).togglePlayPause(isPlaying),
);
}
// A preview would need the output that exclusive mode holds.
final exclusive =
isDesktopPlatform &&
ref.watch(settingsProvider.select((s) => s.exclusiveOutput));
if (!track.hasPreview || exclusive) return const SizedBox.shrink();
// A link that did not play is not offered a second time.
final unavailable = ref.watch(
previewUnavailableLinksProvider.select(
(links) => links.contains(track.previewUrl?.trim()),
),
);
if (unavailable) return const SizedBox.shrink();
final status = ref.watch(
previewPlayerProvider.select(
(state) => state.isActiveUrl(track.previewUrl)
? state.status
: PreviewStatus.idle,
),
);
if (filled) {
return AppIconButton.filled(
icon: switch (status) {
PreviewStatus.loading => AppLoadingIndicator(
size: 20,
strokeWidth: 2,
color: colorScheme.onPrimary,
),
PreviewStatus.playing => const Icon(Icons.pause),
PreviewStatus.paused ||
PreviewStatus.idle => const Icon(Icons.play_arrow),
},
tooltip: switch (status) {
PreviewStatus.loading ||
PreviewStatus.playing => context.l10n.previewStop,
PreviewStatus.paused ||
PreviewStatus.idle => context.l10n.previewPlay,
},
onPressed: () => _onPressed(context, ref),
);
}
final Widget icon;
final String tooltip;
switch (status) {
case PreviewStatus.loading:
icon = AppLoadingIndicator(
size: size * 0.7,
strokeWidth: 2,
color: colorScheme.primary,
);
tooltip = context.l10n.previewStop;
break;
case PreviewStatus.playing:
icon = Icon(
Icons.pause_circle_filled_rounded,
color: colorScheme.primary,
);
tooltip = context.l10n.previewStop;
break;
case PreviewStatus.paused:
icon = Icon(Icons.play_circle_fill_rounded, color: colorScheme.primary);
tooltip = context.l10n.previewPlay;
break;
case PreviewStatus.idle:
icon = Icon(
Icons.play_circle_outline_rounded,
color: colorScheme.onSurfaceVariant,
);
tooltip = context.l10n.previewPlay;
break;
}
return AppIconButton(
iconSize: size,
padding: EdgeInsets.zero,
constraints: _constraints,
style: _style,
icon: icon,
tooltip: tooltip,
onPressed: () => _onPressed(context, ref),
);
}
}