feat: Provide a much cleaner Dart API

This commit is contained in:
2023-09-10 13:28:12 +02:00
parent 2299b766cc
commit d6ce224956
18 changed files with 344 additions and 207 deletions

View File

@@ -0,0 +1,16 @@
import 'package:moxxy_native/src/service/config.dart';
import 'package:moxxy_native/src/service/datasender/types.dart';
/// Wrapper API that is only available to the background service.
abstract class BackgroundService {
/// Send [event] with optional id [id] to the foreground.
Future<void> send(BackgroundEvent event, {String? id});
/// Platform specific initialization routine that is called after
/// the entrypoint has been called.
Future<void> init(ServiceConfig config);
/// Update the notification body, if the platform shows a persistent
/// notification.
void setNotificationBody(String body);
}

View File

@@ -0,0 +1,58 @@
import 'dart:convert';
import 'dart:ui';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';
import 'package:moxlib/moxlib.dart';
import 'package:moxxy_native/pigeon/background_service.g.dart';
import 'package:moxxy_native/src/service/background/base.dart';
import 'package:moxxy_native/src/service/config.dart';
import 'package:moxxy_native/src/service/datasender/types.dart';
import 'package:uuid/uuid.dart';
class PigeonBackgroundService extends BackgroundService {
final MoxxyBackgroundServiceApi _api = MoxxyBackgroundServiceApi();
/// A method channel for Foreground -> Service communication
// TODO(Unknown): Move this into a constant for reuse
final MethodChannel _channel =
const MethodChannel('org.moxxy.moxxy_native/background');
/// A logger.
final Logger _log = Logger('PigeonBackgroundService');
@override
Future<void> send(BackgroundEvent event, {String? id}) async {
final data = DataWrapper(
id ?? const Uuid().v4(),
event,
);
await _api.sendData(jsonEncode(data.toJson()));
}
@override
Future<void> init(
ServiceConfig config,
) async {
// Ensure that the Dart executor is ready to use plugins
WidgetsFlutterBinding.ensureInitialized();
DartPluginRegistrant.ensureInitialized();
// Register the channel for Foreground -> Service communication
_channel.setMethodCallHandler((call) async {
// TODO(Unknown): Maybe do something smarter like pigeon and use Lists instead of Maps
final args = call.arguments! as String;
await config.handleData(jsonDecode(args) as Map<String, dynamic>);
});
// Start execution
_log.finest('Setup complete. Calling main entrypoint...');
await config.entrypoint(config.initialLocale);
}
@override
void setNotificationBody(String body) {
_api.setNotificationBody(body);
}
}

View File

@@ -0,0 +1,55 @@
import 'dart:convert';
import 'dart:ui';
/// A function that can act as a service entrypoint.
typedef EntrypointCallback = Future<void> Function(String initialLocale);
/// A function that can be called when data is received.
typedef HandleEventCallback = Future<void> Function(Map<String, dynamic>? data);
/// Configuration that will be passed to the service's entrypoint
class ServiceConfig {
const ServiceConfig(
this.entrypoint,
this.handleData,
this.initialLocale,
);
/// Reconstruct the configuration from a JSON string.
factory ServiceConfig.fromString(String rawData) {
final data = jsonDecode(rawData) as Map<String, dynamic>;
return ServiceConfig(
PluginUtilities.getCallbackFromHandle(
CallbackHandle.fromRawHandle(
data['entrypoint']! as int,
),
)! as EntrypointCallback,
PluginUtilities.getCallbackFromHandle(
CallbackHandle.fromRawHandle(
data['handleData']! as int,
),
)! as HandleEventCallback,
data['initialLocale']! as String,
);
}
/// The initial locale to use.
final String initialLocale;
/// The entrypoint to call into.
final EntrypointCallback entrypoint;
/// Entry function to call when the service receives data.
final HandleEventCallback handleData;
@override
String toString() {
return jsonEncode({
'entrypoint':
PluginUtilities.getCallbackHandle(entrypoint)!.toRawHandle(),
'handleData':
PluginUtilities.getCallbackHandle(handleData)!.toRawHandle(),
'initialLocale': initialLocale,
});
}
}

View File

@@ -0,0 +1,15 @@
import 'dart:convert';
import 'package:moxlib/moxlib.dart';
import 'package:moxxy_native/pigeon/service.g.dart';
import 'package:moxxy_native/src/service/datasender/types.dart';
class PigeonForegroundServiceDataSender
extends AwaitableDataSender<BackgroundCommand, BackgroundEvent> {
PigeonForegroundServiceDataSender(this._api);
final MoxxyServiceApi _api;
@override
Future<void> sendDataImpl(DataWrapper<JsonImplementation> data) {
return _api.sendData(jsonEncode(data.toJson()));
}
}

View File

@@ -0,0 +1,20 @@
import 'dart:io';
import 'package:moxlib/moxlib.dart';
import 'package:moxxy_native/pigeon/service.g.dart';
import 'package:moxxy_native/src/service/datasender/pigeon.dart';
import 'package:moxxy_native/src/service/exceptions.dart';
typedef ForegroundServiceDataSender
= AwaitableDataSender<BackgroundCommand, BackgroundEvent>;
abstract class BackgroundCommand implements JsonImplementation {}
abstract class BackgroundEvent implements JsonImplementation {}
ForegroundServiceDataSender getForegroundDataSender(MoxxyServiceApi api) {
if (Platform.isAndroid) {
return PigeonForegroundServiceDataSender(api);
} else {
throw UnsupportedPlatformException();
}
}

