39 lines
1.4 KiB
Dart
39 lines
1.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:hive/hive.dart';
|
|
import 'package:kzeduca_app/models/transaction.dart' as app;
|
|
import 'package:kzeduca_app/services/hive_service.dart';
|
|
import 'package:kzeduca_app/services/user_state_service.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class TransactionListNotifier extends ChangeNotifier {
|
|
List<app.Transaction> _transactions = [];
|
|
|
|
List<app.Transaction> get transactions => _transactions;
|
|
|
|
Future<void> loadTransactions() async {
|
|
_transactions = await HiveService().getTransactions();
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> addTransaction(app.Transaction transaction, {BuildContext? context}) async {
|
|
await HiveService().insertTransaction(transaction);
|
|
await loadTransactions();
|
|
if (context != null) {
|
|
final userStateService = Provider.of<UserStateService>(context, listen: false);
|
|
await userStateService.recalculateBalanceFromHive();
|
|
}
|
|
}
|
|
|
|
Future<void> removeTransaction(int index, {BuildContext? context}) async {
|
|
// Remover a transação do Hive
|
|
final box = Hive.box<app.Transaction>(HiveService.transactionsBox);
|
|
final key = box.keyAt(index);
|
|
await box.delete(key);
|
|
await loadTransactions();
|
|
if (context != null) {
|
|
final userStateService = Provider.of<UserStateService>(context, listen: false);
|
|
await userStateService.recalculateBalanceFromHive();
|
|
}
|
|
}
|
|
}
|