Files
BudgetApp/lib/data/repositories/hive_settings_repository.dart
T
Sanders 8b75299d41 Refactors data repositories for single-user mode
This commit refactors the data repositories to remove user-specific filtering. It simplifies the data access logic by retrieving all data from the Hive boxes and removing the need to filter by `userId` in the repository methods.

This change makes the app function in single-user mode and removes the need to manage multiple user contexts within the data layer.

Specifically:
- Removes `userId` parameter from repository methods.
- Updates the setting repository to use fixed keys.
- Removes user ID filtering from queries.
- Removes user ID parameters from Cubits and Blocs
- Updates initial data creation
2025-07-12 21:54:00 +03:00

53 lines
1.8 KiB
Dart

import 'package:budget_app/data/repositories/interfaces/isettings_repository.dart';
import 'package:budget_app/models/app_settings.dart';
import 'package:hive_ce/hive.dart';
/// Реализация репозитория настроек с использованием Hive
class HiveSettingsRepository implements ISettingsRepository {
final Box<AppSettings> _box;
// Фиксированный ключ для хранения настроек приложения
static const String _settingsKey = 'user_settings';
HiveSettingsRepository(this._box);
@override
Future<AppSettings> getSettings() async {
try {
// Получаем настройки по ключу вместо индекса
final settings = _box.get(_settingsKey);
// Если настройки не существуют, создаем новые с значениями по умолчанию
if (settings == null) {
final defaultSettings = AppSettings();
await _box.put(_settingsKey, defaultSettings);
return defaultSettings;
}
return settings;
} catch (e) {
throw Exception('Ошибка получения настроек: $e');
}
}
@override
Future<void> saveSettings(AppSettings settings) async {
try {
// Сохраняем настройки с фиксированным ключом
await _box.put(_settingsKey, settings);
} catch (e) {
throw Exception('Ошибка сохранения настроек: $e');
}
}
@override
Future<void> deleteSettings() async {
try {
// Удаляем настройки по фиксированному ключу
await _box.delete(_settingsKey);
} catch (e) {
throw Exception('Ошибка удаления настроек: $e');
}
}
}