feat: implement per book settings with player configuration and update methods

This commit is contained in:
Dr-Blank 2024-09-17 23:19:05 -04:00
parent fd42ee2343
commit e7dd4de515
No known key found for this signature in database
GPG key ID: 7452CC63F210A266
19 changed files with 1030 additions and 20 deletions

View file

@ -1,5 +1,6 @@
import 'package:flutter/foundation.dart' show immutable;
import 'package:hive/hive.dart';
import 'package:vaani/features/per_book_settings/models/book_settings.dart';
import 'package:vaani/settings/models/models.dart';
@immutable
@ -19,4 +20,8 @@ class AvailableHiveBoxes {
/// stores the a list of [AuthenticatedUser]
static final authenticatedUserBox =
Hive.box<AuthenticatedUser>(name: 'authenticatedUser');
/// stores the a list of [BookSettings]
static final individualBookSettingsBox =
Hive.box<BookSettings>(name: 'bookSettings');
}

View file

@ -1,4 +1,5 @@
import 'package:hive/hive.dart';
import 'package:vaani/features/per_book_settings/models/book_settings.dart';
import 'package:vaani/settings/models/models.dart';
// register all models to Hive for serialization
@ -19,4 +20,12 @@ Future registerModels() async {
'AuthenticatedUser',
((json) => AuthenticatedUser.fromJson(json)),
);
Hive.registerAdapter<BookSettings>(
'BookSettings',
((json) => BookSettings.fromJson(json)),
);
Hive.registerAdapter<BookSettings>(
'_\$BookSettingsImpl', // hack because of freezed
((json) => BookSettings.fromJson(json)),
);
}

View file

