Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

add plugin drag feature

lauirffaa31e7d0898
diff --git a/lib/pages/init_page.dart b/lib/pages/init_page.dart
index 9847f66a..cdbe982c 100644
--- a/lib/pages/init_page.dart
+++ b/lib/pages/init_page.dart
@@ -83,7 +83,7 @@ class _InitPageState extends State<InitPage> {
Future<void> _pluginInit() async {
String statementsText = '';
try {
- await pluginsController.loadPlugins();
+ await pluginsController.loadAllPlugins();
statementsText =
await rootBundle.loadString("assets/statements/statements.txt");
_pluginUpdate();
@@ -113,7 +113,6 @@ class _InitPageState extends State<InitPage> {
onPressed: () async {
try {
await pluginsController.copyPluginsToExternalDirectory();
- await pluginsController.loadPlugins();
} catch (_) {}
KazumiDialog.dismiss();
Modular.to.navigate('/tab/popular/');
diff --git a/lib/pages/plugin_editor/plugin_editor_page.dart b/lib/pages/plugin_editor/plugin_editor_page.dart
index 1eef9ae5..83c82fdf 100644
--- a/lib/pages/plugin_editor/plugin_editor_page.dart
+++ b/lib/pages/plugin_editor/plugin_editor_page.dart
@@ -201,7 +201,7 @@ class _PluginEditorPageState extends State<PluginEditorPage> {
plugin.usePost = usePost;
plugin.useLegacyParser = useLegacyParser;
plugin.referer = refererController.text;
- await pluginsController.tryInstallPlugin(plugin);
+ pluginsController.updatePlugin(plugin);
Navigator.of(context).pop();
},
),
diff --git a/lib/pages/plugin_editor/plugin_view_page.dart b/lib/pages/plugin_editor/plugin_view_page.dart
index bbaa8ea8..dec1c822 100644
--- a/lib/pages/plugin_editor/plugin_view_page.dart
+++ b/lib/pages/plugin_editor/plugin_view_page.dart
@@ -3,11 +3,13 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutter_modular/flutter_modular.dart';
+import 'package:kazumi/utils/logger.dart';
import 'package:kazumi/utils/utils.dart';
import 'package:kazumi/bean/dialog/dialog_helper.dart';
import 'package:kazumi/plugins/plugins.dart';
import 'package:kazumi/plugins/plugins_controller.dart';
import 'package:kazumi/bean/appbar/sys_app_bar.dart';
+import 'package:logger/logger.dart';
class PluginViewPage extends StatefulWidget {
const PluginViewPage({super.key});
@@ -25,9 +27,6 @@ class _PluginViewPageState extends State<PluginViewPage> {
// 已选中的规则名称集合
final Set<String> selectedNames = {};
- // 排序方式状态:false=按安装时间排序,true=按名称排序
- bool sortByName = false;
-
Future<void> _handleUpdate() async {
KazumiDialog.showLoading(msg: '更新中');
int count = await pluginsController.tryUpdateAllPlugin();
@@ -105,7 +104,7 @@ class _PluginViewPageState extends State<PluginViewPage> {
onPressed: () async {
final String msg = textController.text;
try {
- await pluginsController.tryInstallPlugin(Plugin.fromJson(
+ pluginsController.updatePlugin(Plugin.fromJson(
json.decode(Utils.kazumiBase64ToJson(msg))));
KazumiDialog.showToast(message: '导入成功');
} catch (e) {
@@ -186,17 +185,7 @@ class _PluginViewPageState extends State<PluginViewPage> {
),
TextButton(
onPressed: () {
- // 从大到小排序,这样删除时不会影响前面的索引
- final sortedNames = selectedNames.toList()
- ..sort((a, b) => b.compareTo(a));
- for (final name in sortedNames) {
- final plugin = pluginsController.pluginList
- .firstWhere((p) => p.name == name);
- pluginsController
- .deletePluginJsonFile(plugin);
- pluginsController.pluginList
- .removeWhere((p) => p.name == name);
- }
+ pluginsController.removePlugins(selectedNames);
setState(() {
isMultiSelectMode = false;
selectedNames.clear();
@@ -212,15 +201,6 @@ class _PluginViewPageState extends State<PluginViewPage> {
icon: const Icon(Icons.delete),
),
] else ...[
- IconButton(
- onPressed: () {
- setState(() {
- sortByName = !sortByName;
- });
- },
- tooltip: sortByName ? '按名称排序' : '按安装时间排序',
- icon: Icon(
- sortByName ? Icons.sort_by_alpha : Icons.access_time)),
IconButton(
onPressed: () {
_handleUpdate();
@@ -242,30 +222,25 @@ class _PluginViewPageState extends State<PluginViewPage> {
child: Text('啊咧(⊙.⊙) 没有可用规则的说'),
)
: Builder(builder: (context) {
- // 创建列表副本用于排序
- var sortedList = List.from(pluginsController.pluginList);
- // 排序规则:
- // 1. 按名称排序:忽略大小写的字母顺序
- // 2. 按时间排序:安装时间降序(最新的在前面)
- if (sortByName) {
- sortedList.sort((a, b) =>
- a.name.toLowerCase().compareTo(b.name.toLowerCase()));
- } else {
- sortedList.sort((a, b) => pluginsController
- .installTimeTracker
- .getInstallTime(b.name)
- .compareTo(pluginsController.installTimeTracker
- .getInstallTime(a.name)));
- }
-
- return ListView.builder(
- itemCount: sortedList.length,
+ return ReorderableListView.builder(
+ proxyDecorator: (child, index, animation) {
+ return Material(
+ elevation: 0,
+ color: Colors.transparent,
+ child: child,
+ );
+ },
+ onReorder: (int oldIndex, int newIndex) {
+ pluginsController.onReorder(oldIndex, newIndex);
+ },
+ itemCount: pluginsController.pluginList.length,
itemBuilder: (context, index) {
- var plugin = sortedList[index];
+ var plugin = pluginsController.pluginList[index];
bool canUpdate =
pluginsController.pluginUpdateStatus(plugin) ==
'updatable';
return Card(
+ key: ValueKey(index),
margin: const EdgeInsets.fromLTRB(8, 0, 8, 8),
child: ListTile(
shape: RoundedRectangleBorder(
@@ -381,116 +356,96 @@ class _PluginViewPageState extends State<PluginViewPage> {
});
},
)
- : PopupMenuButton<String>(
- 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(plugin);
- pluginsController.pluginList
- .removeWhere(
- (p) => p.name == plugin.name);
- });
- } else if (result == 'Edit') {
- Modular.to.pushNamed(
- '/settings/plugin/editor',
- arguments: plugin);
- } else if (result == 'Share') {
- KazumiDialog.show(builder: (context) {
- return AlertDialog(
- title: const Text('规则链接'),
- content: SelectableText(
- Utils.jsonToKazumiBase64(
- json.encode(pluginsController
- .pluginList[index]
- .toJson())),
- style: const TextStyle(
- fontWeight: FontWeight.bold),
- textAlign: TextAlign.center,
- ),
- actions: [
- TextButton(
- onPressed: () =>
- KazumiDialog.dismiss(),
- child: Text(
- '取消',
- style: TextStyle(
- color: Theme.of(context)
- .colorScheme
- .outline),
- ),
- ),
- TextButton(
- onPressed: () {
- Clipboard.setData(ClipboardData(
- text: Utils.jsonToKazumiBase64(
- json.encode(
- pluginsController
- .pluginList[
- index]
- .toJson()))));
- KazumiDialog.dismiss();
- },
- child: const Text('复制到剪贴板'),
- ),
- ],
- );
- });
- }
- },
- itemBuilder: (BuildContext context) =>
- <PopupMenuEntry<String>>[
- const PopupMenuItem<String>(
- value: 'Update',
- child: Text('更新'),
- ),
- const PopupMenuItem<String>(
- value: 'Edit',
- child: Text('编辑'),
- ),
- const PopupMenuItem<String>(
- value: 'Share',
- child: Text('分享'),
- ),
- const PopupMenuItem<String>(
- value: 'Delete',
- child: Text('删除'),
- ),
- ],
- ),
+ : popupMenuButton(index)
),
);
- },
+ }
);
});
}),
),
);
}
+
+ Widget popupMenuButton(int index){
+ final plugin = pluginsController.pluginList[index];
+ return PopupMenuButton<String>(
+ 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.removePlugin(plugin);
+ });
+ } else if (result == 'Edit') {
+ Modular.to.pushNamed('/settings/plugin/editor', arguments: plugin);
+ } else if (result == 'Share') {
+ KazumiDialog.show(builder: (context) {
+ return AlertDialog(
+ title: const Text('规则链接'),
+ content: SelectableText(
+ Utils.jsonToKazumiBase64(
+ json.encode(pluginsController.pluginList[index].toJson())),
+ style: const TextStyle(fontWeight: FontWeight.bold),
+ textAlign: TextAlign.center,
+ ),
+ actions: [
+ TextButton(
+ onPressed: () => KazumiDialog.dismiss(),
+ child: Text(
+ '取消',
+ style:
+ TextStyle(color: Theme.of(context).colorScheme.outline),
+ ),
+ ),
+ TextButton(
+ onPressed: () {
+ Clipboard.setData(ClipboardData(
+ text: Utils.jsonToKazumiBase64(json.encode(
+ pluginsController.pluginList[index].toJson()))));
+ KazumiDialog.dismiss();
+ },
+ child: const Text('复制到剪贴板'),
+ ),
+ ],
+ );
+ });
+ }
+ },
+ itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
+ const PopupMenuItem<String>(
+ value: 'Update',
+ child: Text('更新'),
+ ),
+ const PopupMenuItem<String>(
+ value: 'Edit',
+ child: Text('编辑'),
+ ),
+ const PopupMenuItem<String>(
+ value: 'Share',
+ child: Text('分享'),
+ ),
+ const PopupMenuItem<String>(
+ value: 'Delete',
+ child: Text('删除'),
+ ),
+ ],
+ );
+ }
}
diff --git a/lib/plugins/plugins_controller.dart b/lib/plugins/plugins_controller.dart
index 0c2c4935..4b35949b 100644
--- a/lib/plugins/plugins_controller.dart
+++ b/lib/plugins/plugins_controller.dart
@@ -30,13 +30,46 @@ abstract class _PluginsController with Store {
// 规则安装时间追踪器
final installTimeTracker = PluginInstallTimeTracker();
- Future<void> loadPlugins() async {
- pluginList.clear();
+ String pluginsFileName = "plugins.json";
+ Future<void> loadAllPlugins() async {
+ pluginList.clear();
final directory = await getApplicationSupportDirectory();
final pluginDirectory = Directory('${directory.path}/plugins');
KazumiLogger().log(Level.info, '插件目录 ${directory.path}/plugins');
+ if (await pluginDirectory.exists()) {
+ final pluginsFile = File('${pluginDirectory.path}/$pluginsFileName');
+ if (await pluginsFile.exists()) {
+ final jsonString = await pluginsFile.readAsString();
+ pluginList = ObservableList.of(getPluginListFromJson(jsonString));
+ KazumiLogger().log(Level.info, '当前插件数量 ${pluginList.length}');
+ } else {}
+ if (pluginList.isEmpty) {
+ var jsonFiles = await loadPlugins();
+ for (var filePath in jsonFiles) {
+ await File(filePath).delete(recursive: true);
+ }
+ if (pluginList.isNotEmpty) {
+ savePlugins();
+ }
+ }
+ } else {
+ KazumiLogger().log(Level.warning, '插件目录不存在');
+ }
+ }
+
+ Future<List<String>> loadPlugins() async {
+ final directory = await getApplicationSupportDirectory();
+ final pluginDirectory = Directory('${directory.path}/plugins');
+ if (await pluginDirectory.exists()) {
+ final pluginsFile = File('${pluginDirectory.path}/$pluginsFileName');
+ if (await pluginsFile.exists()) {
+ return [];
+ }
+ }
+ pluginList.clear();
+ KazumiLogger().log(Level.info, '插件目录 ${directory.path}/plugins');
if (await pluginDirectory.exists()) {
final jsonFiles = pluginDirectory
.listSync()
@@ -55,10 +88,11 @@ abstract class _PluginsController with Store {
plugin.name, stat.modified.millisecondsSinceEpoch);
pluginList.add(plugin);
}
-
KazumiLogger().log(Level.info, '当前插件数量 ${pluginList.length}');
+ return jsonFiles;
} else {
KazumiLogger().log(Level.warning, '插件目录不存在');
+ return [];
}
}
@@ -78,66 +112,70 @@ abstract class _PluginsController with Store {
for (var filePath in jsonFiles) {
final jsonString = await rootBundle.loadString(filePath);
- // panic
- final fileName = filePath.split('/').last;
- final file = File('${pluginDirectory.path}/$fileName');
- await file.writeAsString(jsonString);
+ final plugin = Plugin.fromJson(jsonDecode(jsonString));
+ pluginList.add(plugin);
}
-
+ await savePlugins();
KazumiLogger().log(
Level.info, '已将 ${jsonFiles.length} 个插件文件拷贝到 ${pluginDirectory.path}');
}
- Future<void> savePluginToJsonFile(Plugin plugin) async {
- final directory = await getApplicationSupportDirectory();
- final pluginDirectory = Directory('${directory.path}/plugins');
-
- if (!await pluginDirectory.exists()) {
- await pluginDirectory.create(recursive: true);
- }
-
- final fileName = '${plugin.name}.json';
- final existingFile = File('${pluginDirectory.path}/$fileName');
- if (await existingFile.exists()) {
- await existingFile.delete();
+ List<dynamic> pluginListToJson() {
+ final List<dynamic> json = [];
+ for (var plugin in pluginList) {
+ json.add(plugin.toJson());
}
-
- final newFile = File('${pluginDirectory.path}/$fileName');
- final jsonData = jsonEncode(plugin.toJson());
- await newFile.writeAsString(jsonData);
-
- KazumiLogger().log(Level.info, '已创建插件文件 $fileName');
+ return json;
}
- Future<void> deletePluginJsonFile(Plugin plugin) async {
- final directory = await getApplicationSupportDirectory();
- final pluginDirectory = Directory('${directory.path}/plugins');
-
- if (!await pluginDirectory.exists()) {
- KazumiLogger().log(Level.warning, '插件目录不存在,无法删除文件');
- return;
+ List<Plugin> getPluginListFromJson(String jsonString) {
+ List<dynamic> json = jsonDecode(jsonString);
+ List<Plugin> plugins = [];
+ for (var j in json) {
+ plugins.add(Plugin.fromJson(j));
}
+ return plugins;
+ }
- final fileName = '${plugin.name}.json';
- final files = pluginDirectory.listSync();
+ Future<void> removePlugin(Plugin plugin) async {
+ pluginList.removeWhere((p) => p.name == plugin.name);
+ await savePlugins();
+ }
- // workaround for android/linux case insensitive
- File? targetFile;
- for (var file in files) {
- if (file is File &&
- path.basename(file.path).toLowerCase() == fileName.toLowerCase()) {
- targetFile = file;
+ // update or add plugin
+ void updatePlugin(Plugin plugin) {
+ bool flag = false;
+ for (int i = 0; i < pluginList.length; ++i) {
+ if (pluginList[i].name == plugin.name) {
+ pluginList[i] = plugin;
+ flag = true;
break;
}
}
+ if (!flag) {
+ pluginList.add(plugin);
+ }
+ savePlugins();
+ }
- if (targetFile != null) {
- await targetFile.delete();
- KazumiLogger()
- .log(Level.info, '已删除插件文件 ${path.basename(targetFile.path)}');
- } else {
- KazumiLogger().log(Level.warning, '插件文件 $fileName 不存在');
+ void onReorder(int oldIndex, int newIndex) {
+ if (oldIndex < newIndex) {
+ newIndex -= 1;
}
+ final plugin = pluginList.removeAt(oldIndex);
+ pluginList.insert(newIndex, plugin);
+ savePlugins();
+ }
+
+ Future<void> savePlugins() async {
+ final jsonData = jsonEncode(pluginListToJson());
+ final directory = await getApplicationSupportDirectory();
+ final pluginDirectory = Directory('${directory.path}/plugins');
+ final pluginsFile = File('${pluginDirectory.path}/$pluginsFileName');
+ await pluginsFile.writeAsString(jsonData);
+ KazumiLogger().log(Level.info, '已更新插件文件 $pluginsFileName');
+
+ pluginList = ObservableList.of(pluginList); // 强制替换触发更新
}
Future<void> queryPluginHTTPList() async {
@@ -187,8 +225,8 @@ abstract class _PluginsController with Store {
if (int.parse(pluginHTTPItem.api) > Api.apiLevel) {
return 1;
}
- await savePluginToJsonFile(pluginHTTPItem);
- await loadPlugins();
+ updatePlugin(pluginHTTPItem);
+ await savePlugins();
return 0;
}
return 2;
@@ -206,8 +244,12 @@ abstract class _PluginsController with Store {
return count;
}
- Future<void> tryInstallPlugin(Plugin plugin) async {
- await savePluginToJsonFile(plugin);
- await loadPlugins();
+ void removePlugins(Set<String> pluginNames) {
+ for (int i = pluginList.length - 1; i >= 0; --i) {
+ var name = pluginList[i].name;
+ if (pluginNames.contains(name)) {
+ pluginList.removeAt(i);
+ }
+ }
}
}