diff --git a/CHANGELOG.md b/CHANGELOG.md index 557efef..aafcda2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.1.1 + +- Fix issue https://github.com/ksenia312/nearby_service/issues/16 + ## 0.1.0 **!! BREAKING CHANGES !!** diff --git a/lib/src/platforms/android/nearby_android_service.dart b/lib/src/platforms/android/nearby_android_service.dart index db16f40..c7e7fb4 100644 --- a/lib/src/platforms/android/nearby_android_service.dart +++ b/lib/src/platforms/android/nearby_android_service.dart @@ -170,7 +170,7 @@ class NearbyAndroidService extends NearbyService { @override Stream getCommunicationChannelStateStream() { - return _socketService.stateController.stream.asBroadcastStream(); + return _socketService.state.broadcastStream; } void _requireAndroidDevice(NearbyDevice device) { diff --git a/lib/src/platforms/android/socket_service/nearby_socket_service.dart b/lib/src/platforms/android/socket_service/nearby_socket_service.dart index 406c4ae..283eb94 100644 --- a/lib/src/platforms/android/socket_service/nearby_socket_service.dart +++ b/lib/src/platforms/android/socket_service/nearby_socket_service.dart @@ -5,6 +5,7 @@ import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:nearby_service/nearby_service.dart'; import 'package:nearby_service/src/utils/file_socket.dart'; +import 'package:nearby_service/src/utils/listenable.dart'; import 'package:nearby_service/src/utils/logger.dart'; import 'package:nearby_service/src/utils/random.dart'; import 'package:nearby_service/src/utils/stream_mapper.dart'; @@ -30,10 +31,9 @@ class NearbySocketService { _pingManager, ); - late final stateController = - StreamController.broadcast() - ..add(_state.value) - ..stream.asBroadcastStream().listen((e) => _state.value = e); + late final state = NearbyServiceListenable( + initialValue: CommunicationChannelState.notConnected, + ); NearbyAndroidCommunicationChannelData _androidData = const NearbyAndroidCommunicationChannelData(); @@ -43,12 +43,10 @@ class NearbySocketService { HttpServer? _server; StreamSubscription? _messagesSubscription; - final _state = ValueNotifier(CommunicationChannelState.notConnected); - - CommunicationChannelState get communicationChannelStateValue => _state.value; + CommunicationChannelState get communicationChannelStateValue => state.value; ValueListenable get communicationChannelState => - _state; + state.notifier; /// /// Start a socket with the user's role defined. @@ -63,7 +61,7 @@ class NearbySocketService { Future startSocket({ required NearbyCommunicationChannelData data, }) async { - stateController.add(CommunicationChannelState.loading); + state.add(CommunicationChannelState.loading); _androidData = data.androidData; _connectedDeviceId = data.connectedDeviceId; @@ -133,7 +131,7 @@ class NearbySocketService { _server = null; _connectedDeviceId = null; - stateController.add(CommunicationChannelState.notConnected); + state.add(CommunicationChannelState.notConnected); return true; } catch (e) { return false; @@ -144,7 +142,7 @@ class NearbySocketService { required NearbyServiceMessagesListener socketListener, required NearbyConnectionAndroidInfo info, }) async { - if (_state.value.isLoading) { + if (state.value.isLoading) { final response = await _network.pingServer( address: info.ownerIpAddress, port: _androidData.port, @@ -226,23 +224,23 @@ class NearbySocketService { } }, onDone: () { - stateController.add(CommunicationChannelState.notConnected); + state.add(CommunicationChannelState.notConnected); socketListener.onDone?.call(); }, onError: (e, s) { Logger.error(e); - stateController.add(CommunicationChannelState.notConnected); + state.add(CommunicationChannelState.notConnected); socketListener.onError?.call(e, s); }, cancelOnError: socketListener.cancelOnError, ); } if (_messagesSubscription != null) { - stateController.add(CommunicationChannelState.connected); + state.add(CommunicationChannelState.connected); Logger.info('Socket subscription was created successfully'); socketListener.onCreated?.call(); } else { - stateController.add(CommunicationChannelState.notConnected); + state.add(CommunicationChannelState.notConnected); } } diff --git a/lib/src/platforms/ios/nearby_ios_service.dart b/lib/src/platforms/ios/nearby_ios_service.dart index c773264..8b35a5d 100644 --- a/lib/src/platforms/ios/nearby_ios_service.dart +++ b/lib/src/platforms/ios/nearby_ios_service.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:nearby_service/nearby_service.dart'; +import 'package:nearby_service/src/utils/listenable.dart'; import 'package:nearby_service/src/utils/logger.dart'; import 'package:nearby_service/src/utils/stream_mapper.dart'; @@ -13,20 +14,12 @@ import 'package:nearby_service/src/utils/stream_mapper.dart'; /// device by identifier. /// class NearbyIOSService extends NearbyService { - final _isBrowser = ValueNotifier(true); + final _isBrowser = NearbyServiceListenable(initialValue: true); + final _communicationChannelState = - ValueNotifier(CommunicationChannelState.notConnected); - - late final _isBrowserController = StreamController.broadcast() - ..add(_isBrowser.value) - ..stream.asBroadcastStream().listen((e) => _isBrowser.value = e); - - late final _stateController = - StreamController.broadcast() - ..add(_communicationChannelState.value) - ..stream - .asBroadcastStream() - .listen((e) => _communicationChannelState.value = e); + NearbyServiceListenable( + initialValue: CommunicationChannelState.notConnected, + ); StreamSubscription? _messagesSubscription; StreamSubscription? _resourcesSubscription; @@ -40,13 +33,13 @@ class NearbyIOSService extends NearbyService { 'Use getCommunicationChannelStateStream or communicationChannelStateValue instead', ) ValueListenable get communicationChannelState => - _communicationChannelState; + _communicationChannelState.notifier; /// /// Determines whether the current device is a **Browser** or **Advertiser**. /// @Deprecated('Use getIsBrowserStream or isBrowserValue instead') - ValueListenable get isBrowser => _isBrowser; + ValueListenable get isBrowser => _isBrowser.notifier; /// /// Determines whether the current device is a **Browser** or **Advertiser**. @@ -62,8 +55,7 @@ class NearbyIOSService extends NearbyService { /// status that have sent it a connection request. /// Advertiser accepts or rejects connection requests. /// - Stream getIsBrowserStream() => - _isBrowserController.stream.asBroadcastStream(); + Stream getIsBrowserStream() => _isBrowser.broadcastStream; /// /// Initializes [MCNearbyServiceAdvertiser](https://developer.apple.com/documentation/multipeerconnectivity/mcnearbyserviceadvertiser) @@ -209,7 +201,7 @@ class NearbyIOSService extends NearbyService { NearbyCommunicationChannelData data, ) async { Logger.debug('Creating messages subscription'); - _stateController.add(CommunicationChannelState.loading); + _communicationChannelState.add(CommunicationChannelState.loading); await endCommunicationChannel(); final eventListener = data.messagesListener; @@ -222,12 +214,12 @@ class NearbyIOSService extends NearbyService { .listen( eventListener.onData, onDone: () { - _stateController.add(CommunicationChannelState.notConnected); + _communicationChannelState.add(CommunicationChannelState.notConnected); eventListener.onDone?.call(); }, onError: (e, s) { Logger.error(e); - _stateController.add(CommunicationChannelState.notConnected); + _communicationChannelState.add(CommunicationChannelState.notConnected); eventListener.onError?.call(e, s); }, cancelOnError: eventListener.cancelOnError, @@ -241,7 +233,7 @@ class NearbyIOSService extends NearbyService { onDone: filesListener?.onDone, onError: (e, s) { Logger.error(e); - _stateController.add(CommunicationChannelState.notConnected); + _communicationChannelState.add(CommunicationChannelState.notConnected); filesListener?.onError?.call(e, s); }, cancelOnError: filesListener?.cancelOnError, @@ -249,9 +241,9 @@ class NearbyIOSService extends NearbyService { if (_messagesSubscription != null) { Logger.info('Messages subscription was created successfully'); eventListener.onCreated?.call(); - _stateController.add(CommunicationChannelState.connected); + _communicationChannelState.add(CommunicationChannelState.connected); } else { - _stateController.add(CommunicationChannelState.notConnected); + _communicationChannelState.add(CommunicationChannelState.notConnected); } if (_resourcesSubscription != null) { Logger.info('Resources subscription was created successfully'); @@ -270,7 +262,7 @@ class NearbyIOSService extends NearbyService { await _resourcesSubscription?.cancel(); _messagesSubscription = null; _resourcesSubscription = null; - _stateController.add(CommunicationChannelState.notConnected); + _communicationChannelState.add(CommunicationChannelState.notConnected); Logger.debug('Communication channel was cancelled'); return true; } @@ -289,7 +281,7 @@ class NearbyIOSService extends NearbyService { @override Stream getCommunicationChannelStateStream() { - return _stateController.stream.asBroadcastStream(); + return _communicationChannelState.broadcastStream; } /// @@ -309,7 +301,7 @@ class NearbyIOSService extends NearbyService { /// void setIsBrowser({required bool value}) { Logger.debug('Is Browser Value was set to $value'); - _isBrowserController.add(value); + _isBrowser.add(value); } void _logResult( diff --git a/lib/src/utils/listenable.dart b/lib/src/utils/listenable.dart new file mode 100644 index 0000000..fae3dec --- /dev/null +++ b/lib/src/utils/listenable.dart @@ -0,0 +1,21 @@ +import 'dart:async'; + +import 'package:flutter/foundation.dart'; + +class NearbyServiceListenable { + NearbyServiceListenable({required this.initialValue}); + + final T initialValue; + late final ValueNotifier notifier = ValueNotifier(initialValue); + late final StreamController _controller = StreamController.broadcast() + ..add(notifier.value); + + T get value => notifier.value; + + Stream get broadcastStream => _controller.stream.asBroadcastStream(); + + void add(T value) { + notifier.value = value; + _controller.add(value); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 3eac8e3..6a64f83 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: nearby_service description: Nearby Service Flutter Plugin is used to create connections in a P2P network. Supports sending text messages and files. -version: 0.1.0 +version: 0.1.1 homepage: https://github.com/ksenia312/nearby_service repository: https://github.com/ksenia312/nearby_service