Skip to content

Commit

Permalink
Refactored according to #10.
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyost committed Mar 10, 2021
1 parent 5556a7b commit 0b0475e
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
export 'package:bonsoir_platform_interface/src/actions/action.dart';
export 'package:bonsoir_platform_interface/src/actions/broadcast_action.dart';
export 'package:bonsoir_platform_interface/src/actions/discovery_action.dart';
export 'package:bonsoir_platform_interface/src/actions/method_channel_action.dart';
export 'package:bonsoir_platform_interface/src/events/broadcast_event.dart';
export 'package:bonsoir_platform_interface/src/events/discovery_event.dart';
export 'package:bonsoir_platform_interface/src/events/event.dart';
Expand Down
79 changes: 79 additions & 0 deletions bonsoir_platform_interface/lib/src/actions/action.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import 'dart:math';

import 'package:bonsoir_platform_interface/src/events/event.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

/// This class serves as the stream source for the implementations to override.
abstract class BonsoirAction<T extends BonsoirEvent> {
Expand All @@ -23,3 +27,78 @@ abstract class BonsoirAction<T extends BonsoirEvent> {
/// This returns a JSON representation of the event.
Map<String, dynamic> toJson();
}

/// Abstract class that contains all methods that are communicating with the native side of the plugin.
abstract class MethodChannelBonsoirAction<T extends BonsoirEvent> extends BonsoirAction<T> {
/// The channel name.
static const String _channelName = 'fr.skyost.bonsoir';

/// The channel.
static const MethodChannel channel = MethodChannel(_channelName);

/// The class identifier.
final int _id;

/// The class type.
final String _classType;

/// Whether to print logs.
final bool printLogs;

/// Whether this instance has been stopped.
bool _isStopped = false;

/// The current event stream.
Stream<T>? _eventStream;

/// Creates a new Bonsoir class instance.
MethodChannelBonsoirAction({
required String classType,
this.printLogs = kDebugMode,
}) : _id = _createRandomId(),
_classType = classType;

/// The event stream.
/// Subscribe to it to receive this instance updates.
Stream<T>? get eventStream => _eventStream;

/// Await this method to know when the plugin will be ready.
Future<void> get ready async {
await channel.invokeMethod('$_classType.initialize', toJson());
_eventStream = EventChannel('$_channelName.$_classType.$_id').receiveBroadcastStream().map(transformPlatformEvent);
}

/// Returns whether this instance can be used.
bool get isReady => _eventStream != null && !_isStopped;

/// Returns whether this instance has been stopped.
bool get isStopped => _isStopped;

/// Starts to do either a discover or a broadcast.
Future<void> start() {
assert(isReady, '''$runtimeType should be ready to start in order to call this method.
You must wait until this instance is ready by calling "await $runtimeType.ready".
If you have previously called "$runtimeType.stop()" on this instance, you have to create a new instance of this class.''');
return channel.invokeMethod('$_classType.start', toJson());
}

/// Stops the current discover or broadcast.
Future<void> stop() async {
await channel.invokeMethod('$_classType.stop', toJson());
_isStopped = true;
}

/// Transforms the stream data to a [T].
@protected
T transformPlatformEvent(dynamic event);

/// Converts this Bonsoir class to a JSON map.
@protected
Map<String, dynamic> toJson() => {
'id': _id,
'printLogs': printLogs,
};

/// Allows to generate a random identifier.
static int _createRandomId() => Random().nextInt(100000);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:bonsoir_platform_interface/src/actions/method_channel_action.dart';
import 'package:bonsoir_platform_interface/src/actions/action.dart';
import 'package:bonsoir_platform_interface/src/events/broadcast_event.dart';
import 'package:bonsoir_platform_interface/src/service/service.dart';
import 'package:flutter/foundation.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:bonsoir_platform_interface/src/actions/method_channel_action.dart';
import 'package:bonsoir_platform_interface/src/actions/action.dart';
import 'package:bonsoir_platform_interface/src/events/discovery_event.dart';
import 'package:flutter/foundation.dart';

Expand Down

This file was deleted.

22 changes: 14 additions & 8 deletions bonsoir_platform_interface/lib/src/platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import 'package:flutter/foundation.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

/// A Bonsoir class that allows to either broadcast a service or to discover services on the network.
class BonsoirPlatformInterface extends PlatformInterface {
abstract class BonsoirPlatformInterface extends PlatformInterface {
/// This object is needed to check if the platform instance registering is actually extending the platform interface (this class)
static final Object _token = Object();

/// Setting a default platform instance implementation.
static BonsoirPlatformInterface _instance = BonsoirPlatformInterface();
static BonsoirPlatformInterface _instance = MethodChannelBonsoir();

/// Creates a new Bonsoir platform interface instance.
BonsoirPlatformInterface() : super(token: _token);
Expand All @@ -28,15 +28,21 @@ class BonsoirPlatformInterface extends PlatformInterface {
}

/// This method returns an initialized subclass of [BonsoirAction] holding the eventStreams and other state needed for the implementations.
BonsoirAction<BonsoirBroadcastEvent> createBroadcast(BonsoirService service, {bool printLogs = kDebugMode});

/// This method returns an initialized subclass of [BonsoirAction] holding the eventStreams and other state needed for the implementations.
BonsoirAction<BonsoirDiscoveryEvent> createDiscovery(String type, {bool printLogs = kDebugMode});
}

/// A Bonsoir class that allows to either broadcast a service or to discover services on the network.
class MethodChannelBonsoir extends BonsoirPlatformInterface {
@override
BonsoirAction<BonsoirBroadcastEvent> createBroadcast(BonsoirService service, {bool printLogs = kDebugMode}) {
return BonsoirBroadcastAction(
service: service,
printLogs: printLogs,
);
return BonsoirBroadcastAction(service: service, printLogs: printLogs);
}

/// This method returns an initialized subclass of [BonsoirAction] holding the eventStreams and other state needed for the implementations.
@override
BonsoirAction<BonsoirDiscoveryEvent> createDiscovery(String type, {bool printLogs = kDebugMode}) {
return BonsoirDiscoveryAction(type: type, printLogs: printLogs);
}
}
}

0 comments on commit 0b0475e

Please sign in to comment.