Skip to content

Commit

Permalink
refactors (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit authored Aug 15, 2024
1 parent 7e5bb54 commit 38366a2
Show file tree
Hide file tree
Showing 45 changed files with 526 additions and 639 deletions.
16 changes: 8 additions & 8 deletions lib/core/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ class AppRoutes {
return Future.value(null);
}

static AppRoutes serverDetail({Key? key, required ServerPrivateInfo spi}) {
static AppRoutes serverDetail({Key? key, required Spi spi}) {
return AppRoutes(ServerDetailPage(key: key, spi: spi), 'server_detail');
}

static AppRoutes serverTab({Key? key}) {
return AppRoutes(ServerPage(key: key), 'server_tab');
}

static AppRoutes serverEdit({Key? key, ServerPrivateInfo? spi}) {
static AppRoutes serverEdit({Key? key, Spi? spi}) {
return AppRoutes(
ServerEditPage(spi: spi),
'server_${spi == null ? 'add' : 'edit'}',
Expand Down Expand Up @@ -97,7 +97,7 @@ class AppRoutes {

static AppRoutes ssh({
Key? key,
required ServerPrivateInfo spi,
required Spi spi,
String? initCmd,
Snippet? initSnippet,
}) {
Expand Down Expand Up @@ -133,7 +133,7 @@ class AppRoutes {

static AppRoutes sftp(
{Key? key,
required ServerPrivateInfo spi,
required Spi spi,
String? initPath,
bool isSelect = false}) {
return AppRoutes(
Expand All @@ -150,7 +150,7 @@ class AppRoutes {
return AppRoutes(BackupPage(key: key), 'backup');
}

static AppRoutes docker({Key? key, required ServerPrivateInfo spi}) {
static AppRoutes docker({Key? key, required Spi spi}) {
return AppRoutes(ContainerPage(key: key, spi: spi), 'docker');
}

Expand Down Expand Up @@ -186,7 +186,7 @@ class AppRoutes {
return AppRoutes(PingPage(key: key), 'ping');
}

static AppRoutes process({Key? key, required ServerPrivateInfo spi}) {
static AppRoutes process({Key? key, required Spi spi}) {
return AppRoutes(ProcessPage(key: key, spi: spi), 'process');
}

Expand Down Expand Up @@ -220,15 +220,15 @@ class AppRoutes {
'snippet_result');
}

static AppRoutes iperf({Key? key, required ServerPrivateInfo spi}) {
static AppRoutes iperf({Key? key, required Spi spi}) {
return AppRoutes(IPerfPage(key: key, spi: spi), 'iperf');
}

static AppRoutes serverFuncBtnsOrder({Key? key}) {
return AppRoutes(ServerFuncBtnsOrderPage(key: key), 'server_func_btns_seq');
}

static AppRoutes pve({Key? key, required ServerPrivateInfo spi}) {
static AppRoutes pve({Key? key, required Spi spi}) {
return AppRoutes(PvePage(key: key, spi: spi), 'pve');
}
}
6 changes: 3 additions & 3 deletions lib/core/utils/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ String getPrivateKey(String id) {
}

Future<SSHClient> genClient(
ServerPrivateInfo spi, {
Spi spi, {
void Function(GenSSHClientStatus)? onStatus,

/// Only pass this param if using multi-threading and key login
Expand All @@ -52,10 +52,10 @@ Future<SSHClient> genClient(
String? jumpPrivateKey,
Duration timeout = const Duration(seconds: 5),

/// [ServerPrivateInfo] of the jump server
/// [Spi] of the jump server
///
/// Must pass this param if using multi-threading and key login
ServerPrivateInfo? jumpSpi,
Spi? jumpSpi,

/// Handle keyboard-interactive authentication
FutureOr<List<String>?> Function(SSHUserInfoRequest)? onKeyboardInteractive,
Expand Down
2 changes: 1 addition & 1 deletion lib/core/utils/ssh_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:server_box/data/provider/app.dart';

abstract final class KeybordInteractive {
static FutureOr<List<String>?> defaultHandle(
ServerPrivateInfo spi, {
Spi spi, {
BuildContext? ctx,
}) async {
try {
Expand Down
6 changes: 3 additions & 3 deletions lib/data/model/app/backup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ 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';
import 'package:server_box/data/model/server/snippet.dart';
import 'package:server_box/data/provider/base.dart';
import 'package:server_box/data/res/misc.dart';
import 'package:server_box/data/res/provider.dart';
import 'package:server_box/data/res/rebuild.dart';
import 'package:server_box/data/res/store.dart';

Expand All @@ -23,7 +23,7 @@ class Backup {
// backup format version
final int version;
final String date;
final List<ServerPrivateInfo> spis;
final List<Spi> spis;
final List<Snippet> snippets;
final List<PrivateKeyInfo> keys;
final Map<String, dynamic> container;
Expand Down Expand Up @@ -177,7 +177,7 @@ class Backup {
}
}

Pros.reload();
Provider.reload();
RNodes.app.notify();

_logger.info('Restore success');
Expand Down
2 changes: 1 addition & 1 deletion lib/data/model/app/backup.g.dart

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

2 changes: 1 addition & 1 deletion lib/data/model/server/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'package:server_box/data/model/server/system.dart';
import 'package:server_box/data/model/server/temp.dart';

class Server implements TagPickable {
ServerPrivateInfo spi;
Spi spi;
ServerStatus status;
SSHClient? client;
ServerConn conn;
Expand Down
35 changes: 18 additions & 17 deletions lib/data/model/server/server_private_info.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import 'dart:convert';

import 'package:fl_lib/fl_lib.dart';
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';
import 'package:server_box/data/res/provider.dart';
import 'package:server_box/data/provider/server.dart';

import 'package:server_box/data/model/app/error.dart';

part 'server_private_info.g.dart';

/// In the first version, it's called `ServerPrivateInfo` which was designed to
/// store the private information of a server.
///
///
/// Some params named as `spi` in the codebase which is the abbreviation of `ServerPrivateInfo`.
///
/// Nowaday, more fields are added to this class, but the name is still the same.
/// Nowaday, more fields are added to this class, and it's renamed to `Spi`.
@JsonSerializable()
@HiveType(typeId: 3)
class ServerPrivateInfo {
class Spi {
@HiveField(0)
final String name;
@HiveField(1)
Expand Down Expand Up @@ -58,7 +59,7 @@ class ServerPrivateInfo {

final String id;

const ServerPrivateInfo({
const Spi({
required this.name,
required this.ip,
required this.port,
Expand All @@ -74,17 +75,22 @@ class ServerPrivateInfo {
this.envs,
}) : id = '$user@$ip:$port';

factory ServerPrivateInfo.fromJson(Map<String, dynamic> json) =>
_$ServerPrivateInfoFromJson(json);
factory Spi.fromJson(Map<String, dynamic> json) =>
_$SpiFromJson(json);

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

Map<String, dynamic> toJson() => _$ServerPrivateInfoToJson(this);
@override
String toString() => id;
}

extension Spix on Spi {
String toJsonString() => json.encode(toJson());

Server? get server => Pros.server.pick(spi: this);
Server? get jumpServer => Pros.server.pick(id: jumpId);
VNode<Server>? get server => ServerProvider.pick(spi: this);
VNode<Server>? get jumpServer => ServerProvider.pick(id: jumpId);

bool shouldReconnect(ServerPrivateInfo old) {
bool shouldReconnect(Spi old) {
return id != old.id ||
pwd != old.pwd ||
keyId != old.keyId ||
Expand Down Expand Up @@ -113,12 +119,7 @@ class ServerPrivateInfo {
return (ip_, port_);
}

@override
String toString() {
return id;
}

static const example = ServerPrivateInfo(
static const example = Spi(
name: 'name',
ip: 'ip',
port: 22,
Expand Down
16 changes: 7 additions & 9 deletions lib/data/model/server/server_private_info.g.dart

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

16 changes: 8 additions & 8 deletions lib/data/model/server/snippet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Snippet implements TagPickable {

static final fmtFinder = RegExp(r'\$\{[^{}]+\}');

String fmtWithSpi(ServerPrivateInfo spi) {
String fmtWithSpi(Spi spi) {
return script.replaceAllMapped(
fmtFinder,
(match) {
Expand All @@ -63,7 +63,7 @@ class Snippet implements TagPickable {

Future<void> runInTerm(
Terminal terminal,
ServerPrivateInfo spi, {
Spi spi, {
bool autoEnter = false,
}) async {
final argsFmted = fmtWithSpi(spi);
Expand Down Expand Up @@ -159,12 +159,12 @@ class Snippet implements TagPickable {
}

static final fmtArgs = {
r'${host}': (ServerPrivateInfo spi) => spi.ip,
r'${port}': (ServerPrivateInfo spi) => spi.port.toString(),
r'${user}': (ServerPrivateInfo spi) => spi.user,
r'${pwd}': (ServerPrivateInfo spi) => spi.pwd ?? '',
r'${id}': (ServerPrivateInfo spi) => spi.id,
r'${name}': (ServerPrivateInfo spi) => spi.name,
r'${host}': (Spi spi) => spi.ip,
r'${port}': (Spi spi) => spi.port.toString(),
r'${user}': (Spi spi) => spi.user,
r'${pwd}': (Spi spi) => spi.pwd ?? '',
r'${id}': (Spi spi) => spi.id,
r'${name}': (Spi spi) => spi.name,
};

/// r'${ctrl+ad}' -> TerminalKey.control, a, d
Expand Down
4 changes: 2 additions & 2 deletions lib/data/model/sftp/req.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
part of 'worker.dart';

class SftpReq {
final ServerPrivateInfo spi;
final Spi spi;
final String remotePath;
final String localPath;
final SftpReqType type;
String? privateKey;
ServerPrivateInfo? jumpSpi;
Spi? jumpSpi;
String? jumpPrivateKey;

SftpReq(
Expand Down
20 changes: 20 additions & 0 deletions lib/data/provider/base.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'dart:async';

import 'package:flutter/foundation.dart';

abstract class Provider<T> {
const Provider();

/// (Re)Load data from store / network / etc.
@mustCallSuper
FutureOr<void> load() {
all.add(this);
debugPrint('$runtimeType added');
}

static final all = <Provider>[];

static Future<void> reload() {
return Future.wait(all.map((e) async => await e.load()));
}
}
Loading

0 comments on commit 38366a2

Please sign in to comment.