mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2025-12-24 03:49:30 +00:00
feat: implement per book settings with player configuration and update methods
This commit is contained in:
parent
fd42ee2343
commit
e7dd4de515
19 changed files with 1030 additions and 20 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue