feat: Implement a wrapper around the service APIs

This commit is contained in:
2023-09-10 01:02:56 +02:00
parent 3b5e331aca
commit 2299b766cc
7 changed files with 296 additions and 27 deletions

View File

@@ -1,29 +1,23 @@
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get_it/get_it.dart';
import 'package:moxxy_native/moxxy_native.dart';
import 'package:permission_handler/permission_handler.dart';
@pragma('vm:entrypoint')
Future<void> serviceHandleData(Map<String, dynamic>? data) async {
print('[BG] Received data $data');
GetIt.I.get<BackgroundService>().send(
TestEvent(),
id: data!['id']! as String,
);
}
@pragma('vm:entry-point')
Future<void> entrypoint() async {
WidgetsFlutterBinding.ensureInitialized();
print('CALLED FROM NEW FLUTTERENGINE');
final api = MoxxyBackgroundServiceApi();
final extra = await api.getExtraData();
print('EXTRA DATA: $extra');
MethodChannel('org.moxxy.moxxy_native/background').setMethodCallHandler((call) async {
print('[BG] Received ${call.method} with ${call.arguments}');
});
print('Waiting...');
await Future<void>.delayed(const Duration(seconds: 5));
await api.sendData('Hello from the foreground service');
print('Data sent');
Future<void> serviceEntrypoint(String initialLocale) async {
print('Initial locale: $initialLocale');
}
void main() {
@@ -37,6 +31,20 @@ class MyApp extends StatefulWidget {
MyAppState createState() => MyAppState();
}
class TestCommand extends BackgroundCommand {
@override
Map<String, dynamic> toJson() => {
'request': 'return_name',
};
}
class TestEvent extends BackgroundEvent {
@override
Map<String, dynamic> toJson() => {
'name': 'Moxxy',
};
}
class MyAppState extends State<MyApp> {
String? imagePath;
@@ -138,15 +146,23 @@ class MyAppState extends State<MyApp> {
await Permission.notification.request();
final handle = PluginUtilities.getCallbackHandle(entrypoint)!
.toRawHandle();
final api = MoxxyServiceApi();
await api.configure(handle, 'lol');
MethodChannel("org.moxxy.moxxy_native/foreground").setMethodCallHandler((call) async {
print('[FG] Received ${call.method} with ${call.arguments}');
await api.sendData('Hello from the foreground');
});
await api.start();
final srv = ForegroundService();
await srv.start(
const ServiceConfig(
serviceEntrypoint,
serviceHandleData,
'en',
),
(data) async {
print('[FG] Received data $data');
},
);
await Future<void>.delayed(const Duration(milliseconds: 600));
await srv.dataSender.sendData(
TestCommand(),
awaitable: false,
);
},
child: const Text('Start foreground service')),
],

View File

@@ -41,6 +41,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.17.0"
crypto:
dependency: transitive
description:
name: crypto
sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab
url: "https://pub.dev"
source: hosted
version: "3.0.3"
cupertino_icons:
dependency: "direct main"
description:
@@ -75,6 +83,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
get_it:
dependency: "direct main"
description:
name: get_it
sha256: "529de303c739fca98cd7ece5fca500d8ff89649f1bb4b4e94fb20954abcd7468"
url: "https://pub.dev"
source: hosted
version: "7.6.0"
js:
dependency: transitive
description:
@@ -91,6 +107,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.1"
logging:
dependency: transitive
description:
name: logging
sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
matcher:
dependency: transitive
description:
@@ -115,6 +139,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.8.0"
moxlib:
dependency: transitive
description:
name: moxlib
sha256: "2a76a632d23ea73906964cee4463352995e40199036162217ea323a6c3846e73"
url: "https://git.polynom.me/api/packages/Moxxy/pub/"
source: hosted
version: "0.2.0"
moxxy_native:
dependency: "direct main"
description:
@@ -215,6 +247,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.0"
synchronized:
dependency: transitive
description:
name: synchronized
sha256: "5fcbd27688af6082f5abd611af56ee575342c30e87541d0245f7ff99faa02c60"
url: "https://pub.dev"
source: hosted
version: "3.1.0"
term_glyph:
dependency: transitive
description:
@@ -231,6 +271,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.4.16"
typed_data:
dependency: transitive
description:
name: typed_data
sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c
url: "https://pub.dev"
source: hosted
version: "1.3.2"
uuid:
dependency: transitive
description:
name: uuid
sha256: "648e103079f7c64a36dc7d39369cabb358d377078a051d6ae2ad3aa539519313"
url: "https://pub.dev"
source: hosted
version: "3.0.7"
vector_math:
dependency: transitive
description:

View File

@@ -29,6 +29,7 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
permission_handler: ^10.4.5
get_it: ^7.6.0
dev_dependencies:
flutter_test: