Init snippet page and store

This commit is contained in:
LollipopKit
2021-10-31 15:22:05 +08:00
parent 9f3f07388e
commit 2eb6e19a86
8 changed files with 114 additions and 4 deletions

View File

@@ -35,7 +35,7 @@ class PrivateKeyInfo {
}
}
List<PrivateKeyInfo>? getPrivateKeyInfoList(dynamic data) {
List<PrivateKeyInfo> getPrivateKeyInfoList(dynamic data) {
List<PrivateKeyInfo> ss = [];
if (data is String) {
data = json.decode(data);

View File

@@ -0,0 +1,30 @@
import 'dart:convert';
class Snippet {
late String name;
late String script;
Snippet(this.name, this.script);
Snippet.fromJson(Map<String, dynamic> json) {
name = json['name'].toString();
script = json['script'].toString();
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['name'] = name;
data['script'] = script;
return data;
}
}
List<Snippet> getSnippetList(dynamic data) {
List<Snippet> ss = [];
if (data is String) {
data = json.decode(data);
}
for (var t in data) {
ss.add(Snippet.fromJson(t));
}
return ss;
}

View File

@@ -12,7 +12,7 @@ class PrivateKeyStore extends PersistentStore {
List<PrivateKeyInfo> fetch() {
return getPrivateKeyInfoList(
json.decode(box.get('key', defaultValue: '[]')!))!;
json.decode(box.get('key', defaultValue: '[]')!));
}
void delete(PrivateKeyInfo s) {

View File

@@ -0,0 +1,33 @@
import 'dart:convert';
import 'package:toolbox/core/persistant_store.dart';
import 'package:toolbox/data/model/server/snippet.dart';
class SnippetStore extends PersistentStore {
void put(Snippet snippet) {
final ss = fetch();
if (!have(snippet)) ss.add(snippet);
box.put('snippet', json.encode(ss));
}
List<Snippet> fetch() {
return getSnippetList(
json.decode(box.get('snippet', defaultValue: '[]')!));
}
void delete(Snippet s) {
final ss = fetch();
ss.removeAt(index(s));
box.put('snippet', json.encode(ss));
}
void update(Snippet old, Snippet newInfo) {
final ss = fetch();
ss[index(old)] = newInfo;
box.put('snippet', json.encode(ss));
}
int index(Snippet s) => fetch().indexWhere((e) => e.name == s.name);
bool have(Snippet s) => index(s) != -1;
}

View File

@@ -7,6 +7,7 @@ import 'package:toolbox/data/service/app.dart';
import 'package:toolbox/data/store/private_key.dart';
import 'package:toolbox/data/store/server.dart';
import 'package:toolbox/data/store/setting.dart';
import 'package:toolbox/data/store/snippet.dart';
GetIt locator = GetIt.instance;
@@ -33,6 +34,10 @@ Future<void> setupLocatorForStores() async {
final key = PrivateKeyStore();
await key.init(boxName: 'key');
locator.registerSingleton(key);
final snippet = SnippetStore();
await snippet.init(boxName: 'snippet');
locator.registerSingleton(snippet);
}
Future<void> setupLocator() async {

View File

@@ -1,6 +1,8 @@
import 'package:after_layout/after_layout.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:toolbox/core/analysis.dart';
import 'package:toolbox/core/build_mode.dart';
import 'package:toolbox/core/route.dart';
import 'package:toolbox/core/update.dart';
import 'package:toolbox/core/utils.dart';
@@ -90,8 +92,13 @@ class _MyHomePageState extends State<MyHomePage>
applicationIcon: _buildIcon(),
aboutBoxChildren: const [
UrlText(
text: '\nMade with ❤️ by https://github.com/LollipopKit', replace: 'LollipopKit'),
UrlText(text: '\nThanks https://github.com/RainSunMe for participating in the test.\n\nAll rights reserved.', replace: 'RainSunMe',),
text: '\nMade with ❤️ by https://github.com/LollipopKit',
replace: 'LollipopKit'),
UrlText(
text:
'\nThanks https://github.com/RainSunMe for participating in the test.\n\nAll rights reserved.',
replace: 'RainSunMe',
),
],
),
],
@@ -118,5 +125,6 @@ class _MyHomePageState extends State<MyHomePage>
await GetIt.I.allReady();
await locator<ServerProvider>().loadLocalData();
await doUpdate(context);
await Analysis.init(BuildMode.isDebug);
}
}

View File

@@ -0,0 +1,17 @@
import 'package:flutter/material.dart';
class SnappetEditPage extends StatefulWidget {
const SnappetEditPage({ Key? key }) : super(key: key);
@override
_SnappetEditPageState createState() => _SnappetEditPageState();
}
class _SnappetEditPageState extends State<SnappetEditPage> {
@override
Widget build(BuildContext context) {
return Container(
);
}
}

View File

@@ -0,0 +1,17 @@
import 'package:flutter/material.dart';
class SnappetListPage extends StatefulWidget {
const SnappetListPage({ Key? key }) : super(key: key);
@override
_SnappetListPageState createState() => _SnappetListPageState();
}
class _SnappetListPageState extends State<SnappetListPage> {
@override
Widget build(BuildContext context) {
return Container(
);
}
}