Skip to content

Commit

Permalink
feat: import snippets from network (#510)
Browse files Browse the repository at this point in the history
Fixes #507
  • Loading branch information
lollipopkit authored Aug 3, 2024
1 parent 0e21755 commit b8e5418
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 14 deletions.
7 changes: 0 additions & 7 deletions lib/core/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,4 @@ class AppRoutes {
static AppRoutes pve({Key? key, required ServerPrivateInfo spi}) {
return AppRoutes(PvePage(key: key, spi: spi), 'pve');
}

static AppRoutes kvEditor({Key? key, required Map<String, String> data}) {
return AppRoutes(
KvEditor(key: key, args: KvEditorArgs(data: data)),
'kv_editor',
);
}
}
8 changes: 8 additions & 0 deletions lib/data/model/server/snippet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ class Snippet implements TagPickable {
r'${ctrl': TerminalKey.control,
r'${alt': TerminalKey.alt,
};

static const example = Snippet(
name: 'example',
script: 'echo hello',
tags: ['tag'],
note: 'note',
autoRunOn: ['server_id'],
);
}

class SnippetResult {
Expand Down
8 changes: 5 additions & 3 deletions lib/view/page/setting/platform/ios.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:fl_lib/fl_lib.dart';
import 'package:flutter/material.dart';
import 'package:server_box/core/extension/context/locale.dart';
import 'package:server_box/core/route.dart';
import 'package:server_box/core/utils/misc.dart';
import 'package:server_box/data/res/store.dart';
import 'package:server_box/view/page/setting/platform/platform_pub.dart';
Expand Down Expand Up @@ -111,8 +110,11 @@ class _IOSSettingsPageState extends State<IOSSettingsPage> {

void _onTapWatchApp(Map<String, dynamic> map) async {
final urls = Map<String, String>.from(map['urls'] as Map? ?? {});
final result = await AppRoutes.kvEditor(data: urls).go(context);
if (result == null || result is! Map<String, String>) return;
final result = await KvEditor.route.go(
context,
args: KvEditorArgs(data: urls),
);
if (result == null) return;

final (suc, err) = await context.showLoadingDialog(fn: () async {
await wc.updateApplicationContext({'urls': result});
Expand Down
61 changes: 60 additions & 1 deletion lib/view/page/snippet/edit.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import 'dart:convert';

import 'package:fl_lib/fl_lib.dart';
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:server_box/core/extension/context/locale.dart';
import 'package:server_box/data/model/server/snippet.dart';
import 'package:server_box/data/res/provider.dart';
import 'package:icons_plus/icons_plus.dart';

class SnippetEditPage extends StatefulWidget {
const SnippetEditPage({super.key, this.snippet});
Expand Down Expand Up @@ -102,8 +105,9 @@ class _SnippetEditPageState extends State<SnippetEditPage>

Widget _buildBody() {
return ListView(
padding: const EdgeInsets.all(13),
padding: const EdgeInsets.symmetric(horizontal:13),
children: [
_buildImport(),
Input(
autoFocus: true,
controller: _nameController,
Expand Down Expand Up @@ -183,6 +187,61 @@ class _SnippetEditPageState extends State<SnippetEditPage>
);
}

Widget _buildImport() {
return Btn.tile(
text: l10n.import,
icon: const Icon(BoxIcons.bx_import),
onTap: (c) async {
final data = await c.showImportDialog(
title: l10n.snippet,
modelDef: Snippet.example.toJson(),
);
if (data == null) return;
final str = String.fromCharCodes(data);
final list = json.decode(str) as List;
if (list.isEmpty) return;
final snippets = <Snippet>[];
final errs = <String>[];
for (final item in list) {
try {
final snippet = Snippet.fromJson(item);
snippets.add(snippet);
} catch (e) {
errs.add(e.toString());
}
}
if (snippets.isEmpty) {
c.showSnackBar(libL10n.empty);
return;
}
if (errs.isNotEmpty) {
c.showRoundDialog(
title: l10n.error,
child: SingleChildScrollView(child: Text(errs.join('\n'))),
);
return;
}
final snippetNames = snippets.map((e) => e.name).join(', ');
c.showRoundDialog(
title: l10n.attention,
child: SingleChildScrollView(
child: Text(l10n.askContinue('${l10n.import} [$snippetNames]')),
),
actions: Btn.ok(
onTap: (c) {
for (final snippet in snippets) {
Pros.snippet.add(snippet);
}
c.pop();
context.pop();
},
).toList,
);
},
mainAxisAlignment: MainAxisAlignment.center,
);
}

Widget _buildTip() {
return CardX(
child: Padding(
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: "v1.0.105"
resolved-ref: "0804b1363d451b6d25028ca393f552fbe1a32690"
ref: "v1.0.106"
resolved-ref: "31735eee1d42e6bcb6c3ccd061047401b4a23421"
url: "https://github.com/lppcg/fl_lib"
source: git
version: "0.0.1"
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ dependencies:
fl_lib:
git:
url: https://github.com/lppcg/fl_lib
ref: v1.0.105
ref: v1.0.106

dependency_overrides:
# dartssh2:
Expand Down

0 comments on commit b8e5418

Please sign in to comment.