chore: rename base classes, comments, formatting

This commit is contained in:
ksenia312
2024-02-05 13:25:36 +01:00
parent 4c2360bb27
commit 258cdf0adc
18 changed files with 103 additions and 96 deletions
+7 -7
View File
@@ -194,8 +194,8 @@ class AppService extends ChangeNotifier {
..communicationChannelState.addListener(notifyListeners);
AppState state = AppState.idle;
List<NearbyDeviceBase>? peers;
NearbyDeviceBase? connectedDevice;
List<NearbyDevice>? peers;
NearbyDevice? connectedDevice;
NearbyDeviceInfo? currentDeviceInfo;
NearbyConnectionAndroidInfo? connectionAndroidInfo;
@@ -333,7 +333,7 @@ class AppService extends ChangeNotifier {
updateState(AppState.discoveringPeers);
}
Future<void> connect(NearbyDeviceBase device) async {
Future<void> connect(NearbyDevice device) async {
try {
await _nearbyService.connect(device);
} catch (e) {
@@ -366,7 +366,7 @@ class AppService extends ChangeNotifier {
connectionInfoSubscription = null;
}
Future<void> startListeningConnectedDevice(NearbyDeviceBase device) async {
Future<void> startListeningConnectedDevice(NearbyDevice device) async {
updateState(AppState.loadingConnection);
try {
connectedDeviceSubscription =
@@ -468,7 +468,7 @@ class AppService extends ChangeNotifier {
if (connectedDevice == null) return;
_nearbyService.send(
OutgoingNearbyMessage(
content: NearbyMessageFilesRequest(
content: NearbyMessageFilesRequest.create(
files: [
...paths.map((e) => NearbyFileInfo(path: e)),
],
@@ -494,7 +494,7 @@ class AppService extends ChangeNotifier {
);
}
Future<void> disconnect([NearbyDeviceBase? device]) async {
Future<void> disconnect([NearbyDevice? device]) async {
try {
await _nearbyService.disconnect(device);
} catch (e) {
@@ -1004,7 +1004,7 @@ class _ConnectedSocketBodyState extends State<_ConnectedSocketBody> {
class _DevicePreview extends StatelessWidget {
const _DevicePreview({required this.device, this.largeView = false});
final NearbyDeviceBase device;
final NearbyDevice device;
final bool largeView;
@override
+9 -9
View File
@@ -128,26 +128,26 @@ abstract class NearbyService {
///
/// A single retrieval of the current list of devices in a P2P network.
///
/// Returns the list of [NearbyDeviceBase] that have been stored so far.
/// Returns the list of [NearbyDevice] that have been stored so far.
/// If you want to use a constantly updated list of devices, use [getPeersStream].
///
Future<List<NearbyDeviceBase>> getPeers() {
Future<List<NearbyDevice>> getPeers() {
return NearbyServicePlatform.instance.getPeers();
}
///
/// Returns a constantly updating list of [NearbyDeviceBase] that
/// Returns a constantly updating list of [NearbyDevice] that
/// the platform-specific service has found at each point in time.
///
Stream<List<NearbyDeviceBase>> getPeersStream() {
Stream<List<NearbyDevice>> getPeersStream() {
return NearbyServicePlatform.instance.getPeersStream();
}
///
/// Returns the constantly updating [NearbyDeviceBase] you are currently connected to.
/// Returns the constantly updating [NearbyDevice] you are currently connected to.
/// If it returns null, then there is no connection at the moment.
///
Stream<NearbyDeviceBase?> getConnectedDeviceStream(NearbyDeviceBase device) {
Stream<NearbyDevice?> getConnectedDeviceStream(NearbyDevice device) {
return NearbyServicePlatform.instance.getConnectedDeviceStream(device);
}
@@ -187,7 +187,7 @@ abstract class NearbyService {
/// Note that if [Platform.isIOS] == true, [NearbyIOSDevice] should be passed.
/// If [Platform.isAndroid] == true, [NearbyAndroidDevice] should be passed.
///
Future<bool> connect(NearbyDeviceBase device);
Future<bool> connect(NearbyDevice device);
///
/// Disconnects from passed [device] using a platform-specific service.
@@ -195,8 +195,8 @@ abstract class NearbyService {
/// Note that if [Platform.isIOS] == true, [NearbyIOSDevice] should be passed.
/// If [Platform.isAndroid] == true, [NearbyAndroidDevice] should be passed.
///
/// For IOS [device] is required!!!
Future<bool> disconnect([NearbyDeviceBase? device]);
/// **For IOS [device] is required!!!**
Future<bool> disconnect([NearbyDevice? device]);
///
/// If the device is already connected, it does not mean that you can
+3 -3
View File
@@ -35,14 +35,14 @@ class MethodChannelNearbyService extends NearbyServicePlatform {
}
@override
Future<List<NearbyDeviceBase>> getPeers() async {
Future<List<NearbyDevice>> getPeers() async {
return NearbyDeviceMapper.instance.mapToDeviceList(
await methodChannel.invokeMethod('fetchPeers'),
);
}
@override
Stream<List<NearbyDeviceBase>> getPeersStream() {
Stream<List<NearbyDevice>> getPeersStream() {
const peersChannel = EventChannel("nearby_service_peers");
return peersChannel.receiveBroadcastStream().map((e) {
return NearbyDeviceMapper.instance.mapToDeviceList(e);
@@ -50,7 +50,7 @@ class MethodChannelNearbyService extends NearbyServicePlatform {
}
@override
Stream<NearbyDeviceBase?> getConnectedDeviceStream(NearbyDeviceBase device) {
Stream<NearbyDevice?> getConnectedDeviceStream(NearbyDevice device) {
const connectedDeviceChannel = EventChannel(
"nearby_service_connected_device",
);
+4 -4
View File
@@ -40,20 +40,20 @@ abstract class NearbyServicePlatform extends PlatformInterface {
'openServicesSettings() has not been implemented.');
}
Future<List<NearbyDeviceBase>> getPeers() {
Future<List<NearbyDevice>> getPeers() {
throw UnimplementedError('getPeers() has not been implemented.');
}
Stream<List<NearbyDeviceBase>> getPeersStream() {
Stream<List<NearbyDevice>> getPeersStream() {
throw UnimplementedError('streamPeers() has not been implemented.');
}
Stream<NearbyDeviceBase?> getConnectedDeviceStream(NearbyDeviceBase device) {
Stream<NearbyDevice?> getConnectedDeviceStream(NearbyDevice device) {
throw UnimplementedError(
'getConnectedDeviceStream() has not been implemented.');
}
Future<bool> disconnect(NearbyDeviceBase device) {
Future<bool> disconnect(NearbyDevice device) {
throw UnimplementedError('disconnect() has not been implemented.');
}
}
+7 -7
View File
@@ -5,12 +5,12 @@ import 'package:nearby_service/nearby_service.dart';
///
/// The model of the device found in the P2P network.
///
abstract base class NearbyDeviceBase {
abstract base class NearbyDevice {
///
/// The base device contains [info] and [status].
/// These are parameters that devices will have independent of the platform.
///
const NearbyDeviceBase({required this.info, required this.status});
const NearbyDevice({required this.info, required this.status});
///
/// The minimum information about the device required
@@ -26,7 +26,7 @@ abstract base class NearbyDeviceBase {
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is NearbyDeviceBase &&
other is NearbyDevice &&
runtimeType == other.runtimeType &&
info == other.info &&
status == other.status;
@@ -43,17 +43,17 @@ abstract base class NearbyDeviceBase {
/// If you want to get different data
/// **depending on the platform**, use [byPlatform].
///
/// * The [onAndroid] callback returns this instance of [NearbyDeviceBase],
/// * The [onAndroid] callback returns this instance of [NearbyDevice],
/// cast as [NearbyAndroidDevice] if [Platform.isAndroid] is true.
///
/// * The [onIOS] callback returns this instance of [NearbyDeviceBase],
/// * The [onIOS] callback returns this instance of [NearbyDevice],
/// cast as [NearbyIOSDevice] if [Platform.isIOS] is true.
///
/// * The [onAny] callback returns this instance of [NearbyDeviceBase] with
/// * The [onAny] callback returns this instance of [NearbyDevice] with
/// no casting if both [Platform.isAndroid] and [Platform.isIOS] are false.
///
T? byPlatform<T>({
T Function(NearbyDeviceBase)? onAny,
T Function(NearbyDevice)? onAny,
T Function(NearbyAndroidDevice)? onAndroid,
T Function(NearbyIOSDevice)? onIOS,
}) {
+3 -3
View File
@@ -3,12 +3,12 @@ import 'package:nearby_service/nearby_service.dart';
///
/// Basic Message Abstraction.
///
abstract base class NearbyMessageBase<C extends NearbyMessageContentBase> {
abstract base class NearbyMessage<C extends NearbyMessageContent> {
///
/// The basic message contains only [content] - the content
/// to be sent or received.
///
const NearbyMessageBase({required this.content});
const NearbyMessage({required this.content});
///
/// Model representing content to be sent or received
@@ -25,7 +25,7 @@ abstract base class NearbyMessageBase<C extends NearbyMessageContentBase> {
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is NearbyMessageBase &&
other is NearbyMessage &&
runtimeType == other.runtimeType &&
content == other.content;
+28 -14
View File
@@ -2,28 +2,30 @@ import 'package:nearby_service/nearby_service.dart';
///
/// Abstraction for the message content.
/// The [type] is used to determine, what type of content is it.
/// The [NearbyMessageContentType] is used to
/// determine, what type of content is it.
///
abstract base class NearbyMessageContentBase {
const NearbyMessageContentBase();
abstract base class NearbyMessageContent {
const NearbyMessageContent();
///
/// Contains the conditional logic of creating [NearbyMessageFilesRequest],
/// [NearbyMessageFilesResponse] or [NearbyMessageTextContent]
/// by `type` field of [json].
///
static Content fromJson<Content extends NearbyMessageContentBase>(
Map<String, dynamic>? json) {
static C fromJson<C extends NearbyMessageContent>(
Map<String, dynamic>? json,
) {
try {
final type = NearbyMessageContentType.values.firstWhere(
(e) => e.name == json?['type'],
);
if (type.isText) {
return NearbyMessageTextContent.fromJson(json) as Content;
return NearbyMessageTextContent.fromJson(json) as C;
} else if (type.isFilesRequest) {
return NearbyMessageFilesRequest.fromJson(json) as Content;
return NearbyMessageFilesRequest.fromJson(json) as C;
} else if (type.isFilesResponse) {
return NearbyMessageFilesResponse.fromJson(json) as Content;
return NearbyMessageFilesResponse.fromJson(json) as C;
} else {
throw NearbyServiceException.unsupportedDecoding(json);
}
@@ -32,7 +34,19 @@ abstract base class NearbyMessageContentBase {
}
}
NearbyMessageContentType get type;
NearbyMessageContentType get _type {
final type = byType(
onText: (_) => NearbyMessageContentType.text,
onFilesRequest: (_) => NearbyMessageContentType.filesRequest,
onFilesResponse: (_) => NearbyMessageContentType.filesResponse,
);
if (type != null) {
return type;
}
throw NearbyServiceException(
'Unknown type on NearbyMessageContent - $runtimeType',
);
}
///
/// Check for the content if it is valid for sending or receiving.
@@ -40,13 +54,13 @@ abstract base class NearbyMessageContentBase {
bool get isValid;
///
/// * The [onText] callback returns this instance of [NearbyMessageContentBase],
/// * The [onText] callback returns this instance of [NearbyMessageContent],
/// cast as [NearbyMessageTextContent] if is a text.
///
/// * The [onFilesRequest] callback returns this instance of [NearbyMessageContentBase],
/// * The [onFilesRequest] callback returns this instance of [NearbyMessageContent],
/// cast as [NearbyMessageFilesRequest] if is a files pack request.
///
/// * The [onFilesResponse] callback returns this instance of [NearbyMessageContentBase],
/// * The [onFilesResponse] callback returns this instance of [NearbyMessageContent],
/// cast as [NearbyMessageFilesResponse] if is a files pack response.
///
T? byType<T>({
@@ -65,9 +79,9 @@ abstract base class NearbyMessageContentBase {
}
///
/// Gets [Map] from [NearbyMessageContentBase]
/// Gets [Map] from [NearbyMessageContent]
///
Map<String, dynamic> toJson() {
return {'type': type.name};
return {'type': _type.name};
}
}
+4 -4
View File
@@ -23,12 +23,12 @@ abstract interface class NearbyDeviceMapper {
}
///
/// Converts JSON to a list of [NearbyDeviceBase].
/// Converts JSON to a list of [NearbyDevice].
///
List<NearbyDeviceBase> mapToDeviceList(dynamic value);
List<NearbyDevice> mapToDeviceList(dynamic value);
///
/// Converts JSON to a [NearbyDeviceBase].
/// Converts JSON to a [NearbyDevice].
///
NearbyDeviceBase? mapToDevice(dynamic value);
NearbyDevice? mapToDevice(dynamic value);
}
+3
View File
@@ -25,6 +25,9 @@ class ReceivedNearbyFilesPack implements NearbyReceivedInterface {
@override
final NearbyDeviceInfo sender;
///
/// Received list of [NearbyFileInfo].
///
final List<NearbyFileInfo> files;
Map<String, dynamic> toJson() {
+5 -5
View File
@@ -3,8 +3,8 @@ import 'package:nearby_service/nearby_service.dart';
///
/// The message that will be sent from the current device.
///
final class OutgoingNearbyMessage<C extends NearbyMessageContentBase>
extends NearbyMessageBase<C> implements NearbyOutgoingInterface {
final class OutgoingNearbyMessage<C extends NearbyMessageContent>
extends NearbyMessage<C> implements NearbyOutgoingInterface {
///
/// To send a message, in addition to [content], you need to pass [receiver]
/// to know to whom the message is addressed.
@@ -50,8 +50,8 @@ final class OutgoingNearbyMessage<C extends NearbyMessageContentBase>
///
/// Message received by the current device.
///
final class ReceivedNearbyMessage<C extends NearbyMessageContentBase>
extends NearbyMessageBase<C> implements NearbyReceivedInterface {
final class ReceivedNearbyMessage<C extends NearbyMessageContent>
extends NearbyMessage<C> implements NearbyReceivedInterface {
///
/// The received message contains a [sender] in addition to [content],
/// to know from whom the message came.
@@ -66,7 +66,7 @@ final class ReceivedNearbyMessage<C extends NearbyMessageContentBase>
///
factory ReceivedNearbyMessage.fromJson(Map<String, dynamic>? json) {
return ReceivedNearbyMessage(
content: NearbyMessageContentBase.fromJson(json?['content']),
content: NearbyMessageContent.fromJson(json?['content']),
sender: NearbyDeviceInfo.fromJson(json?['sender']),
);
}
+12 -21
View File
@@ -6,7 +6,7 @@ import 'package:nearby_service/src/utils/random.dart';
///
/// Contains [value] - the message to be sent or received.
///
final class NearbyMessageTextContent extends NearbyMessageContentBase {
final class NearbyMessageTextContent extends NearbyMessageContent {
const NearbyMessageTextContent({required this.value});
///
@@ -23,9 +23,6 @@ final class NearbyMessageTextContent extends NearbyMessageContentBase {
///
final String value;
@override
NearbyMessageContentType get type => NearbyMessageContentType.text;
@override
bool get isValid => value.isNotEmpty;
@@ -56,13 +53,12 @@ final class NearbyMessageTextContent extends NearbyMessageContentBase {
///
/// Sealed class for files content in NearbyMessage.
///
sealed class NearbyMessageFilesContent extends NearbyMessageContentBase {
sealed class NearbyMessageFilesContent extends NearbyMessageContent {
///
/// Here [type] = [NearbyMessageContentType.filesResponse] or
/// [type] = [NearbyMessageContentType.filesRequest]
/// Here type is [NearbyMessageContentType.filesResponse] or
/// [NearbyMessageContentType.filesRequest]
///
/// Also [NearbyMessageFilesContent] contains [id] of the files pack and
/// list of [NearbyFileInfo] to determine the files.
/// Also [NearbyMessageFilesContent] contains [id] of the files pack.
///
const NearbyMessageFilesContent({required this.id});
@@ -103,6 +99,9 @@ sealed class NearbyMessageFilesContent extends NearbyMessageContentBase {
/// Contains info about the [files].
///
final class NearbyMessageFilesRequest extends NearbyMessageFilesContent {
///
/// Adds a [NearbyFileInfo] list to [id] to identify files.
///
const NearbyMessageFilesRequest._({
required super.id,
required this.files,
@@ -110,8 +109,9 @@ final class NearbyMessageFilesRequest extends NearbyMessageFilesContent {
///
/// Basic constructor with [files] to be sent or received.
/// Generates [id] in constructor.
///
NearbyMessageFilesRequest({required this.files})
NearbyMessageFilesRequest.create({required this.files})
: super(
id: RandomUtils.instance.nextInt(1000000, 9999999).toString(),
);
@@ -135,9 +135,6 @@ final class NearbyMessageFilesRequest extends NearbyMessageFilesContent {
///
final List<NearbyFileInfo> files;
@override
NearbyMessageContentType get type => NearbyMessageContentType.filesRequest;
@override
String toString() {
return 'NearbyMessageFileRequest{id: $id, files: $files}';
@@ -157,9 +154,7 @@ final class NearbyMessageFilesRequest extends NearbyMessageFilesContent {
bool get isValid =>
super.isValid &&
files.isNotEmpty &&
files.every(
(element) => element.path.isNotEmpty,
);
files.every((element) => element.path.isNotEmpty);
@override
bool operator ==(Object other) =>
@@ -175,12 +170,11 @@ final class NearbyMessageFilesRequest extends NearbyMessageFilesContent {
///
/// Nearby message File Response. Used for file sending responses.
/// Does not contain files' bytes!
///
final class NearbyMessageFilesResponse extends NearbyMessageFilesContent {
///
/// Used to send a response to a previously received request.
/// Provide [id] from [NearbyMessageFilesRequest] or
/// Provide [id] from [NearbyMessageFilesRequest].
///
NearbyMessageFilesResponse({
required super.id,
@@ -202,9 +196,6 @@ final class NearbyMessageFilesResponse extends NearbyMessageFilesContent {
///
final bool response;
@override
NearbyMessageContentType get type => NearbyMessageContentType.filesResponse;
@override
Map<String, dynamic> toJson() {
return {
@@ -5,7 +5,7 @@ import 'package:nearby_service/src/utils/unknown.dart';
///
/// A device on a P2P network obtained from the Android platform.
///
final class NearbyAndroidDevice extends NearbyDeviceBase {
final class NearbyAndroidDevice extends NearbyDevice {
///
/// The class representing the Android class
/// [WifiP2pDevice](https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pDevice).
@@ -139,7 +139,7 @@ final class NearbyAndroidDevice extends NearbyDeviceBase {
///
class NearbyAndroidMapper implements NearbyDeviceMapper {
@override
List<NearbyDeviceBase> mapToDeviceList(dynamic value) {
List<NearbyDevice> mapToDeviceList(dynamic value) {
final decoded = JSONDecoder.decodeList(value);
return [
...?decoded?.map(
@@ -149,7 +149,7 @@ class NearbyAndroidMapper implements NearbyDeviceMapper {
}
@override
NearbyDeviceBase? mapToDevice(dynamic value) {
NearbyDevice? mapToDevice(dynamic value) {
final decoded = JSONDecoder.decodeMap(value);
if (decoded == null) return null;
@@ -54,7 +54,7 @@ class NearbyAndroidService extends NearbyService {
/// Note! Requires [NearbyAndroidDevice] to be passed.
///
@override
Future<bool> connect(NearbyDeviceBase device) {
Future<bool> connect(NearbyDevice device) {
_requireAndroidDevice(device);
return NearbyServiceAndroidPlatform.instance.connect(device.info.id);
}
@@ -65,7 +65,7 @@ class NearbyAndroidService extends NearbyService {
/// Note! Requires [NearbyAndroidDevice] to be passed.
///
@override
Future<bool> disconnect([NearbyDeviceBase? device]) {
Future<bool> disconnect([NearbyDevice? device]) {
return NearbyServiceAndroidPlatform.instance.disconnect();
}
@@ -133,7 +133,7 @@ class NearbyAndroidService extends NearbyService {
return NearbyServiceAndroidPlatform.instance.getConnectionInfoStream();
}
void _requireAndroidDevice(NearbyDeviceBase device) {
void _requireAndroidDevice(NearbyDevice device) {
assert(
device is NearbyAndroidDevice,
'The Nearby Android Service can only work with the NearbyAndroidDevice and not with ${device.runtimeType}',
@@ -235,7 +235,7 @@ class NearbySocketService {
}
}
void _handleMessage(NearbyMessageBase message) {
void _handleMessage(NearbyMessage message) {
if (message.content is NearbyMessageFilesContent) {
_fileSocketsManager.handleFileMessageContent(
message.content as NearbyMessageFilesContent,
@@ -4,7 +4,7 @@ import 'package:nearby_service/src/utils/json_decoder.dart';
///
/// A device on a P2P network obtained from the IOS platform.
///
final class NearbyIOSDevice extends NearbyDeviceBase {
final class NearbyIOSDevice extends NearbyDevice {
///
/// A class representing an IOS device on a P2P network.
///
@@ -74,7 +74,7 @@ final class NearbyIOSDevice extends NearbyDeviceBase {
///
class NearbyIOSMapper implements NearbyDeviceMapper {
@override
List<NearbyDeviceBase> mapToDeviceList(dynamic value) {
List<NearbyDevice> mapToDeviceList(dynamic value) {
final decoded = JSONDecoder.decodeList(value);
return [
...?decoded?.map(
@@ -84,7 +84,7 @@ class NearbyIOSMapper implements NearbyDeviceMapper {
}
@override
NearbyDeviceBase? mapToDevice(dynamic value) {
NearbyDevice? mapToDevice(dynamic value) {
final decoded = JSONDecoder.decodeMap(value);
if (decoded == null) return null;
@@ -118,7 +118,7 @@ class NearbyIOSService extends NearbyService {
/// Note! Requires [NearbyIOSDevice] to be passed.
///
@override
Future<bool> connect(NearbyDeviceBase device) async {
Future<bool> connect(NearbyDevice device) async {
_requireIOSDevice(device);
final result = _isBrowser.value
? await NearbyServiceIOSPlatform.instance.invite(device.info.id)
@@ -140,7 +140,7 @@ class NearbyIOSService extends NearbyService {
/// Note! Requires [NearbyIOSDevice] to be passed.
///
@override
Future<bool> disconnect([NearbyDeviceBase? device]) async {
Future<bool> disconnect([NearbyDevice? device]) async {
if (device == null) return false;
_requireIOSDevice(device);
final result = await NearbyServiceIOSPlatform.instance.disconnect(
@@ -272,7 +272,7 @@ class NearbyIOSService extends NearbyService {
}
}
void _requireIOSDevice(NearbyDeviceBase device) {
void _requireIOSDevice(NearbyDevice device) {
assert(
device is NearbyIOSDevice,
'The Nearby IOS Service can only work with the NearbyIOSDevice and not with ${device.runtimeType}',
+1 -2
View File
@@ -31,8 +31,7 @@ class NearbyServiceException implements Exception {
);
}
factory NearbyServiceException.invalidMessage(
NearbyMessageContentBase content) {
factory NearbyServiceException.invalidMessage(NearbyMessageContent content) {
return NearbyServiceException(
'The message="$content" is not valid',
);
+4 -4
View File
@@ -8,19 +8,19 @@ class MockNearbyServicePlatform
with MockPlatformInterfaceMixin
implements NearbyServicePlatform {
@override
Stream<NearbyDeviceBase?> getConnectedDeviceStream(device) {
Stream<NearbyDevice?> getConnectedDeviceStream(device) {
// TODO: implement getConnectedDeviceStream
throw UnimplementedError();
}
@override
Future<List<NearbyDeviceBase>> getPeers() {
Future<List<NearbyDevice>> getPeers() {
// TODO: implement getPeers
throw UnimplementedError();
}
@override
Stream<List<NearbyDeviceBase>> getPeersStream() {
Stream<List<NearbyDevice>> getPeersStream() {
// TODO: implement getPeersStream
throw UnimplementedError();
}
@@ -44,7 +44,7 @@ class MockNearbyServicePlatform
}
@override
Future<bool> disconnect(NearbyDeviceBase device) {
Future<bool> disconnect(NearbyDevice device) {
// TODO: implement disconnect
throw UnimplementedError();
}