Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 107 additions & 4 deletions packages/komodo_defi_sdk/example/lib/screens/asset_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,19 @@ class _AssetPageState extends State<AssetPage> {
Future<void> _generateNewAddress() async {
setState(() => _isLoading = true);
try {
final newPubkey = await _sdk.pubkeys.createNewPubkey(widget.asset);
setState(() {
_pubkeys?.keys.add(newPubkey);
});
final stream = _sdk.pubkeys.createNewPubkeyStream(widget.asset);

final newPubkey = await showDialog<PubkeyInfo>(
context: context,
barrierDismissible: false,
builder: (context) => _NewAddressDialog(stream: stream),
);

if (newPubkey != null) {
setState(() {
_pubkeys?.keys.add(newPubkey);
});
}
} catch (e) {
setState(() => _error = e.toString());
} finally {
Expand Down Expand Up @@ -655,3 +664,97 @@ class __TransactionsSectionState extends State<_TransactionsSection> {
}
}
}

class _NewAddressDialog extends StatefulWidget {
const _NewAddressDialog({required this.stream});

final Stream<NewAddressState> stream;

@override
State<_NewAddressDialog> createState() => _NewAddressDialogState();
}

class _NewAddressDialogState extends State<_NewAddressDialog> {
late final StreamSubscription<NewAddressState> _subscription;
NewAddressState? _state;

@override
void initState() {
super.initState();
_subscription = widget.stream.listen((state) {
setState(() => _state = state);
if (state.status == NewAddressStatus.completed) {
Navigator.of(context).pop(state.address);
} else if (state.status == NewAddressStatus.error ||
state.status == NewAddressStatus.cancelled) {
Navigator.of(context).pop(null);
}
});
}

@override
void dispose() {
_subscription.cancel();
super.dispose();
}

@override
Widget build(BuildContext context) {
final state = _state;

String message;
if (state == null) {
message = 'Initializing...';
} else {
switch (state.status) {
case NewAddressStatus.initializing:
case NewAddressStatus.processing:
case NewAddressStatus.waitingForDevice:
case NewAddressStatus.waitingForDeviceConfirmation:
case NewAddressStatus.pinRequired:
case NewAddressStatus.passphraseRequired:
message = state.message ?? 'Processing...';
break;
case NewAddressStatus.confirmAddress:
message = 'Confirm the address on your device';
break;
case NewAddressStatus.completed:
message = 'Completed';
break;
case NewAddressStatus.error:
message = state.error ?? 'Error';
break;
case NewAddressStatus.cancelled:
message = 'Cancelled';
break;
}
}

final showAddress = state?.status == NewAddressStatus.confirmAddress;

return AlertDialog(
Comment thread
takenagain marked this conversation as resolved.
title: const Text('Generating Address'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (showAddress)
SelectableText(state?.expectedAddress ?? '')
else
const SizedBox(
width: 32,
height: 32,
child: CircularProgressIndicator(),
),
const SizedBox(height: 16),
Text(message, textAlign: TextAlign.center),
],
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
],
);
}
}
Loading