58 lines
1.6 KiB
Dart
58 lines
1.6 KiB
Dart
import 'package:hive/hive.dart';
|
|
import '../models/transaction.dart' as app;
|
|
import '../models/budget.dart';
|
|
|
|
class HiveService {
|
|
static const String transactionsBox = 'transactions';
|
|
static const String budgetsBox = 'budgets';
|
|
|
|
static Future<void> init() async {
|
|
await Hive.openBox<app.Transaction>(transactionsBox);
|
|
await Hive.openBox<Budget>(budgetsBox);
|
|
}
|
|
|
|
// --- Métodos para Transações ---
|
|
Future<int> insertTransaction(app.Transaction transaction) async {
|
|
final box = Hive.box<app.Transaction>(transactionsBox);
|
|
int key = await box.add(transaction);
|
|
return key;
|
|
}
|
|
|
|
Future<List<app.Transaction>> getTransactions() async {
|
|
final box = Hive.box<app.Transaction>(transactionsBox);
|
|
return box.values.toList();
|
|
}
|
|
|
|
Future<void> updateTransaction(int key, app.Transaction transaction) async {
|
|
final box = Hive.box<app.Transaction>(transactionsBox);
|
|
await box.put(key, transaction);
|
|
}
|
|
|
|
Future<void> deleteTransaction(int key) async {
|
|
final box = Hive.box<app.Transaction>(transactionsBox);
|
|
await box.delete(key);
|
|
}
|
|
|
|
// --- Métodos para Orçamentos ---
|
|
Future<int> insertBudget(Budget budget) async {
|
|
final box = Hive.box<Budget>(budgetsBox);
|
|
int key = await box.add(budget);
|
|
return key;
|
|
}
|
|
|
|
Future<List<Budget>> getBudgets() async {
|
|
final box = Hive.box<Budget>(budgetsBox);
|
|
return box.values.toList();
|
|
}
|
|
|
|
Future<void> updateBudget(int key, Budget budget) async {
|
|
final box = Hive.box<Budget>(budgetsBox);
|
|
await box.put(key, budget);
|
|
}
|
|
|
|
Future<void> deleteBudget(int key) async {
|
|
final box = Hive.box<Budget>(budgetsBox);
|
|
await box.delete(key);
|
|
}
|
|
}
|