Files
nearby_service/lib/nearby_service_method_channel.dart
Kseniia Nikitina 7049c39cf0 BREAKING CHANGE: Update API for connection, communication channel state and is browser value (#14)
* refactor(lib, example, example_full): use streams for communication channel and is ios browser

* feat: add abstract toJson() to message

* refactor: add deprecations for nearby_service

* feat(example): update main.dart

* feat(nearby_service): add comments

* feat(nearby_service_platform_interface): add deprecation to getConnectedDeviceStream

* feat(nearby_service/message): add concrete implementation for toJson()

* feat(nearby_service/android): add deprecations

* fix(nearby_service/android): reset check for android device

* fix(nearby_service/ios): change ios service to deprecations variant

* chore(nearby_service): edit deprecation messages

* chore: update gitignore

* doc: add CONTRIBUTING file

* doc: update CONTRIBUTING file

* chore: update CONTRIBUTING.md

* chore: update CONTRIBUTING.md

* chore: version 0.1.0

* fix(example_full): move startListeningCommunicationChannelState() upper
2024-08-18 15:42:53 +02:00

73 lines
2.5 KiB
Dart

import 'package:flutter/foundation.dart';
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 {
/// The method channel used to interact with the native platform.
@visibleForTesting
final methodChannel = const MethodChannel('nearby_service');
@override
Future<String?> getPlatformVersion() async {
final result = await methodChannel.invokeMethod<String>(
'getPlatformVersion',
);
return ResultHandler.instance.handle<String?>(result);
}
@override
Future<String?> getPlatformModel() async {
final result = await methodChannel.invokeMethod<String>('getPlatformModel');
return ResultHandler.instance.handle<String?>(result);
}
@override
Future<NearbyDeviceInfo?> getCurrentDeviceInfo() async {
final result = await methodChannel.invokeMethod('getCurrentDevice');
final updatedResult = ResultHandler.instance.handle(result);
return NearbyDeviceMapper.instance.mapToDevice(updatedResult)?.info;
}
@override
Future<void> openServicesSettings() async {
await methodChannel.invokeMethod<bool>('openServicesSettings');
}
@override
Future<List<NearbyDevice>> getPeers() async {
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) {
final updatedResult = ResultHandler.instance.handle(e);
return NearbyDeviceMapper.instance.mapToDeviceList(updatedResult);
});
}
@override
@Deprecated('Use getConnectedDeviceStreamById instead')
Stream<NearbyDevice?> getConnectedDeviceStream(NearbyDevice device) {
return getConnectedDeviceStreamById(device.info.id);
}
@override
Stream<NearbyDevice?> getConnectedDeviceStreamById(String deviceId) {
const connectedDeviceChannel = EventChannel(
"nearby_service_connected_device",
);
return connectedDeviceChannel.receiveBroadcastStream(deviceId).map((e) {
final updatedResult = ResultHandler.instance.handle(e);
return NearbyDeviceMapper.instance.mapToDevice(updatedResult);
});
}
}