[Android, iOS]: Initialization check, no disconnect on closing (#9)

* feat(android, dart): add check for initialization, show proper error messages

* feat(exception): add exception mapper for android and ios

* feat(ios): check initialization in swift

* fix(android): add initialization check for methods, remove disconnect() inside onDetachedFromEngine

* chore: code style, version, changelog
This commit is contained in:
Kseniia Nikitina
2024-08-09 21:22:58 +02:00
committed by GitHub
parent 906e87c6a9
commit a5c77f58e5
22 changed files with 306 additions and 102 deletions
+23 -16
View File
@@ -3,6 +3,7 @@ import 'package:flutter/services.dart';
import 'package:nearby_service/nearby_service.dart';
import 'nearby_service_platform_interface.dart';
import 'src/utils/result_handler.dart';
/// An implementation of [NearbyServicePlatform] that uses method channels.
class MethodChannelNearbyService extends NearbyServicePlatform {
@@ -11,22 +12,24 @@ class MethodChannelNearbyService extends NearbyServicePlatform {
final methodChannel = const MethodChannel('nearby_service');
@override
Future<String?> getPlatformVersion() {
return methodChannel.invokeMethod<String>('getPlatformVersion');
Future<String?> getPlatformVersion() async {
final result = await methodChannel.invokeMethod<String>(
'getPlatformVersion',
);
return ResultHandler.instance.handle<String?>(result);
}
@override
Future<String?> getPlatformModel() {
return methodChannel.invokeMethod<String>('getPlatformModel');
Future<String?> getPlatformModel() async {
final result = await methodChannel.invokeMethod<String>('getPlatformModel');
return ResultHandler.instance.handle<String?>(result);
}
@override
Future<NearbyDeviceInfo?> getCurrentDeviceInfo() async {
return NearbyDeviceMapper.instance
.mapToDevice(
await methodChannel.invokeMethod('getCurrentDevice'),
)
?.info;
final result = await methodChannel.invokeMethod('getCurrentDevice');
final updatedResult = ResultHandler.instance.handle(result);
return NearbyDeviceMapper.instance.mapToDevice(updatedResult)?.info;
}
@override
@@ -36,16 +39,17 @@ class MethodChannelNearbyService extends NearbyServicePlatform {
@override
Future<List<NearbyDevice>> getPeers() async {
return NearbyDeviceMapper.instance.mapToDeviceList(
await methodChannel.invokeMethod('getPeers'),
);
final result = await methodChannel.invokeMethod('getPeers');
final updatedResult = ResultHandler.instance.handle(result);
return NearbyDeviceMapper.instance.mapToDeviceList(updatedResult);
}
@override
Stream<List<NearbyDevice>> getPeersStream() {
const peersChannel = EventChannel("nearby_service_peers");
return peersChannel.receiveBroadcastStream().map((e) {
return NearbyDeviceMapper.instance.mapToDeviceList(e);
final updatedResult = ResultHandler.instance.handle(e);
return NearbyDeviceMapper.instance.mapToDeviceList(updatedResult);
});
}
@@ -54,8 +58,11 @@ class MethodChannelNearbyService extends NearbyServicePlatform {
const connectedDeviceChannel = EventChannel(
"nearby_service_connected_device",
);
return connectedDeviceChannel.receiveBroadcastStream(device.info.id).map(
(e) => NearbyDeviceMapper.instance.mapToDevice(e),
);
return connectedDeviceChannel
.receiveBroadcastStream(device.info.id)
.map((e) {
final updatedResult = ResultHandler.instance.handle(e);
return NearbyDeviceMapper.instance.mapToDevice(updatedResult);
});
}
}