From 02719f45f64eb518ce63340a88fd39163e9cd0cf Mon Sep 17 00:00:00 2001 From: open-trade Date: Mon, 16 Nov 2020 20:20:02 +0800 Subject: [PATCH] refactor --- flutter_hbb/lib/common.dart | 68 +++++++++++++++++++--------------- flutter_hbb/lib/home_page.dart | 3 +- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/flutter_hbb/lib/common.dart b/flutter_hbb/lib/common.dart index 08dde00c6..7f041f427 100644 --- a/flutter_hbb/lib/common.dart +++ b/flutter_hbb/lib/common.dart @@ -24,13 +24,14 @@ class MyTheme { } typedef F1 = void Function(Pointer); -typedef F2 = Pointer Function(); +typedef F2 = Pointer Function(Pointer); +typedef F3 = void Function(Pointer, Pointer); // https://juejin.im/post/6844903864852807694 class FfiModel with ChangeNotifier { - F1 _connectRemote; - F2 _getPeers; F1 _freeCString; + F2 _getByName; + F3 _setByName; FfiModel() { initialzeFFI(); @@ -41,58 +42,65 @@ class FfiModel with ChangeNotifier { } 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() { - return ""; + return getByName("remote_id"); } - void peers() { - var p = _getPeers(); + List peers() { try { - List peers = json.decode(Utf8.fromUtf8(p)); - // https://github.com/brickpop/flutter-rust-ffi - _freeCString(p); - peers = peers + List peers = json.decode(getByName("peers")); + return peers .map((s) => s as List) .map((s) => - [s[0] as String, Peer.fromJson(s[1] as Map)]) + Peer.fromJson(s[0] as String, s[1] as Map)) .toList(); } catch (e) { print(e); } + return []; } Future initialzeFFI() async { final dylib = Platform.isAndroid ? DynamicLibrary.open('librustdesk.so') : DynamicLibrary.process(); - final initialize = dylib.lookupFunction), - void Function(Pointer)>('initialize'); - _connectRemote = dylib - .lookupFunction), F1>('connect_remote'); - _getPeers = dylib.lookupFunction('get_peers'); + _getByName = dylib.lookupFunction('get_by_name'); + _setByName = + dylib.lookupFunction, Pointer), F3>( + 'set_by_name'); _freeCString = dylib .lookupFunction), F1>('rust_cstr_free'); final dir = (await getApplicationDocumentsDirectory()).path; - initialize(Utf8.toUtf8(dir)); + setByName("init", dir); notifyListeners(); } } class Peer { - final String name; - final String email; + final String id; + final String username; + final String hostname; + final String platform; - Peer(this.name, this.email); - - Peer.fromJson(Map json) - : name = json['name'], - email = json['email']; - - Map toJson() => { - 'name': name, - 'email': email, - }; + Peer.fromJson(String id, Map json) + : id = id, + username = json['username'], + hostname = json['hostname'], + platform = json['platform']; } diff --git a/flutter_hbb/lib/home_page.dart b/flutter_hbb/lib/home_page.dart index d117cb5f3..bf02c4436 100644 --- a/flutter_hbb/lib/home_page.dart +++ b/flutter_hbb/lib/home_page.dart @@ -18,6 +18,7 @@ class _HomePageState extends State { @override Widget build(BuildContext context) { ffi = Provider.of(context); + idController.text = ffi.getId(); // This method is rerun every time setState is called return Scaffold( @@ -43,7 +44,6 @@ class _HomePageState extends State { } Widget getSearchBarUI() { - var id = ffi.getId(); return Padding( padding: const EdgeInsets.only(top: 8.0), child: Container( @@ -66,7 +66,6 @@ class _HomePageState extends State { child: Container( padding: const EdgeInsets.only(left: 16, right: 16), child: TextFormField( - initialValue: id, style: TextStyle( fontFamily: 'WorkSans', fontWeight: FontWeight.bold,