refactor
This commit is contained in:
parent
ee64faf082
commit
190dc6006c
@ -1,5 +1,4 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
|
||||||
import 'package:ffi/ffi.dart';
|
import 'package:ffi/ffi.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
@ -32,19 +31,26 @@ 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 _freeCString;
|
|
||||||
F2 _getByName;
|
|
||||||
F3 _setByName;
|
|
||||||
|
|
||||||
FfiModel() {
|
FfiModel() {
|
||||||
initialzeFFI();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
String getId() {
|
Future<Null> init() async {
|
||||||
|
await FFI.init();
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class FFI {
|
||||||
|
static F1 _freeCString;
|
||||||
|
static F2 _getByName;
|
||||||
|
static F3 _setByName;
|
||||||
|
|
||||||
|
static String getId() {
|
||||||
return getByName("remote_id");
|
return getByName("remote_id");
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Peer> peers() {
|
static List<Peer> peers() {
|
||||||
try {
|
try {
|
||||||
List<dynamic> peers = json.decode(getByName("peers"));
|
List<dynamic> peers = json.decode(getByName("peers"));
|
||||||
return peers
|
return peers
|
||||||
@ -58,15 +64,11 @@ class FfiModel with ChangeNotifier {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
void addRemote() {
|
static void connect(String id) {
|
||||||
notifyListeners();
|
|
||||||
}
|
|
||||||
|
|
||||||
void connect(String id) {
|
|
||||||
setByName("connect", id);
|
setByName("connect", id);
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, String> popEvent() {
|
static Map<String, String> popEvent() {
|
||||||
var s = getByName("event");
|
var s = getByName("event");
|
||||||
if (s == "") return null;
|
if (s == "") return null;
|
||||||
try {
|
try {
|
||||||
@ -78,7 +80,7 @@ class FfiModel with ChangeNotifier {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
void login(String password, bool remember) {
|
static void login(String password, bool remember) {
|
||||||
setByName(
|
setByName(
|
||||||
"login",
|
"login",
|
||||||
json.encode({
|
json.encode({
|
||||||
@ -87,15 +89,15 @@ class FfiModel with ChangeNotifier {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
void close() {
|
static void close() {
|
||||||
setByName("close", "");
|
setByName("close", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
void setByName(String name, String value) {
|
static void setByName(String name, String value) {
|
||||||
_setByName(Utf8.toUtf8(name), Utf8.toUtf8(value));
|
_setByName(Utf8.toUtf8(name), Utf8.toUtf8(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
String getByName(String name, {String arg = ""}) {
|
static String getByName(String name, {String arg = ""}) {
|
||||||
var p = _getByName(Utf8.toUtf8(name), Utf8.toUtf8(arg));
|
var p = _getByName(Utf8.toUtf8(name), Utf8.toUtf8(arg));
|
||||||
assert(p != null);
|
assert(p != null);
|
||||||
var res = Utf8.fromUtf8(p);
|
var res = Utf8.fromUtf8(p);
|
||||||
@ -104,7 +106,7 @@ class FfiModel with ChangeNotifier {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Null> initialzeFFI() async {
|
static Future<Null> init() async {
|
||||||
final dylib = Platform.isAndroid
|
final dylib = Platform.isAndroid
|
||||||
? DynamicLibrary.open('librustdesk.so')
|
? DynamicLibrary.open('librustdesk.so')
|
||||||
: DynamicLibrary.process();
|
: DynamicLibrary.process();
|
||||||
@ -116,7 +118,6 @@ class FfiModel with ChangeNotifier {
|
|||||||
.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;
|
||||||
setByName("init", dir);
|
setByName("init", dir);
|
||||||
notifyListeners();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,8 +149,7 @@ void showSuccess(String text) {
|
|||||||
|
|
||||||
// https://material.io/develop/flutter/components/dialogs
|
// https://material.io/develop/flutter/components/dialogs
|
||||||
void enterPasswordDialog(String id, BuildContext context) {
|
void enterPasswordDialog(String id, BuildContext context) {
|
||||||
var ffi = Provider.of<FfiModel>(context);
|
var remember = FFI.getByName("remember", arg: id) == "true";
|
||||||
var remember = ffi.getByName("remember", arg: id) == "true";
|
|
||||||
var dialog = AlertDialog(
|
var dialog = AlertDialog(
|
||||||
title: Text('Please enter your password'),
|
title: Text('Please enter your password'),
|
||||||
contentPadding: EdgeInsets.zero,
|
contentPadding: EdgeInsets.zero,
|
||||||
|
@ -14,13 +14,11 @@ class HomePage extends StatefulWidget {
|
|||||||
|
|
||||||
class _HomePageState extends State<HomePage> {
|
class _HomePageState extends State<HomePage> {
|
||||||
final _idController = TextEditingController();
|
final _idController = TextEditingController();
|
||||||
FfiModel _ffi;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
_ffi = Provider.of<FfiModel>(context);
|
Provider.of<FfiModel>(context);
|
||||||
_idController.text = _ffi.getId();
|
_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(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
|
||||||
import 'common.dart';
|
import 'common.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
import 'dart:ui' as ui;
|
||||||
|
|
||||||
class RemotePage extends StatefulWidget {
|
class RemotePage extends StatefulWidget {
|
||||||
RemotePage({Key key, this.id}) : super(key: key);
|
RemotePage({Key key, this.id}) : super(key: key);
|
||||||
@ -13,13 +13,37 @@ class RemotePage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _RemotePageState extends State<RemotePage> {
|
class _RemotePageState extends State<RemotePage> {
|
||||||
FfiModel _ffi;
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
FFI.connect(widget.id);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
_ffi = Provider.of<FfiModel>(context);
|
|
||||||
_ffi.connect(widget.id);
|
|
||||||
// https://stackoverflow.com/questions/46640116/make-flutter-application-fullscreen
|
// https://stackoverflow.com/questions/46640116/make-flutter-application-fullscreen
|
||||||
SystemChrome.setEnabledSystemUIOverlays([]);
|
SystemChrome.setEnabledSystemUIOverlays([]);
|
||||||
|
return CustomPaint(
|
||||||
|
painter: new ImageEditor(image: null),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ImageEditor extends CustomPainter {
|
||||||
|
ImageEditor({
|
||||||
|
this.image,
|
||||||
|
});
|
||||||
|
|
||||||
|
ui.Image image;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void paint(Canvas canvas, Size size) {
|
||||||
|
if (image = null) return;
|
||||||
|
canvas.drawImage(image, new Offset(0.0, 0.0), new Paint());
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool shouldRepaint(CustomPainter oldDelegate) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user