implemented backend wallets/ledger accounts listing

This commit is contained in:
Stephan D
2025-11-25 23:38:10 +01:00
parent be913bf96c
commit 68f0a1048f
25 changed files with 882 additions and 131 deletions

View File

@@ -2,130 +2,89 @@ import 'package:flutter/material.dart';
import 'package:pweb/models/wallet.dart';
import 'package:pweb/services/wallets.dart';
import 'package:pshared/provider/resource.dart';
import 'package:pshared/utils/exception.dart';
class WalletsProvider with ChangeNotifier {
final WalletsService _service;
WalletsProvider(this._service);
List<Wallet>? _wallets;
bool _isLoading = false;
String? _error;
Wallet? _selectedWallet;
final bool _isHidden = true;
Resource<List<Wallet>> _resource = Resource(data: []);
Resource<List<Wallet>> get resource => _resource;
List<Wallet>? get wallets => _wallets;
bool get isLoading => _isLoading;
String? get error => _error;
List<Wallet> get wallets => _resource.data ?? [];
bool get isLoading => _resource.isLoading;
Exception? get error => _resource.error;
Wallet? _selectedWallet;
Wallet? get selectedWallet => _selectedWallet;
final bool _isHidden = true;
bool get isHidden => _isHidden;
bool _isRefreshingBalances = false;
bool get isRefreshingBalances => _isRefreshingBalances;
void selectWallet(Wallet wallet) {
_selectedWallet = wallet;
notifyListeners();
}
Future<void> loadData() async {
_isLoading = true;
_error = null;
notifyListeners();
Future<void> loadWalletsWithBalances() async {
_setResource(_resource.copyWith(isLoading: true, error: null));
try {
_wallets = await _service.getWallets();
} catch (e) {
_error = e.toString();
} finally {
_isLoading = false;
notifyListeners();
}
}
Future<Wallet?> getWalletById(String walletId) async {
_isLoading = true;
_error = null;
notifyListeners();
try {
final wallet = await _service.getWallet(walletId);
return wallet;
} catch (e) {
_error = e.toString();
return null;
} finally {
_isLoading = false;
notifyListeners();
}
}
void updateName(String walletRef, String newName) {
final index = _wallets?.indexWhere((w) => w.id == walletRef);
if (index != null && index >= 0) {
_wallets![index] = _wallets![index].copyWith(name: newName);
notifyListeners();
}
}
void updateBalance(String walletRef, double newBalance) {
final index = _wallets?.indexWhere((w) => w.id == walletRef);
if (index != null && index >= 0) {
_wallets![index] = _wallets![index].copyWith(balance: newBalance);
notifyListeners();
}
}
Future<void> updateWallet(Wallet wallet) async {
try {
await _service.updateWallet();
final index = _wallets?.indexWhere((w) => w.id == wallet.id);
if (index != null && index >= 0) {
_wallets![index] = wallet;
notifyListeners();
final base = await _service.getWallets();
final withBalances = <Wallet>[];
for (final wallet in base) {
try {
final balance = await _service.getBalance(wallet.id);
withBalances.add(wallet.copyWith(balance: balance));
} catch (e) {
_setResource(_resource.copyWith(error: toException(e)));
withBalances.add(wallet);
}
}
_setResource(Resource(data: withBalances, isLoading: false, error: _resource.error));
} catch (e) {
_error = e.toString();
notifyListeners();
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
}
}
Future<void> refreshBalances() async {
if (wallets.isEmpty) return;
_isRefreshingBalances = true;
notifyListeners();
Future<void> addWallet(Wallet wallet) async {
try {
final newWallet = await _service.createWallet(); // Pass the wallet parameter
_wallets = [...?_wallets, ]; // Add the new wallet
notifyListeners();
final updated = <Wallet>[];
for (final wallet in wallets) {
final balance = await _service.getBalance(wallet.id);
updated.add(wallet.copyWith(balance: balance));
}
_setResource(_resource.copyWith(data: updated));
} catch (e) {
_error = e.toString();
notifyListeners();
}
}
Future<void> deleteWallet(String walletId) async {
try {
await _service.deleteWallet(); // Pass the walletId parameter
_wallets?.removeWhere((w) => w.id == walletId);
notifyListeners();
} catch (e) {
_error = e.toString();
_setResource(_resource.copyWith(error: toException(e)));
} finally {
_isRefreshingBalances = false;
notifyListeners();
}
}
void toggleVisibility(String walletId) {
final index = _wallets?.indexWhere((w) => w.id == walletId);
if (index != null && index >= 0) {
final wallet = _wallets![index];
_wallets![index] = wallet.copyWith(isHidden: !wallet.isHidden);
if (_selectedWallet?.id == walletId) {
_selectedWallet = _wallets![index];
}
notifyListeners();
final index = wallets.indexWhere((w) => w.id == walletId);
if (index < 0) return;
final wallet = wallets[index];
final updated = wallet.copyWith(isHidden: !wallet.isHidden);
final next = List<Wallet>.from(wallets);
next[index] = updated;
_setResource(_resource.copyWith(data: next));
if (_selectedWallet?.id == walletId) {
_selectedWallet = updated;
}
}
}
void _setResource(Resource<List<Wallet>> newResource) {
_resource = newResource;
notifyListeners();
}
}

View File

@@ -4,11 +4,7 @@ import 'package:pweb/models/wallet.dart';
abstract class WalletsService {
Future<List<Wallet>> getWallets();
Future<List<Wallet>> updateWallet();
Future<List<Wallet>> createWallet();
Future<List<Wallet>> deleteWallet();
Future<Wallet> getWallet(String walletRef);
Future<double> getBalance(String walletRef);
}
class MockWalletsService implements WalletsService {
@@ -31,11 +27,11 @@ class MockWalletsService implements WalletsService {
}
@override
Future<List<Wallet>> updateWallet() async => [];
@override
Future<List<Wallet>> createWallet() async => [];
@override
Future<List<Wallet>> deleteWallet() async => [];
}
Future<double> getBalance(String walletRef) async {
final wallet = _wallets.firstWhere(
(w) => w.id == walletRef,
orElse: () => throw Exception('Wallet not found'),
);
return wallet.balance;
}
}