Skip to content

Commit

Permalink
fix: sort of completed tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Dec 12, 2023
1 parent d935b9d commit d07cf83
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
13 changes: 7 additions & 6 deletions pkg/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,8 @@ func (d *Downloader) getProtocolConfig(name string, v any) bool {
func (d *Downloader) watch(task *Task) {
err := task.fetcher.Wait()
if err != nil {
task.Status = base.DownloadStatusError
task.updateStatus(base.DownloadStatusError)
d.storage.Put(bucketTask, task.ID, task.clone())
d.emit(EventKeyError, task, err)
} else {
task.Progress.Used = task.timer.Used()
Expand All @@ -586,7 +587,7 @@ func (d *Downloader) watch(task *Task) {
totalSize := task.Meta.Res.Size
task.Progress.Speed = totalSize / used
task.Progress.Downloaded = totalSize
task.Status = base.DownloadStatusDone
task.updateStatus(base.DownloadStatusDone)
d.storage.Put(bucketTask, task.ID, task.clone())
d.emit(EventKeyDone, task)
d.emit(EventKeyFinally, task, err)
Expand Down Expand Up @@ -658,7 +659,6 @@ func (d *Downloader) doCreate(fetcher fetcher.Fetcher, opts *base.Options) (task
task.fetcher = fetcher
task.Meta = fetcher.Meta()
task.Progress = &Progress{}
task.Status = base.DownloadStatusReady
initTask(task)
if err = fetcher.Create(opts); err != nil {
return
Expand Down Expand Up @@ -699,7 +699,7 @@ func (d *Downloader) doStart(task *Task) (err error) {

cloneTask := task.clone()
isCreate := task.Status == base.DownloadStatusReady
task.Status = base.DownloadStatusRunning
task.updateStatus(base.DownloadStatusRunning)

doStart := func() error {
task.lock.Lock()
Expand All @@ -714,7 +714,8 @@ func (d *Downloader) doStart(task *Task) (err error) {
if task.Meta.Res == nil {
err := task.fetcher.Resolve(task.Meta.Req)
if err != nil {
task.Status = base.DownloadStatusError
task.updateStatus(base.DownloadStatusError)
d.storage.Put(bucketTask, task.ID, task.clone())
d.emit(EventKeyError, task, err)
return err
}
Expand Down Expand Up @@ -768,7 +769,7 @@ func (d *Downloader) doStart(task *Task) (err error) {
func (d *Downloader) doPause(task *Task) (err error) {
err = func() error {
if task.Status != base.DownloadStatusDone {
task.Status = base.DownloadStatusPause
task.updateStatus(base.DownloadStatusPause)
task.timer.Pause()
if task.fetcher != nil {
if err := task.fetcher.Pause(); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/download/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Task struct {
Status base.Status `json:"status"`
Progress *Progress `json:"progress"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`

fetcherBuilder fetcher.FetcherBuilder
fetcher fetcher.Fetcher
Expand All @@ -41,16 +42,23 @@ func NewTask() *Task {
ID: id,
Status: base.DownloadStatusReady,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
}

func (t *Task) updateStatus(status base.Status) {
t.UpdatedAt = time.Now()
t.Status = status
}

func (t *Task) clone() *Task {
return &Task{
ID: t.ID,
Meta: t.Meta,
Status: t.Status,
Progress: t.Progress,
CreatedAt: t.CreatedAt,
UpdatedAt: t.UpdatedAt,
}
}

Expand Down
2 changes: 2 additions & 0 deletions ui/flutter/lib/api/model/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ class Task {
Status status;
Progress progress;
DateTime createdAt;
DateTime updatedAt;

Task({
required this.id,
required this.meta,
required this.status,
required this.progress,
required this.createdAt,
required this.updatedAt,
});

factory Task.fromJson(Map<String, dynamic> json) => _$TaskFromJson(json);
Expand Down
2 changes: 2 additions & 0 deletions ui/flutter/lib/api/model/task.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import 'package:gopeed/app/modules/task/controllers/task_list_controller.dart';
import '../../../../api/model/task.dart';

class TaskDownloadedController extends TaskListController {
TaskDownloadedController() : super([Status.done], SortDirection.asc);
TaskDownloadedController()
: super([Status.done], (a, b) => b.updatedAt.compareTo(a.updatedAt));
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class TaskDownloadingController extends TaskListController {
Status.pause,
Status.wait,
Status.error
], SortDirection.desc);
], (a, b) => b.createdAt.compareTo(a.createdAt));
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import 'package:get/get.dart';
import '../../../../api/api.dart';
import '../../../../api/model/task.dart';

enum SortDirection { asc, desc }

abstract class TaskListController extends GetxController {
List<Status> statuses;
SortDirection sortDirection;
int Function(Task a, Task b) compare;

TaskListController(this.statuses, this.sortDirection);
TaskListController(this.statuses, this.compare);

final tasks = <Task>[].obs;
final isRunning = false.obs;
Expand Down Expand Up @@ -48,13 +46,7 @@ abstract class TaskListController extends GetxController {
getTasksState() async {
final tasks = await getTasks(statuses);
// sort tasks by create time
tasks.sort((a, b) {
if (sortDirection == SortDirection.asc) {
return a.createdAt.compareTo(b.createdAt);
} else {
return b.createdAt.compareTo(a.createdAt);
}
});
tasks.sort(compare);
this.tasks.value = tasks;
}
}

0 comments on commit d07cf83

Please sign in to comment.