shuffle button fixed for shared state mobile & desktop

This commit is contained in:
Kingkor Roy Tirtho 2022-05-13 12:17:47 +06:00
parent 9e98e8f7dd
commit 66a3b97dd0
2 changed files with 27 additions and 12 deletions

View File

@ -1,5 +1,4 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:just_audio/just_audio.dart'; import 'package:just_audio/just_audio.dart';
import 'package:spotube/helpers/zero-pad-num-str.dart'; import 'package:spotube/helpers/zero-pad-num-str.dart';
@ -21,8 +20,6 @@ class PlayerControls extends HookConsumerWidget {
final Playback playback = ref.watch(playbackProvider); final Playback playback = ref.watch(playbackProvider);
final AudioPlayer player = playback.player; final AudioPlayer player = playback.player;
final _shuffled = useState(false);
final onNext = useNextTrack(playback); final onNext = useNextTrack(playback);
final onPrevious = usePreviousTrack(playback); final onPrevious = usePreviousTrack(playback);
@ -92,7 +89,7 @@ class PlayerControls extends HookConsumerWidget {
children: [ children: [
IconButton( IconButton(
icon: const Icon(Icons.shuffle_rounded), icon: const Icon(Icons.shuffle_rounded),
color: _shuffled.value color: playback.shuffled
? Theme.of(context).primaryColor ? Theme.of(context).primaryColor
: iconColor, : iconColor,
onPressed: () { onPressed: () {
@ -101,12 +98,10 @@ class PlayerControls extends HookConsumerWidget {
return; return;
} }
try { try {
if (!_shuffled.value) { if (!playback.shuffled) {
playback.currentPlaylist!.shuffle(); playback.shuffle();
_shuffled.value = true;
} else { } else {
playback.currentPlaylist!.unshuffle(); playback.unshuffle();
_shuffled.value = false;
} }
} catch (e, stack) { } catch (e, stack) {
logger.e("onShuffle", e, stack); logger.e("onShuffle", e, stack);
@ -140,7 +135,6 @@ class PlayerControls extends HookConsumerWidget {
try { try {
await player.pause(); await player.pause();
await player.seek(Duration.zero); await player.seek(Duration.zero);
_shuffled.value = false;
playback.reset(); playback.reset();
} catch (e, stack) { } catch (e, stack) {
logger.e("onStop", e, stack); logger.e("onStop", e, stack);

View File

@ -31,20 +31,24 @@ class CurrentPlaylist {
List<String> get trackIds => tracks.map((e) => e.id!).toList(); List<String> get trackIds => tracks.map((e) => e.id!).toList();
void shuffle() { bool shuffle() {
// won't shuffle if already shuffled // won't shuffle if already shuffled
if (_tempTrack == null) { if (_tempTrack == null) {
_tempTrack = [...tracks]; _tempTrack = [...tracks];
tracks.shuffle(); tracks.shuffle();
return true;
} }
return false;
} }
void unshuffle() { bool unshuffle() {
// without _tempTracks unshuffling can't be done // without _tempTracks unshuffling can't be done
if (_tempTrack != null) { if (_tempTrack != null) {
tracks = [..._tempTrack!]; tracks = [..._tempTrack!];
_tempTrack = null; _tempTrack = null;
return true;
} }
return false;
} }
} }
@ -66,6 +70,7 @@ class Playback extends ChangeNotifier {
StreamSubscription<Duration>? _positionStreamListener; StreamSubscription<Duration>? _positionStreamListener;
Duration _prevPosition = Duration.zero; Duration _prevPosition = Duration.zero;
bool _shuffled = false;
AudioPlayer player; AudioPlayer player;
YoutubeExplode youtube; YoutubeExplode youtube;
@ -138,6 +143,7 @@ class Playback extends ChangeNotifier {
}); });
} }
bool get shuffled => _shuffled;
CurrentPlaylist? get currentPlaylist => _currentPlaylist; CurrentPlaylist? get currentPlaylist => _currentPlaylist;
Track? get currentTrack => _currentTrack; Track? get currentTrack => _currentTrack;
bool get isPlaying => _isPlaying; bool get isPlaying => _isPlaying;
@ -158,6 +164,7 @@ class Playback extends ChangeNotifier {
void reset() { void reset() {
_logger.v("Playback Reset"); _logger.v("Playback Reset");
_isPlaying = false; _isPlaying = false;
_shuffled = false;
duration = null; duration = null;
_currentPlaylist = null; _currentPlaylist = null;
_currentTrack = null; _currentTrack = null;
@ -265,6 +272,20 @@ class Playback extends ChangeNotifier {
_logger.e("startPlaying", e, stack); _logger.e("startPlaying", e, stack);
} }
} }
void shuffle() {
if (currentPlaylist?.shuffle() == true) {
_shuffled = true;
notifyListeners();
}
}
void unshuffle() {
if (currentPlaylist?.unshuffle() == true) {
_shuffled = false;
notifyListeners();
}
}
} }
final playbackProvider = ChangeNotifierProvider<Playback>((ref) { final playbackProvider = ChangeNotifierProvider<Playback>((ref) {