mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-08 17:40:28 +00:00
feat: task file manage (#196)
1. support view task file list 2. support share file on android and iOS
This commit is contained in:
14 files changed
+312
-14
No files matched your search
@@ -334,13 +334,15 @@ class SettingView extends GetView<SettingController> {
|
||||
width: 150,
|
||||
child: DropdownButtonFormField<String>(
|
||||
value: startCfg.value.network,
|
||||
onChanged: (value) async {
|
||||
startCfg.update((val) {
|
||||
val!.network = value!;
|
||||
});
|
||||
onChanged: Util.isDesktop()
|
||||
? (value) async {
|
||||
startCfg.update((val) {
|
||||
val!.network = value!;
|
||||
});
|
||||
|
||||
await debounceSave(needRestart: true);
|
||||
},
|
||||
await debounceSave(needRestart: true);
|
||||
}
|
||||
: null,
|
||||
items: [
|
||||
const DropdownMenuItem<String>(
|
||||
value: 'tcp',
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import 'package:get/get.dart';
|
||||
|
||||
import '../controllers/task_files_controller.dart';
|
||||
|
||||
class TaskFilesBinding extends Bindings {
|
||||
@override
|
||||
void dependencies() {
|
||||
Get.lazyPut<TaskFilesController>(
|
||||
() => TaskFilesController(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
import 'package:get/get.dart';
|
||||
import '../../../../api/api.dart';
|
||||
import '../../../../api/model/resource.dart';
|
||||
import '../../../../api/model/task.dart';
|
||||
|
||||
class FileItem {
|
||||
final bool isDirectory;
|
||||
final String path;
|
||||
final String name;
|
||||
final int size;
|
||||
|
||||
FileItem(this.isDirectory, this.path, this.name, this.size);
|
||||
|
||||
String get fullPath => "${path == "/" ? path : "$path/"}$name";
|
||||
}
|
||||
|
||||
class TaskFilesController extends GetxController {
|
||||
final Map<String, List<FileItem>> _dirMap = {};
|
||||
|
||||
final task = Rx<Task?>(null);
|
||||
final fileList = <FileItem>[].obs;
|
||||
|
||||
@override
|
||||
void onInit() async {
|
||||
super.onInit();
|
||||
|
||||
final taskId = Get.rootDelegate.parameters['id'];
|
||||
final tasks = await getTasks([]);
|
||||
task.value = tasks.firstWhere((element) => element.id == taskId);
|
||||
parseDirMap(task.value!.meta.res.files);
|
||||
toDir("/");
|
||||
}
|
||||
|
||||
void parseDirMap(List<FileInfo> fileList) {
|
||||
for (final file in fileList) {
|
||||
String dir = file.path;
|
||||
if (!dir.startsWith("/")) {
|
||||
dir = "/$dir";
|
||||
}
|
||||
if (!_dirMap.containsKey(dir)) {
|
||||
_dirMap[dir] = [];
|
||||
}
|
||||
|
||||
_dirMap[dir]!.add(FileItem(false, dir, file.name, file.size));
|
||||
|
||||
void findParent(String dir) {
|
||||
final parentDirIndex = dir.lastIndexOf("/");
|
||||
String parentDir =
|
||||
parentDirIndex == 0 ? "/" : dir.substring(0, parentDirIndex);
|
||||
if (!_dirMap.containsKey(parentDir)) {
|
||||
_dirMap[parentDir] = [];
|
||||
}
|
||||
String dirName = dir.substring(dir.lastIndexOf("/") + 1);
|
||||
if (!_dirMap[parentDir]!.any((element) => element.name == dirName)) {
|
||||
_dirMap[parentDir]!.add(FileItem(true, parentDir, dirName, 0));
|
||||
}
|
||||
}
|
||||
|
||||
while (dir != "/" && dir != "") {
|
||||
findParent(dir);
|
||||
dir = dir.substring(0, dir.lastIndexOf("/"));
|
||||
}
|
||||
}
|
||||
|
||||
// sort children, directories first then files, alphabetically
|
||||
_dirMap.forEach((key, value) {
|
||||
value.sort((a, b) {
|
||||
if (a.isDirectory && !b.isDirectory) {
|
||||
return -1;
|
||||
} else if (!a.isDirectory && b.isDirectory) {
|
||||
return 1;
|
||||
} else {
|
||||
return a.name.compareTo(b.name);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void toDir(String dir) {
|
||||
fileList.value = _dirMap[dir]?.toList() ?? [];
|
||||
}
|
||||
|
||||
int dirItemCount(String dir) {
|
||||
return _dirMap[dir]?.length ?? 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
|
||||
import '../../../../util/file_icon.dart';
|
||||
import '../../../../util/icons.dart';
|
||||
import '../../../../util/util.dart';
|
||||
import '../../../views/breadcrumb_view.dart';
|
||||
import '../controllers/task_files_controller.dart';
|
||||
|
||||
class TaskFilesView extends GetView<TaskFilesController> {
|
||||
const TaskFilesView({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
onPressed: () => Get.rootDelegate.popRoute()),
|
||||
// actions: [],
|
||||
title: Obx(() => Text(controller.task.value?.meta.res.name ?? "")),
|
||||
),
|
||||
body: Obx(() {
|
||||
final fileList = controller.fileList;
|
||||
final breadcrumbItems = ["/"];
|
||||
if (fileList.isNotEmpty) {
|
||||
final file = fileList.first;
|
||||
final path = file.path.substring(1);
|
||||
if (path.isNotEmpty) {
|
||||
final pathArr = path.split("/");
|
||||
for (int i = 0; i < pathArr.length; i++) {
|
||||
breadcrumbItems.add(pathArr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Column(
|
||||
children: [
|
||||
Breadcrumb(
|
||||
items: breadcrumbItems,
|
||||
onItemTap: (index) {
|
||||
final targetDirArr = <String>[];
|
||||
for (int i = 0; i <= index; i++) {
|
||||
targetDirArr.add(breadcrumbItems[i]);
|
||||
}
|
||||
controller
|
||||
.toDir(targetDirArr.join("/").replaceFirst('//', '/'));
|
||||
}).paddingOnly(left: 16, top: 16, bottom: 8),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemBuilder: (context, index) {
|
||||
final meta = controller.task.value!.meta;
|
||||
final file = fileList[index];
|
||||
return ListTile(
|
||||
leading: file.isDirectory
|
||||
? const Icon(Icons.folder)
|
||||
: Icon(FaIcons.allIcons[findIcon(file.name)]),
|
||||
title: Text(fileList[index].name),
|
||||
subtitle: file.isDirectory
|
||||
? Text('items'.trParams({
|
||||
'count': controller
|
||||
.dirItemCount(file.fullPath)
|
||||
.toString()
|
||||
}))
|
||||
: Text(Util.fmtByte(file.size)),
|
||||
trailing: file.isDirectory
|
||||
? null
|
||||
: IconButton(
|
||||
icon: const Icon(Icons.share),
|
||||
onPressed: () {
|
||||
final xfile = XFile(Util.safePathJoin([
|
||||
meta.opts.path,
|
||||
meta.res.rootDir,
|
||||
file.fullPath
|
||||
]));
|
||||
Share.shareXFiles([xfile]);
|
||||
}),
|
||||
onTap: () {
|
||||
if (file.isDirectory) {
|
||||
controller.toDir(file.fullPath);
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
itemCount: controller.fileList.length,
|
||||
),
|
||||
)
|
||||
],
|
||||
);
|
||||
}));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user