#155 new: option autoConnect

This commit is contained in:
lollipopkit
2023-09-05 21:14:39 +08:00
parent e32f7536b5
commit 191ffe0173
18 changed files with 142 additions and 66 deletions

View File

@@ -7,23 +7,25 @@ part 'server_private_info.g.dart';
@HiveType(typeId: 3)
class ServerPrivateInfo {
@HiveField(0)
late String name;
final String name;
@HiveField(1)
late String ip;
final String ip;
@HiveField(2)
late int port;
final int port;
@HiveField(3)
late String user;
final String user;
@HiveField(4)
late String pwd;
final String? pwd;
@HiveField(5)
String? pubKeyId;
final String? pubKeyId;
@HiveField(6)
List<String>? tags;
final List<String>? tags;
@HiveField(7)
String? alterUrl;
final String? alterUrl;
@HiveField(8)
final bool? autoConnect;
late String id;
final String id;
ServerPrivateInfo({
required this.name,
@@ -34,19 +36,23 @@ class ServerPrivateInfo {
this.pubKeyId,
this.tags,
this.alterUrl,
this.autoConnect,
}) : id = '$user@$ip:$port';
ServerPrivateInfo.fromJson(Map<String, dynamic> json) {
name = json["name"].toString();
ip = json["ip"].toString();
port = json["port"] ?? 22;
user = json["user"].toString();
pwd = json["authorization"].toString();
pubKeyId = json["pubKeyId"]?.toString();
id = '$user@$ip:$port';
tags = json["tags"]?.cast<String>();
alterUrl = json["alterUrl"]?.toString();
}
/// TODO: if any field is changed, remember to update [id] [name]
ServerPrivateInfo.fromJson(Map<String, dynamic> json)
: ip = json["ip"].toString(),
port = json["port"] ?? 22,
user = json["user"]?.toString() ?? 'root',
id =
'${json["user"]?.toString() ?? "root"}@${json["ip"].toString()}:${json["port"] ?? 22}',
name = json["name"]?.toString() ??
'${json["user"]?.toString() ?? 'root'}@${json["ip"].toString()}:${json["port"] ?? 22}',
pwd = json["authorization"]?.toString(),
pubKeyId = json["pubKeyId"]?.toString(),
tags = json["tags"]?.cast<String>(),
alterUrl = json["alterUrl"]?.toString(),
autoConnect = json["autoConnect"];
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
@@ -58,6 +64,7 @@ class ServerPrivateInfo {
data["pubKeyId"] = pubKeyId;
data["tags"] = tags;
data["alterUrl"] = alterUrl;
data["autoConnect"] = autoConnect;
return data;
}

View File

@@ -21,17 +21,18 @@ class ServerPrivateInfoAdapter extends TypeAdapter<ServerPrivateInfo> {
ip: fields[1] as String,
port: fields[2] as int,
user: fields[3] as String,
pwd: fields[4] as String,
pwd: fields[4] as String?,
pubKeyId: fields[5] as String?,
tags: (fields[6] as List?)?.cast<String>(),
alterUrl: fields[7] as String?,
autoConnect: fields[8] as bool?,
);
}
@override
void write(BinaryWriter writer, ServerPrivateInfo obj) {
writer
..writeByte(8)
..writeByte(9)
..writeByte(0)
..write(obj.name)
..writeByte(1)
@@ -47,7 +48,9 @@ class ServerPrivateInfoAdapter extends TypeAdapter<ServerPrivateInfo> {
..writeByte(6)
..write(obj.tags)
..writeByte(7)
..write(obj.alterUrl);
..write(obj.alterUrl)
..writeByte(8)
..write(obj.autoConnect);
}
@override

View File

@@ -94,6 +94,8 @@ class ServerProvider extends ChangeNotifier {
return Server(spi, initStatus, null, ServerState.disconnected);
}
/// if [spi] is specificed then only refresh this server
/// [onlyFailed] only refresh failed servers
Future<void> refreshData({
ServerPrivateInfo? spi,
bool onlyFailed = false,
@@ -107,6 +109,7 @@ class ServerProvider extends ChangeNotifier {
if (s.state != ServerState.failed) return;
_limiter.reset(s.spi.id);
}
if (!(s.spi.autoConnect ?? true)) return;
return await _getData(s.spi);
}));
}

