This commit is contained in:
John Smith
2022-02-15 13:40:17 -05:00
parent 125901fcd8
commit 7458d0d991
12 changed files with 191 additions and 103 deletions

View File

@@ -1,13 +1,54 @@
import 'dart:async';
import 'package:logger/logger.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:veilid/veilid.dart';
import 'package:logger_flutter_viewer/logger_flutter_viewer.dart';
// Logger
var stacklog = Logger(
printer: PrettyPrinter(
methodCount: 10,
errorMethodCount: 10,
printTime: true,
colors: true,
printEmojis: true),
output: ScreenOutput());
var log = Logger(
printer: PrettyPrinter(
methodCount: 0,
errorMethodCount: 1,
printTime: true,
colors: true,
printEmojis: true,
noBoxingByDefault: true,
),
output: ScreenOutput());
var barelog = Logger(
printer: PrettyPrinter(
methodCount: 0,
errorMethodCount: 0,
printTime: false,
colors: true,
printEmojis: true,
noBoxingByDefault: true,
),
output: ScreenOutput());
class ScreenOutput extends LogOutput {
@override
void output(OutputEvent event) {
LogConsole.output(event);
}
}
// Entrypoint
void main() {
runApp(const MyApp());
}
// Main App
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@@ -34,6 +75,10 @@ class _MyAppState extends State<MyApp> {
} on PlatformException {
veilidVersion = 'Failed to get veilid version.';
}
log.e("Error test");
log.w("Warning test");
stacklog.i("Info test with stacklog");
barelog.d("debug bare-log test");
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
@@ -50,11 +95,9 @@ class _MyAppState extends State<MyApp> {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Veilid Plugin Example App'),
),
body: Center(
child: Text('Veilid version: $_veilidVersion\n'),
title: Text('Veilid Plugin Version $_veilidVersion'),
),
body: LogConsole(dark: Theme.of(context).brightness == Brightness.dark),
),
);
}

View File

@@ -107,6 +107,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
logger:
dependency: "direct main"
description:
name: logger
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
logger_flutter_viewer:
dependency: "direct main"
description:
name: logger_flutter_viewer
url: "https://pub.dartlang.org"
source: hosted
version: "0.8.0"
matcher:
dependency: transitive
description:
@@ -135,6 +149,34 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.2"
sensors_plus:
dependency: transitive
description:
name: sensors_plus
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.1"
sensors_plus_platform_interface:
dependency: transitive
description:
name: sensors_plus_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
sensors_plus_web:
dependency: transitive
description:
name: sensors_plus_web
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
sky_engine:
dependency: transitive
description: flutter

View File

@@ -33,6 +33,8 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
logger: ^1.1.0
logger_flutter_viewer: ^0.8.0
dev_dependencies:
flutter_test:

View File

