feat: добавить системного пользователя и userId в создание категорий
This commit is contained in:
@@ -4,9 +4,12 @@ import '/models/category.dart';
|
||||
import '/models/icon_data_adapter.dart';
|
||||
import '/models/tag.dart';
|
||||
import '/models/transaction_record.dart';
|
||||
import '/models/user.dart';
|
||||
import '/utils/category_utils.dart';
|
||||
import '/utils/tag_utils.dart';
|
||||
import '/utils/transaction_utils.dart';
|
||||
import '/adapters/color_adapter.dart';
|
||||
import '/adapters/icon_data_adapter.dart';
|
||||
|
||||
final _logger = Logger();
|
||||
|
||||
@@ -15,12 +18,19 @@ class HiveService {
|
||||
static const String _categoryBox = 'categories';
|
||||
static const String _tagBox = 'tags';
|
||||
static const String _transactionBox = 'transactions';
|
||||
static const String _userBox = 'users';
|
||||
|
||||
// ID системного пользователя по умолчанию
|
||||
static const String _defaultUserId = 'default_user';
|
||||
|
||||
static Future<void> init() async {
|
||||
_logger.i('Initializing Hive database');
|
||||
await Hive.initFlutter();
|
||||
|
||||
// Регистрация адаптеров
|
||||
if (!Hive.isAdapterRegistered(ColorAdapter().typeId)) {
|
||||
Hive.registerAdapter(ColorAdapter());
|
||||
}
|
||||
if (!Hive.isAdapterRegistered(IconDataAdapter().typeId)) {
|
||||
Hive.registerAdapter(IconDataAdapter());
|
||||
}
|
||||
@@ -33,6 +43,9 @@ class HiveService {
|
||||
if (!Hive.isAdapterRegistered(TransactionRecordAdapter().typeId)) {
|
||||
Hive.registerAdapter(TransactionRecordAdapter());
|
||||
}
|
||||
if (!Hive.isAdapterRegistered(UserAdapter().typeId)) {
|
||||
Hive.registerAdapter(UserAdapter());
|
||||
}
|
||||
|
||||
// Открытие всех Box'ов
|
||||
await Future.wait([
|
||||
@@ -40,6 +53,7 @@ class HiveService {
|
||||
Hive.openBox<Category>(_categoryBox),
|
||||
Hive.openBox<Tag>(_tagBox),
|
||||
Hive.openBox<TransactionRecord>(_transactionBox),
|
||||
Hive.openBox<User>(_userBox),
|
||||
]);
|
||||
|
||||
// Проверка и заполнение начальными данными
|
||||
@@ -50,13 +64,28 @@ class HiveService {
|
||||
static Box<Tag> get tags => Hive.box<Tag>(_tagBox);
|
||||
static Box<TransactionRecord> get transactions =>
|
||||
Hive.box<TransactionRecord>(_transactionBox);
|
||||
static Box<User> get users => Hive.box<User>(_userBox);
|
||||
|
||||
/// Проверяет и заполняет боксы начальными данными при первом запуске
|
||||
static Future<void> _checkAndFillInitialData() async {
|
||||
// Создаем пользователя по умолчанию, если его нет
|
||||
final userBox = users;
|
||||
if (userBox.isEmpty) {
|
||||
_logger.i('Creating default user');
|
||||
final defaultUser = User(
|
||||
id: _defaultUserId,
|
||||
name: 'Пользователь по умолчанию',
|
||||
email: 'default@example.com',
|
||||
);
|
||||
await userBox.put(_defaultUserId, defaultUser);
|
||||
await userBox.flush();
|
||||
}
|
||||
|
||||
final catBox = categories;
|
||||
if (catBox.isEmpty) {
|
||||
_logger.i('Filling initial categories');
|
||||
await catBox.addAll(CategoryUtils.getDefaultCategories());
|
||||
_logger.i('Filling initial categories for default user');
|
||||
// Теперь передаем userId в getDefaultCategories
|
||||
await catBox.addAll(CategoryUtils.getDefaultCategories(_defaultUserId));
|
||||
await catBox.flush();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user