feat(example): establishing connection improvements

This commit is contained in:
ksenia312
2024-02-08 13:09:27 +01:00
parent 35119a98f4
commit 7d49169dea
3 changed files with 53 additions and 24 deletions
+11 -2
View File
@@ -7,21 +7,30 @@ class PeerWidget extends StatelessWidget {
required this.device, required this.device,
required this.isIosBrowser, required this.isIosBrowser,
required this.onConnect, required this.onConnect,
required this.communicationChannelState,
}); });
final NearbyDevice device; final NearbyDevice device;
final bool isIosBrowser; final bool isIosBrowser;
final ValueChanged<NearbyDevice> onConnect; final ValueChanged<NearbyDevice> onConnect;
final CommunicationChannelState communicationChannelState;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final connectedStateCaption = communicationChannelState.isLoading
? 'Wait for connection'
: 'Tap to chat';
final status = final status =
device.status.isConnected ? 'Tap to chat' : device.status.name; device.status.isConnected ? connectedStateCaption : device.status.name;
final canTap = !communicationChannelState.isLoading;
return ListTile( return ListTile(
title: Text( title: Text(
'${isIosBrowser ? 'Found device' : 'Pending invitation'} | ${device.info.displayName} | $status', '${isIosBrowser ? 'Found device' : 'Pending invitation'} | ${device.info.displayName} | $status',
), ),
onTap: () => onConnect(device), onTap: canTap ? () => onConnect(device) : null,
tileColor: Colors.blueAccent, tileColor: Colors.blueAccent,
textColor: Colors.white, textColor: Colors.white,
); );
+41 -22
View File
@@ -57,10 +57,11 @@ class AppBody extends StatefulWidget {
class _AppBodyState extends State<AppBody> { class _AppBodyState extends State<AppBody> {
/// Our service /// Our service
final _nearbyService = NearbyService.getInstance( late final _nearbyService = NearbyService.getInstance(
/// Define log level here /// Define log level here
logLevel: NearbyServiceLogLevel.debug, logLevel: NearbyServiceLogLevel.debug,
); )..communicationChannelState.addListener(() => setState(() {}));
AppState _state = AppState.idle; AppState _state = AppState.idle;
/// Browser OR Advertiser for IOS /// Browser OR Advertiser for IOS
@@ -116,6 +117,7 @@ class _AppBodyState extends State<AppBody> {
device: e, device: e,
isIosBrowser: _isIosBrowser, isIosBrowser: _isIosBrowser,
onConnect: _connect, onConnect: _connect,
communicationChannelState: _communicationChannelState,
), ),
), ),
], ],
@@ -155,6 +157,10 @@ class _AppBodyState extends State<AppBody> {
return Container(); return Container();
} }
CommunicationChannelState get _communicationChannelState {
return _nearbyService.communicationChannelState.value;
}
Future<void> _initialize() async { Future<void> _initialize() async {
await _nearbyService.initialize(); await _nearbyService.initialize();
} }
@@ -199,30 +205,42 @@ class _AppBodyState extends State<AppBody> {
// double connection may be unnecessary // double connection may be unnecessary
final result = await _nearbyService.connect(device); final result = await _nearbyService.connect(device);
if (result || device.status.isConnected) { if (result || device.status.isConnected) {
_connectionCheckTimer = Timer.periodic( final channelStarting = _tryCommunicate(device);
const Duration(seconds: 3), if (!channelStarting) {
(_) { _connectionCheckTimer = Timer.periodic(
NearbyDevice? selectedDevice; const Duration(seconds: 3),
try { (_) => _tryCommunicate(device),
selectedDevice = _peers.firstWhere( );
(element) => element.info.id == device.info.id, }
);
} finally {}
if (selectedDevice.status.isConnected) {
try {
_startCommunicationChannel(device);
} finally {
_connectionCheckTimer?.cancel();
_connectionCheckTimer = null;
}
}
},
);
} }
} }
bool _tryCommunicate(NearbyDevice device) {
NearbyDevice? selectedDevice;
try {
selectedDevice = _peers.firstWhere(
(element) => element.info.id == device.info.id,
);
} catch (_) {
return false;
}
if (selectedDevice.status.isConnected) {
try {
_startCommunicationChannel(device);
} finally {
_connectionCheckTimer?.cancel();
_connectionCheckTimer = null;
}
return true;
}
return false;
}
void _startCommunicationChannel(NearbyDevice device) { void _startCommunicationChannel(NearbyDevice device) {
if (!_communicationChannelState.isNotConnected) return;
_nearbyService.startCommunicationChannel( _nearbyService.startCommunicationChannel(
NearbyCommunicationChannelData( NearbyCommunicationChannelData(
device.info.id, device.info.id,
@@ -260,6 +278,7 @@ class _AppBodyState extends State<AppBody> {
await _nearbyService.stopDiscovery(); await _nearbyService.stopDiscovery();
await _peersSubscription?.cancel(); await _peersSubscription?.cancel();
setState(() { setState(() {
_peers = [];
_state = AppState.idle; _state = AppState.idle;
}); });
} }
+1
View File
@@ -24,6 +24,7 @@ class AppSnackBar {
SnackBar( SnackBar(
content: content, content: content,
action: action, action: action,
duration: Duration(seconds: action != null ? 10 : 2),
), ),
); );
return messenger.closed.then((value) => null); return messenger.closed.then((value) => null);