使用 Hive Object

This commit is contained in:
Junyuan Feng
2022-11-05 23:08:56 +08:00
parent c036b78708
commit 398c49bb99
20 changed files with 504 additions and 102 deletions

View File

@@ -1,8 +1,13 @@
import 'dart:convert';
import 'package:hive_flutter/hive_flutter.dart';
part 'private_key_info.g.dart';
///
/// Code generated by jsonToDartModel https://ashamp.github.io/jsonToDartModel/
///
@HiveType(typeId: 1)
class PrivateKeyInfo {
/*
{
@@ -12,8 +17,11 @@ class PrivateKeyInfo {
}
*/
@HiveField(0)
late String id;
@HiveField(1)
late String privateKey;
@HiveField(2)
late String password;
PrivateKeyInfo(

View File

@@ -0,0 +1,47 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'private_key_info.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class PrivateKeyInfoAdapter extends TypeAdapter<PrivateKeyInfo> {
@override
final int typeId = 1;
@override
PrivateKeyInfo read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return PrivateKeyInfo(
fields[0] as String,
fields[1] as String,
fields[2] as String,
);
}
@override
void write(BinaryWriter writer, PrivateKeyInfo obj) {
writer
..writeByte(3)
..writeByte(0)
..write(obj.id)
..writeByte(1)
..write(obj.privateKey)
..writeByte(2)
..write(obj.password);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is PrivateKeyInfoAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}

View File

@@ -1,8 +1,13 @@
import 'dart:convert';
import 'package:hive_flutter/hive_flutter.dart';
part 'server_private_info.g.dart';
///
/// Code generated by jsonToDartModel https://ashamp.github.io/jsonToDartModel/
///
@HiveType(typeId: 3)
class ServerPrivateInfo {
/*
{
@@ -13,13 +18,21 @@ class ServerPrivateInfo {
}
*/
@HiveField(0)
late String name;
@HiveField(1)
late String ip;
@HiveField(2)
late int port;
@HiveField(3)
late String user;
@HiveField(4)
late String pwd;
@HiveField(5)
String? pubKeyId;
String get id => '$user@$ip:$port';
ServerPrivateInfo(
{required this.name,
required this.ip,

View File

@@ -0,0 +1,56 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'server_private_info.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class ServerPrivateInfoAdapter extends TypeAdapter<ServerPrivateInfo> {
@override
final int typeId = 3;
@override
ServerPrivateInfo read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return ServerPrivateInfo(
name: fields[0] as String,
ip: fields[1] as String,
port: fields[2] as int,
user: fields[3] as String,
pwd: fields[4] as String,
pubKeyId: fields[5] as String?,
);
}
@override
void write(BinaryWriter writer, ServerPrivateInfo obj) {
writer
..writeByte(6)
..writeByte(0)
..write(obj.name)
..writeByte(1)
..write(obj.ip)
..writeByte(2)
..write(obj.port)
..writeByte(3)
..write(obj.user)
..writeByte(4)
..write(obj.pwd)
..writeByte(5)
..write(obj.pubKeyId);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is ServerPrivateInfoAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}

View File

@@ -1,7 +1,14 @@
import 'dart:convert';
import 'package:hive_flutter/hive_flutter.dart';
part 'snippet.g.dart';
@HiveType(typeId: 2)
class Snippet {
@HiveField(0)
late String name;
@HiveField(1)
late String script;
Snippet(this.name, this.script);

View File

@@ -0,0 +1,44 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'snippet.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class SnippetAdapter extends TypeAdapter<Snippet> {
@override
final int typeId = 2;
@override
Snippet read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return Snippet(
fields[0] as String,
fields[1] as String,
);
}
@override
void write(BinaryWriter writer, Snippet obj) {
writer
..writeByte(2)
..writeByte(0)
..write(obj.name)
..writeByte(1)
..write(obj.script);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is SnippetAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}

View File

@@ -26,7 +26,7 @@ class PrivateKeyProvider extends BusyProvider {
void updateInfo(PrivateKeyInfo old, PrivateKeyInfo newInfo) {
final idx = _infos.indexWhere((e) => e.id == old.id);
_infos[idx] = newInfo;
locator<PrivateKeyStore>().update(old, newInfo);
locator<PrivateKeyStore>().put(newInfo);
notifyListeners();
}
}

View File

@@ -38,7 +38,7 @@ class SnippetProvider extends BusyProvider {
void update(Snippet old, Snippet newOne) {
if (!have(old)) return;
_snippets[index(old)] = newOne;
locator<SnippetStore>().update(old, newOne);
locator<SnippetStore>().put(newOne);
notifyListeners();
}

View File

@@ -2,9 +2,9 @@
class BuildData {
static const String name = "ServerBox";
static const int build = 155;
static const int build = 156;
static const String engine =
"Flutter 3.3.4 • channel stable • https://github.com/flutter/flutter.git\nFramework • revision eb6d86ee27 (4 weeks ago) • 2022-10-04 22:31:45 -0700\nEngine • revision c08d7d5efc\nTools • Dart 2.18.2 • DevTools 2.15.0\n";
static const String buildAt = "2022-11-05 21:49:47.105329";
static const int modifications = 0;
static const String buildAt = "2022-11-05 23:02:53.614616";
static const int modifications = 20;
}

View File

@@ -1,38 +1,28 @@
import 'dart:convert';
import 'package:toolbox/core/persistant_store.dart';
import 'package:toolbox/data/model/server/private_key_info.dart';
class PrivateKeyStore extends PersistentStore {
void put(PrivateKeyInfo info) {
final ss = fetch();
if (!have(info)) ss.add(info);
box.put('key', json.encode(ss));
box.put(info.id, info);
}
List<PrivateKeyInfo> fetch() {
return getPrivateKeyInfoList(
json.decode(box.get('key', defaultValue: '[]')!));
final keys = box.keys;
final ps = <PrivateKeyInfo>[];
for (final key in keys) {
final s = box.get(key);
if (s != null) {
ps.add(s);
}
}
return ps;
}
PrivateKeyInfo get(String id) {
final ss = fetch();
return ss.firstWhere((e) => e.id == id);
return box.get(id);
}
void delete(PrivateKeyInfo s) {
final ss = fetch();
ss.removeAt(index(s));
box.put('key', json.encode(ss));
box.delete(s.id);
}
void update(PrivateKeyInfo old, PrivateKeyInfo newInfo) {
final ss = fetch();
ss[index(old)] = newInfo;
box.put('key', json.encode(ss));
}
int index(PrivateKeyInfo s) => fetch().indexWhere((e) => e.id == s.id);
bool have(PrivateKeyInfo s) => index(s) != -1;
}

View File

@@ -1,38 +1,33 @@
import 'dart:convert';
import 'package:toolbox/core/persistant_store.dart';
import 'package:toolbox/data/model/server/server_private_info.dart';
class ServerStore extends PersistentStore {
void put(ServerPrivateInfo info) {
final ss = fetch();
if (!have(info)) ss.add(info);
box.put('servers', json.encode(ss));
box.put(info.id, info);
}
List<ServerPrivateInfo> fetch() {
return getServerInfoList(
json.decode(box.get('servers', defaultValue: '[]')!));
final ids = box.keys;
final List<ServerPrivateInfo> ss = [];
for (final id in ids) {
final s = box.get(id);
if (s != null) {
ss.add(s);
}
}
return ss;
}
void delete(ServerPrivateInfo s) {
final ss = fetch();
ss.removeAt(index(s));
box.put('servers', json.encode(ss));
box.delete(s.id);
}
void update(ServerPrivateInfo old, ServerPrivateInfo newInfo) {
final ss = fetch();
final idx = index(old);
if (idx < 0) {
throw RangeError.index(idx, ss);
if (!have(old)) {
throw Exception('Old ServerPrivateInfo not found');
}
ss[idx] = newInfo;
box.put('servers', json.encode(ss));
put(newInfo);
}
int index(ServerPrivateInfo s) => fetch()
.indexWhere((e) => e.ip == s.ip && e.port == s.port && e.user == e.user);
bool have(ServerPrivateInfo s) => index(s) != -1;
bool have(ServerPrivateInfo s) => box.get(s.id) != null;
}

View File

@@ -7,4 +7,7 @@ class SettingStore extends PersistentStore {
StoreProperty<int> get serverStatusUpdateInterval =>
property('serverStatusUpdateInterval', defaultValue: 2);
StoreProperty<int> get launchPage => property('launchPage', defaultValue: 0);
StoreProperty<int> get storeVersion =>
property('storeVersion', defaultValue: 0);
}

View File

@@ -1,32 +1,24 @@
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));
box.put(snippet.name, snippet);
}
List<Snippet> fetch() {
return getSnippetList(json.decode(box.get('snippet', defaultValue: '[]')!));
final keys = box.keys;
final ss = <Snippet>[];
for (final key in keys) {
final s = box.get(key);
if (s != null) {
ss.add(s);
}
}
return ss;
}
void delete(Snippet s) {
final ss = fetch();
ss.removeAt(index(s));
box.put('snippet', json.encode(ss));
box.delete(s.name);
}
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;
}