feat: Add Packet Log Screen for BLE packet logging and exporting

- Implemented PacketLogScreen to display and filter BLE packet logs.
- Added functionality to export logs as CSV and text files.
- Introduced clipboard copy feature for hex data.
- Implemented clear logs functionality with confirmation dialog.
- Enhanced MeshCoreBleService to log TX and RX packets with descriptions.
- Added BufferReader methods for reading unsigned and signed 16-bit integers (big-endian).
- Updated CayenneLppParser to read values as big-endian.
- Created MessageStorageService for persisting messages to local storage.
- Enhanced map markers to display telemetry data including voltage, humidity, and pressure.
This commit is contained in:
Janez T
2025-10-14 15:27:18 +02:00
parent ccec842672
commit 59de627289
20 changed files with 4960 additions and 153 deletions

View File

@@ -249,6 +249,16 @@ class _ContactTile extends StatelessWidget {
tooltip: 'Request telemetry',
),
onTap: () => _showContactDetails(context, contact),
onLongPress: () {
final connectionProvider = context.read<ConnectionProvider>();
connectionProvider.requestTelemetry(contact.publicKey, zeroHop: true);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Pinging ${contact.displayName} (direct connection)...'),
duration: const Duration(seconds: 2),
),
);
},
),
);
}
@@ -344,11 +354,29 @@ class _ContactTile extends StatelessWidget {
),
),
const SizedBox(height: 8),
if (contact.telemetry!.batteryPercentage != null)
if (contact.telemetry!.batteryMilliVolts != null)
_DetailRow(
'Voltage',
'${(contact.telemetry!.batteryMilliVolts! / 1000).toStringAsFixed(3)}V'
'${contact.telemetry!.batteryPercentage != null ? ' (${contact.telemetry!.batteryPercentage!.toStringAsFixed(1)}%)' : ''}',
)
else if (contact.telemetry!.batteryPercentage != null)
_DetailRow('Battery', '${contact.telemetry!.batteryPercentage!.toStringAsFixed(1)}%'),
if (contact.telemetry!.temperature != null)
_DetailRow('Temperature', '${contact.telemetry!.temperature!.toStringAsFixed(1)}°C'),
_DetailRow('Updated', contact.telemetry!.isRecent ? 'Recently' : 'Stale'),
if (contact.telemetry!.humidity != null)
_DetailRow('Humidity', '${contact.telemetry!.humidity!.toStringAsFixed(1)}%'),
if (contact.telemetry!.pressure != null)
_DetailRow('Pressure', '${contact.telemetry!.pressure!.toStringAsFixed(1)} hPa'),
if (contact.telemetry!.gpsLocation != null)
_DetailRow(
'GPS (Telemetry)',
'${contact.telemetry!.gpsLocation!.latitude.toStringAsFixed(6)}, ${contact.telemetry!.gpsLocation!.longitude.toStringAsFixed(6)}',
),
_DetailRow(
'Updated',
'${_formatTimestamp(contact.telemetry!.timestamp)} (${_formatTimeAgo(contact.telemetry!.timestamp)})',
),
],
],
),
@@ -418,4 +446,35 @@ class _ContactTile extends StatelessWidget {
if (percentage > 20) return Colors.orange;
return Colors.red;
}
String _formatTimestamp(DateTime timestamp) {
final now = DateTime.now();
final today = DateTime(now.year, now.month, now.day);
final timestampDate = DateTime(timestamp.year, timestamp.month, timestamp.day);
if (timestampDate == today) {
// Today - show time only
return '${timestamp.hour.toString().padLeft(2, '0')}:${timestamp.minute.toString().padLeft(2, '0')}:${timestamp.second.toString().padLeft(2, '0')}';
} else {
// Another day - show date and time
return '${timestamp.year}-${timestamp.month.toString().padLeft(2, '0')}-${timestamp.day.toString().padLeft(2, '0')} ${timestamp.hour.toString().padLeft(2, '0')}:${timestamp.minute.toString().padLeft(2, '0')}';
}
}
String _formatTimeAgo(DateTime timestamp) {
final now = DateTime.now();
final diff = now.difference(timestamp);
if (diff.inSeconds < 60) {
return '${diff.inSeconds}s ago';
} else if (diff.inMinutes < 60) {
return '${diff.inMinutes}m ago';
} else if (diff.inHours < 24) {
return '${diff.inHours}h ago';
} else if (diff.inDays == 1) {
return 'yesterday';
} else {
return '${diff.inDays}d ago';
}
}
}

View File

@@ -9,6 +9,8 @@ import 'map_tab.dart';
import 'map_management_screen.dart';
import 'settings_screen.dart';
import 'device_config_screen.dart';
import 'packet_log_screen.dart';
import 'message_history_screen.dart';
class HomeScreen extends StatefulWidget {
final Function(AppThemeMode) onThemeChanged;
@@ -231,6 +233,25 @@ class _HomeScreenState extends State<HomeScreen> with SingleTickerProviderStateM
PopupMenuButton(
icon: const Icon(Icons.more_vert),
itemBuilder: (context) => [
PopupMenuItem(
child: const Row(
children: [
Icon(Icons.history),
SizedBox(width: 8),
Text('Message History'),
],
),
onTap: () {
Future.delayed(Duration.zero, () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const MessageHistoryScreen(),
),
);
});
},
),
PopupMenuItem(
child: const Row(
children: [
@@ -440,21 +461,34 @@ class _HomeScreenState extends State<HomeScreen> with SingleTickerProviderStateM
),
const SizedBox(width: 8),
// Disconnect button (prominent, icon only)
FilledButton(
onPressed: () async {
await provider.disconnect();
if (context.mounted) {
context.read<AppProvider>().clearAllData();
}
// Long press to open packet log viewer
GestureDetector(
onLongPress: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PacketLogScreen(
bleService: provider.bleService,
),
),
);
},
style: FilledButton.styleFrom(
backgroundColor: Colors.red.shade700,
foregroundColor: Colors.white,
padding: const EdgeInsets.all(10),
minimumSize: const Size(40, 40),
shape: const CircleBorder(),
child: FilledButton(
onPressed: () async {
await provider.disconnect();
if (context.mounted) {
context.read<AppProvider>().clearAllData();
}
},
style: FilledButton.styleFrom(
backgroundColor: Colors.red.shade700,
foregroundColor: Colors.white,
padding: const EdgeInsets.all(10),
minimumSize: const Size(40, 40),
shape: const CircleBorder(),
),
child: const Icon(Icons.power_settings_new, size: 20),
),
child: const Icon(Icons.power_settings_new, size: 20),
),
],
),

