[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
**!! BREAKING CHANGES !!**
@@ -170,7 +170,7 @@ class NearbyAndroidService extends NearbyService {
@override
Stream<CommunicationChannelState> getCommunicationChannelStateStream() {
return _socketService.stateController.stream.asBroadcastStream();
return _socketService.state.broadcastStream;
}
void _requireAndroidDevice(NearbyDevice device) {
@@ -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<CommunicationChannelState>.broadcast()
..add(_state.value)
..stream.asBroadcastStream().listen((e) => _state.value = e);
late final state = NearbyServiceListenable<CommunicationChannelState>(
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<CommunicationChannelState> get communicationChannelState =>
_state;
state.notifier;
///
/// Start a socket with the user's role defined.
@@ -63,7 +61,7 @@ class NearbySocketService {
Future<bool> 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);
}
}
+18 -26
View File
@@ -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<bool>(true);
final _isBrowser = NearbyServiceListenable<bool>(initialValue: true);
final _communicationChannelState =
ValueNotifier(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);
NearbyServiceListenable<CommunicationChannelState>(
initialValue: CommunicationChannelState.notConnected,
);
StreamSubscription? _messagesSubscription;
StreamSubscription? _resourcesSubscription;
@@ -40,13 +33,13 @@ class NearbyIOSService extends NearbyService {
'Use getCommunicationChannelStateStream or communicationChannelStateValue instead',
)
ValueListenable<CommunicationChannelState> get communicationChannelState =>
_communicationChannelState;
_communicationChannelState.notifier;
///
/// Determines whether the current device is a **Browser** or **Advertiser**.
///
@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**.
@@ -62,8 +55,7 @@ class NearbyIOSService extends NearbyService {
/// status that have sent it a connection request.
/// Advertiser accepts or rejects connection requests.
///
Stream<bool> getIsBrowserStream() =>
_isBrowserController.stream.asBroadcastStream();
Stream<bool> 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<CommunicationChannelState> 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(
+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
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