feat: new ui (#1455)

This commit is contained in:
Levi authored and GitHub committed 2026-09-05 19:53:41 +08:00
1 parent 3be8db664c
commit 177c225f16
742 files changed
+59951 -5934

No files matched your search

@@ -0,0 +1,20 @@
import 'package:get/get.dart';
import '../controllers/task_controller.dart';
import '../controllers/task_downloaded_controller.dart';
import '../controllers/task_downloading_controller.dart';
class TaskBinding extends Bindings {
@override
void dependencies() {
Get.lazyPut<TaskController>(
() => TaskController(),
);
Get.lazyPut<TaskDownloadingController>(
() => TaskDownloadingController(),
);
Get.lazyPut<TaskDownloadedController>(
() => TaskDownloadedController(),
);
}
}
@@ -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,27 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:gopeed/api/model/task.dart';
class TaskController extends GetxController {
final tabIndex = 0.obs;
final scaffoldKey = GlobalKey<ScaffoldState>();
final selectTask = Rx<Task?>(null);
@override
void onInit() {
super.onInit();
if (kIsWeb) {
BrowserContextMenu.disableContextMenu();
}
}
@override
void onClose() {
super.onClose();
if (kIsWeb) {
BrowserContextMenu.enableContextMenu();
}
}
}
@@ -0,0 +1,8 @@
import 'package:gopeed/app/modules/task/controllers/task_list_controller.dart';
import '../../../../api/model/task.dart';
class TaskDownloadedController extends TaskListController {
TaskDownloadedController()
: super([Status.done], (a, b) => b.updatedAt.compareTo(a.updatedAt));
}
@@ -0,0 +1,21 @@
import '../../../../api/model/task.dart';
import 'task_list_controller.dart';
class TaskDownloadingController extends TaskListController {
TaskDownloadingController()
: super([
Status.ready,
Status.running,
Status.pause,
Status.wait,
Status.error
], (a, b) {
if (a.status == Status.running && b.status != Status.running) {
return -1;
} else if (a.status != Status.running && b.status == Status.running) {
return 1;
} else {
return b.updatedAt.compareTo(a.updatedAt);
}
});
}
@@ -0,0 +1,92 @@
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";
String filePath(String optName) {
return optName.isEmpty
? fullPath
: "${path == "/" ? path : "$path/"}$optName";
}
}
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,54 @@
import 'dart:async';
import 'package:get/get.dart';
import '../../../../api/api.dart';
import '../../../../api/model/task.dart';
abstract class TaskListController extends GetxController {
List<Status> statuses;
int Function(Task a, Task b) compare;
TaskListController(this.statuses, this.compare);
final tasks = <Task>[].obs;
final selectedTaskIds = <String>[].obs;
final isRunning = false.obs;
late final Timer _timer;
@override
void onInit() async {
super.onInit();
start();
_timer = Timer.periodic(const Duration(milliseconds: 1000), (timer) async {
if (isRunning.value) {
await getTasksState();
}
});
}
@override
void onClose() {
super.onClose();
_timer.cancel();
}
void start() async {
await getTasksState();
isRunning.value = true;
}
void stop() {
isRunning.value = false;
}
getTasksState() async {
final tasks = await getTasks(statuses);
// sort tasks by create time
tasks.sort(compare);
this.tasks.value = tasks;
}
}
@@ -0,0 +1,15 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../views/buid_task_list_view.dart';
import '../controllers/task_downloaded_controller.dart';
class TaskDownloadedView extends GetView<TaskDownloadedController> {
const TaskDownloadedView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BuildTaskListView(
tasks: controller.tasks, selectedTaskIds: controller.selectedTaskIds);
}
}
@@ -0,0 +1,15 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../views/buid_task_list_view.dart';
import '../controllers/task_downloading_controller.dart';
class TaskDownloadingView extends GetView<TaskDownloadingController> {
const TaskDownloadingView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BuildTaskListView(
tasks: controller.tasks, selectedTaskIds: controller.selectedTaskIds);
}
}
@@ -0,0 +1,132 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:open_filex/open_filex.dart';
import 'package:path/path.dart';
import 'package:share_plus/share_plus.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../../../api/api.dart' as api;
import '../../../../util/browser_download/browser_download.dart';
import '../../../../util/util.dart';
import '../../../views/breadcrumb_view.dart';
import '../../../views/file_icon.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];
// if resource is single file, use opts.name as file name
final realFileName = meta.res!.name.isEmpty
? (meta.opts.name.isEmpty ? file.name : meta.opts.name)
: "";
final fileRelativePath = file.filePath(realFileName);
final filePath = Util.safePathJoin(
[meta.opts.path, meta.res!.name, fileRelativePath]);
final fileName = basename(filePath);
return ListTile(
leading:
Icon(fileIcon(fileName, isFolder: file.isDirectory)),
title: Text(fileName),
subtitle: file.isDirectory
? Text('items'.trParams({
'count': controller
.dirItemCount(file.fullPath)
.toString()
}))
: Text(Util.fmtByte(file.size)),
trailing: file.isDirectory
? null
: SizedBox(
width: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: Util.isWeb()
? () {
final accessUrl = api.join(
"/fs/tasks/${controller.task.value!.id}$fileRelativePath");
return [
IconButton(
icon:
const Icon(Icons.open_in_new),
onPressed: () {
launchUrl(Uri.parse(accessUrl),
webOnlyWindowName:
"_blank");
}),
IconButton(
icon: const Icon(Icons.download),
onPressed: () {
download(accessUrl, fileName);
})
];
}()
: [
IconButton(
icon: const Icon(Icons.open_in_new),
onPressed: () async {
await OpenFilex.open(filePath);
}),
IconButton(
icon: const Icon(Icons.share),
onPressed: () {
final xfile = XFile(filePath);
Share.shareXFiles([xfile]);
})
],
),
),
onTap: () {
if (file.isDirectory) {
controller.toDir(file.fullPath);
}
},
);
},
itemCount: controller.fileList.length,
),
)
],
);
}));
}
}
@@ -0,0 +1,157 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:open_filex/open_filex.dart';
import 'package:path/path.dart' as path;
import '../../../../api/model/task.dart';
import '../../../../util/file_explorer.dart';
import '../../../../util/util.dart';
import '../../../routes/app_pages.dart';
import '../../../views/copy_button.dart';
import '../controllers/task_controller.dart';
import '../controllers/task_downloaded_controller.dart';
import '../controllers/task_downloading_controller.dart';
import 'task_downloaded_view.dart';
import 'task_downloading_view.dart';
class TaskView extends GetView<TaskController> {
const TaskView({Key? key}) : super(key: key);
String? _displayTaskUrl(Task? task) {
final rawUrl = task?.meta.req.rawUrl;
if (rawUrl != null && rawUrl.isNotEmpty) {
return rawUrl;
}
return task?.meta.req.url;
}
@override
Widget build(BuildContext context) {
final selectTask = controller.selectTask;
return DefaultTabController(
length: 2,
child: Scaffold(
key: controller.scaffoldKey,
appBar: PreferredSize(
preferredSize: const Size.fromHeight(56),
child: AppBar(
bottom: TabBar(
tabs: const [
Tab(
icon: Icon(Icons.file_download),
),
Tab(
icon: Icon(Icons.done),
),
],
onTap: (index) {
if (controller.tabIndex.value != index) {
controller.tabIndex.value = index;
final downloadingController =
Get.find<TaskDownloadingController>();
final downloadedController =
Get.find<TaskDownloadedController>();
switch (index) {
case 0:
downloadingController.start();
downloadedController.stop();
break;
case 1:
downloadingController.stop();
downloadedController.start();
break;
}
}
},
),
)),
body: const TabBarView(
children: [
TaskDownloadingView(),
TaskDownloadedView(),
],
),
endDrawer: Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// through the options in the drawer if there isn't enough vertical
// space to fit everything.
child: Obx(() => ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: [
SizedBox(
height: MediaQuery.of(context).padding.top + 65,
child: DrawerHeader(
child: Text(
'taskDetail'.tr,
style: Theme.of(context).textTheme.titleLarge,
)),
),
ListTile(
title: Text('taskName'.tr),
subtitle: buildTooltipSubtitle(selectTask.value?.name)),
ListTile(
title: Text('taskUrl'.tr),
subtitle: buildTooltipSubtitle(_displayTaskUrl(selectTask.value)),
trailing: CopyButton(_displayTaskUrl(selectTask.value)),
),
ListTile(
title: Text('downloadPath'.tr),
subtitle:
buildTooltipSubtitle(selectTask.value?.explorerUrl),
trailing: IconButton(
icon: const Icon(Icons.folder_open),
onPressed: () {
selectTask.value?.explorer();
},
),
),
],
)),
),
),
);
}
Widget buildTooltipSubtitle(String? text) {
final showText = text ?? "";
return Tooltip(
message: showText,
child: Text(
showText,
overflow: TextOverflow.ellipsis,
),
);
}
}
extension TaskEnhance on Task {
bool get isFolder {
return meta.res?.name.isNotEmpty ?? false;
}
String get explorerUrl {
return path.join(Util.safeDir(meta.opts.path), Util.safeDir(name));
}
Future<void> explorer() async {
if (Util.isDesktop()) {
await FileExplorer.openAndSelectFile(explorerUrl);
} else {
Get.rootDelegate.toNamed(Routes.TASK_FILES, parameters: {'id': id});
}
}
Future<void> open() async {
if (status != Status.done) {
return;
}
if (isFolder) {
await explorer();
} else {
await OpenFilex.open(explorerUrl);
}
}
}