Skip to content

Commit

Permalink
Merge pull request #508 from xxfttkx/branch-fix-time
Browse files Browse the repository at this point in the history
enhance plugin update
  • Loading branch information
Predidit authored Dec 23, 2024
2 parents 731b670 + 4376083 commit 6c71de6
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 42 deletions.
3 changes: 1 addition & 2 deletions lib/pages/plugin_editor/plugin_editor_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ class _PluginEditorPageState extends State<PluginEditorPage> {
plugin.usePost = usePost;
plugin.useLegacyParser = useLegacyParser;
plugin.referer = refererController.text;
await pluginsController.savePluginToJsonFile(plugin);
await pluginsController.loadPlugins();
await pluginsController.tryInstallPlugin(plugin);
Navigator.of(context).pop();
},
),
Expand Down
56 changes: 22 additions & 34 deletions lib/pages/plugin_editor/plugin_shop_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,46 +108,34 @@ class _PluginShopPageState extends State<PluginShopPage> {
if (pluginsController.pluginStatus(
pluginsController.pluginHTTPList[index]) ==
'install') {
try {
KazumiDialog.showToast(message: '导入中');
var pluginHTTPItem =
await pluginsController.queryPluginHTTP(
pluginsController.pluginHTTPList[index].name);
if (pluginHTTPItem != null) {
if (int.parse(pluginHTTPItem.api) > Api.apiLevel) {
KazumiDialog.showToast(message: 'kazumi版本过低, 此规则不兼容当前版本');
return;
}
await pluginsController
.savePluginToJsonFile(pluginHTTPItem);
await pluginsController.loadPlugins();
KazumiDialog.showToast(message: '导入成功');
setState(() {});
}
} catch (e) {
KazumiDialog.showToast(message: '导入中');
int res = await pluginsController.tryUpdatePluginByName(
pluginsController.pluginHTTPList[index].name);
if (res == 0) {
KazumiDialog.showToast(message: '导入成功');
setState(() {

});
} else if (res == 1) {
KazumiDialog.showToast(message: 'kazumi版本过低, 此规则不兼容当前版本');
} else if (res == 2) {
KazumiDialog.showToast(message: '导入规则失败');
}
}
if (pluginsController.pluginStatus(
pluginsController.pluginHTTPList[index]) ==
'update') {
try {
KazumiDialog.showToast(message: '更新中');
var pluginHTTPItem =
await pluginsController.queryPluginHTTP(
pluginsController.pluginHTTPList[index].name);
if (pluginHTTPItem != null) {
if (int.parse(pluginHTTPItem.api) > Api.apiLevel) {
KazumiDialog.showToast(message: 'kazumi版本过低, 此规则不兼容当前版本');
return;
}
await pluginsController
.savePluginToJsonFile(pluginHTTPItem);
await pluginsController.loadPlugins();
KazumiDialog.showToast(message: '更新成功');
setState(() {});
}
} catch (e) {
KazumiDialog.showToast(message: '更新中');
int res = await pluginsController.tryUpdatePluginByName(
pluginsController.pluginHTTPList[index].name);
if (res == 0) {
KazumiDialog.showToast(message: '更新成功');
setState(() {

});
} else if (res == 1) {
KazumiDialog.showToast(message: 'kazumi版本过低, 此规则不兼容当前版本');
} else if (res == 2) {
KazumiDialog.showToast(message: '更新规则失败');
}
}
Expand Down
51 changes: 45 additions & 6 deletions lib/pages/plugin_editor/plugin_view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ class PluginViewPage extends StatefulWidget {
class _PluginViewPageState extends State<PluginViewPage> {
final PluginsController pluginsController = Modular.get<PluginsController>();

Future<void> _handleUpdate() async {
KazumiDialog.showLoading(msg: '更新中');
int count = await pluginsController.tryUpdateAllPlugin();
KazumiDialog.dismiss();
if (count == 0) {
KazumiDialog.showToast(message: '所有规则已是最新');
} else {
KazumiDialog.showToast(message: '更新成功 $count 条');
}
}

void _handleAdd() {
KazumiDialog.show(
builder: (context) {
Expand Down Expand Up @@ -88,15 +99,14 @@ class _PluginViewPageState extends State<PluginViewPage> {
onPressed: () async {
final String msg = textController.text;
try {
await pluginsController.savePluginToJsonFile(
await pluginsController.tryInstallPlugin(
Plugin.fromJson(
json.decode(Utils.kazumiBase64ToJson(msg))));
KazumiDialog.showToast(message: '导入成功');
} catch (e) {
KazumiDialog.dismiss();
KazumiDialog.showToast(message: '导入失败 ${e.toString()}');
}
pluginsController.loadPlugins();
KazumiDialog.dismiss();
},
child: const Text('导入'),
Expand Down Expand Up @@ -128,6 +138,11 @@ class _PluginViewPageState extends State<PluginViewPage> {
appBar: SysAppBar(
title: const Text('规则管理'),
actions: [
IconButton(
onPressed: () {
_handleUpdate();
},
icon: const Icon(Icons.update)),
IconButton(
onPressed: () {
_handleAdd();
Expand All @@ -143,20 +158,40 @@ class _PluginViewPageState extends State<PluginViewPage> {
: ListView.builder(
itemCount: pluginsController.pluginList.length,
itemBuilder: (context, index) {
var plugin = pluginsController.pluginList[index];
bool canUpdate = pluginsController.pluginUpdateStatus(plugin)=='updatable';
return Card(
margin: const EdgeInsets.fromLTRB(8, 0, 8, 8),
child: ListTile(
title: Text(
pluginsController.pluginList[index].name,
plugin.name,
style: const TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(
'Version: ${pluginsController.pluginList[index].version}',
'Version: ${plugin.version}${canUpdate?' (可更新)':''}',
style: const TextStyle(color: Colors.grey),
),
trailing: PopupMenuButton<String>(
onSelected: (String result) {
if (result == 'Delete') {
onSelected: (String result) async {
if (result == 'Update') {
var state = pluginsController.pluginUpdateStatus(plugin);
if (state == "nonexistent") {
KazumiDialog.showToast(message: '规则仓库中没有当前规则');
} else if (state == "latest") {
KazumiDialog.showToast(message: '规则已是最新');
} else if (state == "updatable") {
KazumiDialog.showLoading(msg: '更新中');
int res = await pluginsController.tryUpdatePlugin(plugin);
KazumiDialog.dismiss();
if (res==0) {
KazumiDialog.showToast(message: '更新成功');
} else if (res == 1) {
KazumiDialog.showToast(message: 'kazumi版本过低, 此规则不兼容当前版本');
} else if (res == 2) {
KazumiDialog.showToast(message: '更新规则失败');
}
}
} else if (result == 'Delete') {
setState(() {
pluginsController.deletePluginJsonFile(
pluginsController.pluginList[index]);
Expand Down Expand Up @@ -210,6 +245,10 @@ class _PluginViewPageState extends State<PluginViewPage> {
},
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<String>>[
const PopupMenuItem<String>(
value: 'Update',
child: Text('更新'),
),
const PopupMenuItem<String>(
value: 'Edit',
child: Text('编辑'),
Expand Down
45 changes: 45 additions & 0 deletions lib/plugins/plugins_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:kazumi/modules/plugin/plugin_http_module.dart';
import 'package:logger/logger.dart';
import 'package:path/path.dart' as path;
import 'package:kazumi/utils/logger.dart';
import 'package:kazumi/request/api.dart';

part 'plugins_controller.g.dart';

Expand Down Expand Up @@ -148,4 +149,48 @@ Future<void> deletePluginJsonFile(Plugin plugin) async {
}
return pluginStatus;
}

String pluginUpdateStatus(Plugin plugin) {
if (!pluginHTTPList.any((p) => p.name == plugin.name)) {
return "nonexistent";
}
PluginHTTPItem p = pluginHTTPList.firstWhere(
(p) => p.name == plugin.name,
);
return p.version == plugin.version ? "latest" : "updatable";
}

Future<int> tryUpdatePlugin(Plugin plugin) async {
return await tryUpdatePluginByName(plugin.name);
}

Future<int> tryUpdatePluginByName(String name) async {
var pluginHTTPItem = await queryPluginHTTP(name);
if (pluginHTTPItem != null) {
if (int.parse(pluginHTTPItem.api) > Api.apiLevel) {
return 1;
}
await savePluginToJsonFile(pluginHTTPItem);
await loadPlugins();
return 0;
}
return 2;
}

Future<int> tryUpdateAllPlugin() async {
int count = 0;
for (Plugin plugin in pluginList) {
if (pluginUpdateStatus(plugin) == 'updatable') {
if (await tryUpdatePlugin(plugin) == 0) {
count++;
}
}
}
return count;
}

Future<void> tryInstallPlugin(Plugin plugin) async {
await savePluginToJsonFile(plugin);
await loadPlugins();
}
}

0 comments on commit 6c71de6

Please sign in to comment.