Skip to content

Commit

Permalink
opt.: use json_serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Aug 15, 2024
1 parent 195ddd2 commit 7ce3854
Show file tree
Hide file tree
Showing 18 changed files with 223 additions and 198 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Please only download pkgs from the source that **you trust**!


## 🔖 Feature
- `Status chart` (CPU, Sensors, GPU...), `SSH` Term, `SFTP`, `Docker & Process`...
- `Status chart` (CPU, Sensors, GPU...), `SSH` Term, `SFTP`, `Docker & Process & Systemd`...
- Platform specific: `Bio auth``Msg push``Home widget``watchOS App`...
- English, 简体中文; Deutsch [@its-tom](https://github.com/its-tom), 繁體中文 [@kalashnikov](https://github.com/kalashnikov), Indonesian [@azkadev](https://github.com/azkadev), Français [@FrancXPT](https://github.com/FrancXPT), Dutch [@QazCetelic](https://github.com/QazCetelic), Türkçe [@mikropsoft](https://github.com/mikropsoft); Español, Русский язык, Português, 日本語 (Generated by GPT)

Expand Down
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Linux / Windows | [GitHub](https://github.com/lollipopkit/flutter_server_box/rel


## 🔖 特点
- `状态图表`(CPU、传感器、GPU 等), `SSH` 终端, `SFTP`, `Docker & 进程` 管理...
- `状态图表`(CPU、传感器、GPU 等), `SSH` 终端, `SFTP`, `Docker & 进程 & Systemd` 管理...
- 特殊支持:`生物认证``推送``桌面小部件``watchOS App``跟随系统颜色`...
- 本地化
- English, 简体中文
Expand Down
36 changes: 9 additions & 27 deletions lib/data/model/app/backup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';
import 'dart:io';

import 'package:fl_lib/fl_lib.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:logging/logging.dart';
import 'package:server_box/data/model/server/private_key_info.dart';
import 'package:server_box/data/model/server/server_private_info.dart';
Expand All @@ -11,10 +12,13 @@ import 'package:server_box/data/res/provider.dart';
import 'package:server_box/data/res/rebuild.dart';
import 'package:server_box/data/res/store.dart';

part 'backup.g.dart';

const backupFormatVersion = 1;

final _logger = Logger('Backup');

@JsonSerializable()
class Backup {
// backup format version
final int version;
Expand All @@ -37,31 +41,9 @@ class Backup {
this.lastModTime,
});

Backup.fromJson(Map<String, dynamic> json)
: version = json['version'] as int,
date = json['date'],
spis = (json['spis'] as List)
.map((e) => ServerPrivateInfo.fromJson(e))
.toList(),
snippets =
(json['snippets'] as List).map((e) => Snippet.fromJson(e)).toList(),
keys = (json['keys'] as List)
.map((e) => PrivateKeyInfo.fromJson(e))
.toList(),
container = json['container'] ?? {},
lastModTime = json['lastModTime'],
history = json['history'] ?? {};

Map<String, dynamic> toJson() => {
'version': version,
'date': date,
'spis': spis,
'snippets': snippets,
'keys': keys,
'container': container,
'lastModTime': lastModTime,
'history': history,
};
factory Backup.fromJson(Map<String, dynamic> json) => _$BackupFromJson(json);

Map<String, dynamic> toJson() => _$BackupToJson(this);

Backup.loadFromStore()
: version = backupFormatVersion,
Expand Down Expand Up @@ -201,8 +183,8 @@ class Backup {
_logger.info('Restore success');
}

Backup.fromJsonString(String raw)
: this.fromJson(json.decode(_diyDecrypt(raw)));
factory Backup.fromJsonString(String raw) =>
Backup.fromJson(json.decode(_diyDecrypt(raw)));
}

String _diyEncrypt(String raw) => json.encode(
Expand Down
35 changes: 35 additions & 0 deletions lib/data/model/app/backup.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 5 additions & 36 deletions lib/data/model/server/custom.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:hive_flutter/adapters.dart';
import 'package:json_annotation/json_annotation.dart';

part 'custom.g.dart';

@JsonSerializable()
@HiveType(typeId: 7)
final class ServerCustom {
// @HiveField(0)
Expand All @@ -28,44 +30,11 @@ final class ServerCustom {
this.logoUrl,
});

static ServerCustom fromJson(Map<String, dynamic> json) {
//final temperature = json["temperature"] as String?;
final pveAddr = json['pveAddr'] as String?;
final pveIgnoreCert = json['pveIgnoreCert'] as bool;
final cmds = json['cmds'] as Map<String, dynamic>?;
final preferTempDev = json['preferTempDev'] as String?;
final logoUrl = json['logoUrl'] as String?;
return ServerCustom(
//temperature: temperature,
pveAddr: pveAddr,
pveIgnoreCert: pveIgnoreCert,
cmds: cmds?.cast<String, String>(),
preferTempDev: preferTempDev,
logoUrl: logoUrl,
);
}
factory ServerCustom.fromJson(Map<String, dynamic> json) =>
_$ServerCustomFromJson(json);

Map<String, dynamic> toJson() {
final json = <String, dynamic>{};
// if (temperature != null) {
// json["temperature"] = temperature;
// }
if (pveAddr != null) {
json['pveAddr'] = pveAddr;
}
json['pveIgnoreCert'] = pveIgnoreCert;
Map<String, dynamic> toJson() => _$ServerCustomToJson(this);

if (cmds != null) {
json['cmds'] = cmds;
}
if (preferTempDev != null) {
json['preferTempDev'] = preferTempDev;
}
if (logoUrl != null) {
json['logoUrl'] = logoUrl;
}
return json;
}

@override
String toString() {
Expand Down
23 changes: 23 additions & 0 deletions lib/data/model/server/custom.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 8 additions & 11 deletions lib/data/model/server/private_key_info.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import 'package:hive_flutter/hive_flutter.dart';
import 'package:json_annotation/json_annotation.dart';

part 'private_key_info.g.dart';

@JsonSerializable()
@HiveType(typeId: 1)
class PrivateKeyInfo {
@HiveField(0)
final String id;
@JsonKey(name: 'private_key')
@HiveField(1)
final String key;

Expand All @@ -14,6 +17,11 @@ class PrivateKeyInfo {
required this.key,
});

factory PrivateKeyInfo.fromJson(Map<String, dynamic> json) =>
_$PrivateKeyInfoFromJson(json);

Map<String, dynamic> toJson() => _$PrivateKeyInfoToJson(this);

String? get type {
final lines = key.split('\n');
if (lines.length < 2) {
Expand All @@ -26,15 +34,4 @@ class PrivateKeyInfo {
}
return splited[1];
}

PrivateKeyInfo.fromJson(Map<String, dynamic> json)
: id = json['id'].toString(),
key = json['private_key'].toString();

Map<String, dynamic> toJson() {
final data = <String, String>{};
data['id'] = id;
data['private_key'] = key;
return data;
}
}
16 changes: 16 additions & 0 deletions lib/data/model/server/private_key_info.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 7 additions & 77 deletions lib/data/model/server/server_private_info.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';

import 'package:hive_flutter/hive_flutter.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:server_box/data/model/server/custom.dart';
import 'package:server_box/data/model/server/server.dart';
import 'package:server_box/data/model/server/wol_cfg.dart';
Expand All @@ -11,6 +12,7 @@ import 'package:server_box/data/model/app/error.dart';
part 'server_private_info.g.dart';

/// In former version, it's called `ServerPrivateInfo`.
@JsonSerializable()
@HiveType(typeId: 3)
class ServerPrivateInfo {
@HiveField(0)
Expand All @@ -25,6 +27,7 @@ class ServerPrivateInfo {
final String? pwd;

/// [id] of private key
@JsonKey(name: 'pubKeyId')
@HiveField(5)
final String? keyId;
@HiveField(6)
Expand Down Expand Up @@ -66,83 +69,10 @@ class ServerPrivateInfo {
this.envs,
}) : id = '$user@$ip:$port';

static ServerPrivateInfo fromJson(Map<String, dynamic> json) {
final ip = json['ip'] as String? ?? '';
final port = json['port'] as int? ?? 22;
final user = json['user'] as String? ?? 'root';
final name = json['name'] as String? ?? '';
final pwd = json['pwd'] as String? ?? json['authorization'] as String?;
final keyId = json['pubKeyId'] as String?;
final tags = (json['tags'] as List?)?.cast<String>();
final alterUrl = json['alterUrl'] as String?;
final autoConnect = json['autoConnect'] as bool? ?? true;
final jumpId = json['jumpId'] as String?;
final custom = json['customCmd'] == null
? null
: ServerCustom.fromJson(json['custom'].cast<String, dynamic>());
final wolCfg = json['wolCfg'] == null
? null
: WakeOnLanCfg.fromJson(json['wolCfg'].cast<String, dynamic>());
final envs_ = json['envs'] as Map<String, dynamic>?;
final envs = <String, String>{};
if (envs_ != null) {
envs_.forEach((key, value) {
if (value is String) {
envs[key] = value;
}
});
}

return ServerPrivateInfo(
name: name,
ip: ip,
port: port,
user: user,
pwd: pwd,
keyId: keyId,
tags: tags,
alterUrl: alterUrl,
autoConnect: autoConnect,
jumpId: jumpId,
custom: custom,
wolCfg: wolCfg,
envs: envs.isEmpty ? null : envs,
);
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['name'] = name;
data['ip'] = ip;
data['port'] = port;
data['user'] = user;
if (pwd != null) {
data['pwd'] = pwd;
}
if (keyId != null) {
data['pubKeyId'] = keyId;
}
if (tags != null) {
data['tags'] = tags;
}
if (alterUrl != null) {
data['alterUrl'] = alterUrl;
}
data['autoConnect'] = autoConnect;
if (jumpId != null) {
data['jumpId'] = jumpId;
}
if (custom != null) {
data['custom'] = custom?.toJson();
}
if (wolCfg != null) {
data['wolCfg'] = wolCfg?.toJson();
}
if (envs != null) {
data['envs'] = envs;
}
return data;
}
factory ServerPrivateInfo.fromJson(Map<String, dynamic> json) =>
_$ServerPrivateInfoFromJson(json);

Map<String, dynamic> toJson() => _$ServerPrivateInfoToJson(this);

String toJsonString() => json.encode(toJson());

Expand Down
Loading

0 comments on commit 7ce3854

Please sign in to comment.