This commit is contained in:
open-trade 2020-11-16 20:20:02 +08:00
parent 818a04db16
commit 02719f45f6
2 changed files with 39 additions and 32 deletions

View File

@ -24,13 +24,14 @@ class MyTheme {
} }
typedef F1 = void Function(Pointer<Utf8>); typedef F1 = void Function(Pointer<Utf8>);
typedef F2 = Pointer<Utf8> Function(); typedef F2 = Pointer<Utf8> Function(Pointer<Utf8>);
typedef F3 = void Function(Pointer<Utf8>, Pointer<Utf8>);
// https://juejin.im/post/6844903864852807694 // https://juejin.im/post/6844903864852807694
class FfiModel with ChangeNotifier { class FfiModel with ChangeNotifier {
F1 _connectRemote;
F2 _getPeers;
F1 _freeCString; F1 _freeCString;
F2 _getByName;
F3 _setByName;
FfiModel() { FfiModel() {
initialzeFFI(); initialzeFFI();
@ -41,58 +42,65 @@ class FfiModel with ChangeNotifier {
} }
void connect(String id) { void connect(String id) {
_connectRemote(Utf8.toUtf8(id)); setByName("connect", id);
_setByName(Utf8.toUtf8("connect"), Utf8.toUtf8(id));
}
void setByName(String name, String value) {
_setByName(Utf8.toUtf8(name), Utf8.toUtf8(value));
}
String getByName(String name) {
var p = _getByName(Utf8.toUtf8(name));
var res = Utf8.fromUtf8(p);
// https://github.com/brickpop/flutter-rust-ffi
_freeCString(p);
return res;
} }
String getId() { String getId() {
return ""; return getByName("remote_id");
} }
void peers() { List<Peer> peers() {
var p = _getPeers();
try { try {
List<dynamic> peers = json.decode(Utf8.fromUtf8(p)); List<dynamic> peers = json.decode(getByName("peers"));
// https://github.com/brickpop/flutter-rust-ffi return peers
_freeCString(p);
peers = peers
.map((s) => s as List<dynamic>) .map((s) => s as List<dynamic>)
.map((s) => .map((s) =>
[s[0] as String, Peer.fromJson(s[1] as Map<String, dynamic>)]) Peer.fromJson(s[0] as String, s[1] as Map<String, dynamic>))
.toList(); .toList();
} catch (e) { } catch (e) {
print(e); print(e);
} }
return [];
} }
Future<Null> initialzeFFI() async { Future<Null> initialzeFFI() async {
final dylib = Platform.isAndroid final dylib = Platform.isAndroid
? DynamicLibrary.open('librustdesk.so') ? DynamicLibrary.open('librustdesk.so')
: DynamicLibrary.process(); : DynamicLibrary.process();
final initialize = dylib.lookupFunction<Void Function(Pointer<Utf8>), _getByName = dylib.lookupFunction<F2, F2>('get_by_name');
void Function(Pointer<Utf8>)>('initialize'); _setByName =
_connectRemote = dylib dylib.lookupFunction<Void Function(Pointer<Utf8>, Pointer<Utf8>), F3>(
.lookupFunction<Void Function(Pointer<Utf8>), F1>('connect_remote'); 'set_by_name');
_getPeers = dylib.lookupFunction<F2, F2>('get_peers');
_freeCString = dylib _freeCString = dylib
.lookupFunction<Void Function(Pointer<Utf8>), F1>('rust_cstr_free'); .lookupFunction<Void Function(Pointer<Utf8>), F1>('rust_cstr_free');
final dir = (await getApplicationDocumentsDirectory()).path; final dir = (await getApplicationDocumentsDirectory()).path;
initialize(Utf8.toUtf8(dir)); setByName("init", dir);
notifyListeners(); notifyListeners();
} }
} }
class Peer { class Peer {
final String name; final String id;
final String email; final String username;
final String hostname;
final String platform;
Peer(this.name, this.email); Peer.fromJson(String id, Map<String, dynamic> json)
: id = id,
Peer.fromJson(Map<String, dynamic> json) username = json['username'],
: name = json['name'], hostname = json['hostname'],
email = json['email']; platform = json['platform'];
Map<String, dynamic> toJson() => {
'name': name,
'email': email,
};
} }

View File

@ -18,6 +18,7 @@ class _HomePageState extends State<HomePage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
ffi = Provider.of<FfiModel>(context); ffi = Provider.of<FfiModel>(context);
idController.text = ffi.getId();
// This method is rerun every time setState is called // This method is rerun every time setState is called
return Scaffold( return Scaffold(
@ -43,7 +44,6 @@ class _HomePageState extends State<HomePage> {
} }
Widget getSearchBarUI() { Widget getSearchBarUI() {
var id = ffi.getId();
return Padding( return Padding(
padding: const EdgeInsets.only(top: 8.0), padding: const EdgeInsets.only(top: 8.0),
child: Container( child: Container(
@ -66,7 +66,6 @@ class _HomePageState extends State<HomePage> {
child: Container( child: Container(
padding: const EdgeInsets.only(left: 16, right: 16), padding: const EdgeInsets.only(left: 16, right: 16),
child: TextFormField( child: TextFormField(
initialValue: id,
style: TextStyle( style: TextStyle(
fontFamily: 'WorkSans', fontFamily: 'WorkSans',
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,