Skip to content

Commit

Permalink
fix: Playback Speed Setting Not Retained Between Sessions (#1094)
Browse files Browse the repository at this point in the history
Fixes #1092
  • Loading branch information
Feichtmeier authored Dec 14, 2024
1 parent ba858d6 commit 9c54130
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 20 deletions.
5 changes: 0 additions & 5 deletions lib/app/view/mobile_bottom_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:watch_it/watch_it.dart';

import '../../extensions/build_context_x.dart';
import '../../player/player_model.dart';
import '../../player/view/bottom_player.dart';
import '../app_model.dart';
import 'mobile_navigation_bar.dart';
Expand All @@ -21,10 +20,6 @@ class MobileBottomBar extends StatelessWidget with WatchItMixin {
mainAxisSize: MainAxisSize.min,
children: [
GestureDetector(
onVerticalDragUpdate: (details) {
di<PlayerModel>().bottomPlayerHeight =
context.mediaQuerySize.height - details.globalPosition.dy;
},
onVerticalDragEnd: (details) {
if (details.primaryVelocity != null &&
details.primaryVelocity! < 150) {
Expand Down
16 changes: 13 additions & 3 deletions lib/common/data/player_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class PlayerState {
final String? queueName;
final List<Audio>? queue;
final String? volume;
final String? rate;

const PlayerState({
this.position,
Expand All @@ -19,6 +20,7 @@ class PlayerState {
this.queueName,
this.queue,
this.volume,
this.rate,
});

PlayerState copyWith({
Expand All @@ -28,6 +30,7 @@ class PlayerState {
String? queueName,
List<Audio>? queue,
String? volume,
String? rate,
}) {
return PlayerState(
position: position ?? this.position,
Expand All @@ -36,6 +39,7 @@ class PlayerState {
queueName: queueName ?? this.queueName,
queue: queue ?? this.queue,
volume: volume ?? this.volume,
rate: rate ?? this.rate,
);
}

Expand All @@ -60,6 +64,9 @@ class PlayerState {
if (volume != null) {
result.addAll({'volume': volume});
}
if (rate != null) {
result.addAll({'rate': rate});
}

return result;
}
Expand All @@ -74,6 +81,7 @@ class PlayerState {
? List<Audio>.from(map['queue']?.map((x) => Audio.fromMap(x)))
: null,
volume: map['volume'],
rate: map['rate'],
);
}

Expand All @@ -84,7 +92,7 @@ class PlayerState {

@override
String toString() {
return 'PlayerState(position: $position, duration: $duration, audio: $audio, queueName: $queueName, queue: $queue, volume: $volume)';
return 'PlayerState(position: $position, duration: $duration, audio: $audio, queueName: $queueName, queue: $queue, volume: $volume, rate: $rate,)';
}

@override
Expand All @@ -98,7 +106,8 @@ class PlayerState {
other.audio == audio &&
other.queueName == queueName &&
listEquals(other.queue, queue) &&
other.volume == volume;
other.volume == volume &&
other.rate == rate;
}

@override
Expand All @@ -108,6 +117,7 @@ class PlayerState {
audio.hashCode ^
queueName.hashCode ^
queue.hashCode ^
volume.hashCode;
volume.hashCode ^
rate.hashCode;
}
}
1 change: 1 addition & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,4 @@ const kUseMoreAnimations = 'useMoreAnimations';
const kShowPositionDuration = 'showPositionDuration';
const kSettingsPageId = 'settings';
const kHomePageId = 'homePage';
const kPlaybackRate = 'playbackRate';
4 changes: 2 additions & 2 deletions lib/player/player_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import '../common/view/theme.dart';
import '../radio/online_art_service.dart';
import 'player_service.dart';

const rateValues = [.75, 1.0, 1.25, 1.5, 1.75, 2.0];

class PlayerModel extends SafeChangeNotifier {
PlayerModel({
required PlayerService service,
required OnlineArtService onlineArtService,
}) : _playerService = service,
_onlineArtService = onlineArtService;

static const rateValues = [.75, 1.0, 1.25, 1.5, 1.75, 2.0];

final PlayerService _playerService;
final OnlineArtService _onlineArtService;

Expand Down
5 changes: 5 additions & 0 deletions lib/player/player_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,10 @@ class PlayerService {
setVolume(double.tryParse(playerState.volume!) ?? 100.0);
}

if (playerState.rate != null) {
setRate(double.tryParse(playerState.rate!) ?? 1.0);
}

_estimateNext();

await _setMediaControlsMetaData(audio: playerState.audio!);
Expand Down Expand Up @@ -818,6 +822,7 @@ class PlayerService {
_queue.audios.length <= 100 ? _queue.audios.take(100).toList() : null,
queueName: _queue.audios.length <= 100 ? _queue.name : null,
volume: _volume.toString(),
rate: _rate.toString(),
);

await writeJsonToFile(playerState.toMap(), kPlayerStateFileName);
Expand Down
18 changes: 8 additions & 10 deletions lib/player/view/playback_rate_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,14 @@ class PlaybackRateButton extends StatelessWidget with WatchItMixin {
: (color ?? theme.colorScheme.onSurface)),
),
initialValue: rate,
itemBuilder: (context) {
return rateValues
.map(
(e) => PopupMenuItem(
onTap: () => setRate(e),
child: Text('x$e'),
),
)
.toList();
},
itemBuilder: (context) => PlayerModel.rateValues
.map(
(e) => PopupMenuItem(
onTap: () => setRate(e),
child: Text('x$e'),
),
)
.toList(),
);
}
}

0 comments on commit 9c54130

Please sign in to comment.