|
|
|
@ -4,8 +4,7 @@ import '../../data/nostr/models/nostr_relay.dart';
|
|
|
|
|
|
|
|
|
|
|
|
/// Screen for managing Nostr relays.
|
|
|
|
/// Screen for managing Nostr relays.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Allows users to view, add, remove, and monitor relay health,
|
|
|
|
/// Allows users to view, add, remove, test, and toggle relays.
|
|
|
|
/// and trigger manual syncs.
|
|
|
|
|
|
|
|
class RelayManagementScreen extends StatefulWidget {
|
|
|
|
class RelayManagementScreen extends StatefulWidget {
|
|
|
|
/// Controller for managing relay state.
|
|
|
|
/// Controller for managing relay state.
|
|
|
|
final RelayManagementController controller;
|
|
|
|
final RelayManagementController controller;
|
|
|
|
@ -22,6 +21,7 @@ class RelayManagementScreen extends StatefulWidget {
|
|
|
|
|
|
|
|
|
|
|
|
class _RelayManagementScreenState extends State<RelayManagementScreen> {
|
|
|
|
class _RelayManagementScreenState extends State<RelayManagementScreen> {
|
|
|
|
final TextEditingController _urlController = TextEditingController();
|
|
|
|
final TextEditingController _urlController = TextEditingController();
|
|
|
|
|
|
|
|
final Map<String, bool> _testingRelays = {};
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
void dispose() {
|
|
|
|
@ -29,6 +29,31 @@ class _RelayManagementScreenState extends State<RelayManagementScreen> {
|
|
|
|
super.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> _handleTestRelay(String relayUrl) async {
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
|
|
|
|
_testingRelays[relayUrl] = true;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final success = await widget.controller.testRelay(relayUrl);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
|
|
|
|
_testingRelays[relayUrl] = false;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
|
|
|
SnackBar(
|
|
|
|
|
|
|
|
content: Text(
|
|
|
|
|
|
|
|
success
|
|
|
|
|
|
|
|
? 'Relay test successful'
|
|
|
|
|
|
|
|
: 'Relay test failed',
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
duration: const Duration(seconds: 2),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
return Scaffold(
|
|
|
|
@ -65,12 +90,48 @@ class _RelayManagementScreenState extends State<RelayManagementScreen> {
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
|
|
// Actions section
|
|
|
|
// Top action buttons
|
|
|
|
Padding(
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(16),
|
|
|
|
padding: const EdgeInsets.all(16),
|
|
|
|
child: Column(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
children: [
|
|
|
|
children: [
|
|
|
|
|
|
|
|
// Test All and Toggle All buttons
|
|
|
|
|
|
|
|
Row(
|
|
|
|
|
|
|
|
children: [
|
|
|
|
|
|
|
|
Expanded(
|
|
|
|
|
|
|
|
child: ElevatedButton.icon(
|
|
|
|
|
|
|
|
onPressed: widget.controller.isCheckingHealth
|
|
|
|
|
|
|
|
? null
|
|
|
|
|
|
|
|
: widget.controller.checkRelayHealth,
|
|
|
|
|
|
|
|
icon: widget.controller.isCheckingHealth
|
|
|
|
|
|
|
|
? const SizedBox(
|
|
|
|
|
|
|
|
width: 16,
|
|
|
|
|
|
|
|
height: 16,
|
|
|
|
|
|
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
: const Icon(Icons.network_check),
|
|
|
|
|
|
|
|
label: const Text('Test All'),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
const SizedBox(width: 8),
|
|
|
|
|
|
|
|
Expanded(
|
|
|
|
|
|
|
|
child: ElevatedButton.icon(
|
|
|
|
|
|
|
|
onPressed: widget.controller.relays.isEmpty
|
|
|
|
|
|
|
|
? null
|
|
|
|
|
|
|
|
: widget.controller.toggleAllRelays,
|
|
|
|
|
|
|
|
icon: const Icon(Icons.power_settings_new),
|
|
|
|
|
|
|
|
label: Text(
|
|
|
|
|
|
|
|
widget.controller.relays.isNotEmpty &&
|
|
|
|
|
|
|
|
widget.controller.relays.every((r) => r.isEnabled)
|
|
|
|
|
|
|
|
? 'Turn All Off'
|
|
|
|
|
|
|
|
: 'Turn All On',
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
// Add relay input
|
|
|
|
// Add relay input
|
|
|
|
Row(
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
children: [
|
|
|
|
@ -79,9 +140,7 @@ class _RelayManagementScreenState extends State<RelayManagementScreen> {
|
|
|
|
controller: _urlController,
|
|
|
|
controller: _urlController,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
decoration: InputDecoration(
|
|
|
|
labelText: 'Relay URL',
|
|
|
|
labelText: 'Relay URL',
|
|
|
|
hintText: widget.controller.relays.isNotEmpty
|
|
|
|
hintText: 'wss://relay.example.com',
|
|
|
|
? widget.controller.relays.first.url
|
|
|
|
|
|
|
|
: 'wss://nostrum.satoshinakamoto.win',
|
|
|
|
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
keyboardType: TextInputType.url,
|
|
|
|
keyboardType: TextInputType.url,
|
|
|
|
@ -108,56 +167,6 @@ class _RelayManagementScreenState extends State<RelayManagementScreen> {
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Action buttons
|
|
|
|
|
|
|
|
Wrap(
|
|
|
|
|
|
|
|
spacing: 8,
|
|
|
|
|
|
|
|
runSpacing: 8,
|
|
|
|
|
|
|
|
children: [
|
|
|
|
|
|
|
|
ElevatedButton.icon(
|
|
|
|
|
|
|
|
onPressed: widget.controller.isCheckingHealth
|
|
|
|
|
|
|
|
? null
|
|
|
|
|
|
|
|
: widget.controller.checkRelayHealth,
|
|
|
|
|
|
|
|
icon: widget.controller.isCheckingHealth
|
|
|
|
|
|
|
|
? const SizedBox(
|
|
|
|
|
|
|
|
width: 16,
|
|
|
|
|
|
|
|
height: 16,
|
|
|
|
|
|
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
: const Icon(Icons.health_and_safety),
|
|
|
|
|
|
|
|
label: const Text('Check Health'),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
if (widget.controller.syncEngine != null)
|
|
|
|
|
|
|
|
ElevatedButton.icon(
|
|
|
|
|
|
|
|
onPressed: widget.controller.isSyncing
|
|
|
|
|
|
|
|
? null
|
|
|
|
|
|
|
|
: () async {
|
|
|
|
|
|
|
|
final success = await widget.controller.triggerManualSync();
|
|
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
|
|
|
SnackBar(
|
|
|
|
|
|
|
|
content: Text(
|
|
|
|
|
|
|
|
success
|
|
|
|
|
|
|
|
? 'Sync triggered successfully'
|
|
|
|
|
|
|
|
: 'Sync failed: ${widget.controller.error ?? "Unknown error"}',
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
duration: const Duration(seconds: 2),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
icon: widget.controller.isSyncing
|
|
|
|
|
|
|
|
? const SizedBox(
|
|
|
|
|
|
|
|
width: 16,
|
|
|
|
|
|
|
|
height: 16,
|
|
|
|
|
|
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
: const Icon(Icons.sync),
|
|
|
|
|
|
|
|
label: const Text('Manual Sync'),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
],
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
@ -199,8 +208,9 @@ class _RelayManagementScreenState extends State<RelayManagementScreen> {
|
|
|
|
final relay = widget.controller.relays[index];
|
|
|
|
final relay = widget.controller.relays[index];
|
|
|
|
return _RelayListItem(
|
|
|
|
return _RelayListItem(
|
|
|
|
relay: relay,
|
|
|
|
relay: relay,
|
|
|
|
onConnect: () => widget.controller.connectRelay(relay.url),
|
|
|
|
isTesting: _testingRelays[relay.url] ?? false,
|
|
|
|
onDisconnect: () => widget.controller.disconnectRelay(relay.url),
|
|
|
|
onTest: () => _handleTestRelay(relay.url),
|
|
|
|
|
|
|
|
onToggle: () => widget.controller.toggleRelay(relay.url),
|
|
|
|
onRemove: () {
|
|
|
|
onRemove: () {
|
|
|
|
widget.controller.removeRelay(relay.url);
|
|
|
|
widget.controller.removeRelay(relay.url);
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
@ -227,19 +237,23 @@ class _RelayListItem extends StatelessWidget {
|
|
|
|
/// The relay to display.
|
|
|
|
/// The relay to display.
|
|
|
|
final NostrRelay relay;
|
|
|
|
final NostrRelay relay;
|
|
|
|
|
|
|
|
|
|
|
|
/// Callback when connect is pressed.
|
|
|
|
/// Whether the relay is currently being tested.
|
|
|
|
final VoidCallback onConnect;
|
|
|
|
final bool isTesting;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Callback when test is pressed.
|
|
|
|
|
|
|
|
final VoidCallback onTest;
|
|
|
|
|
|
|
|
|
|
|
|
/// Callback when disconnect is pressed.
|
|
|
|
/// Callback when toggle is pressed.
|
|
|
|
final VoidCallback onDisconnect;
|
|
|
|
final VoidCallback onToggle;
|
|
|
|
|
|
|
|
|
|
|
|
/// Callback when remove is pressed.
|
|
|
|
/// Callback when remove is pressed.
|
|
|
|
final VoidCallback onRemove;
|
|
|
|
final VoidCallback onRemove;
|
|
|
|
|
|
|
|
|
|
|
|
const _RelayListItem({
|
|
|
|
const _RelayListItem({
|
|
|
|
required this.relay,
|
|
|
|
required this.relay,
|
|
|
|
required this.onConnect,
|
|
|
|
required this.isTesting,
|
|
|
|
required this.onDisconnect,
|
|
|
|
required this.onTest,
|
|
|
|
|
|
|
|
required this.onToggle,
|
|
|
|
required this.onRemove,
|
|
|
|
required this.onRemove,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
@ -247,40 +261,103 @@ class _RelayListItem extends StatelessWidget {
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Card(
|
|
|
|
return Card(
|
|
|
|
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
|
|
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
|
|
child: ListTile(
|
|
|
|
child: Padding(
|
|
|
|
leading: CircleAvatar(
|
|
|
|
padding: const EdgeInsets.all(12),
|
|
|
|
backgroundColor: relay.isConnected ? Colors.green : Colors.grey,
|
|
|
|
child: Column(
|
|
|
|
child: Icon(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
relay.isConnected ? Icons.check : Icons.close,
|
|
|
|
|
|
|
|
color: Colors.white,
|
|
|
|
|
|
|
|
size: 20,
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
title: Text(
|
|
|
|
|
|
|
|
relay.url,
|
|
|
|
|
|
|
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
subtitle: Text(
|
|
|
|
|
|
|
|
relay.isConnected ? 'Connected' : 'Disconnected',
|
|
|
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
|
|
|
color: relay.isConnected ? Colors.green : Colors.grey,
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
trailing: Row(
|
|
|
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
|
|
|
children: [
|
|
|
|
children: [
|
|
|
|
IconButton(
|
|
|
|
// Relay URL and status
|
|
|
|
icon: Icon(
|
|
|
|
Row(
|
|
|
|
relay.isConnected ? Icons.link_off : Icons.link,
|
|
|
|
children: [
|
|
|
|
color: relay.isConnected ? Colors.orange : Colors.green,
|
|
|
|
// Status indicator
|
|
|
|
|
|
|
|
Container(
|
|
|
|
|
|
|
|
width: 12,
|
|
|
|
|
|
|
|
height: 12,
|
|
|
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
|
|
|
|
color: relay.isConnected
|
|
|
|
|
|
|
|
? Colors.green
|
|
|
|
|
|
|
|
: relay.isEnabled
|
|
|
|
|
|
|
|
? Colors.orange
|
|
|
|
|
|
|
|
: Colors.grey,
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
const SizedBox(width: 8),
|
|
|
|
|
|
|
|
Expanded(
|
|
|
|
|
|
|
|
child: Text(
|
|
|
|
|
|
|
|
relay.url,
|
|
|
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
|
|
|
fontSize: 14,
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
|
|
|
|
// Status text
|
|
|
|
|
|
|
|
Text(
|
|
|
|
|
|
|
|
relay.isConnected
|
|
|
|
|
|
|
|
? 'Connected'
|
|
|
|
|
|
|
|
: relay.isEnabled
|
|
|
|
|
|
|
|
? 'Enabled (not connected)'
|
|
|
|
|
|
|
|
: 'Disabled',
|
|
|
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
|
|
|
fontSize: 12,
|
|
|
|
|
|
|
|
color: relay.isConnected
|
|
|
|
|
|
|
|
? Colors.green
|
|
|
|
|
|
|
|
: relay.isEnabled
|
|
|
|
|
|
|
|
? Colors.orange
|
|
|
|
|
|
|
|
: Colors.grey,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
tooltip: relay.isConnected ? 'Disconnect' : 'Connect',
|
|
|
|
|
|
|
|
onPressed: relay.isConnected ? onDisconnect : onConnect,
|
|
|
|
|
|
|
|
),
|
|
|
|
),
|
|
|
|
IconButton(
|
|
|
|
const SizedBox(height: 12),
|
|
|
|
icon: const Icon(Icons.delete, color: Colors.red),
|
|
|
|
// Action buttons
|
|
|
|
tooltip: 'Remove',
|
|
|
|
Row(
|
|
|
|
onPressed: onRemove,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
|
|
|
|
children: [
|
|
|
|
|
|
|
|
// Test button
|
|
|
|
|
|
|
|
OutlinedButton.icon(
|
|
|
|
|
|
|
|
onPressed: isTesting ? null : onTest,
|
|
|
|
|
|
|
|
icon: isTesting
|
|
|
|
|
|
|
|
? const SizedBox(
|
|
|
|
|
|
|
|
width: 14,
|
|
|
|
|
|
|
|
height: 14,
|
|
|
|
|
|
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
: const Icon(Icons.network_check, size: 16),
|
|
|
|
|
|
|
|
label: const Text('Test'),
|
|
|
|
|
|
|
|
style: OutlinedButton.styleFrom(
|
|
|
|
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
const SizedBox(width: 8),
|
|
|
|
|
|
|
|
// Toggle switch
|
|
|
|
|
|
|
|
Row(
|
|
|
|
|
|
|
|
children: [
|
|
|
|
|
|
|
|
Text(
|
|
|
|
|
|
|
|
relay.isEnabled ? 'On' : 'Off',
|
|
|
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
|
|
|
fontSize: 12,
|
|
|
|
|
|
|
|
color: Colors.grey[600],
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
const SizedBox(width: 4),
|
|
|
|
|
|
|
|
Switch(
|
|
|
|
|
|
|
|
value: relay.isEnabled,
|
|
|
|
|
|
|
|
onChanged: (_) => onToggle(),
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
const SizedBox(width: 8),
|
|
|
|
|
|
|
|
// Remove button
|
|
|
|
|
|
|
|
IconButton(
|
|
|
|
|
|
|
|
icon: const Icon(Icons.delete, size: 20),
|
|
|
|
|
|
|
|
color: Colors.red,
|
|
|
|
|
|
|
|
tooltip: 'Remove',
|
|
|
|
|
|
|
|
onPressed: onRemove,
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
@ -288,4 +365,3 @@ class _RelayListItem extends StatelessWidget {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|