This commit is contained in:
rang 2025-12-01 15:34:12 +08:00
parent 1ca8e4889a
commit aad510ea45
31 changed files with 777 additions and 239 deletions

View file

@ -10,8 +10,20 @@ class BookSettings with _$BookSettings {
const factory BookSettings({
required String bookId,
@Default(NullablePlayerSettings()) NullablePlayerSettings playerSettings,
BookProgress? progress,
}) = _BookSettings;
factory BookSettings.fromJson(Map<String, dynamic> json) =>
_$BookSettingsFromJson(json);
}
@freezed
class BookProgress with _$BookProgress {
const factory BookProgress({
required DateTime lastUpdate,
@Default(Duration.zero) Duration currentTime,
}) = _BookProgress;
factory BookProgress.fromJson(Map<String, dynamic> json) =>
_$BookProgressFromJson(json);
}

View file

@ -23,6 +23,7 @@ mixin _$BookSettings {
String get bookId => throw _privateConstructorUsedError;
NullablePlayerSettings get playerSettings =>
throw _privateConstructorUsedError;
BookProgress? get progress => throw _privateConstructorUsedError;
/// Serializes this BookSettings to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@ -40,9 +41,13 @@ abstract class $BookSettingsCopyWith<$Res> {
BookSettings value, $Res Function(BookSettings) then) =
_$BookSettingsCopyWithImpl<$Res, BookSettings>;
@useResult
$Res call({String bookId, NullablePlayerSettings playerSettings});
$Res call(
{String bookId,
NullablePlayerSettings playerSettings,
BookProgress? progress});
$NullablePlayerSettingsCopyWith<$Res> get playerSettings;
$BookProgressCopyWith<$Res>? get progress;
}
/// @nodoc
@ -62,6 +67,7 @@ class _$BookSettingsCopyWithImpl<$Res, $Val extends BookSettings>
$Res call({
Object? bookId = null,
Object? playerSettings = null,
Object? progress = freezed,
}) {
return _then(_value.copyWith(
bookId: null == bookId
@ -72,6 +78,10 @@ class _$BookSettingsCopyWithImpl<$Res, $Val extends BookSettings>
? _value.playerSettings
: playerSettings // ignore: cast_nullable_to_non_nullable
as NullablePlayerSettings,
progress: freezed == progress
? _value.progress
: progress // ignore: cast_nullable_to_non_nullable
as BookProgress?,
) as $Val);
}
@ -85,6 +95,20 @@ class _$BookSettingsCopyWithImpl<$Res, $Val extends BookSettings>
return _then(_value.copyWith(playerSettings: value) as $Val);
});
}
/// Create a copy of BookSettings
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$BookProgressCopyWith<$Res>? get progress {
if (_value.progress == null) {
return null;
}
return $BookProgressCopyWith<$Res>(_value.progress!, (value) {
return _then(_value.copyWith(progress: value) as $Val);
});
}
}
/// @nodoc
@ -95,10 +119,15 @@ abstract class _$$BookSettingsImplCopyWith<$Res>
__$$BookSettingsImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({String bookId, NullablePlayerSettings playerSettings});
$Res call(
{String bookId,
NullablePlayerSettings playerSettings,
BookProgress? progress});
@override
$NullablePlayerSettingsCopyWith<$Res> get playerSettings;
@override
$BookProgressCopyWith<$Res>? get progress;
}
/// @nodoc
@ -116,6 +145,7 @@ class __$$BookSettingsImplCopyWithImpl<$Res>
$Res call({
Object? bookId = null,
Object? playerSettings = null,
Object? progress = freezed,
}) {
return _then(_$BookSettingsImpl(
bookId: null == bookId
@ -126,6 +156,10 @@ class __$$BookSettingsImplCopyWithImpl<$Res>
? _value.playerSettings
: playerSettings // ignore: cast_nullable_to_non_nullable
as NullablePlayerSettings,
progress: freezed == progress
? _value.progress
: progress // ignore: cast_nullable_to_non_nullable
as BookProgress?,
));
}
}
@ -135,7 +169,8 @@ class __$$BookSettingsImplCopyWithImpl<$Res>
class _$BookSettingsImpl implements _BookSettings {
const _$BookSettingsImpl(
{required this.bookId,
this.playerSettings = const NullablePlayerSettings()});
this.playerSettings = const NullablePlayerSettings(),
this.progress});
factory _$BookSettingsImpl.fromJson(Map<String, dynamic> json) =>
_$$BookSettingsImplFromJson(json);
@ -145,10 +180,12 @@ class _$BookSettingsImpl implements _BookSettings {
@override
@JsonKey()
final NullablePlayerSettings playerSettings;
@override
final BookProgress? progress;
@override
String toString() {
return 'BookSettings(bookId: $bookId, playerSettings: $playerSettings)';
return 'BookSettings(bookId: $bookId, playerSettings: $playerSettings, progress: $progress)';
}
@override
@ -158,12 +195,15 @@ class _$BookSettingsImpl implements _BookSettings {
other is _$BookSettingsImpl &&
(identical(other.bookId, bookId) || other.bookId == bookId) &&
(identical(other.playerSettings, playerSettings) ||
other.playerSettings == playerSettings));
other.playerSettings == playerSettings) &&
(identical(other.progress, progress) ||
other.progress == progress));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType, bookId, playerSettings);
int get hashCode =>
Object.hash(runtimeType, bookId, playerSettings, progress);
/// Create a copy of BookSettings
/// with the given fields replaced by the non-null parameter values.
@ -184,7 +224,8 @@ class _$BookSettingsImpl implements _BookSettings {
abstract class _BookSettings implements BookSettings {
const factory _BookSettings(
{required final String bookId,
final NullablePlayerSettings playerSettings}) = _$BookSettingsImpl;
final NullablePlayerSettings playerSettings,
final BookProgress? progress}) = _$BookSettingsImpl;
factory _BookSettings.fromJson(Map<String, dynamic> json) =
_$BookSettingsImpl.fromJson;
@ -193,6 +234,8 @@ abstract class _BookSettings implements BookSettings {
String get bookId;
@override
NullablePlayerSettings get playerSettings;
@override
BookProgress? get progress;
/// Create a copy of BookSettings
/// with the given fields replaced by the non-null parameter values.
@ -201,3 +244,174 @@ abstract class _BookSettings implements BookSettings {
_$$BookSettingsImplCopyWith<_$BookSettingsImpl> get copyWith =>
throw _privateConstructorUsedError;
}
BookProgress _$BookProgressFromJson(Map<String, dynamic> json) {
return _BookProgress.fromJson(json);
}
/// @nodoc
mixin _$BookProgress {
DateTime get lastUpdate => throw _privateConstructorUsedError;
Duration get currentTime => throw _privateConstructorUsedError;
/// Serializes this BookProgress to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
/// Create a copy of BookProgress
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$BookProgressCopyWith<BookProgress> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $BookProgressCopyWith<$Res> {
factory $BookProgressCopyWith(
BookProgress value, $Res Function(BookProgress) then) =
_$BookProgressCopyWithImpl<$Res, BookProgress>;
@useResult
$Res call({DateTime lastUpdate, Duration currentTime});
}
/// @nodoc
class _$BookProgressCopyWithImpl<$Res, $Val extends BookProgress>
implements $BookProgressCopyWith<$Res> {
_$BookProgressCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of BookProgress
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? lastUpdate = null,
Object? currentTime = null,
}) {
return _then(_value.copyWith(
lastUpdate: null == lastUpdate
? _value.lastUpdate
: lastUpdate // ignore: cast_nullable_to_non_nullable
as DateTime,
currentTime: null == currentTime
? _value.currentTime
: currentTime // ignore: cast_nullable_to_non_nullable
as Duration,
) as $Val);
}
}
/// @nodoc
abstract class _$$BookProgressImplCopyWith<$Res>
implements $BookProgressCopyWith<$Res> {
factory _$$BookProgressImplCopyWith(
_$BookProgressImpl value, $Res Function(_$BookProgressImpl) then) =
__$$BookProgressImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({DateTime lastUpdate, Duration currentTime});
}
/// @nodoc
class __$$BookProgressImplCopyWithImpl<$Res>
extends _$BookProgressCopyWithImpl<$Res, _$BookProgressImpl>
implements _$$BookProgressImplCopyWith<$Res> {
__$$BookProgressImplCopyWithImpl(
_$BookProgressImpl _value, $Res Function(_$BookProgressImpl) _then)
: super(_value, _then);
/// Create a copy of BookProgress
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? lastUpdate = null,
Object? currentTime = null,
}) {
return _then(_$BookProgressImpl(
lastUpdate: null == lastUpdate
? _value.lastUpdate
: lastUpdate // ignore: cast_nullable_to_non_nullable
as DateTime,
currentTime: null == currentTime
? _value.currentTime
: currentTime // ignore: cast_nullable_to_non_nullable
as Duration,
));
}
}
/// @nodoc
@JsonSerializable()
class _$BookProgressImpl implements _BookProgress {
const _$BookProgressImpl(
{required this.lastUpdate, this.currentTime = Duration.zero});
factory _$BookProgressImpl.fromJson(Map<String, dynamic> json) =>
_$$BookProgressImplFromJson(json);
@override
final DateTime lastUpdate;
@override
@JsonKey()
final Duration currentTime;
@override
String toString() {
return 'BookProgress(lastUpdate: $lastUpdate, currentTime: $currentTime)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$BookProgressImpl &&
(identical(other.lastUpdate, lastUpdate) ||
other.lastUpdate == lastUpdate) &&
(identical(other.currentTime, currentTime) ||
other.currentTime == currentTime));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType, lastUpdate, currentTime);
/// Create a copy of BookProgress
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$BookProgressImplCopyWith<_$BookProgressImpl> get copyWith =>
__$$BookProgressImplCopyWithImpl<_$BookProgressImpl>(this, _$identity);
@override
Map<String, dynamic> toJson() {
return _$$BookProgressImplToJson(
this,
);
}
}
abstract class _BookProgress implements BookProgress {
const factory _BookProgress(
{required final DateTime lastUpdate,
final Duration currentTime}) = _$BookProgressImpl;
factory _BookProgress.fromJson(Map<String, dynamic> json) =
_$BookProgressImpl.fromJson;
@override
DateTime get lastUpdate;
@override
Duration get currentTime;
/// Create a copy of BookProgress
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$BookProgressImplCopyWith<_$BookProgressImpl> get copyWith =>
throw _privateConstructorUsedError;
}

