feat: добавить реализацию Hive базы с интерфейсами репозиториев
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import 'package:hive_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();
|
||||
|
||||
Hive.registerAdapter(CategoryAdapter());
|
||||
Hive.registerAdapter(TagAdapter());
|
||||
Hive.registerAdapter(TransactionRecordAdapter());
|
||||
|
||||
await Hive.openBox<Category>(_categoryBox);
|
||||
await Hive.openBox<Tag>(_tagBox);
|
||||
await 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);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import 'package:hive/hive.dart';
|
||||
import '../interfaces/icategory_repository.dart';
|
||||
import '../../models/category.dart';
|
||||
|
||||
class HiveCategoryRepository implements ICategoryRepository {
|
||||
final Box<Category> _box;
|
||||
|
||||
HiveCategoryRepository(this._box);
|
||||
|
||||
@override
|
||||
Future<List<Category>> getAll() async {
|
||||
return _box.values.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Category?> getById(String id) async {
|
||||
return _box.get(id);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> add(Category category) async {
|
||||
await _box.put(category.id, category);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> update(Category category) async {
|
||||
await add(category);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> delete(String id) async {
|
||||
await _box.delete(id);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Category>> getIncomeCategories() async {
|
||||
return _box.values.where((c) => c.isIncome).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Category>> getExpenseCategories() async {
|
||||
return _box.values.where((c) => !c.isIncome).toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import '../../models/category.dart';
|
||||
|
||||
abstract class ICategoryRepository {
|
||||
Future<List<Category>> getAll();
|
||||
Future<Category?> getById(String id);
|
||||
Future<void> add(Category category);
|
||||
Future<void> update(Category category);
|
||||
Future<void> delete(String id);
|
||||
Future<List<Category>> getIncomeCategories();
|
||||
Future<List<Category>> getExpenseCategories();
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import '../../models/tag.dart';
|
||||
|
||||
abstract class ITagRepository {
|
||||
Future<List<Tag>> getAll();
|
||||
Future<Tag?> getById(String id);
|
||||
Future<void> add(Tag tag);
|
||||
Future<void> update(Tag tag);
|
||||
Future<void> delete(String id);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import '../../models/transaction_record.dart';
|
||||
|
||||
abstract class ITransactionRepository {
|
||||
Future<List<TransactionRecord>> getAll();
|
||||
Future<TransactionRecord?> getById(String id);
|
||||
Future<void> add(TransactionRecord transaction);
|
||||
Future<void> update(TransactionRecord transaction);
|
||||
Future<void> delete(String id);
|
||||
Future<List<TransactionRecord>> getByDateRange(DateTime from, DateTime to);
|
||||
Future<List<TransactionRecord>> getByCategory(String categoryId);
|
||||
Future<List<TransactionRecord>> getByTag(String tagId);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'data/database/hive_service.dart';
|
||||
import 'data/repositories/hive_category_repository.dart';
|
||||
import 'data/repositories/hive_tag_repository.dart';
|
||||
import 'data/repositories/hive_transaction_repository.dart';
|
||||
import 'data/repositories/interfaces/icategory_repository.dart';
|
||||
import 'data/repositories/interfaces/itag_repository.dart';
|
||||
import 'data/repositories/interfaces/itransaction_repository.dart';
|
||||
|
||||
final getIt = GetIt.instance;
|
||||
|
||||
Future<void> initDependencies() async {
|
||||
// Инициализация Hive
|
||||
await HiveService.init();
|
||||
|
||||
// Регистрация репозиториев
|
||||
getIt.registerSingleton<ICategoryRepository>(
|
||||
HiveCategoryRepository(HiveService.categories),
|
||||
);
|
||||
|
||||
getIt.registerSingleton<ITagRepository>(
|
||||
HiveTagRepository(HiveService.tags),
|
||||
);
|
||||
|
||||
getIt.registerSingleton<ITransactionRepository>(
|
||||
HiveTransactionRepository(HiveService.transactions),
|
||||
);
|
||||
}
|
||||
+5
-1
@@ -3,7 +3,11 @@ import 'package:budget_app/pages/home_page.dart'; // Импорт нашей н
|
||||
import 'theme/app_theme.dart'; // Импорт тем приложения
|
||||
|
||||
/// Точка входа в приложение
|
||||
void main() {
|
||||
import 'injection_container.dart' as di;
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await di.initDependencies();
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
part 'category.g.dart';
|
||||
|
||||
@HiveType(typeId: 0)
|
||||
|
||||
/// Модель категории для группировки транзакций
|
||||
/// Содержит основные параметры для визуализации и классификации
|
||||
class Category {
|
||||
/// Уникальный идентификатор категории
|
||||
@HiveField(0)
|
||||
final String id;
|
||||
|
||||
/// Название категории (например: "Еда", "Транспорт")
|
||||
|
||||
Reference in New Issue
Block a user