View File

@@ -2,8 +2,8 @@
class BuildData {
static const String name = "ServerBox";
static const int build = 522;
static const int build = 525;
static const String engine = "3.13.1";
static const String buildAt = "2023-09-01 12:58:00.909396";
static const int modifications = 2;
static const String buildAt = "2023-09-03 17:54:05.367410";
static const int modifications = 4;
}

View File

@@ -13,6 +13,7 @@
"attention": "Achtung",
"auto": "System folgen",
"autoCheckUpdate": "Aktualisierung automatisch prüfen",
"autoConnect": "Automatisch verbinden",
"autoUpdateHomeWidget": "Home-Widget automatisch aktualisieren",
"backup": "Backup",
"backupAndRestore": "Backup und Wiederherstellung",

View File

@@ -13,6 +13,7 @@
"attention": "Attention",
"auto": "Auto",
"autoCheckUpdate": "Auto check update",
"autoConnect": "Auto connect",
"autoUpdateHomeWidget": "Auto update home widget",
"backup": "Backup",
"backupAndRestore": "Backup and Restore",

View File

@@ -13,6 +13,7 @@
"attention": "Perhatian",
"auto": "Auto",
"autoCheckUpdate": "Periksa pembaruan otomatis",
"autoConnect": "Hubungkan otomatis",
"autoUpdateHomeWidget": "Widget Rumah Pembaruan Otomatis",
"backup": "Cadangan",
"backupAndRestore": "Cadangan dan Pulihkan",

View File

@@ -13,6 +13,7 @@
"attention": "注意",
"auto": "自动",
"autoCheckUpdate": "自动检查更新",
"autoConnect": "自动连接",
"autoUpdateHomeWidget": "自动更新桌面小部件",
"backup": "备份",
"backupAndRestore": "备份和恢复",

View File

@@ -13,6 +13,7 @@
"attention": "注意",
"auto": "自動",
"autoCheckUpdate": "自動檢查更新",
"autoConnect": "自動連接",
"autoUpdateHomeWidget": "自動更新桌面小部件",
"backup": "備份",
"backupAndRestore": "備份和還原",

View File

