From 8c3ffa5472b20734620343970d1555d862d95879 Mon Sep 17 00:00:00 2001 From: zjs81 Date: Sun, 11 Jan 2026 11:51:40 -0700 Subject: [PATCH] Refactor code for improved readability and null safety in various files Also updated PR to allow login via map. --- lib/connector/meshcore_connector.dart | 4 +-- lib/screens/chat_screen.dart | 38 ++++++++++++++------------- lib/screens/contacts_screen.dart | 2 +- lib/screens/map_screen.dart | 28 ++++++++++++++++++++ lib/storage/message_store.dart | 4 ++- 5 files changed, 54 insertions(+), 22 deletions(-) diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index a929f19c..b22a3585 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -1996,14 +1996,14 @@ class MeshCoreConnector extends ChangeNotifier { final settings = _appSettingsService!.settings; if (settings.notificationsEnabled && settings.notifyOnNewMessage) { // Find the contact name - if(contact?.type == advTypeChat) { + if (contact?.type == advTypeChat) { _notificationService.showMessageNotification( contactName: contact?.name ?? 'Unknown', message: message.text, contactId: message.senderKeyHex, badgeCount: getTotalUnreadCount(), ); - }else if(contact?.type == advTypeRoom) { + } else if (contact?.type == advTypeRoom) { _notificationService.showMessageNotification( contactName: contact?.name ?? 'Unknown Room', message: message.text.substring(4), diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index bb1388c5..ce4c3e1d 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -208,7 +208,7 @@ class _ChatScreenState extends State { Contact contact = widget.contact; final message = messages[index]; String fourByteHex = ''; - if(widget.contact.type == advTypeRoom) { + if (widget.contact.type == advTypeRoom) { contact = _resolveContactFrom4Bytes( connector, message.fourByteRoomContactKey.isEmpty ? Uint8List.fromList([0, 0, 0, 0]) : message.fourByteRoomContactKey, @@ -763,9 +763,18 @@ class _ChatScreenState extends State { void _openMessagePath(Message message, Contact contact) { final connector = context.read(); - final fourByteHex = message.fourByteRoomContactKey.map((b) => b.toRadixString(16).padLeft(2, '0')).join().toUpperCase(); - final senderName = - message.isOutgoing ? (connector.selfName ?? 'Me') : widget.contact.type == advTypeRoom ? "${contact.name} [$fourByteHex]" : widget.contact.name; + final fourByteHex = message.fourByteRoomContactKey + .map((b) => b.toRadixString(16).padLeft(2, '0')) + .join() + .toUpperCase(); + final String senderName; + if (message.isOutgoing) { + senderName = connector.selfName ?? 'Me'; + } else if (widget.contact.type == advTypeRoom) { + senderName = "${contact.name} [$fourByteHex]"; + } else { + senderName = widget.contact.name; + } final pathMessage = ChannelMessage( senderKey: null, senderName: senderName, @@ -826,11 +835,12 @@ class _ChatScreenState extends State { _retryMessage(message); }, ), - if(widget.contact.type == advTypeRoom) + if (widget.contact.type == advTypeRoom) ListTile( leading: const Icon(Icons.chat), title: const Text('Open Chat'), onTap: () { + Navigator.pop(sheetContext); _openChat(context, contact); }, ), @@ -977,20 +987,12 @@ class _MessageBubble extends StatelessWidget { fallbackTextColor: textColor.withValues(alpha: 0.7), ) else - if(!isOutgoing) - Text( - messageText, - style: TextStyle( - color: textColor, - ), - ), - if(isOutgoing) - Text( - message.text, - style: TextStyle( - color: textColor, - ), + Text( + messageText, + style: TextStyle( + color: textColor, ), + ), if (isOutgoing && message.retryCount > 0) ...[ const SizedBox(height: 4), Text( diff --git a/lib/screens/contacts_screen.dart b/lib/screens/contacts_screen.dart index eed38df1..32799eb4 100644 --- a/lib/screens/contacts_screen.dart +++ b/lib/screens/contacts_screen.dart @@ -681,7 +681,7 @@ class _ContactsScreenState extends State _showRepeaterLogin(context, contact); }, ) - else if(isRoom) + else if (isRoom) ListTile( leading: const Icon(Icons.room, color: Colors.blue), title: const Text('Room Login'), diff --git a/lib/screens/map_screen.dart b/lib/screens/map_screen.dart index 93c89881..4dc79715 100644 --- a/lib/screens/map_screen.dart +++ b/lib/screens/map_screen.dart @@ -18,6 +18,7 @@ import 'channels_screen.dart'; import 'chat_screen.dart'; import 'contacts_screen.dart'; import '../widgets/repeater_login_dialog.dart'; +import '../widgets/room_login_dialog.dart'; import 'repeater_hub_screen.dart'; import 'settings_screen.dart'; @@ -572,6 +573,25 @@ class _MapScreenState extends State { ); } + void _showRoomLogin(BuildContext context, Contact room) { + showDialog( + context: context, + builder: (context) => RoomLoginDialog( + room: room, + onLogin: (password) { + // Navigate to chat screen after successful login + context.read().markContactRead(room.publicKeyHex); + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => ChatScreen(contact: room), + ), + ); + }, + ), + ); + } + void _showNodeInfo(BuildContext context, Contact contact) { showDialog( context: context, @@ -624,6 +644,14 @@ class _MapScreenState extends State { }, child: const Text('Manage Repeater'), ), + if (contact.type == advTypeRoom) + TextButton( + onPressed: () { + Navigator.pop(context); + _showRoomLogin(context, contact); + }, + child: const Text('Join Room'), + ), ], ), ); diff --git a/lib/storage/message_store.dart b/lib/storage/message_store.dart index 8f9e7039..c1bc6401 100644 --- a/lib/storage/message_store.dart +++ b/lib/storage/message_store.dart @@ -87,7 +87,9 @@ class MessageStore { reactions: (json['reactions'] as Map?)?.map( (key, value) => MapEntry(key, value as int), ) ?? {}, - fourByteRoomContactKey: Uint8List.fromList(base64Decode(json['fourByteRoomContactKey'] as String)), + fourByteRoomContactKey: json['fourByteRoomContactKey'] != null + ? Uint8List.fromList(base64Decode(json['fourByteRoomContactKey'] as String)) + : null, ); } }