diff --git a/lib/bloc/podcast/audio_bloc.dart b/lib/bloc/podcast/audio_bloc.dart index d47011f4..5a7e8670 100644 --- a/lib/bloc/podcast/audio_bloc.dart +++ b/lib/bloc/podcast/audio_bloc.dart @@ -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'; @@ -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); }); } diff --git a/lib/core/extensions.dart b/lib/core/extensions.dart index af1c55e4..a3b351ed 100644 --- a/lib/core/extensions.dart +++ b/lib/core/extensions.dart @@ -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 on Iterable { Iterable> chunk(int size) sync* { if (length <= 0) { @@ -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); + } +} diff --git a/lib/services/settings/mobile_settings_service.dart b/lib/services/settings/mobile_settings_service.dart index 9404dc38..f1afa152 100644 --- a/lib/services/settings/mobile_settings_service.dart +++ b/lib/services/settings/mobile_settings_service.dart @@ -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'; @@ -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 diff --git a/lib/ui/widgets/speed_selector.dart b/lib/ui/widgets/speed_selector.dart index 4f2e3d63..a50ce13a 100644 --- a/lib/ui/widgets/speed_selector.dart +++ b/lib/ui/widgets/speed_selector.dart @@ -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'; @@ -69,7 +70,7 @@ class _SpeedSelectorWidgetState extends State { 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, @@ -131,7 +132,7 @@ class _SpeedSliderState extends State { Padding( padding: const EdgeInsets.only(top: 16.0), child: Text( - '${speed.toString()}x', + '${speed.toStringAsFixed(1)}x', style: Theme.of(context).textTheme.headlineSmall, ), ), @@ -148,7 +149,7 @@ class _SpeedSliderState extends State { ? null : () { setState(() { - speed -= 0.25; + (speed -= 0.1).toTenth; audioBloc.playbackSpeed(speed); settingsBloc.setPlaybackSpeed(speed); }); @@ -158,10 +159,10 @@ class _SpeedSliderState extends State { 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; @@ -178,11 +179,11 @@ class _SpeedSliderState extends State { 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); });