View File

@@ -1544,8 +1544,14 @@ class _DetailedCompassDialogState extends State<_DetailedCompassDialog> {
);
}
// Show battery if available
if (_selectedContact!.telemetry?.batteryPercentage != null) {
// Show voltage/battery if available
if (_selectedContact!.telemetry?.batteryMilliVolts != null) {
final volts = (_selectedContact!.telemetry!.batteryMilliVolts! / 1000).toStringAsFixed(3);
final percent = _selectedContact!.telemetry!.batteryPercentage != null
? ' (${_selectedContact!.telemetry!.batteryPercentage!.round()}%)'
: '';
additionalInfo = 'Voltage: ${volts}V$percent';
} else if (_selectedContact!.telemetry?.batteryPercentage != null) {
additionalInfo = 'Battery: ${_selectedContact!.telemetry!.batteryPercentage!.round()}%';
}
} else if (_selectedSarMarker != null) {

View File

@@ -0,0 +1,490 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/messages_provider.dart';
import '../models/message.dart';
/// Screen to view all stored message history
class MessageHistoryScreen extends StatefulWidget {
const MessageHistoryScreen({super.key});
@override
State<MessageHistoryScreen> createState() => _MessageHistoryScreenState();
}
class _MessageHistoryScreenState extends State<MessageHistoryScreen> {
String _searchQuery = '';
MessageFilter _filter = MessageFilter.all;
final TextEditingController _searchController = TextEditingController();
@override
void dispose() {
_searchController.dispose();
super.dispose();
}
void _showStorageInfo(BuildContext context) async {
final messagesProvider = context.read<MessagesProvider>();
final stats = await messagesProvider.getStorageStats();
if (!mounted) return;
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Storage Information'),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_InfoRow(
label: 'Total Messages',
value: '${stats['messageCount']}',
),
const SizedBox(height: 8),
_InfoRow(
label: 'Storage Size',
value: '${stats['storageSizeKB']} KB',
),
const SizedBox(height: 8),
_InfoRow(
label: 'Storage Size (bytes)',
value: '${stats['storageSizeBytes']} bytes',
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Close'),
),
],
),
);
}
void _showClearConfirmation(BuildContext context) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Clear All Messages?'),
content: const Text(
'This will permanently delete all stored messages. This action cannot be undone.',
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
context.read<MessagesProvider>().clearAll();
Navigator.pop(context);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('All messages cleared'),
backgroundColor: Colors.green,
),
);
},
style: TextButton.styleFrom(
foregroundColor: Colors.red,
),
child: const Text('Clear All'),
),
],
),
);
}
List<Message> _filterMessages(List<Message> messages) {
// Apply search filter
var filtered = messages.where((msg) {
if (_searchQuery.isEmpty) return true;
final query = _searchQuery.toLowerCase();
return msg.text.toLowerCase().contains(query) ||
msg.displaySender.toLowerCase().contains(query);
}).toList();
// Apply type filter
switch (_filter) {
case MessageFilter.all:
break;
case MessageFilter.contact:
filtered = filtered.where((m) => m.isContactMessage).toList();
break;
case MessageFilter.channel:
filtered = filtered.where((m) => m.isChannelMessage).toList();
break;
case MessageFilter.sarMarker:
filtered = filtered.where((m) => m.isSarMarker).toList();
break;
}
// Sort by most recent first
filtered.sort((a, b) => b.sentAt.compareTo(a.sentAt));
return filtered;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Message History'),
actions: [
IconButton(
icon: const Icon(Icons.info_outline),
tooltip: 'Storage Info',
onPressed: () => _showStorageInfo(context),
),
IconButton(
icon: const Icon(Icons.delete_forever),
tooltip: 'Clear All',
onPressed: () => _showClearConfirmation(context),
),
],
),
body: Consumer<MessagesProvider>(
builder: (context, messagesProvider, child) {
final messages = _filterMessages(messagesProvider.messages);
return Column(
children: [
// Search and filter bar
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
border: Border(
bottom: BorderSide(
color: Theme.of(context).dividerColor,
width: 1,
),
),
),
child: Column(
children: [
// Search field
TextField(
controller: _searchController,
decoration: InputDecoration(
hintText: 'Search messages...',
prefixIcon: const Icon(Icons.search),
suffixIcon: _searchQuery.isNotEmpty
? IconButton(
icon: const Icon(Icons.clear),
onPressed: () {
setState(() {
_searchController.clear();
_searchQuery = '';
});
},
)
: null,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
),
isDense: true,
),
onChanged: (value) {
setState(() {
_searchQuery = value;
});
},
),
const SizedBox(height: 8),
// Filter chips
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
_FilterChip(
label: 'All (${messagesProvider.messages.length})',
isSelected: _filter == MessageFilter.all,
onTap: () => setState(() => _filter = MessageFilter.all),
),
const SizedBox(width: 8),
_FilterChip(
label: 'Contacts (${messagesProvider.contactMessages.length})',
isSelected: _filter == MessageFilter.contact,
onTap: () => setState(() => _filter = MessageFilter.contact),
),
const SizedBox(width: 8),
_FilterChip(
label: 'Channels (${messagesProvider.channelMessages.length})',
isSelected: _filter == MessageFilter.channel,
onTap: () => setState(() => _filter = MessageFilter.channel),
),
const SizedBox(width: 8),
_FilterChip(
label: 'SAR (${messagesProvider.sarMarkerMessages.length})',
isSelected: _filter == MessageFilter.sarMarker,
onTap: () => setState(() => _filter = MessageFilter.sarMarker),
),
],
),
),
],
),
),
// Message list
Expanded(
child: messages.isEmpty
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
_searchQuery.isNotEmpty
? Icons.search_off
: Icons.inbox_outlined,
size: 64,
color: Theme.of(context).disabledColor,
),
const SizedBox(height: 16),
Text(
_searchQuery.isNotEmpty
? 'No messages found'
: 'No messages stored',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 8),
Text(
_searchQuery.isNotEmpty
? 'Try a different search term'
: 'Messages will appear here once received',
style: Theme.of(context).textTheme.bodyMedium,
textAlign: TextAlign.center,
),
],
),
)
: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: messages.length,
itemBuilder: (context, index) {
final message = messages[index];
return _MessageHistoryCard(message: message);
},
),
),
],
);
},
),
);
}
}
enum MessageFilter {
all,
contact,
channel,
sarMarker,
}
class _FilterChip extends StatelessWidget {
final String label;
final bool isSelected;
final VoidCallback onTap;
const _FilterChip({
required this.label,
required this.isSelected,
required this.onTap,
});
@override
Widget build(BuildContext context) {
return FilterChip(
label: Text(label),
selected: isSelected,
onSelected: (_) => onTap(),
selectedColor: Theme.of(context).colorScheme.primaryContainer,
checkmarkColor: Theme.of(context).colorScheme.onPrimaryContainer,
);
}
}
class _MessageHistoryCard extends StatelessWidget {
final Message message;
const _MessageHistoryCard({required this.message});
String _formatDateTime(DateTime dateTime) {
final now = DateTime.now();
final today = DateTime(now.year, now.month, now.day);
final messageDate = DateTime(dateTime.year, dateTime.month, dateTime.day);
final hour = dateTime.hour.toString().padLeft(2, '0');
final minute = dateTime.minute.toString().padLeft(2, '0');
final timeStr = '$hour:$minute';
if (messageDate == today) {
return 'Today $timeStr';
} else if (messageDate == today.subtract(const Duration(days: 1))) {
return 'Yesterday $timeStr';
} else if (now.difference(dateTime).inDays < 7) {
final weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
final weekday = weekdays[dateTime.weekday - 1];
return '$weekday $timeStr';
} else {
final months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
final month = months[dateTime.month - 1];
return '$month ${dateTime.day}, ${dateTime.year} $timeStr';
}
}
@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.only(bottom: 8),
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Header row
Row(
children: [
// Type icon
Icon(
message.isChannelMessage ? Icons.tag : Icons.person,
size: 16,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(width: 4),
// Sender
Expanded(
child: Text(
message.displaySender,
style: Theme.of(context).textTheme.labelLarge?.copyWith(
fontWeight: FontWeight.bold,
),
overflow: TextOverflow.ellipsis,
),
),
// SAR badge
if (message.isSarMarker) ...[
const SizedBox(width: 8),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 6,
vertical: 2,
),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primaryContainer,
borderRadius: BorderRadius.circular(4),
),
child: Text(
'SAR',
style: Theme.of(context).textTheme.labelSmall?.copyWith(
color: Theme.of(context).colorScheme.onPrimaryContainer,
fontWeight: FontWeight.bold,
),
),
),
],
],
),
const SizedBox(height: 8),
// Message content
if (message.isSarMarker && message.sarMarkerType != null) ...[
Row(
children: [
Text(
message.sarMarkerType!.emoji,
style: const TextStyle(fontSize: 24),
),
const SizedBox(width: 8),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
message.sarMarkerType!.displayName,
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
fontWeight: FontWeight.bold,
),
),
if (message.sarGpsCoordinates != null)
Text(
'${message.sarGpsCoordinates!.latitude.toStringAsFixed(5)}, ${message.sarGpsCoordinates!.longitude.toStringAsFixed(5)}',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
fontFamily: 'monospace',
),
),
],
),
),
],
),
] else
Text(
message.text,
style: Theme.of(context).textTheme.bodyMedium,
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
const SizedBox(height: 8),
// Footer row
Row(
children: [
Icon(
Icons.access_time,
size: 12,
color: Theme.of(context).textTheme.bodySmall?.color,
),
const SizedBox(width: 4),
Text(
_formatDateTime(message.sentAt),
style: Theme.of(context).textTheme.bodySmall,
),
const Spacer(),
Text(
'Received: ${_formatDateTime(message.receivedAt)}',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
fontStyle: FontStyle.italic,
),
),
],
),
],
),
),
);
}
}
class _InfoRow extends StatelessWidget {
final String label;
final String value;
const _InfoRow({
required this.label,
required this.value,
});
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
label,
style: Theme.of(context).textTheme.bodyMedium,
),
Text(
value,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
fontWeight: FontWeight.bold,
),
),
],
);
}
}

