bug fixes: like button, current song
This commit is contained in:
parent
2d4777e0df
commit
7e21135bfe
6 changed files with 84 additions and 55 deletions
|
@ -69,8 +69,9 @@ class LikeButton extends StatelessWidget {
|
|||
onPressed: () {
|
||||
if (song.likeCount < 5) {
|
||||
musicDataStore.incrementLikeCount(song);
|
||||
} else {
|
||||
musicDataStore.resetLikeCount(song);
|
||||
}
|
||||
musicDataStore.resetLikeCount(song);
|
||||
},
|
||||
visualDensity: VisualDensity.compact,
|
||||
);
|
||||
|
|
|
@ -21,7 +21,7 @@ class NextButton extends StatelessWidget {
|
|||
builder: (BuildContext context) {
|
||||
final queue = audioStore.queueStream.value;
|
||||
final int? index = audioStore.queueIndexStream.value;
|
||||
final LoopMode loopMode = audioStore.loopModeStream.value!;
|
||||
final LoopMode loopMode = audioStore.loopModeStream.value ?? LoopMode.off;
|
||||
|
||||
if ((index != null && queue != null && index < queue.length - 1) || loopMode != LoopMode.off) {
|
||||
return IconButton(
|
||||
|
|
|
@ -147,6 +147,7 @@ class AudioPlayerDataSourceImpl implements AudioPlayerDataSource {
|
|||
|
||||
@override
|
||||
Future<void> moveQueueItem(int oldIndex, int newIndex) async {
|
||||
_log.d('moveQueueItem: $oldIndex -> $newIndex');
|
||||
final int oldCurrentIndex = currentIndexStream.value!;
|
||||
int newCurrentIndex = oldCurrentIndex;
|
||||
if (oldIndex == oldCurrentIndex) {
|
||||
|
@ -168,7 +169,8 @@ class AudioPlayerDataSourceImpl implements AudioPlayerDataSource {
|
|||
final before = newQueue.sublist(0, newCurrentIndex);
|
||||
final after = newQueue.sublist(min(newCurrentIndex + 1, newQueue.length));
|
||||
|
||||
replaceQueueAroundCurrentIndex(
|
||||
await replaceQueueAroundIndex(
|
||||
index: currentIndexStream.value!,
|
||||
before: before,
|
||||
after: after,
|
||||
);
|
||||
|
@ -187,28 +189,32 @@ class AudioPlayerDataSourceImpl implements AudioPlayerDataSource {
|
|||
|
||||
@override
|
||||
Future<void> removeQueueIndex(int index) async {
|
||||
_log.d('REMOVE: $index');
|
||||
_log.d('removeQueueIndex: $index');
|
||||
_queue.removeAt(index);
|
||||
if (_isQueueIndexInLoadInterval(index)) {
|
||||
final sourceIndex = _calcSourceIndex(index);
|
||||
final currentSourceIndex = _audioPlayer.currentIndex!;
|
||||
_log.d('INDEX IS LOADED');
|
||||
if (index < _loadStartIndex) {
|
||||
_log.d('$index < $_loadStartIndex --> DECREMENT LOAD START INDEX');
|
||||
_loadStartIndex--;
|
||||
}
|
||||
if (index < _loadEndIndex) {
|
||||
_log.d('$index < $_loadEndIndex --> DECREMENT LOAD END INDEX');
|
||||
_loadEndIndex--;
|
||||
}
|
||||
final sourceIndex = _calcSourceIndex(index);
|
||||
final currentSourceIndex = _audioPlayer.currentIndex!;
|
||||
final isIndexLoaded = _isQueueIndexInLoadInterval(index);
|
||||
|
||||
if (index < _loadStartIndex) {
|
||||
_log.d('$index < $_loadStartIndex --> DECREMENT LOAD START INDEX');
|
||||
_loadStartIndex--;
|
||||
}
|
||||
if (index < _loadEndIndex) {
|
||||
_log.d('$index < $_loadEndIndex --> DECREMENT LOAD END INDEX');
|
||||
_loadEndIndex--;
|
||||
}
|
||||
|
||||
if (isIndexLoaded) {
|
||||
_log.d('index is loaded');
|
||||
await _audioSource.removeAt(sourceIndex);
|
||||
if (sourceIndex >= currentSourceIndex) {
|
||||
_updateLoadedQueue(currentSourceIndex);
|
||||
}
|
||||
} else {
|
||||
_updateCurrentIndex(_audioPlayer.currentIndex!);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: maybe remove this
|
||||
@override
|
||||
Future<void> replaceQueueAroundIndex({
|
||||
required List<SongModel> before,
|
||||
|
@ -238,34 +244,6 @@ class AudioPlayerDataSourceImpl implements AudioPlayerDataSource {
|
|||
_updateCurrentIndex(newSourceIndex);
|
||||
}
|
||||
|
||||
Future<void> replaceQueueAroundCurrentIndex({
|
||||
required List<SongModel> before,
|
||||
required List<SongModel> after,
|
||||
}) async {
|
||||
_log.d('REPLACE QUEUE AROUND CURRENT INDEX');
|
||||
final int currIndex = currentIndexStream.value!;
|
||||
final int newIndex = before.length;
|
||||
_queue = before + [_queue[currIndex]] + after;
|
||||
|
||||
final oldSourceIndex = _calcSourceIndex(currIndex);
|
||||
final queueToLoad = _getQueueToLoad(_queue, newIndex);
|
||||
final newSourceIndex = _calcSourceIndex(newIndex);
|
||||
|
||||
final _before = _songModelsToAudioSource(queueToLoad.sublist(0, newSourceIndex));
|
||||
final _after = _songModelsToAudioSource(
|
||||
queueToLoad.sublist(min(newSourceIndex + 1, queueToLoad.length)),
|
||||
);
|
||||
|
||||
_lockUpdate = true;
|
||||
await _audioSource.removeRange(0, oldSourceIndex);
|
||||
await _audioSource.removeRange(1, _audioSource.length);
|
||||
|
||||
await _audioSource.insertAll(0, _before.children);
|
||||
await _audioSource.addAll(_after.children);
|
||||
_lockUpdate = false;
|
||||
_updateCurrentIndex(newSourceIndex);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setLoopMode(LoopMode loopMode) async {
|
||||
await _audioPlayer.setLoopMode(loopMode.toJA());
|
||||
|
|
|
@ -171,7 +171,6 @@ class SongModel extends Song {
|
|||
playCount: Value(playCount),
|
||||
);
|
||||
|
||||
// FIXME: this will break
|
||||
SongsCompanion toMoorInsert() => SongsCompanion(
|
||||
albumTitle: Value(album),
|
||||
albumId: Value(albumId),
|
||||
|
@ -182,8 +181,13 @@ class SongModel extends Song {
|
|||
albumArtPath: Value(albumArtPath),
|
||||
discNumber: Value(discNumber),
|
||||
trackNumber: Value(trackNumber),
|
||||
// blocked: Value(blocked),
|
||||
blocked: Value(blocked),
|
||||
present: const Value(true),
|
||||
next: Value(next),
|
||||
previous: Value(previous),
|
||||
likeCount: Value(likeCount),
|
||||
skipCount: Value(skipCount),
|
||||
playCount: Value(playCount),
|
||||
);
|
||||
|
||||
MediaItem toMediaItem() => MediaItem(
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import 'package:flutter_fimber/flutter_fimber.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
import '../../domain/entities/loop_mode.dart';
|
||||
|
@ -16,15 +17,22 @@ class AudioPlayerRepositoryImpl implements AudioPlayerRepository {
|
|||
_loopModeSubject.add(LoopMode.off);
|
||||
|
||||
_audioPlayerDataSource.currentIndexStream.listen(
|
||||
(index) => _updateCurrentSong(queueStream.value, index),
|
||||
(index) {
|
||||
_currentIndexSubject.add(index);
|
||||
_updateCurrentSong(queueStream.value, index);
|
||||
},
|
||||
);
|
||||
_queueSubject.listen((queue) => _updateCurrentSong(queue, currentIndexStream.value));
|
||||
_queueSubject.listen((queue) {
|
||||
_updateCurrentSong(queue, currentIndexStream.value);
|
||||
});
|
||||
}
|
||||
|
||||
static final _log = FimberLog('AudioPlayerRepositoryImpl');
|
||||
|
||||
final AudioPlayerDataSource _audioPlayerDataSource;
|
||||
final ManagedQueue _managedQueue;
|
||||
|
||||
// final BehaviorSubject<int> _currentIndexSubject = BehaviorSubject();
|
||||
final BehaviorSubject<int> _currentIndexSubject = BehaviorSubject();
|
||||
final BehaviorSubject<Song> _currentSongSubject = BehaviorSubject();
|
||||
final BehaviorSubject<LoopMode> _loopModeSubject = BehaviorSubject();
|
||||
final BehaviorSubject<List<Song>> _queueSubject = BehaviorSubject();
|
||||
|
@ -40,7 +48,7 @@ class AudioPlayerRepositoryImpl implements AudioPlayerRepository {
|
|||
ValueStream<List<Song>> get queueStream => _queueSubject.stream;
|
||||
|
||||
@override
|
||||
ValueStream<int> get currentIndexStream => _audioPlayerDataSource.currentIndexStream;
|
||||
ValueStream<int> get currentIndexStream => _currentIndexSubject.stream;
|
||||
|
||||
@override
|
||||
Stream<Song> get currentSongStream => _currentSongSubject.stream;
|
||||
|
@ -100,10 +108,16 @@ class AudioPlayerRepositoryImpl implements AudioPlayerRepository {
|
|||
|
||||
@override
|
||||
Future<void> moveQueueItem(int oldIndex, int newIndex) async {
|
||||
_audioPlayerDataSource.moveQueueItem(oldIndex, newIndex);
|
||||
|
||||
_managedQueue.moveQueueItem(oldIndex, newIndex);
|
||||
final newCurrentIndex = _calcNewCurrentIndexOnMove(
|
||||
currentIndexStream.value!,
|
||||
oldIndex,
|
||||
newIndex,
|
||||
);
|
||||
_currentIndexSubject.add(newCurrentIndex);
|
||||
_queueSubject.add(_managedQueue.queue);
|
||||
|
||||
_audioPlayerDataSource.moveQueueItem(oldIndex, newIndex);
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -126,10 +140,13 @@ class AudioPlayerRepositoryImpl implements AudioPlayerRepository {
|
|||
|
||||
@override
|
||||
Future<void> removeQueueIndex(int index) async {
|
||||
_audioPlayerDataSource.removeQueueIndex(index);
|
||||
|
||||
_managedQueue.removeQueueIndex(index);
|
||||
|
||||
final newCurrentIndex = _calcNewCurrentIndexOnRemove(currentIndexStream.value!, index);
|
||||
_currentIndexSubject.add(newCurrentIndex);
|
||||
_queueSubject.add(_managedQueue.queue);
|
||||
|
||||
_audioPlayerDataSource.removeQueueIndex(index);
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -201,7 +218,33 @@ class AudioPlayerRepositoryImpl implements AudioPlayerRepository {
|
|||
|
||||
void _updateCurrentSong(List<Song>? queue, int? index) {
|
||||
if (queue != null && index != null && index < queue.length) {
|
||||
_log.d('Current song: ${queue[index]}');
|
||||
_currentSongSubject.add(queue[index]);
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate the new current index when removing the song at [removeIndex].
|
||||
int _calcNewCurrentIndexOnRemove(int currentIndex, int removeIndex) {
|
||||
int result = currentIndex;
|
||||
if (removeIndex < currentIndex) {
|
||||
result--;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Calculate the new current index when moving a song from [oldIndex] to [newIndex].
|
||||
int _calcNewCurrentIndexOnMove(int currentIndex, int oldIndex, int newIndex) {
|
||||
int newCurrentIndex = currentIndex;
|
||||
if (oldIndex == currentIndex) {
|
||||
newCurrentIndex = newIndex;
|
||||
} else {
|
||||
if (oldIndex < currentIndex) {
|
||||
newCurrentIndex--;
|
||||
}
|
||||
if (newIndex < currentIndex) {
|
||||
newCurrentIndex++;
|
||||
}
|
||||
}
|
||||
return newCurrentIndex;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,9 +23,11 @@ dependencies:
|
|||
flutter_fimber: ^0.6.3
|
||||
flutter_fimber_filelogger: ^2.0.0
|
||||
flutter_mobx: ^2.0.0
|
||||
# path: ../mobx.dart/flutter_mobx
|
||||
get_it: ^7.1.3
|
||||
just_audio: ^0.7.5
|
||||
mobx: ^2.0.1
|
||||
# path: ../mobx.dart/mobx
|
||||
moor: ^4.3.2
|
||||
path: ^1.8.0
|
||||
path_provider: ^2.0.2
|
||||
|
@ -38,6 +40,7 @@ dev_dependencies:
|
|||
flutter_test:
|
||||
sdk: flutter
|
||||
mobx_codegen: ^2.0.1+3
|
||||
# path: ../mobx.dart/mobx_codegen
|
||||
mockito: ^5.0.10
|
||||
moor_generator: ^4.3.1
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue