[iOS, Android]: Use NearbyServiceListenable for stream controllers and their values (#17)

* fix: use NearbyServiceListenable for stream controllers and their values

* bump: 0.1.1, changelog
This commit is contained in:
Kseniia Nikitina
2024-10-13 16:17:11 +02:00
committed by GitHub
parent 7049c39cf0
commit 897997e770
6 changed files with 58 additions and 43 deletions
+4
View File
@@ -1,3 +1,7 @@
## 0.1.1
- Fix issue https://github.com/ksenia312/nearby_service/issues/16
## 0.1.0 ## 0.1.0
**!! BREAKING CHANGES !!** **!! BREAKING CHANGES !!**
@@ -170,7 +170,7 @@ class NearbyAndroidService extends NearbyService {
@override @override
Stream<CommunicationChannelState> getCommunicationChannelStateStream() { Stream<CommunicationChannelState> getCommunicationChannelStateStream() {
return _socketService.stateController.stream.asBroadcastStream(); return _socketService.state.broadcastStream;
} }
void _requireAndroidDevice(NearbyDevice device) { void _requireAndroidDevice(NearbyDevice device) {
@@ -5,6 +5,7 @@ import 'dart:io';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:nearby_service/nearby_service.dart'; import 'package:nearby_service/nearby_service.dart';
import 'package:nearby_service/src/utils/file_socket.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/logger.dart';
import 'package:nearby_service/src/utils/random.dart'; import 'package:nearby_service/src/utils/random.dart';
import 'package:nearby_service/src/utils/stream_mapper.dart'; import 'package:nearby_service/src/utils/stream_mapper.dart';
@@ -30,10 +31,9 @@ class NearbySocketService {
_pingManager, _pingManager,
); );
late final stateController = late final state = NearbyServiceListenable<CommunicationChannelState>(
StreamController<CommunicationChannelState>.broadcast() initialValue: CommunicationChannelState.notConnected,
..add(_state.value) );
..stream.asBroadcastStream().listen((e) => _state.value = e);
NearbyAndroidCommunicationChannelData _androidData = NearbyAndroidCommunicationChannelData _androidData =
const NearbyAndroidCommunicationChannelData(); const NearbyAndroidCommunicationChannelData();
@@ -43,12 +43,10 @@ class NearbySocketService {
HttpServer? _server; HttpServer? _server;
StreamSubscription? _messagesSubscription; StreamSubscription? _messagesSubscription;
final _state = ValueNotifier(CommunicationChannelState.notConnected); CommunicationChannelState get communicationChannelStateValue => state.value;
CommunicationChannelState get communicationChannelStateValue => _state.value;
ValueListenable<CommunicationChannelState> get communicationChannelState => ValueListenable<CommunicationChannelState> get communicationChannelState =>
_state; state.notifier;
/// ///
/// Start a socket with the user's role defined. /// Start a socket with the user's role defined.
@@ -63,7 +61,7 @@ class NearbySocketService {
Future<bool> startSocket({ Future<bool> startSocket({
required NearbyCommunicationChannelData data, required NearbyCommunicationChannelData data,
}) async { }) async {
stateController.add(CommunicationChannelState.loading); state.add(CommunicationChannelState.loading);
_androidData = data.androidData; _androidData = data.androidData;
_connectedDeviceId = data.connectedDeviceId; _connectedDeviceId = data.connectedDeviceId;
@@ -133,7 +131,7 @@ class NearbySocketService {
_server = null; _server = null;
_connectedDeviceId = null; _connectedDeviceId = null;
stateController.add(CommunicationChannelState.notConnected); state.add(CommunicationChannelState.notConnected);
return true; return true;
} catch (e) { } catch (e) {
return false; return false;
@@ -144,7 +142,7 @@ class NearbySocketService {
required NearbyServiceMessagesListener socketListener, required NearbyServiceMessagesListener socketListener,
required NearbyConnectionAndroidInfo info, required NearbyConnectionAndroidInfo info,
}) async { }) async {
if (_state.value.isLoading) { if (state.value.isLoading) {
final response = await _network.pingServer( final response = await _network.pingServer(
address: info.ownerIpAddress, address: info.ownerIpAddress,
port: _androidData.port, port: _androidData.port,
@@ -226,23 +224,23 @@ class NearbySocketService {
} }
}, },
onDone: () { onDone: () {
stateController.add(CommunicationChannelState.notConnected); state.add(CommunicationChannelState.notConnected);
socketListener.onDone?.call(); socketListener.onDone?.call();
}, },
onError: (e, s) { onError: (e, s) {
Logger.error(e); Logger.error(e);
stateController.add(CommunicationChannelState.notConnected); state.add(CommunicationChannelState.notConnected);
socketListener.onError?.call(e, s); socketListener.onError?.call(e, s);
}, },
cancelOnError: socketListener.cancelOnError, cancelOnError: socketListener.cancelOnError,
); );
} }
if (_messagesSubscription != null) { if (_messagesSubscription != null) {
stateController.add(CommunicationChannelState.connected); state.add(CommunicationChannelState.connected);
Logger.info('Socket subscription was created successfully'); Logger.info('Socket subscription was created successfully');
socketListener.onCreated?.call(); socketListener.onCreated?.call();
} else { } else {
stateController.add(CommunicationChannelState.notConnected); state.add(CommunicationChannelState.notConnected);
} }
} }
+18 -26
View File
@@ -2,6 +2,7 @@ import 'dart:async';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:nearby_service/nearby_service.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/logger.dart';
import 'package:nearby_service/src/utils/stream_mapper.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. /// device by identifier.
/// ///
class NearbyIOSService extends NearbyService { class NearbyIOSService extends NearbyService {
final _isBrowser = ValueNotifier<bool>(true); final _isBrowser = NearbyServiceListenable<bool>(initialValue: true);
final _communicationChannelState = final _communicationChannelState =
ValueNotifier(CommunicationChannelState.notConnected); NearbyServiceListenable<CommunicationChannelState>(
initialValue: CommunicationChannelState.notConnected,
late final _isBrowserController = StreamController<bool>.broadcast() );
..add(_isBrowser.value)
..stream.asBroadcastStream().listen((e) => _isBrowser.value = e);
late final _stateController =
StreamController<CommunicationChannelState>.broadcast()
..add(_communicationChannelState.value)
..stream
.asBroadcastStream()
.listen((e) => _communicationChannelState.value = e);
StreamSubscription? _messagesSubscription; StreamSubscription? _messagesSubscription;
StreamSubscription? _resourcesSubscription; StreamSubscription? _resourcesSubscription;
@@ -40,13 +33,13 @@ class NearbyIOSService extends NearbyService {
'Use getCommunicationChannelStateStream or communicationChannelStateValue instead', 'Use getCommunicationChannelStateStream or communicationChannelStateValue instead',
) )
ValueListenable<CommunicationChannelState> get communicationChannelState => ValueListenable<CommunicationChannelState> get communicationChannelState =>
_communicationChannelState; _communicationChannelState.notifier;
/// ///
/// Determines whether the current device is a **Browser** or **Advertiser**. /// Determines whether the current device is a **Browser** or **Advertiser**.
/// ///
@Deprecated('Use getIsBrowserStream or isBrowserValue instead') @Deprecated('Use getIsBrowserStream or isBrowserValue instead')
ValueListenable<bool> get isBrowser => _isBrowser; ValueListenable<bool> get isBrowser => _isBrowser.notifier;
/// ///
/// Determines whether the current device is a **Browser** or **Advertiser**. /// 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. /// status that have sent it a connection request.
/// Advertiser accepts or rejects connection requests. /// Advertiser accepts or rejects connection requests.
/// ///
Stream<bool> getIsBrowserStream() => Stream<bool> getIsBrowserStream() => _isBrowser.broadcastStream;
_isBrowserController.stream.asBroadcastStream();
/// ///
/// Initializes [MCNearbyServiceAdvertiser](https://developer.apple.com/documentation/multipeerconnectivity/mcnearbyserviceadvertiser) /// Initializes [MCNearbyServiceAdvertiser](https://developer.apple.com/documentation/multipeerconnectivity/mcnearbyserviceadvertiser)
@@ -209,7 +201,7 @@ class NearbyIOSService extends NearbyService {
NearbyCommunicationChannelData data, NearbyCommunicationChannelData data,
) async { ) async {
Logger.debug('Creating messages subscription'); Logger.debug('Creating messages subscription');
_stateController.add(CommunicationChannelState.loading); _communicationChannelState.add(CommunicationChannelState.loading);
await endCommunicationChannel(); await endCommunicationChannel();
final eventListener = data.messagesListener; final eventListener = data.messagesListener;
@@ -222,12 +214,12 @@ class NearbyIOSService extends NearbyService {
.listen( .listen(
eventListener.onData, eventListener.onData,
onDone: () { onDone: () {
_stateController.add(CommunicationChannelState.notConnected); _communicationChannelState.add(CommunicationChannelState.notConnected);
eventListener.onDone?.call(); eventListener.onDone?.call();
}, },
onError: (e, s) { onError: (e, s) {
Logger.error(e); Logger.error(e);
_stateController.add(CommunicationChannelState.notConnected); _communicationChannelState.add(CommunicationChannelState.notConnected);
eventListener.onError?.call(e, s); eventListener.onError?.call(e, s);
}, },
cancelOnError: eventListener.cancelOnError, cancelOnError: eventListener.cancelOnError,
@@ -241,7 +233,7 @@ class NearbyIOSService extends NearbyService {
onDone: filesListener?.onDone, onDone: filesListener?.onDone,
onError: (e, s) { onError: (e, s) {
Logger.error(e); Logger.error(e);
_stateController.add(CommunicationChannelState.notConnected); _communicationChannelState.add(CommunicationChannelState.notConnected);
filesListener?.onError?.call(e, s); filesListener?.onError?.call(e, s);
}, },
cancelOnError: filesListener?.cancelOnError, cancelOnError: filesListener?.cancelOnError,
@@ -249,9 +241,9 @@ class NearbyIOSService extends NearbyService {
if (_messagesSubscription != null) { if (_messagesSubscription != null) {
Logger.info('Messages subscription was created successfully'); Logger.info('Messages subscription was created successfully');
eventListener.onCreated?.call(); eventListener.onCreated?.call();
_stateController.add(CommunicationChannelState.connected); _communicationChannelState.add(CommunicationChannelState.connected);
} else { } else {
_stateController.add(CommunicationChannelState.notConnected); _communicationChannelState.add(CommunicationChannelState.notConnected);
} }
if (_resourcesSubscription != null) { if (_resourcesSubscription != null) {
Logger.info('Resources subscription was created successfully'); Logger.info('Resources subscription was created successfully');
@@ -270,7 +262,7 @@ class NearbyIOSService extends NearbyService {
await _resourcesSubscription?.cancel(); await _resourcesSubscription?.cancel();
_messagesSubscription = null; _messagesSubscription = null;
_resourcesSubscription = null; _resourcesSubscription = null;
_stateController.add(CommunicationChannelState.notConnected); _communicationChannelState.add(CommunicationChannelState.notConnected);
Logger.debug('Communication channel was cancelled'); Logger.debug('Communication channel was cancelled');
return true; return true;
} }
@@ -289,7 +281,7 @@ class NearbyIOSService extends NearbyService {
@override @override
Stream<CommunicationChannelState> getCommunicationChannelStateStream() { Stream<CommunicationChannelState> getCommunicationChannelStateStream() {
return _stateController.stream.asBroadcastStream(); return _communicationChannelState.broadcastStream;
} }
/// ///
@@ -309,7 +301,7 @@ class NearbyIOSService extends NearbyService {
/// ///
void setIsBrowser({required bool value}) { void setIsBrowser({required bool value}) {
Logger.debug('Is Browser Value was set to $value'); Logger.debug('Is Browser Value was set to $value');
_isBrowserController.add(value); _isBrowser.add(value);
} }
void _logResult( void _logResult(
+21
View File
@@ -0,0 +1,21 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
class NearbyServiceListenable<T> {
NearbyServiceListenable({required this.initialValue});
final T initialValue;
late final ValueNotifier<T> notifier = ValueNotifier<T>(initialValue);
late final StreamController<T> _controller = StreamController<T>.broadcast()
..add(notifier.value);
T get value => notifier.value;
Stream<T> get broadcastStream => _controller.stream.asBroadcastStream();
void add(T value) {
notifier.value = value;
_controller.add(value);
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
name: nearby_service name: nearby_service
description: Nearby Service Flutter Plugin is used to create connections in a P2P network. Supports sending text messages and files. 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 homepage: https://github.com/ksenia312/nearby_service
repository: https://github.com/ksenia312/nearby_service repository: https://github.com/ksenia312/nearby_service