feat: Implement account management features

- Added ExpenseDetailDialog for displaying expense details and actions.
- Created ExpensesTab to list expenses for a group.
- Developed GroupExpensesPage to manage group expenses with tabs for expenses, balances, and settlements.
- Introduced SettlementsTab to show optimized repayment plans.
- Refactored create_trip_content.dart to remove CountBloc and related logic.
- Added Account model to manage user accounts and group members.
- Replaced CountRepository with AccountRepository for account-related operations.
- Removed CountService and CountRepository as part of the refactor.
- Updated main.dart and home.dart to integrate new account management components.
This commit is contained in:
Dayron
2025-10-21 00:42:36 +02:00
parent a3ced0e812
commit c69618cbd9
21 changed files with 182 additions and 747 deletions

View File

@@ -0,0 +1,117 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:travel_mate/services/error_service.dart';
import '../data/models/group_member.dart';
import '../data/models/account.dart';
class AccountRepository {
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final _errorService = ErrorService();
CollectionReference get _accountCollection => _firestore.collection('accounts');
CollectionReference _membersCollection(String accountId) {
return _accountCollection.doc(accountId).collection('members');
}
Future<String> createAccountWithMembers({
required Account account,
required List<GroupMember> members,
}) async {
try {
return await _firestore.runTransaction<String>((transaction) async {
final accountRef = _accountCollection.doc();
final accountData = account.toMap();
transaction.set(accountRef, accountData);
for (var member in members) {
final memberRef = accountRef.collection('members').doc(member.userId);
transaction.set(memberRef, member.toMap());
}
return accountRef.id;
});
} catch (e) {
throw Exception('Erreur lors de la création du compte: $e');
}
}
Stream<List<Account>> getAccountByUserId(String userId) {
return _accountCollection
.snapshots()
.asyncMap((snapshot) async {
List<Account> userAccounts = [];
for (var accountDoc in snapshot.docs) {
try {
final accountId = accountDoc.id;
final memberDoc = await accountDoc.reference
.collection('members')
.doc(userId)
.get();
if (memberDoc.exists) {
final accountData = accountDoc.data() as Map<String, dynamic>;
final account = Account.fromMap(accountData);
final members = await getAccountMembers(accountId);
userAccounts.add(account.copyWith(members: members));
} else {
_errorService.logInfo('account_repository.dart', 'Utilisateur NON membre de $accountId');
}
} catch (e, stackTrace) {
_errorService.logError(e.toString(), stackTrace);
}
}
return userAccounts;
})
.distinct((prev, next) {
if (prev.length != next.length) return false;
final prevIds = prev.map((a) => a.id).toSet();
final nextIds = next.map((a) => a.id).toSet();
final identical = prevIds.difference(nextIds).isEmpty &&
nextIds.difference(prevIds).isEmpty;
return identical;
})
.handleError((error, stackTrace) {
_errorService.logError(error, stackTrace);
return <Account>[];
});
}
Future<List<GroupMember>> getAccountMembers(String accountId) async {
try {
final snapshot = await _membersCollection(accountId).get();
return snapshot.docs
.map((doc) {
return GroupMember.fromMap(
doc.data() as Map<String, dynamic>,
doc.id,
);
})
.toList();
} catch (e) {
throw Exception('Erreur lors de la récupération des membres: $e');
}
}
Future<DocumentSnapshot> getAccountById(String accountId) async {
return await _firestore.collection('accounts').doc(accountId).get();
}
Future<void> createAccount(Map<String, dynamic> accountData) async {
await _firestore.collection('accounts').add(accountData);
}
Future<void> updateAccount(String accountId, Map<String, dynamic> accountData) async {
await _firestore.collection('accounts').doc(accountId).update(accountData);
}
Future<void> deleteAccount(String accountId) async {
await _firestore.collection('accounts').doc(accountId).delete();
}
}

View File

