More NWC container fixes

This commit is contained in:
franzap
2025-01-03 19:12:38 -03:00
parent 7dad1b0bb9
commit e65d4e35d7
3 changed files with 177 additions and 195 deletions
+64 -68
View File
@@ -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'),
),
],
),
);
}
}
+6
View File
@@ -68,3 +68,9 @@ class NwcConnectionNotifier extends StateNotifier<AsyncValue<NwcConnection?>> {
final nwcConnectionProvider =
StateNotifierProvider<NwcConnectionNotifier, AsyncValue<NwcConnection?>>(
(ref) => NwcConnectionNotifier(ref));
extension NwcExt on AsyncValue<NwcConnection?> {
bool get isPresent {
return hasValue && value != null && !hasError;
}
}
+107 -127
View File
@@ -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<int>(
// 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<int>(
// 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),
),
);
}
}