@@ -119,7 +119,14 @@ pub extern "C" fn startup_veilid_core(port: i64, config: FfiStr) {
move |update: veilid_core::VeilidUpdate| -> veilid_core::SystemPinBoxFuture<()> {
let sink = sink.clone();
Box::pin(async move {
sink.item_json(update);
match update {
veilid_core::VeilidUpdate::Shutdown => {
sink.close();
}
_ => {
sink.item_json(update);
}
}
})
},
);
@@ -159,6 +166,16 @@ pub extern "C" fn shutdown_veilid_core(port: i64) {
});
}
#[no_mangle]
pub extern "C" fn debug(port: i64, command: FfiStr) {
let command = command.into_opt_string().unwrap_or_default();
DartIsolateWrapper::new(port).spawn_result(async move {
let veilid_api = get_veilid_api().await?;
let out = veilid_api.debug(command).await?;
APIResult::Ok(out)
});
}
#[no_mangle]
pub extern "C" fn veilid_version_string() -> *mut c_char {
veilid_core::veilid_version_string().into_ffi_value()

View File

@@ -13,12 +13,12 @@ pub struct DartIsolateWrapper {
}
const MESSAGE_OK: i32 = 0;
const MESSAGE_ERR: i32 = 1;
//const MESSAGE_ERR: i32 = 1;
const MESSAGE_OK_JSON: i32 = 2;
const MESSAGE_ERR_JSON: i32 = 3;
const MESSAGE_STREAM_ITEM: i32 = 4;
//const MESSAGE_STREAM_ITEM: i32 = 4;
const MESSAGE_STREAM_ITEM_JSON: i32 = 5;
const MESSAGE_STREAM_ABORT: i32 = 6;
//const MESSAGE_STREAM_ABORT: i32 = 6;
const MESSAGE_STREAM_ABORT_JSON: i32 = 7;
const MESSAGE_STREAM_CLOSE: i32 = 8;
@@ -29,6 +29,17 @@ impl DartIsolateWrapper {
}
}
pub fn spawn_result<F, T, E>(self, future: F)
where
F: Future<Output = Result<T, E>> + Send + 'static,
T: IntoDart,
E: Serialize,
{
async_std::task::spawn(async move {
self.result(future.await);
});
}
pub fn spawn_result_json<F, T, E>(self, future: F)
where
F: Future<Output = Result<T, E>> + Send + 'static,
@@ -40,10 +51,10 @@ impl DartIsolateWrapper {
});
}
pub fn result<T: IntoDart, E: IntoDart>(&self, result: Result<T, E>) -> bool {
pub fn result<T: IntoDart, E: Serialize>(&self, result: Result<T, E>) -> bool {
match result {
Ok(v) => self.ok(v),
Err(e) => self.err(e),
Err(e) => self.err_json(e),
}
}
pub fn result_json<T: Serialize, E: Serialize>(&self, result: Result<T, E>) -> bool {
@@ -64,10 +75,10 @@ impl DartIsolateWrapper {
])
}
pub fn err<E: IntoDart>(&self, error: E) -> bool {
self.isolate
.post(vec![MESSAGE_ERR.into_dart(), error.into_dart()])
}
// pub fn err<E: IntoDart>(&self, error: E) -> bool {
// self.isolate
// .post(vec![MESSAGE_ERR.into_dart(), error.into_dart()])
// }
pub fn err_json<E: Serialize>(&self, error: E) -> bool {
self.isolate.post(vec![
@@ -89,14 +100,14 @@ impl DartIsolateStream {
}
}
pub fn item<T: IntoDart>(&self, value: T) -> bool {
let isolate = self.isolate.lock();
if let Some(isolate) = &*isolate {
isolate.post(vec![MESSAGE_STREAM_ITEM.into_dart(), value.into_dart()])
} else {
false
}
}
// pub fn item<T: IntoDart>(&self, value: T) -> bool {
// let isolate = self.isolate.lock();
// if let Some(isolate) = &*isolate {
// isolate.post(vec![MESSAGE_STREAM_ITEM.into_dart(), value.into_dart()])
// } else {
// false
// }
// }
pub fn item_json<T: Serialize>(&self, value: T) -> bool {
let isolate = self.isolate.lock();
@@ -110,14 +121,14 @@ impl DartIsolateStream {
}
}
pub fn abort<E: IntoDart>(self, error: E) -> bool {
let mut isolate = self.isolate.lock();
if let Some(isolate) = isolate.take() {
isolate.post(vec![MESSAGE_STREAM_ABORT.into_dart(), error.into_dart()])
} else {
false
}
}
// pub fn abort<E: IntoDart>(self, error: E) -> bool {
// let mut isolate = self.isolate.lock();
// if let Some(isolate) = isolate.take() {
// isolate.post(vec![MESSAGE_STREAM_ABORT.into_dart(), error.into_dart()])
// } else {
// false
// }
// }
pub fn abort_json<E: Serialize>(self, error: E) -> bool {
let mut isolate = self.isolate.lock();