2021-02-26 22:07:18 +01:00
|
|
|
import '../entities/shuffle_mode.dart';
|
2021-03-06 19:09:19 +01:00
|
|
|
import '../modules/queue_manager.dart';
|
2021-02-26 22:07:18 +01:00
|
|
|
import '../repositories/audio_player_repository.dart';
|
|
|
|
import '../repositories/persistent_player_state_repository.dart';
|
|
|
|
import '../repositories/platform_integration_repository.dart';
|
|
|
|
|
|
|
|
class SetShuffleMode {
|
|
|
|
SetShuffleMode(
|
|
|
|
this._audioPlayerRepository,
|
|
|
|
this._platformIntegrationRepository,
|
|
|
|
this._playerStateRepository,
|
2021-03-06 19:09:19 +01:00
|
|
|
this._queueManagerModule,
|
2021-02-26 22:07:18 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
final AudioPlayerRepository _audioPlayerRepository;
|
|
|
|
final PlatformIntegrationRepository _platformIntegrationRepository;
|
|
|
|
final PlayerStateRepository _playerStateRepository;
|
|
|
|
|
2021-03-06 19:09:19 +01:00
|
|
|
final QueueManagerModule _queueManagerModule;
|
2021-02-26 22:07:18 +01:00
|
|
|
|
2021-04-16 18:58:27 +02:00
|
|
|
Future<void> call(ShuffleMode shuffleMode, {bool updateQueue = true}) async {
|
2021-02-26 22:07:18 +01:00
|
|
|
_audioPlayerRepository.setShuffleMode(shuffleMode);
|
|
|
|
|
2021-04-16 18:58:27 +02:00
|
|
|
if (updateQueue) {
|
|
|
|
final currentIndex = _audioPlayerRepository.currentIndexStream.value;
|
|
|
|
final originalIndex = _queueManagerModule.queue[currentIndex].originalIndex;
|
2021-02-26 22:07:18 +01:00
|
|
|
|
2021-04-16 18:58:27 +02:00
|
|
|
await _queueManagerModule.reshuffleQueue(shuffleMode, currentIndex);
|
|
|
|
final queue = _queueManagerModule.queue;
|
2021-02-26 22:07:18 +01:00
|
|
|
|
2021-04-16 18:58:27 +02:00
|
|
|
final songList = queue.map((e) => e.song).toList();
|
|
|
|
final splitIndex = shuffleMode == ShuffleMode.none ? originalIndex : 0;
|
|
|
|
_audioPlayerRepository.replaceQueueAroundIndex(
|
|
|
|
index: currentIndex,
|
|
|
|
before: songList.sublist(0, splitIndex),
|
|
|
|
after: songList.sublist(splitIndex + 1),
|
|
|
|
);
|
2021-02-26 22:07:18 +01:00
|
|
|
|
2021-04-16 18:58:27 +02:00
|
|
|
_platformIntegrationRepository.setQueue(songList);
|
|
|
|
}
|
2021-02-26 22:07:18 +01:00
|
|
|
}
|
|
|
|
}
|