mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-11-22 04:10:55 +01:00
refactor(player): share the current queue index rule
The Mornye player layout and its queue repeated the same "reported index while it still names the current item, else first match" rule; one provider now derives it, with a test for stale, shuffled and empty queues.
This commit is contained in:
1 parent
e049a2b436
commit
26695a8d34
4 files changed
+59
-23
No files matched your search
@@ -309,6 +309,21 @@ final playQueueProvider = StreamProvider<List<MediaItem>>((ref) {
|
||||
return ref.watch(musicPlayerRuntimeProvider).queueEvents();
|
||||
});
|
||||
|
||||
/// Prefers the reported queue index while it still names the current item.
|
||||
final currentQueueIndexProvider = Provider<int>((ref) {
|
||||
final queue = ref.watch(playQueueProvider).value ?? const <MediaItem>[];
|
||||
final id = ref.watch(currentMediaItemProvider.select((s) => s.value?.id));
|
||||
final index = ref.watch(
|
||||
playbackStateProvider.select((state) => state.value?.queueIndex),
|
||||
);
|
||||
return index != null &&
|
||||
index >= 0 &&
|
||||
index < queue.length &&
|
||||
queue[index].id == id
|
||||
? index
|
||||
: queue.indexWhere((item) => item.id == id);
|
||||
});
|
||||
|
||||
class MusicPlayerController {
|
||||
MusicPlayerController({MusicPlayerRuntime? runtime})
|
||||
: _runtime = runtime ?? musicPlayerRuntime;
|
||||
|
||||
@@ -99,16 +99,7 @@ class MornyePlayerLayout extends ConsumerWidget {
|
||||
? Duration.zero
|
||||
: const Duration(milliseconds: 380);
|
||||
final queue = ref.watch(playQueueProvider).value ?? const <MediaItem>[];
|
||||
final reportedIndex = ref.watch(
|
||||
playbackStateProvider.select((state) => state.value?.queueIndex),
|
||||
);
|
||||
final currentIndex =
|
||||
reportedIndex != null &&
|
||||
reportedIndex >= 0 &&
|
||||
reportedIndex < queue.length &&
|
||||
queue[reportedIndex].id == mediaItem.id
|
||||
? reportedIndex
|
||||
: queue.indexWhere((item) => item.id == mediaItem.id);
|
||||
final currentIndex = ref.watch(currentQueueIndexProvider);
|
||||
final landscape =
|
||||
MediaQuery.sizeOf(context).width > MediaQuery.sizeOf(context).height;
|
||||
Widget hideControls(Widget child) => PlayerLyricsControlsVisibility(
|
||||
|
||||
@@ -17,27 +17,15 @@ class MornyePlayerQueue extends ConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final queue = ref.watch(playQueueProvider).value ?? const <MediaItem>[];
|
||||
final currentId = ref.watch(
|
||||
currentMediaItemProvider.select((item) => item.value?.id),
|
||||
);
|
||||
final playback = ref.watch(
|
||||
playbackStateProvider.select(
|
||||
(state) => (
|
||||
index: state.value?.queueIndex,
|
||||
shuffle: state.value?.shuffleMode == AudioServiceShuffleMode.all,
|
||||
repeat: state.value?.repeatMode ?? AudioServiceRepeatMode.none,
|
||||
),
|
||||
),
|
||||
);
|
||||
final reportedIndex = playback.index;
|
||||
final currentIndex =
|
||||
reportedIndex != null &&
|
||||
reportedIndex >= 0 &&
|
||||
reportedIndex < queue.length &&
|
||||
queue[reportedIndex].id == currentId
|
||||
? reportedIndex
|
||||
: queue.indexWhere((item) => item.id == currentId);
|
||||
final start = currentIndex + 1;
|
||||
final start = ref.watch(currentQueueIndexProvider) + 1;
|
||||
final count = queue.length - start;
|
||||
final controller = ref.read(musicPlayerControllerProvider);
|
||||
final autoMix = ref.watch(settingsProvider.select((s) => s.autoMix));
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'package:audio_service/audio_service.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mornye/providers/music_player_provider.dart';
|
||||
|
||||
Future<int> _index(List<String> ids, String? current, int? reported) async {
|
||||
final queue = [for (final id in ids) MediaItem(id: id, title: id)];
|
||||
final container = ProviderContainer(
|
||||
overrides: [
|
||||
playQueueProvider.overrideWith((ref) => Stream.value(queue)),
|
||||
currentMediaItemProvider.overrideWithBuild(
|
||||
(ref, notifier) => Stream.value(
|
||||
current == null ? null : MediaItem(id: current, title: current),
|
||||
),
|
||||
),
|
||||
playbackStateProvider.overrideWith(
|
||||
(ref) => Stream.value(PlaybackState(queueIndex: reported)),
|
||||
),
|
||||
],
|
||||
);
|
||||
addTearDown(container.dispose);
|
||||
final subscription = container.listen(currentQueueIndexProvider, (_, _) {});
|
||||
await container.read(playQueueProvider.future);
|
||||
await container.read(currentMediaItemProvider.future);
|
||||
await container.read(playbackStateProvider.future);
|
||||
return subscription.read();
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('reported index wins only while it names the current item', () async {
|
||||
// A repeated track keeps the reported copy.
|
||||
expect(await _index(['a', 'b', 'a'], 'a', 2), 2);
|
||||
// Shuffle or a stale report falls back to the first match.
|
||||
expect(await _index(['a', 'b', 'c'], 'c', 0), 2);
|
||||
expect(await _index(['a', 'b', 'c'], 'b', 7), 1);
|
||||
expect(await _index(['a', 'b', 'c'], 'b', -1), 1);
|
||||
expect(await _index(['a', 'b', 'c'], 'b', null), 1);
|
||||
expect(await _index(['a', 'b'], 'x', 1), -1);
|
||||
expect(await _index([], 'a', 0), -1);
|
||||
expect(await _index(['a'], null, 0), -1);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user