Skip to content

Commit

Permalink
Update speed selector to use 0.1 value increments and top speed as 3x.
Browse files Browse the repository at this point in the history
  • Loading branch information
amugofjava committed Jul 21, 2024
1 parent 0a90cd5 commit 760d812
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/bloc/podcast/audio_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';

import 'package:anytime/bloc/bloc.dart';
import 'package:anytime/core/extensions.dart';
import 'package:anytime/entities/episode.dart';
import 'package:anytime/entities/sleep.dart';
import 'package:anytime/services/audio/audio_player_service.dart';
Expand Down Expand Up @@ -130,7 +131,7 @@ class AudioBloc extends Bloc {
/// Listen for requests to adjust the playback speed.
void _handlePlaybackSpeedTransitions() {
_playbackSpeedSubject.listen((double speed) async {
await audioPlayerService.setPlaybackSpeed(speed);
await audioPlayerService.setPlaybackSpeed(speed.toTenth);
});
}

Expand Down
9 changes: 9 additions & 0 deletions lib/core/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math';

extension IterableExtensions<E> on Iterable<E> {
Iterable<List<E>> chunk(int size) sync* {
if (length <= 0) {
Expand Down Expand Up @@ -38,3 +40,10 @@ extension ExtString on String? {
return this ?? '';
}
}

extension ExtDouble on double {
double get toTenth {
var mod = pow(10.0, 1).toDouble();
return ((this * mod).round().toDouble() / mod);
}
}
9 changes: 8 additions & 1 deletion lib/services/settings/mobile_settings_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math';

import 'package:anytime/core/environment.dart';
import 'package:anytime/entities/app_settings.dart';
import 'package:anytime/services/settings/settings_service.dart';
Expand Down Expand Up @@ -67,7 +69,12 @@ class MobileSettingsService extends SettingsService {

@override
double get playbackSpeed {
return _sharedPreferences.getDouble('speed') ?? 1.0;
var speed = _sharedPreferences.getDouble('speed') ?? 1.0;

// We used to use 0.25 increments and now we use 0.1. Round
// any setting that uses the old 0.25.
var mod = pow(10.0, 1).toDouble();
return ((speed * mod).round().toDouble() / mod);
}

@override
Expand Down
17 changes: 9 additions & 8 deletions lib/ui/widgets/speed_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:anytime/bloc/podcast/audio_bloc.dart';
import 'package:anytime/bloc/settings/settings_bloc.dart';
import 'package:anytime/core/extensions.dart';
import 'package:anytime/entities/app_settings.dart';
import 'package:anytime/l10n/L.dart';
import 'package:anytime/ui/widgets/slider_handle.dart';
Expand Down Expand Up @@ -69,7 +70,7 @@ class _SpeedSelectorWidgetState extends State<SpeedSelectorWidget> {
width: 48.0,
child: Center(
child: Text(
snapshot.data!.playbackSpeed == 1.0 ? 'x1' : 'x${snapshot.data!.playbackSpeed}',
snapshot.data!.playbackSpeed == 1.0 ? 'x1' : 'x${snapshot.data!.playbackSpeed.toTenth}',
semanticsLabel: L.of(context)!.playback_speed_label,
style: TextStyle(
fontSize: 16.0,
Expand Down Expand Up @@ -131,7 +132,7 @@ class _SpeedSliderState extends State<SpeedSlider> {
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: Text(
'${speed.toString()}x',
'${speed.toStringAsFixed(1)}x',
style: Theme.of(context).textTheme.headlineSmall,
),
),
Expand All @@ -148,7 +149,7 @@ class _SpeedSliderState extends State<SpeedSlider> {
? null
: () {
setState(() {
speed -= 0.25;
(speed -= 0.1).toTenth;
audioBloc.playbackSpeed(speed);
settingsBloc.setPlaybackSpeed(speed);
});
Expand All @@ -158,10 +159,10 @@ class _SpeedSliderState extends State<SpeedSlider> {
Expanded(
flex: 4,
child: Slider(
value: speed,
value: speed.toTenth,
min: 0.5,
max: 2.0,
divisions: 6,
max: 3.0,
divisions: 26,
onChanged: (value) {
setState(() {
speed = value;
Expand All @@ -178,11 +179,11 @@ class _SpeedSliderState extends State<SpeedSlider> {
tooltip: L.of(context)!.semantics_increase_playback_speed,
iconSize: 28.0,
icon: const Icon(Icons.add_circle_outline),
onPressed: (speed >= 2.0)
onPressed: (speed >= 3.0)
? null
: () {
setState(() {
speed += 0.25;
(speed += 0.1).toTenth;
audioBloc.playbackSpeed(speed);
settingsBloc.setPlaybackSpeed(speed);
});
Expand Down

0 comments on commit 760d812

Please sign in to comment.