showAddCashuWalletDialog function

Future<CashuWallet?> showAddCashuWalletDialog(
  1. BuildContext context,
  2. NdkFlutter ndkFlutter, {
  3. String defaultMintUrl = _defaultMintUrl,
  4. bool returnToWalletType = false,
  5. AlbyGoConnectConfig albyGoConnectConfig = kDefaultAlbyGoConnectConfig,
})

Shows a dialog to add a Cashu wallet.

Returns the created CashuWallet if successful, or null if cancelled.

Implementation

Future<CashuWallet?> showAddCashuWalletDialog(
  BuildContext context,
  NdkFlutter ndkFlutter, {
  String defaultMintUrl = _defaultMintUrl,
  bool returnToWalletType = false,
  AlbyGoConnectConfig albyGoConnectConfig = kDefaultAlbyGoConnectConfig,
}) async {
  final l10n = AppLocalizations.of(context)!;
  final mintUrlController = TextEditingController(text: defaultMintUrl);

  return showDialog<CashuWallet?>(
    context: context,
    builder: (dialogContext) {
      return AlertDialog(
        title: Row(
          children: [
            IconButton(
              onPressed: () async {
                Navigator.of(dialogContext).pop();
                if (returnToWalletType && context.mounted) {
                  await showAddWalletTypeDialog(
                    context,
                    ndkFlutter,
                    albyGoConnectConfig: albyGoConnectConfig,
                  );
                }
              },
              icon: const Icon(Icons.arrow_back),
            ),
            Expanded(child: Text(l10n.addCashuWalletTitle)),
            IconButton(
              onPressed: () => Navigator.of(dialogContext).pop(),
              icon: const Icon(Icons.close),
            ),
          ],
        ),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(l10n.enterMintUrl),
            const SizedBox(height: 16),
            TextField(
              controller: mintUrlController,
              decoration: InputDecoration(
                border: const OutlineInputBorder(),
                labelText: l10n.mintUrl,
                hintText: l10n.mintUrlHint,
              ),
            ),
          ],
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(dialogContext).pop(),
            child: Text(l10n.cancel),
          ),
          TextButton(
            onPressed: () async {
              final mintUrl = mintUrlController.text.trim();
              if (mintUrl.isEmpty) {
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(
                    content: Text(l10n.pleaseEnterMintUrl),
                    backgroundColor: Colors.red,
                  ),
                );
                return;
              }

              final scaffoldMessenger = ScaffoldMessenger.of(context);

              try {
                await ndkFlutter.ndk.cashu.addMintToKnownMints(
                  mintUrl: mintUrl,
                );
                final mintInfo = await ndkFlutter.ndk.cashu.getMintInfoNetwork(
                  mintUrl: mintUrl,
                );

                final cashuWallet = CashuWallet(
                  id: mintUrl,
                  name: mintInfo.name ?? mintUrl,
                  mintUrl: mintUrl,
                  mintInfo: mintInfo,
                  supportedUnits: mintInfo.supportedUnits,
                );

                await ndkFlutter.ndk.wallets.addWallet(cashuWallet);

                if (context.mounted) {
                  Navigator.of(context).pop(cashuWallet);
                }
                scaffoldMessenger.showSnackBar(
                  SnackBar(
                    content: Text(l10n.cashuWalletAdded),
                    backgroundColor: Colors.green,
                  ),
                );
              } catch (e) {
                scaffoldMessenger.showSnackBar(
                  SnackBar(
                    content: Text('${l10n.failedToAddMint}: $e'),
                    backgroundColor: Colors.red,
                  ),
                );
              }
            },
            child: Text(l10n.add),
          ),
        ],
      );
    },
  );
}