@ -11,6 +11,7 @@ import 'package:vaani/features/downloads/providers/download_manager.dart'
downloadStatusProvider,
simpleDownloadManagerProvider;
import 'package:vaani/features/item_viewer/view/library_item_page.dart';
import 'package:vaani/features/per_book_settings/providers/book_settings_provider.dart';
import 'package:vaani/features/player/providers/audiobook_player.dart';
import 'package:vaani/features/player/providers/player_form.dart';
import 'package:vaani/main.dart';
@ -423,6 +424,7 @@ class DynamicItemPlayIcon extends StatelessWidget {
}
}
/// Handles the play button pressed on the library item
Future<void> libraryItemPlayButtonOnPressed({
required WidgetRef ref,
required shelfsdk.BookExpanded book,
@ -434,6 +436,7 @@ Future<void> libraryItemPlayButtonOnPressed({
final isCurrentBookSetInPlayer = player.book == book;
final isPlayingThisBook = player.playing && isCurrentBookSetInPlayer;
Future<void>? setSourceFuture;
// set the book to the player if not already set
if (!isCurrentBookSetInPlayer) {
debugPrint('Setting the book ${book.libraryItemId}');
@ -442,7 +445,7 @@ Future<void> libraryItemPlayButtonOnPressed({
final libItem =
await ref.read(libraryItemProvider(book.libraryItemId).future);
final downloadedUris = await downloadManager.getDownloadedFiles(libItem);
await player.setSourceAudiobook(
setSourceFuture = player.setSourceAudiobook(
book,
initialPosition: userMediaProgress?.currentTime,
downloadedUris: downloadedUris,
@ -455,10 +458,32 @@ Future<void> libraryItemPlayButtonOnPressed({
return;
}
}
// set the volume as this is the first time playing and dismissing causes the volume to go to 0
var bookPlayerSettings =
ref.read(bookSettingsProvider(book.libraryItemId)).playerSettings;
var appPlayerSettings = ref.read(appSettingsProvider).playerSettings;
var configurePlayerForEveryBook =
appPlayerSettings.configurePlayerForEveryBook;
await Future.wait([
setSourceFuture ?? Future.value(),
// set the volume
player.setVolume(
configurePlayerForEveryBook
? bookPlayerSettings.preferredDefaultVolume ??
appPlayerSettings.preferredDefaultVolume
: appPlayerSettings.preferredDefaultVolume,
),
// set the speed
player.setSpeed(
configurePlayerForEveryBook
? bookPlayerSettings.preferredDefaultSpeed ??
appPlayerSettings.preferredDefaultSpeed
: appPlayerSettings.preferredDefaultSpeed,
),
]);
// toggle play/pause
await player.play();
// set the volume as this is the first time playing and dismissing causes the volume to go to 0
await player.setVolume(
ref.read(appSettingsProvider).playerSettings.preferredDefaultVolume,
);
}

View file

@ -0,0 +1,17 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:vaani/features/per_book_settings/models/nullable_player_settings.dart';
part 'book_settings.freezed.dart';
part 'book_settings.g.dart';
/// per book settings
@freezed
class BookSettings with _$BookSettings {
const factory BookSettings({
required String bookId,
@Default(NullablePlayerSettings()) NullablePlayerSettings playerSettings,
}) = _BookSettings;
factory BookSettings.fromJson(Map<String, dynamic> json) =>
_$BookSettingsFromJson(json);
}

View file

@ -0,0 +1,203 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
part of 'book_settings.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
T _$identity<T>(T value) => value;
final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');
BookSettings _$BookSettingsFromJson(Map<String, dynamic> json) {
return _BookSettings.fromJson(json);
}
/// @nodoc
mixin _$BookSettings {
String get bookId => throw _privateConstructorUsedError;
NullablePlayerSettings get playerSettings =>
throw _privateConstructorUsedError;
/// Serializes this BookSettings to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
/// Create a copy of BookSettings
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$BookSettingsCopyWith<BookSettings> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $BookSettingsCopyWith<$Res> {
factory $BookSettingsCopyWith(
BookSettings value, $Res Function(BookSettings) then) =
_$BookSettingsCopyWithImpl<$Res, BookSettings>;
@useResult
$Res call({String bookId, NullablePlayerSettings playerSettings});
$NullablePlayerSettingsCopyWith<$Res> get playerSettings;
}
/// @nodoc
class _$BookSettingsCopyWithImpl<$Res, $Val extends BookSettings>
implements $BookSettingsCopyWith<$Res> {
_$BookSettingsCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of BookSettings
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? bookId = null,
Object? playerSettings = null,
}) {
return _then(_value.copyWith(
bookId: null == bookId
? _value.bookId
: bookId // ignore: cast_nullable_to_non_nullable
as String,
playerSettings: null == playerSettings
? _value.playerSettings
: playerSettings // ignore: cast_nullable_to_non_nullable
as NullablePlayerSettings,
) as $Val);
}
/// Create a copy of BookSettings
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$NullablePlayerSettingsCopyWith<$Res> get playerSettings {
return $NullablePlayerSettingsCopyWith<$Res>(_value.playerSettings,
(value) {
return _then(_value.copyWith(playerSettings: value) as $Val);
});
}
}
/// @nodoc
abstract class _$$BookSettingsImplCopyWith<$Res>
implements $BookSettingsCopyWith<$Res> {
factory _$$BookSettingsImplCopyWith(
_$BookSettingsImpl value, $Res Function(_$BookSettingsImpl) then) =
__$$BookSettingsImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({String bookId, NullablePlayerSettings playerSettings});
@override
$NullablePlayerSettingsCopyWith<$Res> get playerSettings;
}
/// @nodoc
class __$$BookSettingsImplCopyWithImpl<$Res>
extends _$BookSettingsCopyWithImpl<$Res, _$BookSettingsImpl>
implements _$$BookSettingsImplCopyWith<$Res> {
__$$BookSettingsImplCopyWithImpl(
_$BookSettingsImpl _value, $Res Function(_$BookSettingsImpl) _then)
: super(_value, _then);
/// Create a copy of BookSettings
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? bookId = null,
Object? playerSettings = null,
}) {
return _then(_$BookSettingsImpl(
bookId: null == bookId
? _value.bookId
: bookId // ignore: cast_nullable_to_non_nullable
as String,
playerSettings: null == playerSettings
? _value.playerSettings
: playerSettings // ignore: cast_nullable_to_non_nullable
as NullablePlayerSettings,
));
}
}
/// @nodoc
@JsonSerializable()
class _$BookSettingsImpl implements _BookSettings {
const _$BookSettingsImpl(
{required this.bookId,
this.playerSettings = const NullablePlayerSettings()});
factory _$BookSettingsImpl.fromJson(Map<String, dynamic> json) =>
_$$BookSettingsImplFromJson(json);
@override
final String bookId;
@override
@JsonKey()
final NullablePlayerSettings playerSettings;
@override
String toString() {
return 'BookSettings(bookId: $bookId, playerSettings: $playerSettings)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$BookSettingsImpl &&
(identical(other.bookId, bookId) || other.bookId == bookId) &&
(identical(other.playerSettings, playerSettings) ||
other.playerSettings == playerSettings));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType, bookId, playerSettings);
/// Create a copy of BookSettings
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$BookSettingsImplCopyWith<_$BookSettingsImpl> get copyWith =>
__$$BookSettingsImplCopyWithImpl<_$BookSettingsImpl>(this, _$identity);
@override
Map<String, dynamic> toJson() {
return _$$BookSettingsImplToJson(
this,
);
}
}
abstract class _BookSettings implements BookSettings {
const factory _BookSettings(
{required final String bookId,
final NullablePlayerSettings playerSettings}) = _$BookSettingsImpl;
factory _BookSettings.fromJson(Map<String, dynamic> json) =
_$BookSettingsImpl.fromJson;
@override
String get bookId;
@override
NullablePlayerSettings get playerSettings;
/// Create a copy of BookSettings
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$BookSettingsImplCopyWith<_$BookSettingsImpl> get copyWith =>
throw _privateConstructorUsedError;
}

View file

@ -0,0 +1,22 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'book_settings.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
_$BookSettingsImpl _$$BookSettingsImplFromJson(Map<String, dynamic> json) =>
_$BookSettingsImpl(
bookId: json['bookId'] as String,
playerSettings: json['playerSettings'] == null
? const NullablePlayerSettings()
: NullablePlayerSettings.fromJson(
json['playerSettings'] as Map<String, dynamic>),
);
Map<String, dynamic> _$$BookSettingsImplToJson(_$BookSettingsImpl instance) =>
<String, dynamic>{
'bookId': instance.bookId,
'playerSettings': instance.playerSettings,
};

View file

@ -0,0 +1,21 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:vaani/settings/models/app_settings.dart';
part 'nullable_player_settings.freezed.dart';
part 'nullable_player_settings.g.dart';
@freezed
class NullablePlayerSettings with _$NullablePlayerSettings {
const factory NullablePlayerSettings({
MinimizedPlayerSettings? miniPlayerSettings,
ExpandedPlayerSettings? expandedPlayerSettings,
double? preferredDefaultVolume,
double? preferredDefaultSpeed,
List<double>? speedOptions,
SleepTimerSettings? sleepTimerSettings,
Duration? playbackReportInterval,
}) = _NullablePlayerSettings;
factory NullablePlayerSettings.fromJson(Map<String, dynamic> json) =>
_$NullablePlayerSettingsFromJson(json);
}

View file

@ -0,0 +1,377 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
part of 'nullable_player_settings.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
T _$identity<T>(T value) => value;
final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');
NullablePlayerSettings _$NullablePlayerSettingsFromJson(
Map<String, dynamic> json) {
return _NullablePlayerSettings.fromJson(json);
}
/// @nodoc
mixin _$NullablePlayerSettings {
MinimizedPlayerSettings? get miniPlayerSettings =>
throw _privateConstructorUsedError;
ExpandedPlayerSettings? get expandedPlayerSettings =>
throw _privateConstructorUsedError;
double? get preferredDefaultVolume => throw _privateConstructorUsedError;
double? get preferredDefaultSpeed => throw _privateConstructorUsedError;
List<double>? get speedOptions => throw _privateConstructorUsedError;
SleepTimerSettings? get sleepTimerSettings =>
throw _privateConstructorUsedError;
Duration? get playbackReportInterval => throw _privateConstructorUsedError;
/// Serializes this NullablePlayerSettings to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
/// Create a copy of NullablePlayerSettings
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$NullablePlayerSettingsCopyWith<NullablePlayerSettings> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $NullablePlayerSettingsCopyWith<$Res> {
factory $NullablePlayerSettingsCopyWith(NullablePlayerSettings value,
$Res Function(NullablePlayerSettings) then) =
_$NullablePlayerSettingsCopyWithImpl<$Res, NullablePlayerSettings>;
@useResult
$Res call(
{MinimizedPlayerSettings? miniPlayerSettings,
ExpandedPlayerSettings? expandedPlayerSettings,
double? preferredDefaultVolume,
double? preferredDefaultSpeed,
List<double>? speedOptions,
SleepTimerSettings? sleepTimerSettings,
Duration? playbackReportInterval});
$MinimizedPlayerSettingsCopyWith<$Res>? get miniPlayerSettings;
$ExpandedPlayerSettingsCopyWith<$Res>? get expandedPlayerSettings;
$SleepTimerSettingsCopyWith<$Res>? get sleepTimerSettings;
}
/// @nodoc
class _$NullablePlayerSettingsCopyWithImpl<$Res,
$Val extends NullablePlayerSettings>
implements $NullablePlayerSettingsCopyWith<$Res> {
_$NullablePlayerSettingsCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of NullablePlayerSettings
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? miniPlayerSettings = freezed,
Object? expandedPlayerSettings = freezed,
Object? preferredDefaultVolume = freezed,
Object? preferredDefaultSpeed = freezed,
Object? speedOptions = freezed,
Object? sleepTimerSettings = freezed,
Object? playbackReportInterval = freezed,
}) {
return _then(_value.copyWith(
miniPlayerSettings: freezed == miniPlayerSettings
? _value.miniPlayerSettings
: miniPlayerSettings // ignore: cast_nullable_to_non_nullable
as MinimizedPlayerSettings?,
expandedPlayerSettings: freezed == expandedPlayerSettings
? _value.expandedPlayerSettings
: expandedPlayerSettings // ignore: cast_nullable_to_non_nullable
as ExpandedPlayerSettings?,
preferredDefaultVolume: freezed == preferredDefaultVolume
? _value.preferredDefaultVolume
: preferredDefaultVolume // ignore: cast_nullable_to_non_nullable
as double?,
preferredDefaultSpeed: freezed == preferredDefaultSpeed
? _value.preferredDefaultSpeed
: preferredDefaultSpeed // ignore: cast_nullable_to_non_nullable
as double?,
speedOptions: freezed == speedOptions
? _value.speedOptions
: speedOptions // ignore: cast_nullable_to_non_nullable
as List<double>?,
sleepTimerSettings: freezed == sleepTimerSettings
? _value.sleepTimerSettings
: sleepTimerSettings // ignore: cast_nullable_to_non_nullable
as SleepTimerSettings?,
playbackReportInterval: freezed == playbackReportInterval
? _value.playbackReportInterval
: playbackReportInterval // ignore: cast_nullable_to_non_nullable
as Duration?,
) as $Val);
}
/// Create a copy of NullablePlayerSettings
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$MinimizedPlayerSettingsCopyWith<$Res>? get miniPlayerSettings {
if (_value.miniPlayerSettings == null) {
return null;
}
return $MinimizedPlayerSettingsCopyWith<$Res>(_value.miniPlayerSettings!,
(value) {
return _then(_value.copyWith(miniPlayerSettings: value) as $Val);
});
}
/// Create a copy of NullablePlayerSettings
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$ExpandedPlayerSettingsCopyWith<$Res>? get expandedPlayerSettings {
if (_value.expandedPlayerSettings == null) {
return null;
}
return $ExpandedPlayerSettingsCopyWith<$Res>(_value.expandedPlayerSettings!,
(value) {
return _then(_value.copyWith(expandedPlayerSettings: value) as $Val);
});
}
/// Create a copy of NullablePlayerSettings
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$SleepTimerSettingsCopyWith<$Res>? get sleepTimerSettings {
if (_value.sleepTimerSettings == null) {
return null;
}
return $SleepTimerSettingsCopyWith<$Res>(_value.sleepTimerSettings!,
(value) {
return _then(_value.copyWith(sleepTimerSettings: value) as $Val);
});
}
}
/// @nodoc
abstract class _$$NullablePlayerSettingsImplCopyWith<$Res>
implements $NullablePlayerSettingsCopyWith<$Res> {
factory _$$NullablePlayerSettingsImplCopyWith(
_$NullablePlayerSettingsImpl value,
$Res Function(_$NullablePlayerSettingsImpl) then) =
__$$NullablePlayerSettingsImplCopyWithImpl<$Res>;
@override
@useResult
$Res call(
{MinimizedPlayerSettings? miniPlayerSettings,
ExpandedPlayerSettings? expandedPlayerSettings,
double? preferredDefaultVolume,
double? preferredDefaultSpeed,
List<double>? speedOptions,
SleepTimerSettings? sleepTimerSettings,
Duration? playbackReportInterval});
@override
$MinimizedPlayerSettingsCopyWith<$Res>? get miniPlayerSettings;
@override
$ExpandedPlayerSettingsCopyWith<$Res>? get expandedPlayerSettings;
@override
$SleepTimerSettingsCopyWith<$Res>? get sleepTimerSettings;
}
/// @nodoc
class __$$NullablePlayerSettingsImplCopyWithImpl<$Res>
extends _$NullablePlayerSettingsCopyWithImpl<$Res,
_$NullablePlayerSettingsImpl>
implements _$$NullablePlayerSettingsImplCopyWith<$Res> {
__$$NullablePlayerSettingsImplCopyWithImpl(
_$NullablePlayerSettingsImpl _value,
$Res Function(_$NullablePlayerSettingsImpl) _then)
: super(_value, _then);
/// Create a copy of NullablePlayerSettings
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? miniPlayerSettings = freezed,
Object? expandedPlayerSettings = freezed,
Object? preferredDefaultVolume = freezed,
Object? preferredDefaultSpeed = freezed,
Object? speedOptions = freezed,
Object? sleepTimerSettings = freezed,
Object? playbackReportInterval = freezed,
}) {
return _then(_$NullablePlayerSettingsImpl(
miniPlayerSettings: freezed == miniPlayerSettings
? _value.miniPlayerSettings
: miniPlayerSettings // ignore: cast_nullable_to_non_nullable
as MinimizedPlayerSettings?,
expandedPlayerSettings: freezed == expandedPlayerSettings
? _value.expandedPlayerSettings
: expandedPlayerSettings // ignore: cast_nullable_to_non_nullable
as ExpandedPlayerSettings?,
preferredDefaultVolume: freezed == preferredDefaultVolume
? _value.preferredDefaultVolume
: preferredDefaultVolume // ignore: cast_nullable_to_non_nullable
as double?,
preferredDefaultSpeed: freezed == preferredDefaultSpeed
? _value.preferredDefaultSpeed
: preferredDefaultSpeed // ignore: cast_nullable_to_non_nullable
as double?,
speedOptions: freezed == speedOptions
? _value._speedOptions
: speedOptions // ignore: cast_nullable_to_non_nullable
as List<double>?,
sleepTimerSettings: freezed == sleepTimerSettings
? _value.sleepTimerSettings
: sleepTimerSettings // ignore: cast_nullable_to_non_nullable
as SleepTimerSettings?,
playbackReportInterval: freezed == playbackReportInterval
? _value.playbackReportInterval
: playbackReportInterval // ignore: cast_nullable_to_non_nullable
as Duration?,
));
}
}
/// @nodoc
@JsonSerializable()
class _$NullablePlayerSettingsImpl implements _NullablePlayerSettings {
const _$NullablePlayerSettingsImpl(
{this.miniPlayerSettings,
this.expandedPlayerSettings,
this.preferredDefaultVolume,
this.preferredDefaultSpeed,
final List<double>? speedOptions,
this.sleepTimerSettings,
this.playbackReportInterval})
: _speedOptions = speedOptions;
factory _$NullablePlayerSettingsImpl.fromJson(Map<String, dynamic> json) =>
_$$NullablePlayerSettingsImplFromJson(json);
@override
final MinimizedPlayerSettings? miniPlayerSettings;
@override
final ExpandedPlayerSettings? expandedPlayerSettings;
@override
final double? preferredDefaultVolume;
@override
final double? preferredDefaultSpeed;
final List<double>? _speedOptions;
@override
List<double>? get speedOptions {
final value = _speedOptions;
if (value == null) return null;
if (_speedOptions is EqualUnmodifiableListView) return _speedOptions;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(value);
}
@override
final SleepTimerSettings? sleepTimerSettings;
@override
final Duration? playbackReportInterval;
@override
String toString() {
return 'NullablePlayerSettings(miniPlayerSettings: $miniPlayerSettings, expandedPlayerSettings: $expandedPlayerSettings, preferredDefaultVolume: $preferredDefaultVolume, preferredDefaultSpeed: $preferredDefaultSpeed, speedOptions: $speedOptions, sleepTimerSettings: $sleepTimerSettings, playbackReportInterval: $playbackReportInterval)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$NullablePlayerSettingsImpl &&
(identical(other.miniPlayerSettings, miniPlayerSettings) ||
other.miniPlayerSettings == miniPlayerSettings) &&
(identical(other.expandedPlayerSettings, expandedPlayerSettings) ||
other.expandedPlayerSettings == expandedPlayerSettings) &&
(identical(other.preferredDefaultVolume, preferredDefaultVolume) ||
other.preferredDefaultVolume == preferredDefaultVolume) &&
(identical(other.preferredDefaultSpeed, preferredDefaultSpeed) ||
other.preferredDefaultSpeed == preferredDefaultSpeed) &&
const DeepCollectionEquality()
.equals(other._speedOptions, _speedOptions) &&
(identical(other.sleepTimerSettings, sleepTimerSettings) ||
other.sleepTimerSettings == sleepTimerSettings) &&
(identical(other.playbackReportInterval, playbackReportInterval) ||
other.playbackReportInterval == playbackReportInterval));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(
runtimeType,
miniPlayerSettings,
expandedPlayerSettings,
preferredDefaultVolume,
preferredDefaultSpeed,
const DeepCollectionEquality().hash(_speedOptions),
sleepTimerSettings,
playbackReportInterval);
/// Create a copy of NullablePlayerSettings
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$NullablePlayerSettingsImplCopyWith<_$NullablePlayerSettingsImpl>
get copyWith => __$$NullablePlayerSettingsImplCopyWithImpl<
_$NullablePlayerSettingsImpl>(this, _$identity);
@override
Map<String, dynamic> toJson() {
return _$$NullablePlayerSettingsImplToJson(
this,
);
}
}
abstract class _NullablePlayerSettings implements NullablePlayerSettings {
const factory _NullablePlayerSettings(
{final MinimizedPlayerSettings? miniPlayerSettings,
final ExpandedPlayerSettings? expandedPlayerSettings,
final double? preferredDefaultVolume,
final double? preferredDefaultSpeed,
final List<double>? speedOptions,
final SleepTimerSettings? sleepTimerSettings,
final Duration? playbackReportInterval}) = _$NullablePlayerSettingsImpl;
factory _NullablePlayerSettings.fromJson(Map<String, dynamic> json) =
_$NullablePlayerSettingsImpl.fromJson;
@override
MinimizedPlayerSettings? get miniPlayerSettings;
@override
ExpandedPlayerSettings? get expandedPlayerSettings;
@override
double? get preferredDefaultVolume;
@override
double? get preferredDefaultSpeed;
@override
List<double>? get speedOptions;
@override
SleepTimerSettings? get sleepTimerSettings;
@override
Duration? get playbackReportInterval;
/// Create a copy of NullablePlayerSettings
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$NullablePlayerSettingsImplCopyWith<_$NullablePlayerSettingsImpl>
get copyWith => throw _privateConstructorUsedError;
}

View file

@ -0,0 +1,47 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'nullable_player_settings.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
_$NullablePlayerSettingsImpl _$$NullablePlayerSettingsImplFromJson(
Map<String, dynamic> json) =>
_$NullablePlayerSettingsImpl(
miniPlayerSettings: json['miniPlayerSettings'] == null
? null
: MinimizedPlayerSettings.fromJson(
json['miniPlayerSettings'] as Map<String, dynamic>),
expandedPlayerSettings: json['expandedPlayerSettings'] == null
? null
: ExpandedPlayerSettings.fromJson(
json['expandedPlayerSettings'] as Map<String, dynamic>),
preferredDefaultVolume:
(json['preferredDefaultVolume'] as num?)?.toDouble(),
preferredDefaultSpeed:
(json['preferredDefaultSpeed'] as num?)?.toDouble(),
speedOptions: (json['speedOptions'] as List<dynamic>?)
?.map((e) => (e as num).toDouble())
.toList(),
sleepTimerSettings: json['sleepTimerSettings'] == null
? null
: SleepTimerSettings.fromJson(
json['sleepTimerSettings'] as Map<String, dynamic>),
playbackReportInterval: json['playbackReportInterval'] == null
? null
: Duration(
microseconds: (json['playbackReportInterval'] as num).toInt()),
);
Map<String, dynamic> _$$NullablePlayerSettingsImplToJson(
_$NullablePlayerSettingsImpl instance) =>
<String, dynamic>{
'miniPlayerSettings': instance.miniPlayerSettings,
'expandedPlayerSettings': instance.expandedPlayerSettings,
'preferredDefaultVolume': instance.preferredDefaultVolume,
'preferredDefaultSpeed': instance.preferredDefaultSpeed,
'speedOptions': instance.speedOptions,
'sleepTimerSettings': instance.sleepTimerSettings,
'playbackReportInterval': instance.playbackReportInterval?.inMicroseconds,
};

View file

@ -0,0 +1,57 @@
import 'package:logging/logging.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:vaani/db/available_boxes.dart';
import 'package:vaani/features/per_book_settings/models/book_settings.dart'
as model;
import 'package:vaani/features/per_book_settings/models/nullable_player_settings.dart';
part 'book_settings_provider.g.dart';
final _box = AvailableHiveBoxes.individualBookSettingsBox;
final _logger = Logger('BookSettingsProvider');
model.BookSettings readFromBoxOrCreate(String bookId) {
final foundSettings = _box.get(bookId);
if (foundSettings != null) {
_logger.fine('found book settings for $bookId in box: $foundSettings');
return foundSettings;
} else {
// create a new settings object
final settings = model.BookSettings(
bookId: bookId,
playerSettings: const NullablePlayerSettings(),
);
_logger.fine('created new book settings for $bookId: $settings');
writeToBox(settings);
return settings;
}
}
void writeToBox(model.BookSettings newSettings) {
_box.put(newSettings.bookId, newSettings);
_logger.fine(
'wrote book settings for ${newSettings.bookId} to box: $newSettings',
);
}
void updateState(model.BookSettings newSettings, {bool force = false}) {
// check if the settings are different
final foundSettings = _box.get(newSettings.bookId);
if (foundSettings == newSettings && !force) {
return;
}
writeToBox(newSettings);
}
@riverpod
class BookSettings extends _$BookSettings {
@override
model.BookSettings build(String bookId) {
return readFromBoxOrCreate(bookId);
}
void update(model.BookSettings newSettings, {bool force = false}) {
updateState(newSettings, force: force);
}
}

View file

@ -0,0 +1,174 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'book_settings_provider.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$bookSettingsHash() => r'b976df954edf98ec6ccb3eb41e9d07dd4a9193eb';
/// Copied from Dart SDK
class _SystemHash {
_SystemHash._();
static int combine(int hash, int value) {
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + value);
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
return hash ^ (hash >> 6);
}
static int finish(int hash) {
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
// ignore: parameter_assignments
hash = hash ^ (hash >> 11);
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
}
}
abstract class _$BookSettings
extends BuildlessAutoDisposeNotifier<model.BookSettings> {
late final String bookId;
model.BookSettings build(
String bookId,
);
}
/// See also [BookSettings].
@ProviderFor(BookSettings)
const bookSettingsProvider = BookSettingsFamily();
/// See also [BookSettings].
class BookSettingsFamily extends Family<model.BookSettings> {
/// See also [BookSettings].
const BookSettingsFamily();
/// See also [BookSettings].
BookSettingsProvider call(
String bookId,
) {
return BookSettingsProvider(
bookId,
);
}
@override
BookSettingsProvider getProviderOverride(
covariant BookSettingsProvider provider,
) {
return call(
provider.bookId,
);
}
static const Iterable<ProviderOrFamily>? _dependencies = null;
@override
Iterable<ProviderOrFamily>? get dependencies => _dependencies;
static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;
@override
Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
_allTransitiveDependencies;
@override
String? get name => r'bookSettingsProvider';
}
/// See also [BookSettings].
class BookSettingsProvider
extends AutoDisposeNotifierProviderImpl<BookSettings, model.BookSettings> {
/// See also [BookSettings].
BookSettingsProvider(
String bookId,
) : this._internal(
() => BookSettings()..bookId = bookId,
from: bookSettingsProvider,
name: r'bookSettingsProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$bookSettingsHash,
dependencies: BookSettingsFamily._dependencies,
allTransitiveDependencies:
BookSettingsFamily._allTransitiveDependencies,
bookId: bookId,
);
BookSettingsProvider._internal(
super._createNotifier, {
required super.name,
required super.dependencies,
required super.allTransitiveDependencies,
required super.debugGetCreateSourceHash,
required super.from,
required this.bookId,
}) : super.internal();
final String bookId;
@override
model.BookSettings runNotifierBuild(
covariant BookSettings notifier,
) {
return notifier.build(
bookId,
);
}
@override
Override overrideWith(BookSettings Function() create) {
return ProviderOverride(
origin: this,
override: BookSettingsProvider._internal(
() => create()..bookId = bookId,
from: from,
name: null,
dependencies: null,
allTransitiveDependencies: null,
debugGetCreateSourceHash: null,
bookId: bookId,
),
);
}
@override
AutoDisposeNotifierProviderElement<BookSettings, model.BookSettings>
createElement() {
return _BookSettingsProviderElement(this);
}
@override
bool operator ==(Object other) {
return other is BookSettingsProvider && other.bookId == bookId;
}
@override
int get hashCode {
var hash = _SystemHash.combine(0, runtimeType.hashCode);
hash = _SystemHash.combine(hash, bookId.hashCode);
return _SystemHash.finish(hash);
}
}
mixin BookSettingsRef on AutoDisposeNotifierProviderRef<model.BookSettings> {
/// The parameter `bookId` of this provider.
String get bookId;
}
class _BookSettingsProviderElement
extends AutoDisposeNotifierProviderElement<BookSettings, model.BookSettings>
with BookSettingsRef {
_BookSettingsProviderElement(super.provider);
@override
String get bookId => (origin as BookSettingsProvider).bookId;
}
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member