View File

@@ -6,6 +6,7 @@ import '../providers/messages_provider.dart';
import '../providers/contacts_provider.dart';
import '../providers/map_provider.dart';
import '../providers/connection_provider.dart';
import '../providers/app_provider.dart';
import '../models/message.dart';
import '../models/sar_marker.dart';
@@ -24,6 +25,10 @@ class _MessagesTabState extends State<MessagesTab> {
int _characterCount = 0;
static const int _maxCharacters = 160;
// Message recipient selection
String? _selectedRecipientId; // null = broadcast to public channel (channel 0)
MessageRecipientType _recipientType = MessageRecipientType.room;
@override
void initState() {
super.initState();
@@ -62,30 +67,39 @@ class _MessagesTabState extends State<MessagesTab> {
}
try {
// Default to sending to room/channel (first available room)
final rooms = contactsProvider.rooms;
if (_recipientType == MessageRecipientType.contact && _selectedRecipientId != null) {
// Send to specific contact
final contact = contactsProvider.contacts.firstWhere(
(c) => c.publicKeyHex == _selectedRecipientId,
);
await connectionProvider.sendTextMessage(
contactPublicKey: contact.publicKey,
text: text,
);
} else {
// Send to room/channel
// Default to channel 0 (public channel) if no specific room selected
int channelIdx = 0;
if (rooms.isNotEmpty) {
// Send to first available room
final defaultRoom = rooms.first;
final channelIdx = defaultRoom.outPath.isNotEmpty
? defaultRoom.outPath[0]
: 0;
if (_selectedRecipientId != null) {
// Try to find selected room
final rooms = contactsProvider.rooms;
try {
final targetRoom = rooms.firstWhere(
(r) => r.publicKeyHex == _selectedRecipientId,
);
if (targetRoom.outPath.isNotEmpty) {
channelIdx = targetRoom.outPath[0];
}
} catch (e) {
// Room not found, use default channel 0
}
}
await connectionProvider.sendChannelMessage(
channelIdx: channelIdx,
text: text,
);
} else {
// No rooms available, show error
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('No channels available'),
backgroundColor: Colors.orange,
),
);
return;
}
_textController.clear();
@@ -110,6 +124,82 @@ class _MessagesTabState extends State<MessagesTab> {
}
}
void _showRecipientSelector() {
showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
builder: (context) => _RecipientSelectorSheet(
selectedRecipientId: _selectedRecipientId,
selectedRecipientType: _recipientType,
onSelect: (recipientId, recipientType) {
setState(() {
_selectedRecipientId = recipientId;
_recipientType = recipientType;
});
// Fetch messages from the newly selected channel/room
_syncMessagesForRecipient();
},
),
);
}
/// Sync messages when recipient changes
Future<void> _syncMessagesForRecipient() async {
final appProvider = context.read<AppProvider>();
if (!appProvider.connectionProvider.deviceInfo.isConnected) {
return;
}
try {
debugPrint('🔄 [MessagesTab] Syncing messages after channel/room change...');
final messageCount = await appProvider.syncMessages();
if (!mounted) return;
if (messageCount > 0) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Synced $messageCount message${messageCount == 1 ? '' : 's'} from ${_getRecipientDisplayName()}'),
backgroundColor: Colors.green,
duration: const Duration(seconds: 2),
),
);
}
} catch (e) {
debugPrint('❌ [MessagesTab] Error syncing messages: $e');
}
}
String _getRecipientDisplayName() {
final contactsProvider = context.read<ContactsProvider>();
if (_selectedRecipientId == null) {
// Default to public channel
return 'Public Channel';
}
if (_recipientType == MessageRecipientType.contact) {
try {
final contact = contactsProvider.contacts.firstWhere(
(c) => c.publicKeyHex == _selectedRecipientId,
);
return contact.displayName;
} catch (e) {
return 'Public Channel';
}
} else {
try {
final room = contactsProvider.rooms.firstWhere(
(r) => r.publicKeyHex == _selectedRecipientId,
);
return room.displayName;
} catch (e) {
return 'Public Channel';
}
}
}
void _showSarDialog() {
showModalBottomSheet(
context: context,
@@ -151,30 +241,40 @@ class _MessagesTabState extends State<MessagesTab> {
? '$sarMessage $notes'
: sarMessage;
// Default to sending to room/channel (first available room)
final rooms = contactsProvider.rooms;
// Send to selected recipient (contact or room)
if (_recipientType == MessageRecipientType.contact && _selectedRecipientId != null) {
// Send to specific contact
final contact = contactsProvider.contacts.firstWhere(
(c) => c.publicKeyHex == _selectedRecipientId,
);
await connectionProvider.sendTextMessage(
contactPublicKey: contact.publicKey,
text: fullMessage,
);
} else {
// Send to room/channel
// Default to channel 0 (public channel) if no specific room selected
int channelIdx = 0;
if (rooms.isNotEmpty) {
// Send to first available room
final defaultRoom = rooms.first;
final channelIdx = defaultRoom.outPath.isNotEmpty
? defaultRoom.outPath[0]
: 0;
if (_selectedRecipientId != null) {
// Try to find selected room
final rooms = contactsProvider.rooms;
try {
final targetRoom = rooms.firstWhere(
(r) => r.publicKeyHex == _selectedRecipientId,
);
if (targetRoom.outPath.isNotEmpty) {
channelIdx = targetRoom.outPath[0];
}
} catch (e) {
// Room not found, use default channel 0
}
}
await connectionProvider.sendChannelMessage(
channelIdx: channelIdx,
text: fullMessage,
);
} else {
// No rooms available, show error
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('No channels available'),
backgroundColor: Colors.orange,
),
);
return;
}
if (!mounted) return;
@@ -197,11 +297,80 @@ class _MessagesTabState extends State<MessagesTab> {
}
Future<void> _handleRefresh() async {
final appProvider = context.read<AppProvider>();
final messageCount = await appProvider.syncMessages();
if (!mounted) return;
if (messageCount > 0) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Synced $messageCount message${messageCount == 1 ? '' : 's'}'),
backgroundColor: Colors.green,
duration: const Duration(seconds: 2),
),
);
}
}
List<Message> _getFilteredMessages(MessagesProvider messagesProvider) {
// If viewing a specific contact, show all their messages indefinitely
if (_recipientType == MessageRecipientType.contact && _selectedRecipientId != null) {
final contactMessages = messagesProvider.contactMessages
.where((m) => m.senderPublicKeyPrefix != null)
.toList();
// Filter by selected contact
return contactMessages
.where((m) {
final senderHex = m.senderPublicKeyPrefix!
.map((b) => b.toRadixString(16).padLeft(2, '0'))
.join();
return _selectedRecipientId!.startsWith(senderHex);
})
.toList()
..sort((a, b) => b.sentAt.compareTo(a.sentAt));
}
// For channels/rooms, limit to recent 100 messages
if (_recipientType == MessageRecipientType.room) {
if (_selectedRecipientId != null) {
// Filter by specific channel
final contactsProvider = context.read<ContactsProvider>();
try {
final room = contactsProvider.rooms.firstWhere(
(r) => r.publicKeyHex == _selectedRecipientId,
);
final channelIdx = room.outPath.isNotEmpty ? room.outPath[0] : 0;
return messagesProvider
.getMessagesForChannel(channelIdx)
.take(100)
.toList();
} catch (e) {
// Room not found, show all channel messages
return messagesProvider.channelMessages
.take(100)
.toList()
..sort((a, b) => b.sentAt.compareTo(a.sentAt));
}
}
// Default: show recent channel messages (public channel)
return messagesProvider.channelMessages
.take(100)
.toList()
..sort((a, b) => b.sentAt.compareTo(a.sentAt));
}
// Fallback: show all recent messages
return messagesProvider.getRecentMessages(count: 100);
}
@override
Widget build(BuildContext context) {
return Consumer<MessagesProvider>(
builder: (context, messagesProvider, child) {
final messages = messagesProvider.getRecentMessages(count: 100);
final messages = _getFilteredMessages(messagesProvider);
return Column(
children: [
@@ -231,28 +400,31 @@ class _MessagesTabState extends State<MessagesTab> {
],
),
)
: ListView.builder(
reverse: true,
padding: const EdgeInsets.all(8),
itemCount: messages.length,
itemBuilder: (context, index) {
final message = messages[index];
return _MessageBubble(
message: message,
onTap: message.isSarMarker &&
message.sarGpsCoordinates != null
? () {
final mapProvider =
context.read<MapProvider>();
mapProvider.navigateToLocation(
location: message.sarGpsCoordinates!,
zoom: 15.0,
);
widget.onNavigateToMap();
}
: null,
);
},
: RefreshIndicator(
onRefresh: _handleRefresh,
child: ListView.builder(
reverse: true,
padding: const EdgeInsets.all(8),
itemCount: messages.length,
itemBuilder: (context, index) {
final message = messages[index];
return _MessageBubble(
message: message,
onTap: message.isSarMarker &&
message.sarGpsCoordinates != null
? () {
final mapProvider =
context.read<MapProvider>();
mapProvider.navigateToLocation(
location: message.sarGpsCoordinates!,
zoom: 15.0,
);
widget.onNavigateToMap();
}
: null,
);
},
),
),
),
@@ -268,66 +440,115 @@ class _MessagesTabState extends State<MessagesTab> {
),
),
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
child: Column(
children: [
// SAR quick action button
IconButton(
icon: const Icon(Icons.add_location_alt),
tooltip: 'Send SAR marker',
onPressed: _showSarDialog,
style: IconButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.primaryContainer,
foregroundColor: Theme.of(context).colorScheme.onPrimaryContainer,
),
),
const SizedBox(width: 8),
// Text field with embedded send button
Expanded(
child: TextField(
controller: _textController,
focusNode: _focusNode,
maxLength: _maxCharacters,
maxLines: null,
maxLengthEnforcement: MaxLengthEnforcement.enforced,
style: const TextStyle(fontSize: 14),
decoration: InputDecoration(
hintText: 'Message to channel...',
hintStyle: const TextStyle(fontSize: 14),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(24),
),
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 10,
),
isDense: true,
counterText: _characterCount >= 150
? '$_characterCount/$_maxCharacters'
: '',
counterStyle: TextStyle(
fontSize: 10,
color: _characterCount > _maxCharacters * 0.9
? Colors.orange
: Theme.of(context).textTheme.bodySmall?.color,
),
suffixIcon: IconButton(
icon: Icon(
Icons.send_rounded,
size: 22,
color: _textController.text.trim().isEmpty
? Theme.of(context).disabledColor
: Theme.of(context).colorScheme.primary,
// Recipient selector bar
Consumer<ContactsProvider>(
builder: (context, contactsProvider, child) {
return Container(
margin: const EdgeInsets.only(bottom: 8),
child: InkWell(
onTap: _showRecipientSelector,
borderRadius: BorderRadius.circular(8),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceVariant,
borderRadius: BorderRadius.circular(8),
),
child: Row(
children: [
Icon(
_recipientType == MessageRecipientType.contact
? Icons.person
: Icons.tag,
size: 18,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(width: 8),
Expanded(
child: Text(
'To: ${_getRecipientDisplayName()}',
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
fontWeight: FontWeight.w500,
),
overflow: TextOverflow.ellipsis,
),
),
Icon(
Icons.arrow_drop_down,
color: Theme.of(context).colorScheme.primary,
),
],
),
),
onPressed: _textController.text.trim().isEmpty
? null
: _sendMessage,
tooltip: 'Send',
),
);
},
),
// Message input row
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
// SAR quick action button
IconButton(
icon: const Icon(Icons.add_location_alt),
tooltip: 'Send SAR marker',
onPressed: _showSarDialog,
style: IconButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.primaryContainer,
foregroundColor: Theme.of(context).colorScheme.onPrimaryContainer,
),
),
textInputAction: TextInputAction.send,
onSubmitted: (_) => _sendMessage(),
),
const SizedBox(width: 8),
// Text field with embedded send button
Expanded(
child: TextField(
controller: _textController,
focusNode: _focusNode,
maxLength: _maxCharacters,
maxLines: null,
maxLengthEnforcement: MaxLengthEnforcement.enforced,
style: const TextStyle(fontSize: 14),
decoration: InputDecoration(
hintText: 'Type a message...',
hintStyle: const TextStyle(fontSize: 14),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(24),
),
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 10,
),
isDense: true,
counterText: _characterCount >= 150
? '$_characterCount/$_maxCharacters'
: '',
counterStyle: TextStyle(
fontSize: 10,
color: _characterCount > _maxCharacters * 0.9
? Colors.orange
: Theme.of(context).textTheme.bodySmall?.color,
),
suffixIcon: IconButton(
icon: Icon(
Icons.send_rounded,
size: 22,
color: _textController.text.trim().isEmpty
? Theme.of(context).disabledColor
: Theme.of(context).colorScheme.primary,
),
onPressed: _textController.text.trim().isEmpty
? null
: _sendMessage,
tooltip: 'Send',
),
),
textInputAction: TextInputAction.send,
onSubmitted: (_) => _sendMessage(),
),
),
],
),
],
),
@@ -948,3 +1169,211 @@ class _MarkerTypeChip extends StatelessWidget {
);
}
}
// Message recipient type enum
enum MessageRecipientType {
contact,
room,
}
// Recipient Selector Sheet
class _RecipientSelectorSheet extends StatefulWidget {
final String? selectedRecipientId;
final MessageRecipientType selectedRecipientType;
final void Function(String?, MessageRecipientType) onSelect;
const _RecipientSelectorSheet({
required this.selectedRecipientId,
required this.selectedRecipientType,
required this.onSelect,
});
@override
State<_RecipientSelectorSheet> createState() => _RecipientSelectorSheetState();
}
class _RecipientSelectorSheetState extends State<_RecipientSelectorSheet> with SingleTickerProviderStateMixin {
late TabController _tabController;
@override
void initState() {
super.initState();
_tabController = TabController(
length: 2,
vsync: this,
initialIndex: widget.selectedRecipientType == MessageRecipientType.contact ? 0 : 1,
);
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
height: MediaQuery.of(context).size.height * 0.7,
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
borderRadius: const BorderRadius.vertical(top: Radius.circular(20)),
),
child: Column(
children: [
// Header
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Theme.of(context).dividerColor,
width: 1,
),
),
),
child: Row(
children: [
Text(
'Select Recipient',
style: Theme.of(context).textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
),
),
const Spacer(),
IconButton(
icon: const Icon(Icons.close),
onPressed: () => Navigator.pop(context),
),
],
),
),
// Tab bar
TabBar(
controller: _tabController,
tabs: const [
Tab(
icon: Icon(Icons.person),
text: 'Contacts',
),
Tab(
icon: Icon(Icons.tag),
text: 'Channels',
),
],
),
// Tab view
Expanded(
child: TabBarView(
controller: _tabController,
children: [
// Contacts tab
Consumer<ContactsProvider>(
builder: (context, contactsProvider, child) {
final contacts = contactsProvider.chatContacts;
if (contacts.isEmpty) {
return const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.person_off, size: 64, color: Colors.grey),
SizedBox(height: 16),
Text('No contacts available'),
],
),
);
}
return ListView.builder(
itemCount: contacts.length,
itemBuilder: (context, index) {
final contact = contacts[index];
final isSelected = widget.selectedRecipientType == MessageRecipientType.contact &&
widget.selectedRecipientId == contact.publicKeyHex;
return ListTile(
leading: CircleAvatar(
child: contact.roleEmoji != null
? Text(contact.roleEmoji!)
: const Icon(Icons.person),
),
title: Text(contact.displayName),
subtitle: Text(
contact.publicKeyShort,
style: const TextStyle(fontFamily: 'monospace', fontSize: 12),
),
trailing: isSelected
? Icon(
Icons.check_circle,
color: Theme.of(context).colorScheme.primary,
)
: null,
selected: isSelected,
onTap: () {
widget.onSelect(contact.publicKeyHex, MessageRecipientType.contact);
Navigator.pop(context);
},
);
},
);
},
),
// Channels/Rooms tab
Consumer<ContactsProvider>(
builder: (context, contactsProvider, child) {
final rooms = contactsProvider.rooms;
if (rooms.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.tag, size: 64, color: Colors.grey),
const SizedBox(height: 16),
const Text('No channels available'),
],
),
);
}
return ListView.builder(
itemCount: rooms.length,
itemBuilder: (context, index) {
final room = rooms[index];
final isSelected = widget.selectedRecipientType == MessageRecipientType.room &&
widget.selectedRecipientId == room.publicKeyHex;
return ListTile(
leading: const CircleAvatar(
child: Icon(Icons.tag),
),
title: Text(room.displayName),
subtitle: Text(
room.publicKeyShort,
style: const TextStyle(fontFamily: 'monospace', fontSize: 12),
),
trailing: isSelected
? Icon(
Icons.check_circle,
color: Theme.of(context).colorScheme.primary,
)
: null,
selected: isSelected,
onTap: () {
widget.onSelect(room.publicKeyHex, MessageRecipientType.room);
Navigator.pop(context);
},
);
},
);
},
),
],
),
),
],
),
);
}
}

