38 lines
1.2 KiB
Dart
38 lines
1.2 KiB
Dart
import 'package:hive_ce_flutter/hive_flutter.dart';
|
|
import '/models/category.dart';
|
|
import '/models/tag.dart';
|
|
import '/models/transaction_record.dart';
|
|
|
|
class HiveService {
|
|
static const String _categoryBox = 'categories';
|
|
static const String _tagBox = 'tags';
|
|
static const String _transactionBox = 'transactions';
|
|
|
|
static Future<void> init() async {
|
|
await Hive.initFlutter();
|
|
|
|
// Регистрация адаптеров
|
|
if (!Hive.isAdapterRegistered(CategoryAdapter().typeId)) {
|
|
Hive.registerAdapter(CategoryAdapter());
|
|
}
|
|
if (!Hive.isAdapterRegistered(TagAdapter().typeId)) {
|
|
Hive.registerAdapter(TagAdapter());
|
|
}
|
|
if (!Hive.isAdapterRegistered(TransactionRecordAdapter().typeId)) {
|
|
Hive.registerAdapter(TransactionRecordAdapter());
|
|
}
|
|
|
|
// Открытие всех Box'ов
|
|
await Future.wait([
|
|
Hive.openBox<Category>(_categoryBox),
|
|
Hive.openBox<Tag>(_tagBox),
|
|
Hive.openBox<TransactionRecord>(_transactionBox),
|
|
]);
|
|
}
|
|
|
|
static Box<Category> get categories => Hive.box<Category>(_categoryBox);
|
|
static Box<Tag> get tags => Hive.box<Tag>(_tagBox);
|
|
static Box<TransactionRecord> get transactions =>
|
|
Hive.box<TransactionRecord>(_transactionBox);
|
|
}
|