RhythmBox/lib/services/server/active_sourced_track.dart

52 lines
1.7 KiB
Dart
Raw Permalink Normal View History

2024-08-26 17:49:05 +00:00
import 'package:get/get.dart';
import 'package:rhythm_box/providers/audio_player.dart';
2024-09-02 13:20:30 +00:00
import 'package:rhythm_box/providers/error_notifier.dart';
2024-08-26 17:49:05 +00:00
import 'package:rhythm_box/services/audio_player/audio_player.dart';
import 'package:rhythm_box/services/sourced_track/models/source_info.dart';
import 'package:rhythm_box/services/sourced_track/sourced_track.dart';
2024-08-30 04:56:28 +00:00
import 'package:rhythm_box/widgets/tracks/querying_track_info.dart';
2024-08-26 17:49:05 +00:00
class ActiveSourcedTrackProvider extends GetxController {
Rx<SourcedTrack?> state = Rx(null);
void updateTrack(SourcedTrack? sourcedTrack) {
state.value = sourcedTrack;
}
Future<void> populateSibling() async {
if (state.value == null) return;
state.value = await state.value!.copyWithSibling();
}
Future<void> swapSibling(SourceInfo sibling) async {
2024-08-30 04:56:28 +00:00
final query = Get.find<QueryingTrackInfoProvider>();
query.isQueryingTrackInfo.value = true;
try {
if (state.value == null) return;
2024-08-30 14:06:24 +00:00
await audioPlayer.pause();
2024-08-30 04:56:28 +00:00
await populateSibling();
final newTrack = await state.value!.swapWithSibling(sibling);
if (newTrack == null) return;
2024-08-26 17:49:05 +00:00
2024-08-30 04:56:28 +00:00
state.value = newTrack;
2024-08-26 17:49:05 +00:00
2024-08-30 04:56:28 +00:00
final playback = Get.find<AudioPlayerProvider>();
final oldActiveIndex = audioPlayer.currentIndex;
2024-08-26 17:49:05 +00:00
2024-08-30 04:56:28 +00:00
await playback.addTracksAtFirst([newTrack]);
await Future.delayed(const Duration(milliseconds: 30));
2024-08-26 17:49:05 +00:00
2024-08-30 04:56:28 +00:00
await audioPlayer.removeTrack(oldActiveIndex);
await playback.jumpToTrack(newTrack);
} catch (e, stack) {
2024-09-02 13:20:30 +00:00
Get.find<ErrorNotifier>().logError(
'[Playback] Failed to swap with siblings. Error: $e',
trace: stack);
2024-08-30 04:56:28 +00:00
} finally {
query.isQueryingTrackInfo.value = false;
2024-08-30 14:06:24 +00:00
await audioPlayer.resume();
2024-08-30 04:56:28 +00:00
}
2024-08-26 17:49:05 +00:00
}
}