View file

@ -13,10 +13,28 @@ _$BookSettingsImpl _$$BookSettingsImplFromJson(Map<String, dynamic> json) =>
? const NullablePlayerSettings()
: NullablePlayerSettings.fromJson(
json['playerSettings'] as Map<String, dynamic>),
progress: json['progress'] == null
? null
: BookProgress.fromJson(json['progress'] as Map<String, dynamic>),
);
Map<String, dynamic> _$$BookSettingsImplToJson(_$BookSettingsImpl instance) =>
<String, dynamic>{
'bookId': instance.bookId,
'playerSettings': instance.playerSettings,
'progress': instance.progress,
};
_$BookProgressImpl _$$BookProgressImplFromJson(Map<String, dynamic> json) =>
_$BookProgressImpl(
lastUpdate: DateTime.parse(json['lastUpdate'] as String),
currentTime: json['currentTime'] == null
? Duration.zero
: Duration(microseconds: (json['currentTime'] as num).toInt()),
);
Map<String, dynamic> _$$BookProgressImplToJson(_$BookProgressImpl instance) =>
<String, dynamic>{
'lastUpdate': instance.lastUpdate.toIso8601String(),
'currentTime': instance.currentTime.inMicroseconds,
};

View file

@ -19,9 +19,9 @@ model.BookSettings readFromBoxOrCreate(String bookId) {
} else {
// create a new settings object
final settings = model.BookSettings(
bookId: bookId,
playerSettings: const NullablePlayerSettings(),
);
bookId: bookId,
playerSettings: const NullablePlayerSettings(),
progress: model.BookProgress(lastUpdate: DateTime.now()));
_logger.fine('created new book settings for $bookId: $settings');
writeToBox(settings);
return settings;
@ -56,3 +56,18 @@ class BookSettings extends _$BookSettings {
updateState(newSettings, force: force);
}
}
@riverpod
class BookProgressSettings extends _$BookProgressSettings {
@override
model.BookProgress build(String bookId) {
final progress =
ref.read(bookSettingsProvider(bookId).select((v) => v.progress));
if (progress == null) {
return model.BookProgress(
lastUpdate: DateTime.now(),
);
}
return progress;
}
}

