mirror of
https://github.com/zjs81/meshcore-open.git
synced 2026-07-28 22:38:43 +10:00
Merge pull request #27 from zjs81/dev-roomserver-fixes
Dev roomserver fixes
This commit is contained in:
@@ -1996,12 +1996,21 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
final settings = _appSettingsService!.settings;
|
final settings = _appSettingsService!.settings;
|
||||||
if (settings.notificationsEnabled && settings.notifyOnNewMessage) {
|
if (settings.notificationsEnabled && settings.notifyOnNewMessage) {
|
||||||
// Find the contact name
|
// Find the contact name
|
||||||
_notificationService.showMessageNotification(
|
if (contact?.type == advTypeChat) {
|
||||||
contactName: contact?.name ?? 'Unknown',
|
_notificationService.showMessageNotification(
|
||||||
message: message.text,
|
contactName: contact?.name ?? 'Unknown',
|
||||||
contactId: message.senderKeyHex,
|
message: message.text,
|
||||||
badgeCount: getTotalUnreadCount(),
|
contactId: message.senderKeyHex,
|
||||||
);
|
badgeCount: getTotalUnreadCount(),
|
||||||
|
);
|
||||||
|
} else if (contact?.type == advTypeRoom) {
|
||||||
|
_notificationService.showMessageNotification(
|
||||||
|
contactName: contact?.name ?? 'Unknown Room',
|
||||||
|
message: message.text.substring(4),
|
||||||
|
contactId: message.senderKeyHex,
|
||||||
|
badgeCount: getTotalUnreadCount(),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_handleQueuedMessageReceived();
|
_handleQueuedMessageReceived();
|
||||||
@@ -2027,7 +2036,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
final baseTextOffset = timestampOffset + 4;
|
final baseTextOffset = timestampOffset + 4;
|
||||||
|
|
||||||
if (frame.length <= baseTextOffset) return null;
|
if (frame.length <= baseTextOffset) return null;
|
||||||
|
final fourBytePubMSG = frame.sublist(baseTextOffset, baseTextOffset + 4);
|
||||||
final senderPrefix = frame.sublist(prefixOffset, prefixOffset + prefixLen);
|
final senderPrefix = frame.sublist(prefixOffset, prefixOffset + prefixLen);
|
||||||
final flags = frame[txtTypeOffset];
|
final flags = frame[txtTypeOffset];
|
||||||
final shiftedType = flags >> 2;
|
final shiftedType = flags >> 2;
|
||||||
@@ -2065,6 +2074,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
status: MessageStatus.delivered,
|
status: MessageStatus.delivered,
|
||||||
pathLength: pathLenByte == 0xFF ? 0 : pathLenByte,
|
pathLength: pathLenByte == 0xFF ? 0 : pathLenByte,
|
||||||
pathBytes: Uint8List(0),
|
pathBytes: Uint8List(0),
|
||||||
|
fourByteRoomContactKey: fourBytePubMSG
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ class Message {
|
|||||||
final int? pathLength;
|
final int? pathLength;
|
||||||
final Uint8List pathBytes;
|
final Uint8List pathBytes;
|
||||||
final Map<String, int> reactions;
|
final Map<String, int> reactions;
|
||||||
|
final Uint8List fourByteRoomContactKey;
|
||||||
|
|
||||||
Message({
|
Message({
|
||||||
required this.senderKey,
|
required this.senderKey,
|
||||||
@@ -40,8 +41,10 @@ class Message {
|
|||||||
this.tripTimeMs,
|
this.tripTimeMs,
|
||||||
this.pathLength,
|
this.pathLength,
|
||||||
Uint8List? pathBytes,
|
Uint8List? pathBytes,
|
||||||
|
Uint8List? fourByteRoomContactKey,
|
||||||
Map<String, int>? reactions,
|
Map<String, int>? reactions,
|
||||||
}) : pathBytes = pathBytes ?? Uint8List(0),
|
}) : pathBytes = pathBytes ?? Uint8List(0),
|
||||||
|
fourByteRoomContactKey = fourByteRoomContactKey ?? Uint8List(0),
|
||||||
reactions = reactions ?? {};
|
reactions = reactions ?? {};
|
||||||
|
|
||||||
String get senderKeyHex => pubKeyToHex(senderKey);
|
String get senderKeyHex => pubKeyToHex(senderKey);
|
||||||
@@ -58,6 +61,7 @@ class Message {
|
|||||||
Uint8List? pathBytes,
|
Uint8List? pathBytes,
|
||||||
bool? isCli,
|
bool? isCli,
|
||||||
Map<String, int>? reactions,
|
Map<String, int>? reactions,
|
||||||
|
Uint8List? fourByteRoomContactKey,
|
||||||
}) {
|
}) {
|
||||||
return Message(
|
return Message(
|
||||||
senderKey: senderKey,
|
senderKey: senderKey,
|
||||||
@@ -76,6 +80,7 @@ class Message {
|
|||||||
pathLength: pathLength ?? this.pathLength,
|
pathLength: pathLength ?? this.pathLength,
|
||||||
pathBytes: pathBytes ?? this.pathBytes,
|
pathBytes: pathBytes ?? this.pathBytes,
|
||||||
reactions: reactions ?? this.reactions,
|
reactions: reactions ?? this.reactions,
|
||||||
|
fourByteRoomContactKey: fourByteRoomContactKey ?? this.fourByteRoomContactKey,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/scheduler.dart';
|
import 'package:flutter/scheduler.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
@@ -162,13 +163,12 @@ class _ChatScreenState extends State<ChatScreen> {
|
|||||||
body: Consumer<MeshCoreConnector>(
|
body: Consumer<MeshCoreConnector>(
|
||||||
builder: (context, connector, child) {
|
builder: (context, connector, child) {
|
||||||
final messages = connector.getMessages(widget.contact);
|
final messages = connector.getMessages(widget.contact);
|
||||||
|
|
||||||
return Column(
|
return Column(
|
||||||
children: [
|
children: [
|
||||||
Expanded(
|
Expanded(
|
||||||
child: messages.isEmpty
|
child: messages.isEmpty
|
||||||
? _buildEmptyState()
|
? _buildEmptyState()
|
||||||
: _buildMessageList(messages),
|
: _buildMessageList(messages, connector),
|
||||||
),
|
),
|
||||||
_buildInputBar(connector),
|
_buildInputBar(connector),
|
||||||
],
|
],
|
||||||
@@ -199,18 +199,29 @@ class _ChatScreenState extends State<ChatScreen> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildMessageList(List<Message> messages) {
|
Widget _buildMessageList(List<Message> messages, MeshCoreConnector connector) {
|
||||||
return ListView.builder(
|
return ListView.builder(
|
||||||
controller: _scrollController,
|
controller: _scrollController,
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 16),
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 16),
|
||||||
itemCount: messages.length,
|
itemCount: messages.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
|
Contact contact = widget.contact;
|
||||||
final message = messages[index];
|
final message = messages[index];
|
||||||
|
String fourByteHex = '';
|
||||||
|
if (widget.contact.type == advTypeRoom) {
|
||||||
|
contact = _resolveContactFrom4Bytes(
|
||||||
|
connector,
|
||||||
|
message.fourByteRoomContactKey.isEmpty ? Uint8List.fromList([0, 0, 0, 0]) : message.fourByteRoomContactKey,
|
||||||
|
);
|
||||||
|
fourByteHex = message.fourByteRoomContactKey.map((b) => b.toRadixString(16).padLeft(2, '0')).join().toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
return _MessageBubble(
|
return _MessageBubble(
|
||||||
message: message,
|
message: message,
|
||||||
senderName: widget.contact.name,
|
senderName: widget.contact.type == advTypeRoom ? "${contact.name} [$fourByteHex]" : contact.name,
|
||||||
onTap: () => _openMessagePath(message),
|
isRoomServer: widget.contact.type == advTypeRoom,
|
||||||
onLongPress: () => _showMessageActions(message),
|
onTap: () => _openMessagePath(message, contact),
|
||||||
|
onLongPress: () => _showMessageActions(message, contact),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -579,6 +590,13 @@ class _ChatScreenState extends State<ChatScreen> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Contact _resolveContactFrom4Bytes(MeshCoreConnector connector, Uint8List key4Bytes) {
|
||||||
|
return connector.contacts.firstWhere(
|
||||||
|
(c) => listEquals(c.publicKey.sublist(0, 4), key4Bytes.sublist(0, 4)),
|
||||||
|
orElse: () => widget.contact,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
String _currentPathLabel(Contact contact) {
|
String _currentPathLabel(Contact contact) {
|
||||||
// Check if user has set a path override
|
// Check if user has set a path override
|
||||||
if (contact.pathOverride != null) {
|
if (contact.pathOverride != null) {
|
||||||
@@ -684,6 +702,15 @@ class _ChatScreenState extends State<ChatScreen> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _openChat(BuildContext context, Contact contact) {
|
||||||
|
// Check if this is a repeater
|
||||||
|
context.read<MeshCoreConnector>().markContactRead(contact.publicKeyHex);
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(builder: (context) => ChatScreen(contact: contact)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _showCustomPathDialog(BuildContext context) async {
|
Future<void> _showCustomPathDialog(BuildContext context) async {
|
||||||
final connector = Provider.of<MeshCoreConnector>(context, listen: false);
|
final connector = Provider.of<MeshCoreConnector>(context, listen: false);
|
||||||
|
|
||||||
@@ -734,10 +761,20 @@ class _ChatScreenState extends State<ChatScreen> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void _openMessagePath(Message message) {
|
void _openMessagePath(Message message, Contact contact) {
|
||||||
final connector = context.read<MeshCoreConnector>();
|
final connector = context.read<MeshCoreConnector>();
|
||||||
final senderName =
|
final fourByteHex = message.fourByteRoomContactKey
|
||||||
message.isOutgoing ? (connector.selfName ?? 'Me') : widget.contact.name;
|
.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(
|
final pathMessage = ChannelMessage(
|
||||||
senderKey: null,
|
senderKey: null,
|
||||||
senderName: senderName,
|
senderName: senderName,
|
||||||
@@ -757,7 +794,7 @@ class _ChatScreenState extends State<ChatScreen> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _showMessageActions(Message message) {
|
void _showMessageActions(Message message, Contact contact) {
|
||||||
showModalBottomSheet(
|
showModalBottomSheet(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (sheetContext) => SafeArea(
|
builder: (sheetContext) => SafeArea(
|
||||||
@@ -798,6 +835,15 @@ class _ChatScreenState extends State<ChatScreen> {
|
|||||||
_retryMessage(message);
|
_retryMessage(message);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
if (widget.contact.type == advTypeRoom)
|
||||||
|
ListTile(
|
||||||
|
leading: const Icon(Icons.chat),
|
||||||
|
title: const Text('Open Chat'),
|
||||||
|
onTap: () {
|
||||||
|
Navigator.pop(sheetContext);
|
||||||
|
_openChat(context, contact);
|
||||||
|
},
|
||||||
|
),
|
||||||
ListTile(
|
ListTile(
|
||||||
leading: const Icon(Icons.close),
|
leading: const Icon(Icons.close),
|
||||||
title: const Text('Cancel'),
|
title: const Text('Cancel'),
|
||||||
@@ -862,12 +908,14 @@ class _ChatScreenState extends State<ChatScreen> {
|
|||||||
class _MessageBubble extends StatelessWidget {
|
class _MessageBubble extends StatelessWidget {
|
||||||
final Message message;
|
final Message message;
|
||||||
final String senderName;
|
final String senderName;
|
||||||
|
final bool isRoomServer;
|
||||||
final VoidCallback? onTap;
|
final VoidCallback? onTap;
|
||||||
final VoidCallback? onLongPress;
|
final VoidCallback? onLongPress;
|
||||||
|
|
||||||
const _MessageBubble({
|
const _MessageBubble({
|
||||||
required this.message,
|
required this.message,
|
||||||
required this.senderName,
|
required this.senderName,
|
||||||
|
required this.isRoomServer,
|
||||||
this.onTap,
|
this.onTap,
|
||||||
this.onLongPress,
|
this.onLongPress,
|
||||||
});
|
});
|
||||||
@@ -886,7 +934,10 @@ class _MessageBubble extends StatelessWidget {
|
|||||||
? colorScheme.onErrorContainer
|
? colorScheme.onErrorContainer
|
||||||
: (isOutgoing ? colorScheme.onPrimary : colorScheme.onSurface);
|
: (isOutgoing ? colorScheme.onPrimary : colorScheme.onSurface);
|
||||||
final metaColor = textColor.withValues(alpha: 0.7);
|
final metaColor = textColor.withValues(alpha: 0.7);
|
||||||
|
String messageText = message.text;
|
||||||
|
if (isRoomServer && !isOutgoing) {
|
||||||
|
messageText = message.text.substring(4.clamp(0, message.text.length));
|
||||||
|
}
|
||||||
return Padding(
|
return Padding(
|
||||||
padding: const EdgeInsets.symmetric(vertical: 4),
|
padding: const EdgeInsets.symmetric(vertical: 4),
|
||||||
child: Column(
|
child: Column(
|
||||||
@@ -937,7 +988,7 @@ class _MessageBubble extends StatelessWidget {
|
|||||||
)
|
)
|
||||||
else
|
else
|
||||||
Text(
|
Text(
|
||||||
message.text,
|
messageText,
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
color: textColor,
|
color: textColor,
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import '../widgets/list_filter_widget.dart';
|
|||||||
import '../widgets/empty_state.dart';
|
import '../widgets/empty_state.dart';
|
||||||
import '../widgets/quick_switch_bar.dart';
|
import '../widgets/quick_switch_bar.dart';
|
||||||
import '../widgets/repeater_login_dialog.dart';
|
import '../widgets/repeater_login_dialog.dart';
|
||||||
|
import '../widgets/room_login_dialog.dart';
|
||||||
import '../widgets/unread_badge.dart';
|
import '../widgets/unread_badge.dart';
|
||||||
import 'channels_screen.dart';
|
import 'channels_screen.dart';
|
||||||
import 'chat_screen.dart';
|
import 'chat_screen.dart';
|
||||||
@@ -384,6 +385,8 @@ class _ContactsScreenState extends State<ContactsScreen>
|
|||||||
// Check if this is a repeater
|
// Check if this is a repeater
|
||||||
if (contact.type == advTypeRepeater) {
|
if (contact.type == advTypeRepeater) {
|
||||||
_showRepeaterLogin(context, contact);
|
_showRepeaterLogin(context, contact);
|
||||||
|
} else if (contact.type == advTypeRoom) {
|
||||||
|
_showRoomLogin(context, contact);
|
||||||
} else {
|
} else {
|
||||||
context.read<MeshCoreConnector>().markContactRead(contact.publicKeyHex);
|
context.read<MeshCoreConnector>().markContactRead(contact.publicKeyHex);
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
@@ -436,6 +439,25 @@ class _ContactsScreenState extends State<ContactsScreen>
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<MeshCoreConnector>().markContactRead(room.publicKeyHex);
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (context) => ChatScreen(contact: room),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
void _showGroupOptions(BuildContext context, ContactGroup group, List<Contact> contacts) {
|
void _showGroupOptions(BuildContext context, ContactGroup group, List<Contact> contacts) {
|
||||||
final members = _resolveGroupContacts(group, contacts);
|
final members = _resolveGroupContacts(group, contacts);
|
||||||
showModalBottomSheet(
|
showModalBottomSheet(
|
||||||
@@ -642,6 +664,7 @@ class _ContactsScreenState extends State<ContactsScreen>
|
|||||||
Contact contact,
|
Contact contact,
|
||||||
) {
|
) {
|
||||||
final isRepeater = contact.type == advTypeRepeater;
|
final isRepeater = contact.type == advTypeRepeater;
|
||||||
|
final isRoom = contact.type == advTypeRoom;
|
||||||
|
|
||||||
showModalBottomSheet(
|
showModalBottomSheet(
|
||||||
context: context,
|
context: context,
|
||||||
@@ -658,6 +681,15 @@ class _ContactsScreenState extends State<ContactsScreen>
|
|||||||
_showRepeaterLogin(context, contact);
|
_showRepeaterLogin(context, contact);
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
else if (isRoom)
|
||||||
|
ListTile(
|
||||||
|
leading: const Icon(Icons.room, color: Colors.blue),
|
||||||
|
title: const Text('Room Login'),
|
||||||
|
onTap: () {
|
||||||
|
Navigator.pop(context);
|
||||||
|
_showRoomLogin(context, contact);
|
||||||
|
},
|
||||||
|
)
|
||||||
else
|
else
|
||||||
ListTile(
|
ListTile(
|
||||||
leading: const Icon(Icons.chat),
|
leading: const Icon(Icons.chat),
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import 'channels_screen.dart';
|
|||||||
import 'chat_screen.dart';
|
import 'chat_screen.dart';
|
||||||
import 'contacts_screen.dart';
|
import 'contacts_screen.dart';
|
||||||
import '../widgets/repeater_login_dialog.dart';
|
import '../widgets/repeater_login_dialog.dart';
|
||||||
|
import '../widgets/room_login_dialog.dart';
|
||||||
import 'repeater_hub_screen.dart';
|
import 'repeater_hub_screen.dart';
|
||||||
import 'settings_screen.dart';
|
import 'settings_screen.dart';
|
||||||
|
|
||||||
@@ -572,6 +573,25 @@ class _MapScreenState extends State<MapScreen> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<MeshCoreConnector>().markContactRead(room.publicKeyHex);
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (context) => ChatScreen(contact: room),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
void _showNodeInfo(BuildContext context, Contact contact) {
|
void _showNodeInfo(BuildContext context, Contact contact) {
|
||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
@@ -624,6 +644,14 @@ class _MapScreenState extends State<MapScreen> {
|
|||||||
},
|
},
|
||||||
child: const Text('Manage Repeater'),
|
child: const Text('Manage Repeater'),
|
||||||
),
|
),
|
||||||
|
if (contact.type == advTypeRoom)
|
||||||
|
TextButton(
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.pop(context);
|
||||||
|
_showRoomLogin(context, contact);
|
||||||
|
},
|
||||||
|
child: const Text('Join Room'),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ class MessageStore {
|
|||||||
'pathLength': msg.pathLength,
|
'pathLength': msg.pathLength,
|
||||||
'pathBytes': msg.pathBytes.isNotEmpty ? base64Encode(msg.pathBytes) : null,
|
'pathBytes': msg.pathBytes.isNotEmpty ? base64Encode(msg.pathBytes) : null,
|
||||||
'reactions': msg.reactions,
|
'reactions': msg.reactions,
|
||||||
|
'fourByteRoomContactKey': base64Encode(msg.fourByteRoomContactKey),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,6 +87,9 @@ class MessageStore {
|
|||||||
reactions: (json['reactions'] as Map<String, dynamic>?)?.map(
|
reactions: (json['reactions'] as Map<String, dynamic>?)?.map(
|
||||||
(key, value) => MapEntry(key, value as int),
|
(key, value) => MapEntry(key, value as int),
|
||||||
) ?? {},
|
) ?? {},
|
||||||
|
fourByteRoomContactKey: json['fourByteRoomContactKey'] != null
|
||||||
|
? Uint8List.fromList(base64Decode(json['fourByteRoomContactKey'] as String))
|
||||||
|
: null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,416 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import '../models/contact.dart';
|
||||||
|
import '../services/storage_service.dart';
|
||||||
|
import '../connector/meshcore_connector.dart';
|
||||||
|
import '../connector/meshcore_protocol.dart';
|
||||||
|
import '../utils/app_logger.dart';
|
||||||
|
import 'path_management_dialog.dart';
|
||||||
|
|
||||||
|
class RoomLoginDialog extends StatefulWidget {
|
||||||
|
final Contact room;
|
||||||
|
final Function(String password) onLogin;
|
||||||
|
|
||||||
|
const RoomLoginDialog({
|
||||||
|
super.key,
|
||||||
|
required this.room,
|
||||||
|
required this.onLogin,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<RoomLoginDialog> createState() => _RoomLoginDialogState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _RoomLoginDialogState extends State<RoomLoginDialog> {
|
||||||
|
final TextEditingController _passwordController = TextEditingController();
|
||||||
|
final StorageService _storage = StorageService();
|
||||||
|
bool _savePassword = false;
|
||||||
|
bool _isLoading = true;
|
||||||
|
bool _obscurePassword = true;
|
||||||
|
late MeshCoreConnector _connector;
|
||||||
|
int _currentAttempt = 0;
|
||||||
|
static const int _maxAttempts = 5;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_connector = Provider.of<MeshCoreConnector>(context, listen: false);
|
||||||
|
_loadSavedPassword();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _loadSavedPassword() async {
|
||||||
|
final savedPassword =
|
||||||
|
await _storage.getRepeaterPassword(widget.room.publicKeyHex);
|
||||||
|
if (savedPassword != null) {
|
||||||
|
setState(() {
|
||||||
|
_passwordController.text = savedPassword;
|
||||||
|
_savePassword = true;
|
||||||
|
_isLoading = false;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
setState(() {
|
||||||
|
_isLoading = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_passwordController.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool _isLoggingIn = false;
|
||||||
|
|
||||||
|
Contact _resolveRepeater(MeshCoreConnector connector) {
|
||||||
|
return connector.contacts.firstWhere(
|
||||||
|
(c) => c.publicKeyHex == widget.room.publicKeyHex,
|
||||||
|
orElse: () => widget.room,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _handleLogin() async {
|
||||||
|
if (_isLoggingIn) return;
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
_isLoggingIn = true;
|
||||||
|
_currentAttempt = 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
final password = _passwordController.text;
|
||||||
|
final room = _resolveRepeater(_connector);
|
||||||
|
appLogger.info(
|
||||||
|
'Login started for ${room.name} (${room.publicKeyHex})',
|
||||||
|
tag: 'RoomLogin',
|
||||||
|
);
|
||||||
|
final selection = await _connector.preparePathForContactSend(room);
|
||||||
|
final loginFrame = buildSendLoginFrame(room.publicKey, password);
|
||||||
|
final pathLengthValue = selection.useFlood ? -1 : selection.hopCount;
|
||||||
|
final timeoutMs = _connector.calculateTimeout(
|
||||||
|
pathLength: pathLengthValue,
|
||||||
|
messageBytes: loginFrame.length,
|
||||||
|
);
|
||||||
|
final timeoutSeconds = (timeoutMs / 1000).ceil();
|
||||||
|
final timeout = Duration(milliseconds: timeoutMs);
|
||||||
|
final selectionLabel =
|
||||||
|
selection.useFlood ? 'flood' : '${selection.hopCount} hops';
|
||||||
|
appLogger.info(
|
||||||
|
'Login routing: $selectionLabel',
|
||||||
|
tag: 'RoomLogin',
|
||||||
|
);
|
||||||
|
bool? loginResult;
|
||||||
|
for (int attempt = 0; attempt < _maxAttempts; attempt++) {
|
||||||
|
if (!mounted) return;
|
||||||
|
setState(() {
|
||||||
|
_currentAttempt = attempt + 1;
|
||||||
|
});
|
||||||
|
|
||||||
|
appLogger.info(
|
||||||
|
'Sending login attempt ${attempt + 1}/$_maxAttempts',
|
||||||
|
tag: 'RoomLogin',
|
||||||
|
);
|
||||||
|
await _connector.sendFrame(
|
||||||
|
loginFrame,
|
||||||
|
);
|
||||||
|
|
||||||
|
loginResult = await _awaitLoginResponse(timeout);
|
||||||
|
if (loginResult == true) {
|
||||||
|
appLogger.info(
|
||||||
|
'Login succeeded for ${room.name}',
|
||||||
|
tag: 'RoomLogin',
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (loginResult == false) {
|
||||||
|
appLogger.warn(
|
||||||
|
'Login failed for ${room.name}',
|
||||||
|
tag: 'RoomLogin',
|
||||||
|
);
|
||||||
|
throw Exception('Wrong password or node is unreachable');
|
||||||
|
}
|
||||||
|
appLogger.warn(
|
||||||
|
'Login attempt ${attempt + 1} timed out after ${timeoutSeconds}s',
|
||||||
|
tag: 'RoomLogin',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loginResult == null) {
|
||||||
|
appLogger.warn(
|
||||||
|
'Login timed out for ${room.name}',
|
||||||
|
tag: 'RoomLogin',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loginResult == true) {
|
||||||
|
_connector.recordRepeaterPathResult(room, selection, true, null);
|
||||||
|
} else {
|
||||||
|
_connector.recordRepeaterPathResult(room, selection, false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loginResult != true) {
|
||||||
|
throw Exception('Wrong password or node is unreachable');
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we got a response, login succeeded
|
||||||
|
// Save password if requested
|
||||||
|
if (_savePassword) {
|
||||||
|
await _storage.saveRepeaterPassword(
|
||||||
|
widget.room.publicKeyHex, password);
|
||||||
|
} else {
|
||||||
|
// Remove saved password if user unchecked the box
|
||||||
|
await _storage.removeRepeaterPassword(widget.room.publicKeyHex);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mounted) {
|
||||||
|
Navigator.pop(context, password);
|
||||||
|
Future.microtask(() => widget.onLogin(password));
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
final room = _resolveRepeater(_connector);
|
||||||
|
appLogger.warn(
|
||||||
|
'Login error for ${room.name}: $e',
|
||||||
|
tag: 'RoomLogin',
|
||||||
|
);
|
||||||
|
if (mounted) {
|
||||||
|
setState(() {
|
||||||
|
_isLoggingIn = false;
|
||||||
|
});
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text('Login failed: $e'),
|
||||||
|
backgroundColor: Colors.red,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool?> _awaitLoginResponse(Duration timeout) async {
|
||||||
|
final completer = Completer<bool?>();
|
||||||
|
Timer? timer;
|
||||||
|
StreamSubscription<Uint8List>? subscription;
|
||||||
|
final targetPrefix = widget.room.publicKey.sublist(0, 6);
|
||||||
|
|
||||||
|
subscription = _connector.receivedFrames.listen((frame) {
|
||||||
|
if (frame.isEmpty) return;
|
||||||
|
final code = frame[0];
|
||||||
|
if (code != pushCodeLoginSuccess && code != pushCodeLoginFail) return;
|
||||||
|
if (frame.length < 8) return;
|
||||||
|
final prefix = frame.sublist(2, 8);
|
||||||
|
if (!listEquals(prefix, targetPrefix)) return;
|
||||||
|
|
||||||
|
completer.complete(code == pushCodeLoginSuccess);
|
||||||
|
subscription?.cancel();
|
||||||
|
timer?.cancel();
|
||||||
|
});
|
||||||
|
|
||||||
|
timer = Timer(timeout, () {
|
||||||
|
if (!completer.isCompleted) {
|
||||||
|
completer.complete(null);
|
||||||
|
subscription?.cancel();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
final result = await completer.future;
|
||||||
|
timer.cancel();
|
||||||
|
await subscription.cancel();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final connector = context.watch<MeshCoreConnector>();
|
||||||
|
final repeater = _resolveRepeater(connector);
|
||||||
|
final isFloodMode = repeater.pathOverride == -1;
|
||||||
|
return AlertDialog(
|
||||||
|
title: Row(
|
||||||
|
children: [
|
||||||
|
const Icon(Icons.group, color: Colors.purple),
|
||||||
|
const SizedBox(width: 8),
|
||||||
|
Expanded(
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
const Text('Room Login'),
|
||||||
|
Text(
|
||||||
|
repeater.name,
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 14,
|
||||||
|
fontWeight: FontWeight.normal,
|
||||||
|
color: Colors.grey[600],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
content: _isLoading
|
||||||
|
? const Center(
|
||||||
|
child: Padding(
|
||||||
|
padding: EdgeInsets.all(20.0),
|
||||||
|
child: CircularProgressIndicator(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
const Text(
|
||||||
|
'Enter the room password to access settings and status.',
|
||||||
|
style: TextStyle(fontSize: 14),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
TextField(
|
||||||
|
controller: _passwordController,
|
||||||
|
obscureText: _obscurePassword,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
labelText: 'Password',
|
||||||
|
hintText: 'Enter password',
|
||||||
|
border: const OutlineInputBorder(),
|
||||||
|
prefixIcon: const Icon(Icons.lock),
|
||||||
|
suffixIcon: IconButton(
|
||||||
|
icon: Icon(
|
||||||
|
_obscurePassword
|
||||||
|
? Icons.visibility
|
||||||
|
: Icons.visibility_off,
|
||||||
|
),
|
||||||
|
onPressed: () {
|
||||||
|
setState(() {
|
||||||
|
_obscurePassword = !_obscurePassword;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
onSubmitted: (_) => _handleLogin(),
|
||||||
|
autofocus: _passwordController.text.isEmpty,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
CheckboxListTile(
|
||||||
|
value: _savePassword,
|
||||||
|
onChanged: (value) {
|
||||||
|
setState(() {
|
||||||
|
_savePassword = value ?? false;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
title: const Text(
|
||||||
|
'Save password',
|
||||||
|
style: TextStyle(fontSize: 14),
|
||||||
|
),
|
||||||
|
subtitle: const Text(
|
||||||
|
'Password will be stored securely on this device',
|
||||||
|
style: TextStyle(fontSize: 12),
|
||||||
|
),
|
||||||
|
controlAffinity: ListTileControlAffinity.leading,
|
||||||
|
contentPadding: EdgeInsets.zero,
|
||||||
|
),
|
||||||
|
const Divider(),
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
const Text(
|
||||||
|
'Routing',
|
||||||
|
style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
const Spacer(),
|
||||||
|
PopupMenuButton<String>(
|
||||||
|
icon: Icon(isFloodMode ? Icons.waves : Icons.route),
|
||||||
|
tooltip: 'Routing mode',
|
||||||
|
onSelected: (mode) async {
|
||||||
|
if (mode == 'flood') {
|
||||||
|
await connector.setPathOverride(repeater, pathLen: -1);
|
||||||
|
} else {
|
||||||
|
await connector.setPathOverride(repeater, pathLen: null);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
itemBuilder: (context) => [
|
||||||
|
PopupMenuItem(
|
||||||
|
value: 'auto',
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
Icon(Icons.auto_mode, size: 20, color: !isFloodMode ? Theme.of(context).primaryColor : null),
|
||||||
|
const SizedBox(width: 8),
|
||||||
|
Text(
|
||||||
|
'Auto (use saved path)',
|
||||||
|
style: TextStyle(
|
||||||
|
fontWeight: !isFloodMode ? FontWeight.bold : FontWeight.normal,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
PopupMenuItem(
|
||||||
|
value: 'flood',
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
Icon(Icons.waves, size: 20, color: isFloodMode ? Theme.of(context).primaryColor : null),
|
||||||
|
const SizedBox(width: 8),
|
||||||
|
Text(
|
||||||
|
'Force Flood Mode',
|
||||||
|
style: TextStyle(
|
||||||
|
fontWeight: isFloodMode ? FontWeight.bold : FontWeight.normal,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
const SizedBox(height: 4),
|
||||||
|
Text(
|
||||||
|
repeater.pathLabel,
|
||||||
|
style: const TextStyle(fontSize: 11, color: Colors.grey),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
Align(
|
||||||
|
alignment: Alignment.centerLeft,
|
||||||
|
child: TextButton.icon(
|
||||||
|
onPressed: () => PathManagementDialog.show(context, contact: repeater),
|
||||||
|
icon: const Icon(Icons.timeline, size: 18),
|
||||||
|
label: const Text('Manage Paths'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(context),
|
||||||
|
child: const Text('Cancel'),
|
||||||
|
),
|
||||||
|
if (_isLoggingIn)
|
||||||
|
SizedBox(
|
||||||
|
width: double.infinity,
|
||||||
|
child: FilledButton(
|
||||||
|
onPressed: null,
|
||||||
|
child: Row(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
const SizedBox(
|
||||||
|
width: 16,
|
||||||
|
height: 16,
|
||||||
|
child: CircularProgressIndicator(
|
||||||
|
strokeWidth: 2,
|
||||||
|
color: Colors.white,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 12),
|
||||||
|
Text('Attempt $_currentAttempt/$_maxAttempts'),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
else
|
||||||
|
FilledButton.icon(
|
||||||
|
onPressed: _isLoading ? null : _handleLogin,
|
||||||
|
icon: const Icon(Icons.login, size: 18),
|
||||||
|
label: const Text('Login'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user