2024-08-26 18:22:47 +00:00
|
|
|
import 'package:collection/collection.dart';
|
2024-08-26 17:49:05 +00:00
|
|
|
import 'package:get/get.dart';
|
2024-08-26 18:22:47 +00:00
|
|
|
import 'package:rhythm_box/providers/audio_player.dart';
|
2024-08-26 17:49:05 +00:00
|
|
|
import 'package:rhythm_box/services/audio_player/audio_player.dart';
|
|
|
|
import 'package:rhythm_box/services/local_track.dart';
|
|
|
|
import 'package:rhythm_box/services/sourced_track/sourced_track.dart';
|
2024-08-27 06:35:16 +00:00
|
|
|
import 'package:rhythm_box/widgets/tracks/querying_track_info.dart';
|
2024-08-26 18:22:47 +00:00
|
|
|
import 'package:spotify/spotify.dart';
|
2024-08-26 17:49:05 +00:00
|
|
|
|
|
|
|
class SourcedTrackProvider extends GetxController {
|
2024-08-26 18:22:47 +00:00
|
|
|
Rx<SourcedTrack?> sourcedTrack = Rx(null);
|
|
|
|
|
2024-08-26 17:49:05 +00:00
|
|
|
Future<SourcedTrack?> fetch(RhythmMedia? media) async {
|
|
|
|
final track = media?.track;
|
|
|
|
if (track == null || track is LocalTrack) {
|
2024-08-26 18:22:47 +00:00
|
|
|
sourcedTrack.value = null;
|
2024-08-26 17:49:05 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-08-26 18:22:47 +00:00
|
|
|
final AudioPlayerProvider playback = Get.find();
|
2024-08-27 06:35:16 +00:00
|
|
|
final QueryingTrackInfoProvider query = Get.find();
|
2024-08-26 18:22:47 +00:00
|
|
|
|
|
|
|
ever(playback.state.value.tracks.obs, (List<Track> tracks) {
|
|
|
|
if (tracks.isEmpty || tracks.none((element) => element.id == track.id)) {
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-08-27 15:14:54 +00:00
|
|
|
final isCurrentTrack = playback.state.value.activeTrack?.id == track.id;
|
|
|
|
|
|
|
|
if (isCurrentTrack) query.isQueryingTrackInfo.value = true;
|
|
|
|
|
2024-08-26 18:22:47 +00:00
|
|
|
sourcedTrack.value = await SourcedTrack.fetchFromTrack(track: track);
|
2024-08-27 15:14:54 +00:00
|
|
|
|
|
|
|
if (isCurrentTrack) query.isQueryingTrackInfo.value = false;
|
2024-08-26 18:22:47 +00:00
|
|
|
|
|
|
|
return sourcedTrack.value;
|
|
|
|
}
|
2024-08-26 17:49:05 +00:00
|
|
|
|
2024-08-26 18:22:47 +00:00
|
|
|
void invalidate() {
|
|
|
|
sourcedTrack.value = null;
|
|
|
|
fetch(null);
|
2024-08-26 17:49:05 +00:00
|
|
|
}
|
|
|
|
}
|