View file

@ -172,5 +172,153 @@ class _BookSettingsProviderElement
@override
String get bookId => (origin as BookSettingsProvider).bookId;
}
String _$bookProgressSettingsHash() =>
r'be890f6b4f90565620a48c347cb86266fc232374';
abstract class _$BookProgressSettings
extends BuildlessAutoDisposeNotifier<model.BookProgress> {
late final String bookId;
model.BookProgress build(
String bookId,
);
}
/// See also [BookProgressSettings].
@ProviderFor(BookProgressSettings)
const bookProgressSettingsProvider = BookProgressSettingsFamily();
/// See also [BookProgressSettings].
class BookProgressSettingsFamily extends Family<model.BookProgress> {
/// See also [BookProgressSettings].
const BookProgressSettingsFamily();
/// See also [BookProgressSettings].
BookProgressSettingsProvider call(
String bookId,
) {
return BookProgressSettingsProvider(
bookId,
);
}
@override
BookProgressSettingsProvider getProviderOverride(
covariant BookProgressSettingsProvider 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'bookProgressSettingsProvider';
}
/// See also [BookProgressSettings].
class BookProgressSettingsProvider extends AutoDisposeNotifierProviderImpl<
BookProgressSettings, model.BookProgress> {
/// See also [BookProgressSettings].
BookProgressSettingsProvider(
String bookId,
) : this._internal(
() => BookProgressSettings()..bookId = bookId,
from: bookProgressSettingsProvider,
name: r'bookProgressSettingsProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$bookProgressSettingsHash,
dependencies: BookProgressSettingsFamily._dependencies,
allTransitiveDependencies:
BookProgressSettingsFamily._allTransitiveDependencies,
bookId: bookId,
);
BookProgressSettingsProvider._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.BookProgress runNotifierBuild(
covariant BookProgressSettings notifier,
) {
return notifier.build(
bookId,
);
}
@override
Override overrideWith(BookProgressSettings Function() create) {
return ProviderOverride(
origin: this,
override: BookProgressSettingsProvider._internal(
() => create()..bookId = bookId,
from: from,
name: null,
dependencies: null,
allTransitiveDependencies: null,
debugGetCreateSourceHash: null,
bookId: bookId,
),
);
}
@override
AutoDisposeNotifierProviderElement<BookProgressSettings, model.BookProgress>
createElement() {
return _BookProgressSettingsProviderElement(this);
}
@override
bool operator ==(Object other) {
return other is BookProgressSettingsProvider && other.bookId == bookId;
}
@override
int get hashCode {
var hash = _SystemHash.combine(0, runtimeType.hashCode);
hash = _SystemHash.combine(hash, bookId.hashCode);
return _SystemHash.finish(hash);
}
}
@Deprecated('Will be removed in 3.0. Use Ref instead')
// ignore: unused_element
mixin BookProgressSettingsRef
on AutoDisposeNotifierProviderRef<model.BookProgress> {
/// The parameter `bookId` of this provider.
String get bookId;
}
class _BookProgressSettingsProviderElement
extends AutoDisposeNotifierProviderElement<BookProgressSettings,
model.BookProgress> with BookProgressSettingsRef {
_BookProgressSettingsProviderElement(super.provider);
@override
String get bookId => (origin as BookProgressSettingsProvider).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, deprecated_member_use_from_same_package