From e65d4e35d73e29d56a661ad50f72b5f9114c80c6 Mon Sep 17 00:00:00 2001 From: franzap <_@franzap.com> Date: Fri, 3 Jan 2025 19:12:38 -0300 Subject: [PATCH] More NWC container fixes --- lib/screens/settings_screen.dart | 132 +++++++++-------- lib/utils/nwc.dart | 6 + lib/widgets/zap_button.dart | 234 ++++++++++++++----------------- 3 files changed, 177 insertions(+), 195 deletions(-) diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart index 42e1ead..7cf246f 100644 --- a/lib/screens/settings_screen.dart +++ b/lib/screens/settings_screen.dart @@ -112,7 +112,7 @@ class SettingsScreen extends HookConsumerWidget { Divider(), Gap(20), Text( - 'Wallet', + 'Nostr Wallet Connect', style: context.theme.textTheme.headlineLarge! .copyWith(fontWeight: FontWeight.bold), ), @@ -246,76 +246,72 @@ class NwcContainer extends HookConsumerWidget { final controller = useTextEditingController(text: ''); final isTextFieldEmpty = useState(true); - if (nwcConnection.isLoading) { - return Padding( - padding: const EdgeInsets.all(5), - child: SmallCircularProgressIndicator(), - ); - } - - return Column( - mainAxisSize: MainAxisSize.min, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - if (!nwcConnection.hasError) - Text( - "Nostr Wallet Connect ${nwcConnection.hasValue ? 'connected' : 'URI'}"), - if (nwcConnection.hasError) - Text('Error connecting: ${nwcConnection.error}'), - if (nwcConnection.hasValue && - nwcConnection.value != null && - !dialogMode) - ElevatedButton( - onPressed: () async { - await ref.read(nwcConnectionProvider.notifier).disconnectWallet(); - }, - style: ElevatedButton.styleFrom( - disabledBackgroundColor: Colors.transparent, - backgroundColor: Colors.transparent), - child: Text('Disconnect'), - ), - if ((nwcConnection.hasValue && nwcConnection.value == null) || - nwcConnection.hasError) - TextField( - autocorrect: false, - keyboardType: TextInputType.none, - controller: controller, - onChanged: (value) { - isTextFieldEmpty.value = value.isEmpty; - }, - decoration: InputDecoration( - hintText: 'nostr+walletconnect://...', - suffixIcon: AsyncButtonBuilder( - disabled: isTextFieldEmpty.value, - loadingWidget: SmallCircularProgressIndicator(), - onPressed: () async { - final nwcUri = controller.text.trim(); - await ref - .read(nwcConnectionProvider.notifier) - .connectWallet(nwcUri); - if (dialogMode && context.mounted) { - Navigator.of(context).pop(); - } - }, - builder: (context, child, callback, buttonState) { - return Padding( - padding: const EdgeInsets.only(bottom: 2), - child: SizedBox( - child: ElevatedButton( - onPressed: callback, - style: ElevatedButton.styleFrom( - disabledBackgroundColor: Colors.transparent, - backgroundColor: Colors.transparent), - child: child, + return Padding( + padding: const EdgeInsets.all(4), + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + if (!nwcConnection.hasValue && nwcConnection.isLoading) + SmallCircularProgressIndicator(), + if (nwcConnection.isPresent) Text('Wallet connected'), + if (nwcConnection.hasError) + Text('Error connecting: ${nwcConnection.error}'), + if (!nwcConnection.isPresent && !nwcConnection.isLoading) + TextField( + autocorrect: false, + keyboardType: TextInputType.none, + controller: controller, + onChanged: (value) { + isTextFieldEmpty.value = value.isEmpty; + }, + decoration: InputDecoration( + hintText: 'nostr+walletconnect://...', + suffixIcon: AsyncButtonBuilder( + disabled: isTextFieldEmpty.value, + loadingWidget: SmallCircularProgressIndicator(), + onPressed: () async { + final nwcUri = controller.text.trim(); + await ref + .read(nwcConnectionProvider.notifier) + .connectWallet(nwcUri); + if (dialogMode && context.mounted) { + Navigator.of(context).pop(); + } + }, + builder: (context, child, callback, buttonState) { + return Padding( + padding: const EdgeInsets.only(bottom: 2), + child: SizedBox( + child: ElevatedButton( + onPressed: callback, + style: ElevatedButton.styleFrom( + disabledBackgroundColor: Colors.transparent, + backgroundColor: Colors.transparent), + child: child, + ), ), - ), - ); - }, - child: Text('Connect'), + ); + }, + child: Text('Connect'), + ), ), ), - ), - ], + if (nwcConnection.isPresent && !dialogMode) + ElevatedButton( + onPressed: () async { + controller.clear(); + await ref + .read(nwcConnectionProvider.notifier) + .disconnectWallet(); + }, + style: ElevatedButton.styleFrom( + disabledBackgroundColor: Colors.transparent, + backgroundColor: Colors.transparent), + child: Text('Disconnect'), + ), + ], + ), ); } } diff --git a/lib/utils/nwc.dart b/lib/utils/nwc.dart index 0d35706..a6cc54a 100644 --- a/lib/utils/nwc.dart +++ b/lib/utils/nwc.dart @@ -68,3 +68,9 @@ class NwcConnectionNotifier extends StateNotifier> { final nwcConnectionProvider = StateNotifierProvider>( (ref) => NwcConnectionNotifier(ref)); + +extension NwcExt on AsyncValue { + bool get isPresent { + return hasValue && value != null && !hasError; + } +} diff --git a/lib/widgets/zap_button.dart b/lib/widgets/zap_button.dart index 3177c29..b09d369 100644 --- a/lib/widgets/zap_button.dart +++ b/lib/widgets/zap_button.dart @@ -1,5 +1,4 @@ import 'package:flutter/material.dart'; -import 'package:gap/gap.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:zapstore/main.data.dart'; import 'package:zapstore/models/app.dart'; @@ -31,133 +30,114 @@ class ZapButton extends HookConsumerWidget { final amountController = TextEditingController(); - return app.developer.value?.lud16 != null - ? ElevatedButton( - onPressed: () async { - // final loggedInUser = user ?? - // await showDialog( - // context: context, - // builder: (_) => AlertDialog( - // title: Text( - // 'Sign in to zap', - // style: TextStyle(fontWeight: FontWeight.bold), - // ), - // content: SignInDialogBox(publicKeyAllowed: false), - // ), - // ); + if (app.developer.value?.lud16 == null) { + return Container(); + } - // if (loggedInUser == null) { - // return; - // } - - if (nwcConnection.value == null) { - await showDialog( - // ignore: use_build_context_synchronously - context: context, - builder: (_) => AlertDialog( - title: Text('Connect your wallet').bold, - content: NwcContainer(dialogMode: true), - ), - ); - } - - if (ref.read(nwcConnectionProvider).value == null) { - return; - } - - final amount = await showDialog( - // ignore: use_build_context_synchronously - context: context, - builder: (context) { - return AlertDialog( - title: Text('Choose zap amount').bold, - content: Column( - mainAxisSize: MainAxisSize.min, - children: [ - if (user == null) - SignInButton( - publicKeyAllowed: false, - minimal: true, - signedOutText: - '⚠️ If you do not sign in, you will be zapping anonymously', - ), - TextField( - controller: amountController, - keyboardType: TextInputType.number, - decoration: InputDecoration( - hintText: "Enter amount in sats"), - ), - SizedBox(height: 20), - Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - ElevatedButton( - onPressed: () => - Navigator.of(context).pop(21), - child: Text('⚡️ 21'), - ), - ElevatedButton( - onPressed: () => - Navigator.of(context).pop(210), - child: Text('⚡️ 210'), - ), - ElevatedButton( - onPressed: () => - Navigator.of(context).pop(2100), - child: Text('⚡️ 2100'), - ), - ], - ), - ], - ), - actions: [ - TextButton( - child: Text('Cancel'), - onPressed: () { - Navigator.of(context) - .pop(); // Dismiss the dialog without returning an amount - }, - ), - TextButton( - child: Text('Zap').bold, - onPressed: () { - Navigator.of(context).pop( - int.tryParse(amountController.text) ?? 0); - }, - ), - ], - ); - }, - ) ?? - 0; - - if (amount > 0) { - try { - // ignore: use_build_context_synchronously - context.showInfo('Zap sent!', - description: '$amount sats on their way'); - if (user != null) { - await user.zap(amount, event: app.latestMetadata!); - } else { - await anonUser!.zap(amount, - event: app.latestMetadata!, signer: pkSigner); - } - } catch (e) { - // ignore: use_build_context_synchronously - context.showError( - title: 'Unable to zap', description: e.toString()); - } - } - }, - style: ElevatedButton.styleFrom(backgroundColor: Colors.grey[800]), - child: Text( - '⚡️ Zap this release', - style: TextStyle( - color: Colors.white, - fontWeight: FontWeight.bold, - fontSize: 15), + return ElevatedButton( + onPressed: () async { + if (!nwcConnection.isPresent) { + await showDialog( + // ignore: use_build_context_synchronously + context: context, + builder: (_) => AlertDialog( + title: Text('Connect your wallet').bold, + content: NwcContainer(dialogMode: true), ), - ) - : Container(); + ); + } + + if (!ref.read(nwcConnectionProvider).isPresent) { + return; + } + + final amount = await showDialog( + // ignore: use_build_context_synchronously + context: context, + builder: (context) { + return AlertDialog( + title: Text('Choose zap amount').bold, + content: Column( + mainAxisSize: MainAxisSize.min, + children: [ + if (user == null) + SignInButton( + publicKeyAllowed: false, + minimal: true, + signedOutText: + '⚠️ If you do not sign in, you will be zapping anonymously', + ), + TextField( + controller: amountController, + keyboardType: TextInputType.number, + decoration: + InputDecoration(hintText: "Enter amount in sats"), + ), + SizedBox(height: 20), + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + ElevatedButton( + onPressed: () => Navigator.of(context).pop(21), + child: Text('⚡️ 21'), + ), + ElevatedButton( + onPressed: () => Navigator.of(context).pop(210), + child: Text('⚡️ 210'), + ), + ElevatedButton( + onPressed: () => Navigator.of(context).pop(2100), + child: Text('⚡️ 2100'), + ), + ], + ), + ], + ), + actions: [ + TextButton( + child: Text('Cancel'), + onPressed: () { + Navigator.of(context) + .pop(); // Dismiss the dialog without returning an amount + }, + ), + TextButton( + child: Text('Zap').bold, + onPressed: () { + Navigator.of(context) + .pop(int.tryParse(amountController.text) ?? 0); + }, + ), + ], + ); + }, + ); + + if (amount != null && amount > 0) { + try { + // ignore: use_build_context_synchronously + context.showInfo('Zap sent!', + description: + '$amount sats on their way. Your zap should show up in a few seconds.'); + if (user != null) { + await user.zap(amount, event: app.latestMetadata!); + } else { + await anonUser! + .zap(amount, event: app.latestMetadata!, signer: pkSigner); + } + } catch (e) { + // ignore: use_build_context_synchronously + context.showError( + title: 'Unable to zap', description: e.toString()); + } + } + }, + style: ElevatedButton.styleFrom(backgroundColor: Colors.grey[800]), + child: Text( + '⚡️ Zap this release', + style: TextStyle( + color: Colors.white, fontWeight: FontWeight.bold, fontSize: 15), + ), + ); } }