View File

@@ -0,0 +1,568 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:share_plus/share_plus.dart';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import '../models/ble_packet_log.dart';
import '../services/meshcore_ble_service.dart';
class PacketLogScreen extends StatefulWidget {
final MeshCoreBleService bleService;
const PacketLogScreen({
super.key,
required this.bleService,
});
@override
State<PacketLogScreen> createState() => _PacketLogScreenState();
}
class _PacketLogScreenState extends State<PacketLogScreen> {
bool _autoScroll = true;
final ScrollController _scrollController = ScrollController();
String _searchQuery = '';
PacketDirection? _filterDirection;
@override
void dispose() {
_scrollController.dispose();
super.dispose();
}
List<BlePacketLog> get _filteredLogs {
var logs = widget.bleService.packetLogs;
// Filter by direction
if (_filterDirection != null) {
logs = logs.where((log) => log.direction == _filterDirection).toList();
}
// Filter by search query
if (_searchQuery.isNotEmpty) {
final query = _searchQuery.toLowerCase();
logs = logs.where((log) {
return log.hexData.toLowerCase().contains(query) ||
(log.description?.toLowerCase().contains(query) ?? false) ||
log.summary.toLowerCase().contains(query);
}).toList();
}
return logs;
}
Future<void> _exportLogs(BuildContext context) async {
try {
final logs = _filteredLogs;
if (logs.isEmpty) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('No logs to export')),
);
}
return;
}
// Create CSV content
final buffer = StringBuffer();
buffer.writeln('Timestamp,Direction,Size (bytes),Code,Hex Data,Description');
for (final log in logs) {
buffer.writeln(log.toCsvRow());
}
// Save to temporary file
final tempDir = await getTemporaryDirectory();
final file = File('${tempDir.path}/ble_packets_${DateTime.now().millisecondsSinceEpoch}.csv');
await file.writeAsString(buffer.toString());
// Share the file
await Share.shareXFiles(
[XFile(file.path)],
subject: 'MeshCore BLE Packet Logs',
text: 'Exported ${logs.length} BLE packets from MeshCore SAR app',
);
} catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Export failed: $e')),
);
}
}
}
Future<void> _exportAsText(BuildContext context) async {
try {
final logs = _filteredLogs;
if (logs.isEmpty) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('No logs to export')),
);
}
return;
}
// Create text content
final buffer = StringBuffer();
buffer.writeln('MeshCore BLE Packet Logs');
buffer.writeln('=' * 80);
buffer.writeln('Exported: ${DateTime.now().toIso8601String()}');
buffer.writeln('Total packets: ${logs.length}');
buffer.writeln('=' * 80);
buffer.writeln();
for (final log in logs) {
buffer.writeln(log.toLogString());
}
// Save to temporary file
final tempDir = await getTemporaryDirectory();
final file = File('${tempDir.path}/ble_packets_${DateTime.now().millisecondsSinceEpoch}.txt');
await file.writeAsString(buffer.toString());
// Share the file
await Share.shareXFiles(
[XFile(file.path)],
subject: 'MeshCore BLE Packet Logs',
text: 'Exported ${logs.length} BLE packets from MeshCore SAR app',
);
} catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Export failed: $e')),
);
}
}
}
void _copyToClipboard(BuildContext context, BlePacketLog log) {
Clipboard.setData(ClipboardData(text: log.hexData));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Hex data copied to clipboard'),
duration: Duration(seconds: 1),
),
);
}
void _clearLogs(BuildContext context) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Clear Packet Logs'),
content: const Text('Are you sure you want to clear all packet logs? This cannot be undone.'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
widget.bleService.clearPacketLogs();
Navigator.pop(context);
setState(() {});
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Packet logs cleared')),
);
},
style: TextButton.styleFrom(foregroundColor: Colors.red),
child: const Text('Clear'),
),
],
),
);
}
@override
Widget build(BuildContext context) {
final logs = _filteredLogs;
return Scaffold(
appBar: AppBar(
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('BLE Packet Logs'),
Text(
'${logs.length} packets',
style: Theme.of(context).textTheme.bodySmall,
),
],
),
actions: [
// Direction filter
PopupMenuButton<PacketDirection?>(
icon: Icon(_filterDirection == null
? Icons.filter_list
: _filterDirection == PacketDirection.rx
? Icons.arrow_downward
: Icons.arrow_upward),
tooltip: 'Filter by direction',
onSelected: (direction) {
setState(() {
_filterDirection = direction;
});
},
itemBuilder: (context) => [
PopupMenuItem(
value: null,
child: Row(
children: [
Icon(Icons.filter_list,
color: _filterDirection == null ? Theme.of(context).colorScheme.primary : null),
const SizedBox(width: 8),
Text('All',
style: TextStyle(
fontWeight: _filterDirection == null ? FontWeight.bold : FontWeight.normal)),
],
),
),
PopupMenuItem(
value: PacketDirection.rx,
child: Row(
children: [
Icon(Icons.arrow_downward,
color: _filterDirection == PacketDirection.rx
? Theme.of(context).colorScheme.primary
: null),
const SizedBox(width: 8),
Text('RX (Received)',
style: TextStyle(
fontWeight:
_filterDirection == PacketDirection.rx ? FontWeight.bold : FontWeight.normal)),
],
),
),
PopupMenuItem(
value: PacketDirection.tx,
child: Row(
children: [
Icon(Icons.arrow_upward,
color: _filterDirection == PacketDirection.tx
? Theme.of(context).colorScheme.primary
: null),
const SizedBox(width: 8),
Text('TX (Sent)',
style: TextStyle(
fontWeight:
_filterDirection == PacketDirection.tx ? FontWeight.bold : FontWeight.normal)),
],
),
),
],
),
// Auto-scroll toggle
IconButton(
icon: Icon(_autoScroll ? Icons.vertical_align_bottom : Icons.vertical_align_center),
tooltip: _autoScroll ? 'Disable auto-scroll' : 'Enable auto-scroll',
onPressed: () {
setState(() {
_autoScroll = !_autoScroll;
});
},
),
// Export menu
PopupMenuButton(
icon: const Icon(Icons.share),
tooltip: 'Export logs',
itemBuilder: (context) => [
const PopupMenuItem(
value: 'csv',
child: Row(
children: [
Icon(Icons.table_chart),
SizedBox(width: 8),
Text('Export as CSV'),
],
),
),
const PopupMenuItem(
value: 'txt',
child: Row(
children: [
Icon(Icons.text_snippet),
SizedBox(width: 8),
Text('Export as Text'),
],
),
),
],
onSelected: (value) {
if (value == 'csv') {
_exportLogs(context);
} else if (value == 'txt') {
_exportAsText(context);
}
},
),
// Clear logs
IconButton(
icon: const Icon(Icons.delete_outline),
tooltip: 'Clear logs',
onPressed: () => _clearLogs(context),
),
],
),
body: Column(
children: [
// Search bar
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: InputDecoration(
hintText: 'Search logs...',
prefixIcon: const Icon(Icons.search),
suffixIcon: _searchQuery.isNotEmpty
? IconButton(
icon: const Icon(Icons.clear),
onPressed: () {
setState(() {
_searchQuery = '';
});
},
)
: null,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
),
),
onChanged: (value) {
setState(() {
_searchQuery = value;
});
},
),
),
// Logs list
Expanded(
child: logs.isEmpty
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.list_alt,
size: 64,
color: Colors.grey[400],
),
const SizedBox(height: 16),
Text(
_searchQuery.isNotEmpty || _filterDirection != null
? 'No matching packets found'
: 'No packets logged yet',
style: TextStyle(
fontSize: 16,
color: Colors.grey[600],
),
),
if (_searchQuery.isNotEmpty || _filterDirection != null) ...[
const SizedBox(height: 8),
TextButton.icon(
onPressed: () {
setState(() {
_searchQuery = '';
_filterDirection = null;
});
},
icon: const Icon(Icons.clear_all),
label: const Text('Clear filters'),
),
],
],
),
)
: ListView.builder(
controller: _scrollController,
itemCount: logs.length,
itemBuilder: (context, index) {
final log = logs[index];
// Auto-scroll to bottom
if (_autoScroll && index == logs.length - 1) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (_scrollController.hasClients) {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 200),
curve: Curves.easeOut,
);
}
});
}
return _PacketLogCard(
log: log,
onCopy: () => _copyToClipboard(context, log),
);
},
),
),
],
),
);
}
}
class _PacketLogCard extends StatelessWidget {
final BlePacketLog log;
final VoidCallback onCopy;
const _PacketLogCard({
required this.log,
required this.onCopy,
});
@override
Widget build(BuildContext context) {
final isRx = log.direction == PacketDirection.rx;
final directionColor = isRx ? Colors.green : Colors.blue;
return Card(
margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
child: ExpansionTile(
leading: CircleAvatar(
backgroundColor: directionColor.withOpacity(0.2),
child: Icon(
isRx ? Icons.arrow_downward : Icons.arrow_upward,
color: directionColor,
size: 20,
),
),
title: Row(
children: [
Text(
isRx ? 'RX' : 'TX',
style: TextStyle(
fontWeight: FontWeight.bold,
color: directionColor,
fontSize: 12,
),
),
const SizedBox(width: 8),
if (log.description != null)
Flexible(
child: Text(
log.description!,
style: const TextStyle(fontSize: 14),
overflow: TextOverflow.ellipsis,
),
)
else
Text(
'Code: ${log.responseCode != null ? "0x${log.responseCode!.toRadixString(16).padLeft(2, '0')}" : "N/A"}',
style: const TextStyle(fontSize: 14),
),
],
),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 4),
Text(
'${log.rawData.length} bytes • ${_formatTimestamp(log.timestamp)}',
style: TextStyle(fontSize: 12, color: Colors.grey[600]),
),
],
),
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Hex data
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Hex: ',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.grey[700],
),
),
Expanded(
child: SelectableText(
log.hexData,
style: const TextStyle(
fontFamily: 'monospace',
fontSize: 12,
),
),
),
IconButton(
icon: const Icon(Icons.copy, size: 18),
tooltip: 'Copy hex data',
onPressed: onCopy,
padding: EdgeInsets.zero,
constraints: const BoxConstraints(),
),
],
),
const SizedBox(height: 8),
// Metadata
Wrap(
spacing: 16,
runSpacing: 8,
children: [
_InfoChip(
icon: Icons.schedule,
label: log.timestamp.toIso8601String(),
),
_InfoChip(
icon: Icons.data_usage,
label: '${log.rawData.length} bytes',
),
if (log.responseCode != null)
_InfoChip(
icon: Icons.tag,
label: 'Code: 0x${log.responseCode!.toRadixString(16).padLeft(2, '0')} (${log.responseCode})',
),
],
),
],
),
),
],
),
);
}
String _formatTimestamp(DateTime timestamp) {
final now = DateTime.now();
final diff = now.difference(timestamp);
if (diff.inSeconds < 60) {
return '${diff.inSeconds}s ago';
} else if (diff.inMinutes < 60) {
return '${diff.inMinutes}m ago';
} else if (diff.inHours < 24) {
return '${diff.inHours}h ago';
} else {
return '${timestamp.hour.toString().padLeft(2, '0')}:${timestamp.minute.toString().padLeft(2, '0')}:${timestamp.second.toString().padLeft(2, '0')}';
}
}
}
class _InfoChip extends StatelessWidget {
final IconData icon;
final String label;
const _InfoChip({
required this.icon,
required this.label,
});
@override
Widget build(BuildContext context) {
return Chip(
avatar: Icon(icon, size: 16),
label: Text(
label,
style: const TextStyle(fontSize: 11),
),
padding: const EdgeInsets.all(4),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
);
}
}