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
@@ -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};
}
}