refactor(lib): create structure with base and interface
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
export 'nearby_device_base.dart';
|
||||
export 'nearby_message_base.dart';
|
||||
export 'nearby_message_content_base.dart';
|
||||
@@ -0,0 +1,68 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:nearby_service/nearby_service.dart';
|
||||
|
||||
///
|
||||
/// The model of the device found in the P2P network.
|
||||
///
|
||||
abstract base class NearbyDeviceBase {
|
||||
///
|
||||
/// 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});
|
||||
|
||||
///
|
||||
/// The minimum information about the device required
|
||||
/// to display it in the list and connect to it.
|
||||
///
|
||||
final NearbyDeviceInfo info;
|
||||
|
||||
///
|
||||
/// The connection status of the device.
|
||||
///
|
||||
final NearbyDeviceStatus status;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is NearbyDeviceBase &&
|
||||
runtimeType == other.runtimeType &&
|
||||
info == other.info &&
|
||||
status == other.status;
|
||||
|
||||
@override
|
||||
int get hashCode => info.hashCode ^ status.hashCode;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'NearbyDevice{info: $info, status: $status}';
|
||||
}
|
||||
|
||||
///
|
||||
/// If you want to get different data
|
||||
/// **depending on the platform**, use [byPlatform].
|
||||
///
|
||||
/// * The [onAndroid] callback returns this instance of [NearbyDeviceBase],
|
||||
/// cast as [NearbyAndroidDevice] if [Platform.isAndroid] is true.
|
||||
///
|
||||
/// * The [onIOS] callback returns this instance of [NearbyDeviceBase],
|
||||
/// cast as [NearbyIOSDevice] if [Platform.isIOS] is true.
|
||||
///
|
||||
/// * The [onAny] callback returns this instance of [NearbyDeviceBase] with
|
||||
/// no casting if both [Platform.isAndroid] and [Platform.isIOS] are false.
|
||||
///
|
||||
T? byPlatform<T>({
|
||||
T Function(NearbyDeviceBase)? onAny,
|
||||
T Function(NearbyAndroidDevice)? onAndroid,
|
||||
T Function(NearbyIOSDevice)? onIOS,
|
||||
}) {
|
||||
if (this is NearbyIOSDevice && onIOS != null) {
|
||||
return onIOS(this as NearbyIOSDevice);
|
||||
} else if (this is NearbyAndroidDevice && onAndroid != null) {
|
||||
return onAndroid(this as NearbyAndroidDevice);
|
||||
} else {
|
||||
return onAny?.call(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import 'package:nearby_service/nearby_service.dart';
|
||||
|
||||
///
|
||||
/// Basic Message Abstraction.
|
||||
///
|
||||
abstract base class NearbyMessageBase<Content extends NearbyMessageContentBase> {
|
||||
///
|
||||
/// The basic message contains only [content] - the content
|
||||
/// to be sent or received.
|
||||
///
|
||||
const NearbyMessageBase({required this.content});
|
||||
|
||||
///
|
||||
/// Model representing content to be sent or received
|
||||
///
|
||||
final Content content;
|
||||
|
||||
///
|
||||
/// Checks if [content] is not empty
|
||||
///
|
||||
bool get isValid {
|
||||
return content.isValid;
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is NearbyMessageBase &&
|
||||
runtimeType == other.runtimeType &&
|
||||
content == other.content;
|
||||
|
||||
@override
|
||||
int get hashCode => content.hashCode;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'NearbyMessage{content: $content}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import 'package:nearby_service/nearby_service.dart';
|
||||
|
||||
///
|
||||
/// Abstraction for the message content.
|
||||
/// The [type] is used to determine, what type of content is it.
|
||||
///
|
||||
abstract base class NearbyMessageContentBase {
|
||||
const NearbyMessageContentBase();
|
||||
|
||||
///
|
||||
/// 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) {
|
||||
try {
|
||||
final type = NearbyMessageContentType.values.firstWhere(
|
||||
(e) => e.name == json?['type'],
|
||||
);
|
||||
if (type.isText) {
|
||||
return NearbyMessageTextContent.fromJson(json) as Content;
|
||||
} else if (type.isFilesRequest) {
|
||||
return NearbyMessageFilesRequest.fromJson(json) as Content;
|
||||
} else if (type.isFilesResponse) {
|
||||
return NearbyMessageFilesResponse.fromJson(json) as Content;
|
||||
} else {
|
||||
throw NearbyServiceException.unsupportedDecoding(json);
|
||||
}
|
||||
} catch (e) {
|
||||
throw NearbyServiceException(e);
|
||||
}
|
||||
}
|
||||
|
||||
NearbyMessageContentType get type;
|
||||
|
||||
///
|
||||
/// Check for the content if it is valid for sending or receiving.
|
||||
///
|
||||
bool get isValid;
|
||||
|
||||
///
|
||||
/// * The [onText] callback returns this instance of [NearbyMessageContentBase],
|
||||
/// cast as [NearbyMessageTextContent] if is a text.
|
||||
///
|
||||
/// * The [onFilesRequest] callback returns this instance of [NearbyMessageContentBase],
|
||||
/// cast as [NearbyMessageFilesRequest] if is a files pack request.
|
||||
///
|
||||
/// * The [onFilesResponse] callback returns this instance of [NearbyMessageContentBase],
|
||||
/// cast as [NearbyMessageFilesResponse] if is a files pack response.
|
||||
///
|
||||
T? byType<T>({
|
||||
T Function(NearbyMessageTextContent)? onText,
|
||||
T Function(NearbyMessageFilesRequest)? onFilesRequest,
|
||||
T Function(NearbyMessageFilesResponse)? onFilesResponse,
|
||||
}) {
|
||||
if (this is NearbyMessageTextContent && onText != null) {
|
||||
return onText(this as NearbyMessageTextContent);
|
||||
} else if (this is NearbyMessageFilesRequest && onFilesRequest != null) {
|
||||
return onFilesRequest(this as NearbyMessageFilesRequest);
|
||||
} else if (this is NearbyMessageFilesResponse && onFilesResponse != null) {
|
||||
return onFilesResponse(this as NearbyMessageFilesResponse);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
///
|
||||
/// Gets [Map] from [NearbyMessageContentBase]
|
||||
///
|
||||
Map<String, dynamic> toJson() {
|
||||
return {'type': type.name};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user