refactor set/getByName "peers" "option"

This commit is contained in:
csf
2022-08-08 17:53:51 +08:00
parent 0ef1659b87
commit c5d0628291
15 changed files with 414 additions and 553 deletions

View File

@@ -2,6 +2,7 @@ import 'dart:convert';
import 'package:dash_chat_2/dash_chat_2.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hbb/models/platform_model.dart';
import '../../mobile/widgets/overlay.dart';
import 'model.dart';
@@ -72,7 +73,7 @@ class ChatModel with ChangeNotifier {
}
}
receive(int id, String text) {
receive(int id, String text) async {
if (text.isEmpty) return;
// first message show overlay icon
if (chatIconOverlayEntry == null) {
@@ -82,7 +83,7 @@ class ChatModel with ChangeNotifier {
if (id == clientModeID) {
chatUser = ChatUser(
firstName: _ffi.target?.ffiModel.pi.username,
id: _ffi.target?.getId() ?? "",
id: await bind.mainGetLastRemoteId(),
);
} else {
final client = _ffi.target?.serverModel.clients[id];

View File

@@ -444,7 +444,7 @@ class FileModel extends ChangeNotifier {
items.items.forEach((from) async {
_jobId++;
await bind.sessionSendFiles(
id: '${_ffi.target?.getId()}',
id: await bind.mainGetLastRemoteId(),
actId: _jobId,
path: from.path,
to: PathUtil.join(toPath, from.name, isWindows),

View File

@@ -881,11 +881,6 @@ class FFI {
this.qualityMonitorModel = QualityMonitorModel(WeakReference(this));
}
/// Get the remote id for current client.
String getId() {
return getByName('remote_id'); // TODO
}
/// Send a mouse tap event(down and up).
void tap(MouseButtons button) {
sendMouse('down', button);
@@ -963,9 +958,9 @@ class FFI {
}
/// List the saved peers.
List<Peer> peers() {
Future<List<Peer>> peers() async {
try {
var str = getByName('peers'); // TODO
var str = await bind.mainGetRecentPeers();
if (str == "") return [];
List<dynamic> peers = json.decode(str);
return peers
@@ -1046,33 +1041,6 @@ class FFI {
platformFFI.setByName(name, value);
}
String getOption(String name) {
return platformFFI.getByName("option", name);
}
Future<String> getLocalOption(String name) {
return bind.mainGetLocalOption(key: name);
}
Future<void> setLocalOption(String key, String value) {
return bind.mainSetLocalOption(key: key, value: value);
}
Future<String> getPeerOption(String id, String key) {
return bind.mainGetPeerOption(id: id, key: key);
}
Future<void> setPeerOption(String id, String key, String value) {
return bind.mainSetPeerOption(id: id, key: key, value: value);
}
void setOption(String name, String value) {
Map<String, String> res = Map()
..["name"] = name
..["value"] = value;
return platformFFI.setByName('option', jsonEncode(res));
}
handleMouse(Map<String, dynamic> evt, {double tabBarHeight = 0.0}) {
var type = '';
var isMove = false;
@@ -1148,8 +1116,8 @@ class FFI {
return await bind.mainGetSoundInputs();
}
String getDefaultAudioInput() {
final input = getOption('audio-input');
Future<String> getDefaultAudioInput() async {
final input = await bind.mainGetOption(key: 'audio-input');
if (input.isEmpty && Platform.isWindows) {
return "System Sound";
}
@@ -1157,11 +1125,14 @@ class FFI {
}
void setDefaultAudioInput(String input) {
setOption('audio-input', input);
bind.mainSetOption(key: 'audio-input', value: input);
}
Future<Map<String, String>> getHttpHeaders() async {
return {"Authorization": "Bearer " + await getLocalOption("access_token")};
return {
"Authorization":
"Bearer " + await bind.mainGetLocalOption(key: "access_token")
};
}
}
@@ -1233,11 +1204,12 @@ void initializeCursorAndCanvas(FFI ffi) async {
/// Translate text based on the pre-defined dictionary.
/// note: params [FFI?] can be used to replace global FFI implementation
/// for example: during global initialization, gFFI not exists yet.
String translate(String name, {FFI? ffi}) {
if (name.startsWith('Failed to') && name.contains(': ')) {
return name.split(': ').map((x) => translate(x)).join(': ');
}
var a = 'translate';
var b = '{"locale": "$localeName", "text": "$name"}';
return (ffi ?? gFFI).getByName(a, b);
}
// String translate(String name, {FFI? ffi}) {
// if (name.startsWith('Failed to') && name.contains(': ')) {
// return name.split(': ').map((x) => translate(x)).join(': ');
// }
// var a = 'translate';
// var b = '{"locale": "$localeName", "text": "$name"}';
//
// return (ffi ?? gFFI).getByName(a, b);
// }

View File

@@ -29,6 +29,7 @@ typedef HandleEvent = void Function(Map<String, dynamic> evt);
class PlatformFFI {
String _dir = '';
String _homeDir = '';
F2? _translate;
F2? _getByName;
F3? _setByName;
var _eventHandlers = Map<String, Map<String, HandleEvent>>();
@@ -75,6 +76,19 @@ class PlatformFFI {
}
}
String translate(String name, String locale) {
if (_translate == null) return '';
var a = name.toNativeUtf8();
var b = locale.toNativeUtf8();
var p = _translate!(a, b);
assert(p != nullptr);
final res = p.toDartString();
calloc.free(p);
calloc.free(a);
calloc.free(b);
return res;
}
/// Send **get** command to the Rust core based on [name] and [arg].
/// Return the result as a string.
String getByName(String name, [String arg = '']) {
@@ -118,6 +132,7 @@ class PlatformFFI {
: DynamicLibrary.process();
debugPrint('initializing FFI ${_appType}');
try {
_translate = dylib.lookupFunction<F2, F2>('translate');
_getByName = dylib.lookupFunction<F2, F2>('get_by_name');
_setByName =
dylib.lookupFunction<Void Function(Pointer<Utf8>, Pointer<Utf8>), F3>(

View File

@@ -3,6 +3,7 @@ import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_hbb/models/platform_model.dart';
import 'package:wakelock/wakelock.dart';
import '../common.dart';
@@ -57,7 +58,7 @@ class ServerModel with ChangeNotifier {
set verificationMethod(String method) {
_verificationMethod = method;
gFFI.setOption("verification-method", method);
bind.mainSetOption(key: "verification-method", value: method);
}
String get temporaryPasswordLength {
@@ -70,7 +71,7 @@ class ServerModel with ChangeNotifier {
set temporaryPasswordLength(String length) {
_temporaryPasswordLength = length;
gFFI.setOption("temporary-password-length", length);
bind.mainSetOption(key: "temporary-password-length", value: length);
}
TextEditingController get serverId => _serverId;
@@ -85,7 +86,7 @@ class ServerModel with ChangeNotifier {
ServerModel(this.parent) {
() async {
_emptyIdShow = translate("Generating ...", ffi: this.parent.target);
_emptyIdShow = translate("Generating ...");
_serverId = TextEditingController(text: this._emptyIdShow);
/**
* 1. check android permission
@@ -153,11 +154,13 @@ class ServerModel with ChangeNotifier {
});
}
updatePasswordModel() {
updatePasswordModel() async {
var update = false;
final temporaryPassword = gFFI.getByName("temporary_password");
final verificationMethod = gFFI.getOption("verification-method");
final temporaryPasswordLength = gFFI.getOption("temporary-password-length");
final verificationMethod =
await bind.mainGetOption(key: "verification-method");
final temporaryPasswordLength =
await bind.mainGetOption(key: "temporary-password-length");
final oldPwdText = _serverPasswd.text;
if (_serverPasswd.text != temporaryPassword) {
_serverPasswd.text = temporaryPassword;
@@ -325,7 +328,7 @@ class ServerModel with ChangeNotifier {
const maxCount = 10;
while (count < maxCount) {
await Future.delayed(Duration(seconds: 1));
final id = parent.target?.getByName("server_id") ?? "";
final id = await bind.mainGetMyId();
if (id.isEmpty) {
continue;
} else {