131 lines
2.5 KiB
Dart
Executable File
131 lines
2.5 KiB
Dart
Executable File
import 'package:hive/hive.dart';
|
|
import 'package:kzeduca_app/screens/add_transaction_screen.dart';
|
|
|
|
part 'transaction.g.dart';
|
|
|
|
@HiveType(typeId: 0)
|
|
enum TransactionType {
|
|
@HiveField(0)
|
|
expense,
|
|
@HiveField(1)
|
|
income,
|
|
}
|
|
|
|
@HiveType(typeId: 1)
|
|
enum TransactionCategory {
|
|
@HiveField(0)
|
|
food,
|
|
@HiveField(1)
|
|
transport,
|
|
@HiveField(2)
|
|
social,
|
|
@HiveField(3)
|
|
education,
|
|
@HiveField(4)
|
|
medical,
|
|
@HiveField(5)
|
|
shopping,
|
|
@HiveField(6)
|
|
salary,
|
|
@HiveField(7)
|
|
invest,
|
|
@HiveField(8)
|
|
business,
|
|
@HiveField(9)
|
|
others,
|
|
@HiveField(7)
|
|
house,
|
|
@HiveField(8)
|
|
utilities,
|
|
@HiveField(9)
|
|
subscriptions,
|
|
@HiveField(10)
|
|
leisure,
|
|
@HiveField(11)
|
|
gym,
|
|
@HiveField(12)
|
|
gifts,
|
|
@HiveField(13)
|
|
pets,
|
|
@HiveField(14)
|
|
freelance,
|
|
@HiveField(15)
|
|
dividends,
|
|
@HiveField(16)
|
|
loan,
|
|
@HiveField(17)
|
|
refund,
|
|
@HiveField(18)
|
|
tax,
|
|
@HiveField(19)
|
|
bonus,
|
|
@HiveField(20)
|
|
allowance,
|
|
@HiveField(21)
|
|
pension,
|
|
@HiveField(22)
|
|
scholarship,
|
|
@HiveField(23)
|
|
grant,
|
|
}
|
|
|
|
@HiveType(typeId: 2)
|
|
class Transaction {
|
|
@HiveField(0)
|
|
final int? id;
|
|
@HiveField(1)
|
|
final String title;
|
|
@HiveField(2)
|
|
final double amount;
|
|
@HiveField(3)
|
|
final TransactionType type;
|
|
@HiveField(4)
|
|
final TransactionCategory category;
|
|
@HiveField(5)
|
|
final DateTime date;
|
|
@HiveField(6)
|
|
final String? note;
|
|
|
|
Transaction({
|
|
this.id,
|
|
required this.title,
|
|
required this.amount,
|
|
required this.type,
|
|
required this.category,
|
|
required this.date,
|
|
this.note,
|
|
});
|
|
|
|
Map<String, dynamic> toMap() {
|
|
return {
|
|
'id': id,
|
|
'title': title,
|
|
'amount': amount,
|
|
'type': type.name, // ✅ mais limpo que split('.').last
|
|
'category': category.name,
|
|
'date': date.toIso8601String(),
|
|
'note': note,
|
|
};
|
|
}
|
|
|
|
factory Transaction.fromMap(Map<String, dynamic> map) {
|
|
return Transaction(
|
|
id: map['id'] is int
|
|
? map['id'] as int
|
|
: int.tryParse(map['id']?.toString() ?? ''),
|
|
title: map['title']?.toString() ?? 'Sem título',
|
|
amount: (map['amount'] as num?)?.toDouble() ?? 0.0,
|
|
type: TransactionType.values.firstWhere(
|
|
(e) => e.name == map['type'],
|
|
orElse: () => TransactionType.expense,
|
|
),
|
|
category: TransactionCategory.values.firstWhere(
|
|
(e) => e.name == map['category'],
|
|
orElse: () => TransactionCategory.others,
|
|
),
|
|
date: DateTime.tryParse(map['date']?.toString() ?? '') ?? DateTime.now(),
|
|
note: map['note']?.toString(),
|
|
);
|
|
}
|
|
}
|