View file

@ -1,9 +1,11 @@
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:vaani/features/per_book_settings/providers/book_settings_provider.dart';
import 'package:vaani/features/player/providers/audiobook_player.dart';
import 'package:vaani/features/player/view/player_when_expanded.dart';
import 'package:vaani/features/player/view/widgets/speed_selector.dart';
import 'package:vaani/settings/app_settings_provider.dart';
final _logger = Logger('PlayerSpeedAdjustButton');
@ -15,6 +17,9 @@ class PlayerSpeedAdjustButton extends HookConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final player = ref.watch(audiobookPlayerProvider);
final bookId = player.book?.libraryItemId ?? '_';
final bookSettings = ref.watch(bookSettingsProvider(bookId));
final appSettings = ref.watch(appSettingsProvider);
final notifier = ref.watch(audiobookPlayerProvider.notifier);
return TextButton(
child: Text('${player.speed}x'),
@ -31,6 +36,25 @@ class PlayerSpeedAdjustButton extends HookConsumerWidget {
return SpeedSelector(
onSpeedSelected: (speed) {
notifier.setSpeed(speed);
if (appSettings.playerSettings.configurePlayerForEveryBook) {
ref
.read(
bookSettingsProvider(bookId).notifier,
)
.update(
bookSettings.copyWith
.playerSettings(preferredDefaultSpeed: speed),
);
} else {
ref
.read(
appSettingsProvider.notifier,
)
.update(
appSettings.copyWith
.playerSettings(preferredDefaultSpeed: speed),
);
}
},
);
},

View file

@ -48,7 +48,7 @@ class AppSettings extends _$AppSettings {
.themeSettings(isDarkMode: !state.themeSettings.isDarkMode);
}
void updateState(model.AppSettings newSettings) {
void update(model.AppSettings newSettings) {
state = newSettings;
}

View file

@ -6,7 +6,7 @@ part of 'app_settings_provider.dart';
// RiverpodGenerator
// **************************************************************************
String _$appSettingsHash() => r'ab8fa4602242704a71b34dc2bb5dcb0c91092797';
String _$appSettingsHash() => r'e0e132b782b97f11d9791d4f1e45bf4ee67dd99b';
/// See also [AppSettings].
@ProviderFor(AppSettings)

View file

@ -44,6 +44,7 @@ class PlayerSettings with _$PlayerSettings {
@Default([0.75, 1, 1.25, 1.5, 1.75, 2]) List<double> speedOptions,
@Default(SleepTimerSettings()) SleepTimerSettings sleepTimerSettings,
@Default(Duration(seconds: 10)) Duration playbackReportInterval,
@Default(true) bool configurePlayerForEveryBook,
}) = _PlayerSettings;
factory PlayerSettings.fromJson(Map<String, dynamic> json) =>

View file

@ -478,6 +478,7 @@ mixin _$PlayerSettings {
SleepTimerSettings get sleepTimerSettings =>
throw _privateConstructorUsedError;
Duration get playbackReportInterval => throw _privateConstructorUsedError;
bool get configurePlayerForEveryBook => throw _privateConstructorUsedError;
/// Serializes this PlayerSettings to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@ -502,7 +503,8 @@ abstract class $PlayerSettingsCopyWith<$Res> {
double preferredDefaultSpeed,
List<double> speedOptions,
SleepTimerSettings sleepTimerSettings,
Duration playbackReportInterval});
Duration playbackReportInterval,
bool configurePlayerForEveryBook});
$MinimizedPlayerSettingsCopyWith<$Res> get miniPlayerSettings;
$ExpandedPlayerSettingsCopyWith<$Res> get expandedPlayerSettings;
@ -531,6 +533,7 @@ class _$PlayerSettingsCopyWithImpl<$Res, $Val extends PlayerSettings>
Object? speedOptions = null,
Object? sleepTimerSettings = null,
Object? playbackReportInterval = null,
Object? configurePlayerForEveryBook = null,
}) {
return _then(_value.copyWith(
miniPlayerSettings: null == miniPlayerSettings
@ -561,6 +564,10 @@ class _$PlayerSettingsCopyWithImpl<$Res, $Val extends PlayerSettings>
? _value.playbackReportInterval
: playbackReportInterval // ignore: cast_nullable_to_non_nullable
as Duration,
configurePlayerForEveryBook: null == configurePlayerForEveryBook
? _value.configurePlayerForEveryBook
: configurePlayerForEveryBook // ignore: cast_nullable_to_non_nullable
as bool,
) as $Val);
}
@ -613,7 +620,8 @@ abstract class _$$PlayerSettingsImplCopyWith<$Res>
double preferredDefaultSpeed,
List<double> speedOptions,
SleepTimerSettings sleepTimerSettings,
Duration playbackReportInterval});
Duration playbackReportInterval,
bool configurePlayerForEveryBook});
@override
$MinimizedPlayerSettingsCopyWith<$Res> get miniPlayerSettings;
@ -643,6 +651,7 @@ class __$$PlayerSettingsImplCopyWithImpl<$Res>
Object? speedOptions = null,
Object? sleepTimerSettings = null,
Object? playbackReportInterval = null,
Object? configurePlayerForEveryBook = null,
}) {
return _then(_$PlayerSettingsImpl(
miniPlayerSettings: null == miniPlayerSettings
@ -673,6 +682,10 @@ class __$$PlayerSettingsImplCopyWithImpl<$Res>
? _value.playbackReportInterval
: playbackReportInterval // ignore: cast_nullable_to_non_nullable
as Duration,
configurePlayerForEveryBook: null == configurePlayerForEveryBook
? _value.configurePlayerForEveryBook
: configurePlayerForEveryBook // ignore: cast_nullable_to_non_nullable
as bool,
));
}
}
@ -687,7 +700,8 @@ class _$PlayerSettingsImpl implements _PlayerSettings {
this.preferredDefaultSpeed = 1,
final List<double> speedOptions = const [0.75, 1, 1.25, 1.5, 1.75, 2],
this.sleepTimerSettings = const SleepTimerSettings(),
this.playbackReportInterval = const Duration(seconds: 10)})
this.playbackReportInterval = const Duration(seconds: 10),
this.configurePlayerForEveryBook = true})
: _speedOptions = speedOptions;
factory _$PlayerSettingsImpl.fromJson(Map<String, dynamic> json) =>
@ -720,10 +734,13 @@ class _$PlayerSettingsImpl implements _PlayerSettings {
@override
@JsonKey()
final Duration playbackReportInterval;
@override
@JsonKey()
final bool configurePlayerForEveryBook;
@override
String toString() {
return 'PlayerSettings(miniPlayerSettings: $miniPlayerSettings, expandedPlayerSettings: $expandedPlayerSettings, preferredDefaultVolume: $preferredDefaultVolume, preferredDefaultSpeed: $preferredDefaultSpeed, speedOptions: $speedOptions, sleepTimerSettings: $sleepTimerSettings, playbackReportInterval: $playbackReportInterval)';
return 'PlayerSettings(miniPlayerSettings: $miniPlayerSettings, expandedPlayerSettings: $expandedPlayerSettings, preferredDefaultVolume: $preferredDefaultVolume, preferredDefaultSpeed: $preferredDefaultSpeed, speedOptions: $speedOptions, sleepTimerSettings: $sleepTimerSettings, playbackReportInterval: $playbackReportInterval, configurePlayerForEveryBook: $configurePlayerForEveryBook)';
}
@override
@ -744,7 +761,11 @@ class _$PlayerSettingsImpl implements _PlayerSettings {
(identical(other.sleepTimerSettings, sleepTimerSettings) ||
other.sleepTimerSettings == sleepTimerSettings) &&
(identical(other.playbackReportInterval, playbackReportInterval) ||
other.playbackReportInterval == playbackReportInterval));
other.playbackReportInterval == playbackReportInterval) &&
(identical(other.configurePlayerForEveryBook,
configurePlayerForEveryBook) ||
other.configurePlayerForEveryBook ==
configurePlayerForEveryBook));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@ -757,7 +778,8 @@ class _$PlayerSettingsImpl implements _PlayerSettings {
preferredDefaultSpeed,
const DeepCollectionEquality().hash(_speedOptions),
sleepTimerSettings,
playbackReportInterval);
playbackReportInterval,
configurePlayerForEveryBook);
/// Create a copy of PlayerSettings
/// with the given fields replaced by the non-null parameter values.
@ -784,7 +806,8 @@ abstract class _PlayerSettings implements PlayerSettings {
final double preferredDefaultSpeed,
final List<double> speedOptions,
final SleepTimerSettings sleepTimerSettings,
final Duration playbackReportInterval}) = _$PlayerSettingsImpl;
final Duration playbackReportInterval,
final bool configurePlayerForEveryBook}) = _$PlayerSettingsImpl;
factory _PlayerSettings.fromJson(Map<String, dynamic> json) =
_$PlayerSettingsImpl.fromJson;
@ -803,6 +826,8 @@ abstract class _PlayerSettings implements PlayerSettings {
SleepTimerSettings get sleepTimerSettings;
@override
Duration get playbackReportInterval;
@override
bool get configurePlayerForEveryBook;
/// Create a copy of PlayerSettings
/// with the given fields replaced by the non-null parameter values.

View file

@ -72,6 +72,8 @@ _$PlayerSettingsImpl _$$PlayerSettingsImplFromJson(Map<String, dynamic> json) =>
? const Duration(seconds: 10)
: Duration(
microseconds: (json['playbackReportInterval'] as num).toInt()),
configurePlayerForEveryBook:
json['configurePlayerForEveryBook'] as bool? ?? true,
);
Map<String, dynamic> _$$PlayerSettingsImplToJson(
@ -84,6 +86,7 @@ Map<String, dynamic> _$$PlayerSettingsImplToJson(
'speedOptions': instance.speedOptions,
'sleepTimerSettings': instance.sleepTimerSettings,
'playbackReportInterval': instance.playbackReportInterval.inMicroseconds,
'configurePlayerForEveryBook': instance.configurePlayerForEveryBook,
};
_$ExpandedPlayerSettingsImpl _$$ExpandedPlayerSettingsImplFromJson(

View file

@ -65,7 +65,7 @@ class AppSettingsPage extends HookConsumerWidget {
? const Icon(Icons.auto_fix_high)
: const Icon(Icons.auto_fix_off),
onToggle: (value) {
ref.read(appSettingsProvider.notifier).updateState(
ref.read(appSettingsProvider.notifier).update(
appSettings.copyWith.themeSettings(
useMaterialThemeOnItemPage: value,
),
@ -114,7 +114,7 @@ class AppSettingsPage extends HookConsumerWidget {
Switch(
value: sleepTimerSettings.autoTurnOnTimer,
onChanged: (value) {
ref.read(appSettingsProvider.notifier).updateState(
ref.read(appSettingsProvider.notifier).update(
appSettings.copyWith.playerSettings
.sleepTimerSettings(
autoTurnOnTimer: value,
@ -208,7 +208,7 @@ class AppSettingsPage extends HookConsumerWidget {
);
ref
.read(appSettingsProvider.notifier)
.updateState(newSettings);
.update(newSettings);
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(

View file

@ -36,7 +36,7 @@ class AutoSleepTimerSettingsPage extends HookConsumerWidget {
? const Icon(Icons.timer)
: const Icon(Icons.timer_off),
onToggle: (value) {
ref.read(appSettingsProvider.notifier).updateState(
ref.read(appSettingsProvider.notifier).update(
appSettings.copyWith.playerSettings.sleepTimerSettings(
autoTurnOnTimer: value,
),
@ -59,7 +59,7 @@ class AutoSleepTimerSettingsPage extends HookConsumerWidget {
sleepTimerSettings.autoTurnOnTime.toTimeOfDay(),
);
if (selected != null) {
ref.read(appSettingsProvider.notifier).updateState(
ref.read(appSettingsProvider.notifier).update(
appSettings.copyWith.playerSettings
.sleepTimerSettings(
autoTurnOnTime: selected.toDuration(),
@ -87,7 +87,7 @@ class AutoSleepTimerSettingsPage extends HookConsumerWidget {
sleepTimerSettings.autoTurnOffTime.toTimeOfDay(),
);
if (selected != null) {
ref.read(appSettingsProvider.notifier).updateState(
ref.read(appSettingsProvider.notifier).update(
appSettings.copyWith.playerSettings
.sleepTimerSettings(
autoTurnOffTime: selected.toDuration(),