addWallet method

Future<void> addWallet(
  1. Wallet wallet
)

Add a new wallet to the system

Implementation

Future<void> addWallet(Wallet wallet) async {
  await _repository.storeWallet(wallet);
  await _addWalletToMemory(wallet);

  // Initialize with provider
  final provider = _providers[wallet.type];
  if (provider != null) {
    final updatedWallet = await provider.initialize(wallet);
    if (updatedWallet != null) {
      // Replace old wallet with updated one while preserving order
      final list = _wallets.toList();
      final existingIndex = list.indexWhere((w) => w.id == wallet.id);
      if (existingIndex >= 0) {
        list[existingIndex] = updatedWallet;
        _wallets.clear();
        _wallets.addAll(list);
        _safeAddWallets(list);
      }
      // Also update in repository (addWallet handles updates too)
      await _repository.storeWallet(updatedWallet);
    }
  }

  if (wallet.canReceive &&
      _repository.getDefaultWalletIdForReceiving() == null) {
    _repository.setDefaultWalletForReceiving(wallet.id);
  }

  if (wallet.canSend && _repository.getDefaultWalletIdForSending() == null) {
    _repository.setDefaultWalletForSending(wallet.id);
  }

  _updateCombinedStreams();
}