2020-04-07 22:06:43 +02:00
|
|
|
import 'package:meta/meta.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
|
2020-04-10 20:12:26 +02:00
|
|
|
import '../../domain/entities/playback_state.dart';
|
2020-04-07 22:06:43 +02:00
|
|
|
import '../../domain/entities/song.dart';
|
|
|
|
import '../../domain/repositories/audio_repository.dart';
|
|
|
|
|
|
|
|
part 'audio_store.g.dart';
|
|
|
|
|
|
|
|
class AudioStore extends _AudioStore with _$AudioStore {
|
2020-08-12 20:59:00 +02:00
|
|
|
AudioStore({@required AudioRepository audioRepository})
|
|
|
|
: super(audioRepository);
|
2020-04-07 22:06:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract class _AudioStore with Store {
|
2020-08-12 20:59:00 +02:00
|
|
|
_AudioStore(this._audioRepository);
|
2020-04-07 22:06:43 +02:00
|
|
|
|
|
|
|
final AudioRepository _audioRepository;
|
|
|
|
|
2020-04-10 20:12:26 +02:00
|
|
|
bool _initialized = false;
|
|
|
|
final List<ReactionDisposer> _disposers = [];
|
2020-04-09 17:50:28 +02:00
|
|
|
|
|
|
|
@observable
|
|
|
|
ObservableStream<Song> currentSong;
|
|
|
|
@observable
|
|
|
|
Song song;
|
|
|
|
|
2020-04-10 20:12:26 +02:00
|
|
|
@observable
|
|
|
|
ObservableStream<PlaybackState> playbackStateStream;
|
|
|
|
|
2020-04-11 20:23:02 +02:00
|
|
|
@observable
|
|
|
|
ObservableStream<int> currentPositionStream;
|
|
|
|
|
2020-07-12 18:25:34 +02:00
|
|
|
@observable
|
|
|
|
ObservableStream<List<Song>> queueStream;
|
|
|
|
|
|
|
|
@observable
|
|
|
|
ObservableStream<int> queueIndexStream;
|
|
|
|
|
2020-04-09 17:50:28 +02:00
|
|
|
@action
|
2020-05-27 21:54:33 +02:00
|
|
|
void init() {
|
2020-04-10 20:12:26 +02:00
|
|
|
if (!_initialized) {
|
|
|
|
print('AudioStore.init');
|
|
|
|
currentSong = _audioRepository.currentSongStream.asObservable();
|
|
|
|
|
2020-04-11 20:23:02 +02:00
|
|
|
currentPositionStream =
|
|
|
|
_audioRepository.currentPositionStream.asObservable(initialValue: 0);
|
|
|
|
|
2020-07-12 18:25:34 +02:00
|
|
|
queueStream = _audioRepository.queueStream.asObservable(initialValue: []);
|
|
|
|
|
|
|
|
queueIndexStream = _audioRepository.queueIndexStream.asObservable();
|
|
|
|
|
2020-04-10 20:12:26 +02:00
|
|
|
_disposers.add(autorun((_) {
|
|
|
|
updateSong(currentSong.value);
|
|
|
|
}));
|
2020-04-09 17:50:28 +02:00
|
|
|
|
2020-04-10 20:12:26 +02:00
|
|
|
playbackStateStream = _audioRepository.playbackStateStream.asObservable();
|
|
|
|
|
|
|
|
_initialized = true;
|
|
|
|
}
|
2020-04-09 17:50:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void dispose() {
|
2020-04-10 20:12:26 +02:00
|
|
|
print('AudioStore.dispose');
|
2020-08-12 20:59:00 +02:00
|
|
|
for (final ReactionDisposer d in _disposers) {
|
|
|
|
d();
|
|
|
|
}
|
2020-04-09 17:50:28 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 22:06:43 +02:00
|
|
|
@action
|
|
|
|
Future<void> playSong(int index, List<Song> songList) async {
|
|
|
|
_audioRepository.playSong(index, songList);
|
|
|
|
}
|
|
|
|
|
2020-04-10 20:12:26 +02:00
|
|
|
@action
|
|
|
|
Future<void> play() async {
|
|
|
|
_audioRepository.play();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
Future<void> pause() async {
|
|
|
|
_audioRepository.pause();
|
|
|
|
}
|
|
|
|
|
2020-07-12 18:25:34 +02:00
|
|
|
@action
|
|
|
|
Future<void> skipToNext() async {
|
|
|
|
_audioRepository.skipToNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
Future<void> skipToPrevious() async {
|
|
|
|
_audioRepository.skipToPrevious();
|
|
|
|
}
|
|
|
|
|
2020-04-09 17:50:28 +02:00
|
|
|
@action
|
|
|
|
Future<void> updateSong(Song streamValue) async {
|
|
|
|
print('updateSong');
|
|
|
|
if (streamValue != null && streamValue != song) {
|
|
|
|
print('actually updating...');
|
|
|
|
song = streamValue;
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 20:12:26 +02:00
|
|
|
}
|