feat: Migrate to moxlib 0.2.0

This commit is contained in:
2023-06-18 12:19:24 +02:00
parent 7a999cf860
commit 3bc880079c
13 changed files with 124 additions and 75 deletions

View File

@@ -6,13 +6,21 @@ class AndroidCryptographyImplementation extends CryptographyImplementation {
final _methodChannel = const MethodChannel('me.polynom.moxplatform_android');
@override
Future<CryptographyResult?> encryptFile(String sourcePath, String destPath, Uint8List key, Uint8List iv, CipherAlgorithm algorithm, String hashSpec) async {
final dynamic resultRaw = await _methodChannel.invokeMethod<dynamic>('encryptFile', [
Future<CryptographyResult?> encryptFile(
String sourcePath,
String destPath,
Uint8List key,
Uint8List iv,
CipherAlgorithm algorithm,
String hashSpec,
) async {
final dynamic resultRaw =
await _methodChannel.invokeMethod<dynamic>('encryptFile', [
sourcePath,
destPath,
key,
iv,
algorithm.toInt(),
algorithm.value,
hashSpec,
]);
if (resultRaw == null) return null;
@@ -26,13 +34,21 @@ class AndroidCryptographyImplementation extends CryptographyImplementation {
}
@override
Future<CryptographyResult?> decryptFile(String sourcePath, String destPath, Uint8List key, Uint8List iv, CipherAlgorithm algorithm, String hashSpec) async {
final dynamic resultRaw = await _methodChannel.invokeMethod<dynamic>('decryptFile', [
Future<CryptographyResult?> decryptFile(
String sourcePath,
String destPath,
Uint8List key,
Uint8List iv,
CipherAlgorithm algorithm,
String hashSpec,
) async {
final dynamic resultRaw =
await _methodChannel.invokeMethod<dynamic>('decryptFile', [
sourcePath,
destPath,
key,
iv,
algorithm.toInt(),
algorithm.value,
hashSpec,
]);
if (resultRaw == null) return null;
@@ -47,7 +63,8 @@ class AndroidCryptographyImplementation extends CryptographyImplementation {
@override
Future<Uint8List?> hashFile(String path, String hashSpec) async {
final dynamic resultsRaw = await _methodChannel.invokeMethod<dynamic>('hashFile', [
final dynamic resultsRaw =
await _methodChannel.invokeMethod<dynamic>('hashFile', [
path,
hashSpec,
]);

View File

@@ -4,18 +4,19 @@ import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:get_it/get_it.dart';
import 'package:logging/logging.dart';
import 'package:moxlib/awaitabledatasender.dart';
import 'package:moxlib/moxlib.dart';
import 'package:moxplatform/moxplatform.dart';
import 'package:moxplatform_android/src/service_android.dart';
import 'package:moxplatform_platform_interface/moxplatform_platform_interface.dart';
/// An [AwaitableDataSender] that uses flutter_background_service.
class BackgroundServiceDataSender extends AwaitableDataSender<BackgroundCommand, BackgroundEvent> {
class BackgroundServiceDataSender
extends AwaitableDataSender<BackgroundCommand, BackgroundEvent> {
BackgroundServiceDataSender()
: _channel = const MethodChannel('me.polynom.moxplatform_android'), super();
: _channel = const MethodChannel('me.polynom.moxplatform_android'),
super();
final MethodChannel _channel;
@override
Future<void> sendDataImpl(DataWrapper data) async {
await _channel.invokeMethod<void>('sendData', jsonEncode(data.toJson()));
@@ -39,11 +40,15 @@ Future<void> androidEntrypoint() async {
);
final data = jsonDecode(result!) as Map<String, dynamic>;
final entrypointHandle = data['genericEntrypoint']! as int;
final entrypointCallbackHandle = CallbackHandle.fromRawHandle(entrypointHandle);
final entrypoint = PluginUtilities.getCallbackFromHandle(entrypointCallbackHandle);
final entrypointCallbackHandle =
CallbackHandle.fromRawHandle(entrypointHandle);
final entrypoint =
PluginUtilities.getCallbackFromHandle(entrypointCallbackHandle);
final handleUIEventHandle = data['eventHandle']! as int;
final handleUIEventCallbackHandle = CallbackHandle.fromRawHandle(handleUIEventHandle);
final handleUIEvent = PluginUtilities.getCallbackFromHandle(handleUIEventCallbackHandle);
final handleUIEventCallbackHandle =
CallbackHandle.fromRawHandle(handleUIEventHandle);
final handleUIEvent =
PluginUtilities.getCallbackFromHandle(handleUIEventCallbackHandle);
final srv = AndroidBackgroundService();
GetIt.I.registerSingleton<BackgroundService>(srv);
@@ -55,24 +60,25 @@ Future<void> androidEntrypoint() async {
/// The Android specific implementation of the [IsolateHandler].
class AndroidIsolateHandler extends IsolateHandler {
AndroidIsolateHandler()
: _channel = const MethodChannel('me.polynom.moxplatform_android'),
_dataSender = BackgroundServiceDataSender(),
_log = Logger('AndroidIsolateHandler'),
super();
: _channel = const MethodChannel('me.polynom.moxplatform_android'),
_dataSender = BackgroundServiceDataSender(),
_log = Logger('AndroidIsolateHandler'),
super();
final BackgroundServiceDataSender _dataSender;
final MethodChannel _channel;
final Logger _log;
@override
Future<void> attach(Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent) async {
Future<void> attach(
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent,
) async {
_channel.setMethodCallHandler((MethodCall call) async {
final args = call.arguments as String;
await handleIsolateEvent(jsonDecode(args) as Map<String, dynamic>);
});
}
@override
Future<void> start(
Future<void> Function() entrypoint,
@@ -82,13 +88,16 @@ class AndroidIsolateHandler extends IsolateHandler {
_log.finest('Called start');
WidgetsFlutterBinding.ensureInitialized();
final androidEntrypointHandle = PluginUtilities.getCallbackHandle(androidEntrypoint)!.toRawHandle();
final androidEntrypointHandle =
PluginUtilities.getCallbackHandle(androidEntrypoint)!.toRawHandle();
_log.finest('androidEntrypointHandle: $androidEntrypointHandle');
await _channel.invokeMethod<void>('configure', <dynamic>[
androidEntrypointHandle,
jsonEncode({
'genericEntrypoint': PluginUtilities.getCallbackHandle(entrypoint)!.toRawHandle(),
'eventHandle': PluginUtilities.getCallbackHandle(handleUIEvent)!.toRawHandle()
'genericEntrypoint':
PluginUtilities.getCallbackHandle(entrypoint)!.toRawHandle(),
'eventHandle':
PluginUtilities.getCallbackHandle(handleUIEvent)!.toRawHandle()
}),
]);

View File

@@ -4,19 +4,19 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:moxlib/awaitabledatasender.dart';
import 'package:moxlib/moxlib.dart';
import 'package:moxplatform/moxplatform.dart';
import 'package:moxplatform_platform_interface/moxplatform_platform_interface.dart';
import 'package:uuid/uuid.dart';
class AndroidBackgroundService extends BackgroundService {
AndroidBackgroundService()
: _log = Logger('AndroidBackgroundService'),
super();
: _log = Logger('AndroidBackgroundService'),
super();
@internal
static const MethodChannel channel = MethodChannel('me.polynom.moxplatform_android_bg');
static const MethodChannel channel =
MethodChannel('me.polynom.moxplatform_android_bg');
final Logger _log;
@override
@@ -28,7 +28,7 @@ class AndroidBackgroundService extends BackgroundService {
}
@override
void sendEvent(BackgroundEvent event, { String? id }) {
void sendEvent(BackgroundEvent event, {String? id}) {
final data = DataWrapper(
id ?? const Uuid().v4(),
event,
@@ -54,7 +54,7 @@ class AndroidBackgroundService extends BackgroundService {
final args = call.arguments! as String;
await handleEvent(jsonDecode(args) as Map<String, dynamic>);
});
setNotification('Moxxy', 'Preparing...');
_log.finest('Running...');