Vaani/lib/settings/app_settings_provider.dart
Dr.Blank 3cf0a0b124
feat: extensive settings for media controls through notification (#28)
* feat: add notification settings customisation options

* feat: add notification settings page and update routing
2024-09-25 03:13:42 -04:00

62 lines
1.5 KiB
Dart

// this provider is used to provide the app settings to the app
import 'package:logging/logging.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:vaani/db/available_boxes.dart';
import 'package:vaani/settings/models/app_settings.dart' as model;
part 'app_settings_provider.g.dart';
final _box = AvailableHiveBoxes.userPrefsBox;
final _logger = Logger('AppSettingsProvider');
model.AppSettings loadOrCreateAppSettings() {
// see if the settings are already in the box
model.AppSettings? settings;
if (_box.isNotEmpty) {
try {
settings = _box.getAt(0);
_logger.fine('found settings in box: $settings');
} catch (e) {
_logger.warning('error reading settings from box: $e'
'\nclearing box');
_box.clear();
}
} else {
_logger.fine('no settings found in box, creating new settings');
}
return settings ?? const model.AppSettings();
}
@Riverpod(keepAlive: true)
class AppSettings extends _$AppSettings {
@override
model.AppSettings build() {
state = loadOrCreateAppSettings();
ref.listenSelf((_, __) {
writeToBox();
});
return state;
}
// write the settings to the box
void writeToBox() {
_box.clear();
_box.add(state);
_logger.fine('wrote settings to box: $state');
}
void toggleDarkMode() {
state = state.copyWith
.themeSettings(isDarkMode: !state.themeSettings.isDarkMode);
}
void update(model.AppSettings newSettings) {
state = newSettings;
}
void reset() {
state = const model.AppSettings();
}
}