2024-05-08 05:03:49 -04:00
|
|
|
// this provider is used to provide the app settings to the app
|
|
|
|
|
|
2024-06-28 06:01:56 -04:00
|
|
|
import 'package:logging/logging.dart';
|
2024-05-08 05:03:49 -04:00
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
2024-08-23 04:21:46 -04:00
|
|
|
import 'package:vaani/db/available_boxes.dart';
|
|
|
|
|
import 'package:vaani/settings/models/app_settings.dart' as model;
|
2024-05-08 05:03:49 -04:00
|
|
|
|
|
|
|
|
part 'app_settings_provider.g.dart';
|
|
|
|
|
|
|
|
|
|
final _box = AvailableHiveBoxes.userPrefsBox;
|
|
|
|
|
|
2024-06-28 06:01:56 -04:00
|
|
|
final _logger = Logger('AppSettingsProvider');
|
|
|
|
|
|
2024-09-25 03:13:42 -04:00
|
|
|
model.AppSettings loadOrCreateAppSettings() {
|
2024-08-20 08:36:39 -04:00
|
|
|
// see if the settings are already in the box
|
2024-09-25 03:13:42 -04:00
|
|
|
model.AppSettings? settings;
|
2024-08-20 08:36:39 -04:00
|
|
|
if (_box.isNotEmpty) {
|
2024-09-25 03:13:42 -04:00
|
|
|
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();
|
|
|
|
|
}
|
2024-08-20 08:36:39 -04:00
|
|
|
} else {
|
2024-09-25 03:13:42 -04:00
|
|
|
_logger.fine('no settings found in box, creating new settings');
|
2024-08-20 08:36:39 -04:00
|
|
|
}
|
2024-09-25 03:13:42 -04:00
|
|
|
return settings ?? const model.AppSettings();
|
2024-08-20 08:36:39 -04:00
|
|
|
}
|
|
|
|
|
|
2024-06-06 15:35:30 -04:00
|
|
|
@Riverpod(keepAlive: true)
|
2024-05-08 05:03:49 -04:00
|
|
|
class AppSettings extends _$AppSettings {
|
|
|
|
|
@override
|
|
|
|
|
model.AppSettings build() {
|
2024-09-25 03:13:42 -04:00
|
|
|
state = loadOrCreateAppSettings();
|
2024-05-08 05:03:49 -04:00
|
|
|
ref.listenSelf((_, __) {
|
|
|
|
|
writeToBox();
|
|
|
|
|
});
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// write the settings to the box
|
|
|
|
|
void writeToBox() {
|
|
|
|
|
_box.clear();
|
|
|
|
|
_box.add(state);
|
2024-06-28 06:01:56 -04:00
|
|
|
_logger.fine('wrote settings to box: $state');
|
2024-05-08 05:03:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void toggleDarkMode() {
|
2024-08-20 11:39:26 -04:00
|
|
|
state = state.copyWith
|
|
|
|
|
.themeSettings(isDarkMode: !state.themeSettings.isDarkMode);
|
2024-05-08 05:03:49 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-17 23:19:05 -04:00
|
|
|
void update(model.AppSettings newSettings) {
|
2024-05-08 05:03:49 -04:00
|
|
|
state = newSettings;
|
|
|
|
|
}
|
2024-08-20 08:36:39 -04:00
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
|
state = const model.AppSettings();
|
|
|
|
|
}
|
2024-05-08 05:03:49 -04:00
|
|
|
}
|
2024-10-02 09:18:06 -04:00
|
|
|
|
|
|
|
|
// SleepTimerSettings provider but only rebuilds when the sleep timer settings change
|
|
|
|
|
@Riverpod(keepAlive: true)
|
|
|
|
|
class SleepTimerSettings extends _$SleepTimerSettings {
|
|
|
|
|
@override
|
|
|
|
|
model.SleepTimerSettings build() {
|
|
|
|
|
final settings = ref.read(appSettingsProvider).sleepTimerSettings;
|
|
|
|
|
state = settings;
|
|
|
|
|
ref.listen(appSettingsProvider, (a, b) {
|
|
|
|
|
if (a?.sleepTimerSettings != b.sleepTimerSettings) {
|
|
|
|
|
state = b.sleepTimerSettings;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
}
|