View File

@@ -0,0 +1,25 @@
import 'package:flutter/cupertino.dart';
import 'package:get_it/get_it.dart';
import 'package:moxxy_native/pigeon/background_service.g.dart';
import 'package:moxxy_native/src/service/background/base.dart';
import 'package:moxxy_native/src/service/background/pigeon.dart';
import 'package:moxxy_native/src/service/config.dart';
/// An entrypoint that should be used when the service runs
/// in a new Flutter Engine.
@pragma('vm:entry-point')
Future<void> pigeonEntrypoint() async {
// ignore: avoid_print
print('androidEntrypoint: Called on new FlutterEngine');
// Pull and deserialize the extra data passed on.
WidgetsFlutterBinding.ensureInitialized();
final config = ServiceConfig.fromString(
await MoxxyBackgroundServiceApi().getExtraData(),
);
// Setup the background service
final srv = PigeonBackgroundService();
GetIt.I.registerSingleton<BackgroundService>(srv);
await srv.init(config);
}

View File

@@ -0,0 +1,8 @@
import 'dart:io';
/// An exception representing that moxxy_native does not support the given platform.
class UnsupportedPlatformException implements Exception {
UnsupportedPlatformException();
String get message => 'Unsupported platform "${Platform.operatingSystem}"';
}

View File

@@ -0,0 +1,49 @@
import 'dart:io';
import 'package:moxlib/moxlib.dart';
import 'package:moxxy_native/src/service/config.dart';
import 'package:moxxy_native/src/service/datasender/types.dart';
import 'package:moxxy_native/src/service/exceptions.dart';
import 'package:moxxy_native/src/service/foreground/pigeon.dart';
/// Wrapper API that is only available to the UI isolate.
// TODO(Unknown): Dumb naming. Name it something better
abstract class ForegroundService {
/// Perform setup such that we [handleData] is called whenever the background service
/// sends data to the foreground.
Future<void> attach(HandleEventCallback handleData);
/// Start the background service with the config [config]. Additionally, perform
/// setup such that [uiHandleData] is called whenever the background service sends
/// data to the foreground.
Future<void> start(ServiceConfig config, HandleEventCallback uiHandleData);
/// Return true if the background service is running. False, if not.
Future<bool> isRunning();
/// Return the [AwaitableDataSender] that is used to send data to the background service.
ForegroundServiceDataSender getDataSender();
/// Convenience wrapper around getDataSender().sendData. The arguments are the same
/// as for [AwaitableDataSender].
Future<BackgroundEvent?> send(
BackgroundCommand command, {
bool awaitable = true,
});
}
/// "Singleton" ForegroundService instance to prevent having to type "GetIt.I.get<ForegroundService>()"
ForegroundService? _service;
/// Either returns or creates a [ForegroundService] object of the correct type for the
/// current platform.
ForegroundService getForegroundService() {
if (_service == null) {
if (Platform.isAndroid) {
_service = PigeonForegroundService();
} else {
throw UnsupportedPlatformException();
}
}
return _service!;
}

View File

@@ -0,0 +1,82 @@
import 'dart:convert';
import 'dart:ui';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';
import 'package:moxxy_native/pigeon/service.g.dart';
import 'package:moxxy_native/src/service/config.dart';
import 'package:moxxy_native/src/service/datasender/pigeon.dart';
import 'package:moxxy_native/src/service/datasender/types.dart';
import 'package:moxxy_native/src/service/entrypoints/pigeon.dart';
import 'package:moxxy_native/src/service/foreground/base.dart';
class PigeonForegroundService extends ForegroundService {
PigeonForegroundService() {
_dataSender = PigeonForegroundServiceDataSender(_api);
}
/// Pigeon channel to the native side.
final MoxxyServiceApi _api = MoxxyServiceApi();
/// A method channel for background service -> UI isolate communication.
final MethodChannel _channel =
const MethodChannel('org.moxxy.moxxy_native/foreground');
/// The data sender backing this class.
late final PigeonForegroundServiceDataSender _dataSender;
/// A logger.
final Logger _log = Logger('PigeonForegroundService');
@override
Future<void> attach(
HandleEventCallback handleData,
) async {
_channel.setMethodCallHandler((call) async {
await handleData(
jsonDecode(call.arguments! as String) as Map<String, dynamic>,
);
});
}
@override
Future<void> start(
ServiceConfig config,
HandleEventCallback uiHandleData,
) async {
await _api.configure(
PluginUtilities.getCallbackHandle(
pigeonEntrypoint,
)!
.toRawHandle(),
config.toString(),
);
// Prepare the method channel
await attach(uiHandleData);
// Start the service
await _api.start();
_log.finest('Background service started...');
}
@override
Future<bool> isRunning() async {
WidgetsFlutterBinding.ensureInitialized();
return _api.isRunning();
}
@override
ForegroundServiceDataSender getDataSender() => _dataSender;
@override
Future<BackgroundEvent?> send(
BackgroundCommand command, {
bool awaitable = true,
}) {
return _dataSender.sendData(
command,
awaitable: awaitable,
);
}
}