@@ -1,165 +0,0 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'dart:io';
import '../data/models/expense.dart';
class CountRepository {
final FirebaseFirestore _firestore;
final FirebaseStorage _storage;
CountRepository({
FirebaseFirestore? firestore,
FirebaseStorage? storage,
}) : _firestore = firestore ?? FirebaseFirestore.instance,
_storage = storage ?? FirebaseStorage.instance;
// Créer une dépense
Future<String> createExpense(Expense expense) async {
final docRef = await _firestore
.collection('groups')
.doc(expense.groupId)
.collection('expenses')
.add(expense.toMap());
return docRef.id;
}
// Mettre à jour une dépense
Future<void> updateExpense(String groupId, Expense expense) async {
await _firestore
.collection('groups')
.doc(groupId)
.collection('expenses')
.doc(expense.id)
.update(expense.toMap());
}
// Supprimer une dépense
Future<void> deleteExpense(String groupId, String expenseId) async {
final expense = await _firestore
.collection('groups')
.doc(groupId)
.collection('expenses')
.doc(expenseId)
.get();
final data = expense.data();
if (data != null && data['receiptUrl'] != null) {
await deleteReceipt(data['receiptUrl']);
}
await _firestore
.collection('groups')
.doc(groupId)
.collection('expenses')
.doc(expenseId)
.delete();
}
// Archiver une dépense
Future<void> archiveExpense(String groupId, String expenseId) async {
await _firestore
.collection('groups')
.doc(groupId)
.collection('expenses')
.doc(expenseId)
.update({'isArchived': true});
}
// Marquer une split comme payée
Future<void> markSplitAsPaid({
required String groupId,
required String expenseId,
required String userId,
}) async {
final doc = await _firestore
.collection('groups')
.doc(groupId)
.collection('expenses')
.doc(expenseId)
.get();
final expense = Expense.fromFirestore(doc);
final updatedSplits = expense.splits.map((split) {
if (split.userId == userId) {
return ExpenseSplit(
userId: split.userId,
userName: split.userName,
amount: split.amount,
isPaid: true,
);
}
return split;
}).toList();
await _firestore
.collection('groups')
.doc(groupId)
.collection('expenses')
.doc(expenseId)
.update({
'splits': updatedSplits.map((s) => s.toMap()).toList(),
});
}
// Stream des dépenses d'un groupe
Stream<List<Expense>> getExpensesStream(String groupId, {bool includeArchived = false}) {
Query query = _firestore
.collection('groups')
.doc(groupId)
.collection('expenses')
.orderBy('date', descending: true);
if (!includeArchived) {
query = query.where('isArchived', isEqualTo: false);
}
return query.snapshots().map((snapshot) {
return snapshot.docs.map((doc) => Expense.fromFirestore(doc)).toList();
});
}
// Uploader un reçu
Future<String> uploadReceipt(String groupId, String expenseId, File imageFile) async {
final fileName = 'receipts/$groupId/$expenseId/${DateTime.now().millisecondsSinceEpoch}.jpg';
final ref = _storage.ref().child(fileName);
final uploadTask = await ref.putFile(imageFile);
return await uploadTask.ref.getDownloadURL();
}
// Supprimer un reçu
Future<void> deleteReceipt(String receiptUrl) async {
try {
final ref = _storage.refFromURL(receiptUrl);
await ref.delete();
} catch (e) {
// Le fichier n'existe peut-être plus
print('Erreur lors de la suppression du reçu: $e');
}
}
// Obtenir les taux de change (API externe ou valeurs fixes)
Future<Map<ExpenseCurrency, double>> getExchangeRates() async {
// TODO: Intégrer une API de taux de change réels
// Pour l'instant, valeurs approximatives
return {
ExpenseCurrency.eur: 1.0,
ExpenseCurrency.usd: 0.92,
ExpenseCurrency.gbp: 1.17,
ExpenseCurrency.jpy: 0.0062,
ExpenseCurrency.chf: 1.05,
ExpenseCurrency.cad: 0.68,
ExpenseCurrency.aud: 0.61,
};
}
// Convertir un montant en EUR
Future<double> convertToEur(double amount, ExpenseCurrency currency) async {
if (currency == ExpenseCurrency.eur) return amount;
final rates = await getExchangeRates();
final rate = rates[currency] ?? 1.0;
return amount * rate;
}
}