@@ -44,7 +44,8 @@ class _ServerEditPageState extends State<ServerEditPage> with AfterLayoutMixin {
late ServerProvider _serverProvider;
late S _s;
bool usePublicKey = false;
final usePublicKey = ValueNotifier(false);
final autoConnect = ValueNotifier(true);
int? _pubKeyIndex;
PrivateKeyInfo? _keyInfo;
List<String> _tags = [];
@@ -178,13 +179,24 @@ class _ServerEditPageState extends State<ServerEditPage> with AfterLayoutMixin {
Text(_s.keyAuth),
width13,
Switch(
value: usePublicKey,
onChanged: (val) => setState(() => usePublicKey = val),
value: usePublicKey.value,
onChanged: (val) => setState(() => usePublicKey.value = val),
),
],
),
Row(
children: [
width13,
Text(_s.autoConnect),
width13,
Switch(
value: autoConnect.value,
onChanged: (val) => setState(() => autoConnect.value = val),
),
],
)
];
if (usePublicKey) {
if (usePublicKey.value) {
children.add(_buildKeyAuth());
} else {
children.add(Input(
@@ -254,7 +266,7 @@ class _ServerEditPageState extends State<ServerEditPage> with AfterLayoutMixin {
showSnackBar(context, Text(_s.plzEnterHost));
return;
}
if (!usePublicKey && _passwordController.text == '') {
if (!usePublicKey.value && _passwordController.text == '') {
final cancel = await showRoundDialog<bool>(
context: context,
title: Text(_s.attention),
@@ -274,7 +286,7 @@ class _ServerEditPageState extends State<ServerEditPage> with AfterLayoutMixin {
return;
}
}
if (usePublicKey && _pubKeyIndex == -1) {
if (usePublicKey.value && _pubKeyIndex == -1) {
showSnackBar(context, Text(_s.plzSelectKey));
return;
}
@@ -296,10 +308,11 @@ class _ServerEditPageState extends State<ServerEditPage> with AfterLayoutMixin {
port: int.parse(_portController.text),
user: _usernameController.text,
pwd: authorization,
pubKeyId: usePublicKey ? _keyInfo!.id : null,
pubKeyId: usePublicKey.value ? _keyInfo!.id : null,
tags: _tags,
alterUrl:
_alterUrlController.text == '' ? null : _alterUrlController.text,
autoConnect: autoConnect.value,
);
if (widget.spi == null) {
@@ -336,12 +349,13 @@ class _ServerEditPageState extends State<ServerEditPage> with AfterLayoutMixin {
if (widget.spi?.pubKeyId == null) {
_passwordController.text = widget.spi?.pwd ?? '';
} else {
usePublicKey = true;
usePublicKey.value = true;
}
if (widget.spi?.tags != null) {
_tags = widget.spi!.tags!;
}
_alterUrlController.text = widget.spi?.alterUrl ?? '';
autoConnect.value = widget.spi?.autoConnect ?? true;
setState(() {});
}
}

View File

@@ -193,13 +193,19 @@ class _ServerPageState extends State<ServerPage>
_showFailReason(si.status);
}
},
onLongPress: () => setState(() {
if (_flipedCardIds.contains(si.spi.id)) {
_flipedCardIds.remove(si.spi.id);
onLongPress: () {
if (si.state == ServerState.finished) {
setState(() {
if (_flipedCardIds.contains(si.spi.id)) {
_flipedCardIds.remove(si.spi.id);
} else {
_flipedCardIds.add(si.spi.id);
}
});
} else {
_flipedCardIds.add(si.spi.id);
AppRoute.serverEdit(spi: si.spi).go(context);
}
}),
},
child: Padding(
padding: const EdgeInsets.all(13),
child: _buildRealServerCard(si),
@@ -221,10 +227,12 @@ class _ServerPageState extends State<ServerPage>
final title = _buildServerCardTitle(srv.status, srv.state, srv.spi);
final List<Widget> children = [title];
if (_flipedCardIds.contains(srv.spi.id)) {
children.addAll(_buildFlipedCard(srv));
} else if (srv.state == ServerState.finished) {
children.addAll(_buildNormalCard(srv.status, srv.spi));
if (srv.state == ServerState.finished) {
if (_flipedCardIds.contains(srv.spi.id)) {
children.addAll(_buildFlipedCard(srv));
} else {
children.addAll(_buildNormalCard(srv.status, srv.spi));
}
}
return AnimatedContainer(
@@ -304,6 +312,22 @@ class _ServerPageState extends State<ServerPage>
ServerState cs,
ServerPrivateInfo spi,
) {
Widget? rightCorner;
if (!(spi.autoConnect ?? true) && cs == ServerState.disconnected) {
rightCorner = InkWell(
onTap: () => _serverProvider.refreshData(spi: spi),
child: const Padding(
padding: EdgeInsets.symmetric(horizontal: 7),
child: Icon(
Icons.link,
size: 21,
color: Colors.grey,
),
),
);
} else if (_settingStore.serverTabUseOldUI.fetch()) {
rightCorner = ServerFuncBtnsTopRight(spi: spi, s: _s);
}
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 7),
child: Row(
@@ -326,8 +350,7 @@ class _ServerPageState extends State<ServerPage>
Row(
children: [
_buildTopRightText(ss, cs),
if (_settingStore.serverTabUseOldUI.fetch())
ServerFuncBtnsTopRight(spi: spi, s: _s)
if (rightCorner != null) rightCorner,
],
)
],
@@ -489,12 +512,12 @@ class _ServerPageState extends State<ServerPage>
}
double _calcCardHeight(ServerState cs, String id) {
if (_flipedCardIds.contains(id)) {
return 77.0;
}
if (cs != ServerState.finished) {
return 23.0;
}
if (_flipedCardIds.contains(id)) {
return 77.0;
}
if (_settingStore.moveOutServerTabFuncBtns.fetch() &&
// Discussion #146
!_settingStore.serverTabUseOldUI.fetch()) {