| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  | import 'dart:convert'; | 
					
						
							|  |  |  | import 'package:flutter/material.dart'; | 
					
						
							|  |  |  | import 'package:flutter_hbb/common/formatter/id_formatter.dart'; | 
					
						
							|  |  |  | import '../../../models/platform_model.dart'; | 
					
						
							|  |  |  | import 'package:flutter_hbb/models/peer_model.dart'; | 
					
						
							|  |  |  | import 'package:flutter_hbb/common.dart'; | 
					
						
							|  |  |  | import 'package:flutter_hbb/common/widgets/peer_card.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  | Future<List<Peer>> getAllPeers() async { | 
					
						
							| 
									
										
										
										
											2024-02-12 21:39:19 +08:00
										 |  |  |   Map<String, dynamic> recentPeers = jsonDecode(bind.mainLoadRecentPeersSync()); | 
					
						
							|  |  |  |   Map<String, dynamic> lanPeers = jsonDecode(bind.mainLoadLanPeersSync()); | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |   Map<String, dynamic> combinedPeers = {}; | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-12 21:39:19 +08:00
										 |  |  |   void mergePeers(Map<String, dynamic> peers) { | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |     if (peers.containsKey("peers")) { | 
					
						
							|  |  |  |       dynamic peerData = peers["peers"]; | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |       if (peerData is String) { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |           peerData = jsonDecode(peerData); | 
					
						
							|  |  |  |         } catch (e) { | 
					
						
							|  |  |  |           print("Error decoding peers: $e"); | 
					
						
							|  |  |  |           return; | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |       if (peerData is List) { | 
					
						
							|  |  |  |         for (var peer in peerData) { | 
					
						
							|  |  |  |           if (peer is Map && peer.containsKey("id")) { | 
					
						
							|  |  |  |             String id = peer["id"]; | 
					
						
							|  |  |  |             if (!combinedPeers.containsKey(id)) { | 
					
						
							|  |  |  |               combinedPeers[id] = peer; | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-12 21:39:19 +08:00
										 |  |  |   mergePeers(recentPeers); | 
					
						
							|  |  |  |   mergePeers(lanPeers); | 
					
						
							| 
									
										
										
										
											2024-03-20 15:05:54 +08:00
										 |  |  |   for (var p in gFFI.abModel.allPeers()) { | 
					
						
							|  |  |  |     if (!combinedPeers.containsKey(p.id)) { | 
					
						
							|  |  |  |       combinedPeers[p.id] = p.toJson(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   for (var p in gFFI.groupModel.peers.map((e) => Peer.copy(e)).toList()) { | 
					
						
							|  |  |  |     if (!combinedPeers.containsKey(p.id)) { | 
					
						
							|  |  |  |       combinedPeers[p.id] = p.toJson(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |   List<Peer> parsedPeers = []; | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |   for (var peer in combinedPeers.values) { | 
					
						
							|  |  |  |     parsedPeers.add(Peer.fromJson(peer)); | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |   return parsedPeers; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  | class AutocompletePeerTile extends StatefulWidget { | 
					
						
							| 
									
										
										
										
											2023-10-27 17:00:13 +08:00
										 |  |  |   final VoidCallback onSelect; | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  |   final Peer peer; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const AutocompletePeerTile({ | 
					
						
							|  |  |  |     Key? key, | 
					
						
							| 
									
										
										
										
											2023-10-27 17:00:13 +08:00
										 |  |  |     required this.onSelect, | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  |     required this.peer, | 
					
						
							|  |  |  |   }) : super(key: key); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							| 
									
										
										
										
											2024-02-12 21:39:19 +08:00
										 |  |  |   AutocompletePeerTileState createState() => AutocompletePeerTileState(); | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-12 21:39:19 +08:00
										 |  |  | class AutocompletePeerTileState extends State<AutocompletePeerTile> { | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  |   List _frontN<T>(List list, int n) { | 
					
						
							|  |  |  |     if (list.length <= n) { | 
					
						
							|  |  |  |       return list; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return list.sublist(0, n); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  |   @override | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |   Widget build(BuildContext context) { | 
					
						
							| 
									
										
										
										
											2024-02-12 21:39:19 +08:00
										 |  |  |     final double tileRadius = 5; | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |     final name = | 
					
						
							|  |  |  |         '${widget.peer.username}${widget.peer.username.isNotEmpty && widget.peer.hostname.isNotEmpty ? '@' : ''}${widget.peer.hostname}'; | 
					
						
							|  |  |  |     final greyStyle = TextStyle( | 
					
						
							|  |  |  |         fontSize: 11, | 
					
						
							|  |  |  |         color: Theme.of(context).textTheme.titleLarge?.color?.withOpacity(0.6)); | 
					
						
							|  |  |  |     final child = GestureDetector( | 
					
						
							|  |  |  |         onTap: () => widget.onSelect(), | 
					
						
							|  |  |  |         child: Padding( | 
					
						
							|  |  |  |             padding: EdgeInsets.only(left: 5, right: 5), | 
					
						
							|  |  |  |             child: Container( | 
					
						
							|  |  |  |                 height: 42, | 
					
						
							|  |  |  |                 margin: EdgeInsets.only(bottom: 5), | 
					
						
							|  |  |  |                 child: Row( | 
					
						
							|  |  |  |                   mainAxisSize: MainAxisSize.max, | 
					
						
							|  |  |  |                   children: [ | 
					
						
							|  |  |  |                     Container( | 
					
						
							|  |  |  |                         decoration: BoxDecoration( | 
					
						
							|  |  |  |                           color: str2color( | 
					
						
							|  |  |  |                               '${widget.peer.id}${widget.peer.platform}', 0x7f), | 
					
						
							|  |  |  |                           borderRadius: BorderRadius.only( | 
					
						
							| 
									
										
										
										
											2024-02-12 21:39:19 +08:00
										 |  |  |                             topLeft: Radius.circular(tileRadius), | 
					
						
							|  |  |  |                             bottomLeft: Radius.circular(tileRadius), | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |                           ), | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  |                         ), | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |                         alignment: Alignment.center, | 
					
						
							|  |  |  |                         width: 42, | 
					
						
							|  |  |  |                         height: null, | 
					
						
							|  |  |  |                         child: Padding( | 
					
						
							|  |  |  |                             padding: EdgeInsets.all(6), | 
					
						
							|  |  |  |                             child: getPlatformImage(widget.peer.platform, | 
					
						
							|  |  |  |                                 size: 30))), | 
					
						
							|  |  |  |                     Expanded( | 
					
						
							|  |  |  |                       child: Container( | 
					
						
							|  |  |  |                           padding: EdgeInsets.only(left: 10), | 
					
						
							|  |  |  |                           decoration: BoxDecoration( | 
					
						
							|  |  |  |                             color: Theme.of(context).colorScheme.background, | 
					
						
							|  |  |  |                             borderRadius: BorderRadius.only( | 
					
						
							| 
									
										
										
										
											2024-02-12 21:39:19 +08:00
										 |  |  |                               topRight: Radius.circular(tileRadius), | 
					
						
							|  |  |  |                               bottomRight: Radius.circular(tileRadius), | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |                             ), | 
					
						
							|  |  |  |                           ), | 
					
						
							|  |  |  |                           child: Row( | 
					
						
							|  |  |  |                             children: [ | 
					
						
							|  |  |  |                               Expanded( | 
					
						
							|  |  |  |                                   child: Container( | 
					
						
							|  |  |  |                                       margin: EdgeInsets.only(top: 2), | 
					
						
							|  |  |  |                                       child: Container( | 
					
						
							|  |  |  |                                           margin: EdgeInsets.only(top: 2), | 
					
						
							|  |  |  |                                           child: Column( | 
					
						
							|  |  |  |                                             children: [ | 
					
						
							|  |  |  |                                               Container( | 
					
						
							|  |  |  |                                                   margin: | 
					
						
							|  |  |  |                                                       EdgeInsets.only(top: 2), | 
					
						
							|  |  |  |                                                   child: Row(children: [ | 
					
						
							|  |  |  |                                                     getOnline( | 
					
						
							|  |  |  |                                                         8, widget.peer.online), | 
					
						
							|  |  |  |                                                     Expanded( | 
					
						
							|  |  |  |                                                         child: Text( | 
					
						
							|  |  |  |                                                       widget.peer.alias.isEmpty | 
					
						
							|  |  |  |                                                           ? formatID( | 
					
						
							|  |  |  |                                                               widget.peer.id) | 
					
						
							|  |  |  |                                                           : widget.peer.alias, | 
					
						
							|  |  |  |                                                       overflow: | 
					
						
							|  |  |  |                                                           TextOverflow.ellipsis, | 
					
						
							|  |  |  |                                                       style: Theme.of(context) | 
					
						
							|  |  |  |                                                           .textTheme | 
					
						
							|  |  |  |                                                           .titleSmall, | 
					
						
							|  |  |  |                                                     )), | 
					
						
							| 
									
										
										
										
											2024-02-12 21:39:19 +08:00
										 |  |  |                                                     widget.peer.alias.isNotEmpty | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |                                                         ? Padding( | 
					
						
							|  |  |  |                                                             padding: | 
					
						
							|  |  |  |                                                                 const EdgeInsets | 
					
						
							|  |  |  |                                                                     .only( | 
					
						
							|  |  |  |                                                                     left: 5, | 
					
						
							|  |  |  |                                                                     right: 5), | 
					
						
							|  |  |  |                                                             child: Text( | 
					
						
							|  |  |  |                                                               "(${widget.peer.id})", | 
					
						
							|  |  |  |                                                               style: greyStyle, | 
					
						
							|  |  |  |                                                               overflow: | 
					
						
							|  |  |  |                                                                   TextOverflow | 
					
						
							|  |  |  |                                                                       .ellipsis, | 
					
						
							|  |  |  |                                                             )) | 
					
						
							|  |  |  |                                                         : Container(), | 
					
						
							|  |  |  |                                                   ])), | 
					
						
							|  |  |  |                                               Align( | 
					
						
							|  |  |  |                                                 alignment: Alignment.centerLeft, | 
					
						
							|  |  |  |                                                 child: Text( | 
					
						
							|  |  |  |                                                   name, | 
					
						
							|  |  |  |                                                   style: greyStyle, | 
					
						
							|  |  |  |                                                   textAlign: TextAlign.start, | 
					
						
							|  |  |  |                                                   overflow: | 
					
						
							|  |  |  |                                                       TextOverflow.ellipsis, | 
					
						
							|  |  |  |                                                 ), | 
					
						
							|  |  |  |                                               ), | 
					
						
							|  |  |  |                                             ], | 
					
						
							|  |  |  |                                           )))), | 
					
						
							|  |  |  |                             ], | 
					
						
							|  |  |  |                           )), | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  |                   ], | 
					
						
							|  |  |  |                 )))); | 
					
						
							|  |  |  |     final colors = _frontN(widget.peer.tags, 25) | 
					
						
							| 
									
										
										
										
											2024-03-20 15:05:54 +08:00
										 |  |  |         .map((e) => gFFI.abModel.getCurrentAbTagColor(e)) | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  |         .toList(); | 
					
						
							| 
									
										
										
										
											2023-10-24 05:30:43 +05:30
										 |  |  |     return Tooltip( | 
					
						
							|  |  |  |       message: isMobile | 
					
						
							|  |  |  |           ? '' | 
					
						
							|  |  |  |           : widget.peer.tags.isNotEmpty | 
					
						
							|  |  |  |               ? '${translate('Tags')}: ${widget.peer.tags.join(', ')}' | 
					
						
							|  |  |  |               : '', | 
					
						
							|  |  |  |       child: Stack(children: [ | 
					
						
							|  |  |  |         child, | 
					
						
							|  |  |  |         if (colors.isNotEmpty) | 
					
						
							|  |  |  |           Positioned( | 
					
						
							|  |  |  |             top: 5, | 
					
						
							|  |  |  |             right: 10, | 
					
						
							|  |  |  |             child: CustomPaint( | 
					
						
							|  |  |  |               painter: TagPainter(radius: 3, colors: colors), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |       ]), | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-12-17 06:52:18 +05:30
										 |  |  | } |