|
@@ -3,10 +3,53 @@ import { DownloadCenterTask } from '../common/util/DownloadCenterManager';
|
|
|
import { CommonConstants } from '../common/constants/CommonConstants';
|
|
import { CommonConstants } from '../common/constants/CommonConstants';
|
|
|
import { StrUtil } from '@pura/harmony-utils';
|
|
import { StrUtil } from '@pura/harmony-utils';
|
|
|
|
|
|
|
|
|
|
+@Observed
|
|
|
|
|
+class DownloadCenterTaskState implements DownloadCenterTask {
|
|
|
|
|
+ taskId: string = '';
|
|
|
|
|
+ title: string = '';
|
|
|
|
|
+ fileName: string = '';
|
|
|
|
|
+ coverPath: string = '';
|
|
|
|
|
+ sizeText: string = '';
|
|
|
|
|
+ sourceUrl: string = '';
|
|
|
|
|
+ targetPath: string = '';
|
|
|
|
|
+ downloadDir: string = '';
|
|
|
|
|
+ totalBytes: number = 0;
|
|
|
|
|
+ downloadedBytes: number = 0;
|
|
|
|
|
+ progress: number = 0;
|
|
|
|
|
+ speedBytesPerSec: number = 0;
|
|
|
|
|
+ status: 'pending' | 'downloading' | 'paused' | 'completed' | 'failed' = 'pending';
|
|
|
|
|
+ errorMessage: string = '';
|
|
|
|
|
+ createdAt: number = 0;
|
|
|
|
|
+ finishedAt: number = 0;
|
|
|
|
|
+
|
|
|
|
|
+ constructor(task: DownloadCenterTask) {
|
|
|
|
|
+ this.apply(task);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ apply(task: DownloadCenterTask): void {
|
|
|
|
|
+ this.taskId = task.taskId;
|
|
|
|
|
+ this.title = task.title;
|
|
|
|
|
+ this.fileName = task.fileName;
|
|
|
|
|
+ this.coverPath = task.coverPath;
|
|
|
|
|
+ this.sizeText = task.sizeText;
|
|
|
|
|
+ this.sourceUrl = task.sourceUrl;
|
|
|
|
|
+ this.targetPath = task.targetPath;
|
|
|
|
|
+ this.downloadDir = task.downloadDir;
|
|
|
|
|
+ this.totalBytes = task.totalBytes;
|
|
|
|
|
+ this.downloadedBytes = task.downloadedBytes;
|
|
|
|
|
+ this.progress = task.progress;
|
|
|
|
|
+ this.speedBytesPerSec = task.speedBytesPerSec;
|
|
|
|
|
+ this.status = task.status;
|
|
|
|
|
+ this.errorMessage = task.errorMessage;
|
|
|
|
|
+ this.createdAt = task.createdAt;
|
|
|
|
|
+ this.finishedAt = task.finishedAt;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
@Component
|
|
@Component
|
|
|
struct DownloadCenterTaskRow {
|
|
struct DownloadCenterTaskRow {
|
|
|
@Prop themeColor: string = CommonConstants.DEFAULT_THEME_COLOR;
|
|
@Prop themeColor: string = CommonConstants.DEFAULT_THEME_COLOR;
|
|
|
- @Prop task: DownloadCenterTask;
|
|
|
|
|
|
|
+ @ObjectLink task: DownloadCenterTaskState;
|
|
|
@Prop isDownloading: boolean = true;
|
|
@Prop isDownloading: boolean = true;
|
|
|
@Prop refreshVersion: number = 0;
|
|
@Prop refreshVersion: number = 0;
|
|
|
onPauseTask: (taskId: string) => void = (_taskId: string): void => {};
|
|
onPauseTask: (taskId: string) => void = (_taskId: string): void => {};
|
|
@@ -227,8 +270,8 @@ export struct DownloadCenter {
|
|
|
@Prop activeTasksProp: DownloadCenterTask[] = [];
|
|
@Prop activeTasksProp: DownloadCenterTask[] = [];
|
|
|
@Prop completedTasksProp: DownloadCenterTask[] = [];
|
|
@Prop completedTasksProp: DownloadCenterTask[] = [];
|
|
|
@Link selectedIndexes: number[];
|
|
@Link selectedIndexes: number[];
|
|
|
- @State activeTasks: DownloadCenterTask[] = [];
|
|
|
|
|
- @State completedTasks: DownloadCenterTask[] = [];
|
|
|
|
|
|
|
+ @State activeTasks: DownloadCenterTaskState[] = [];
|
|
|
|
|
+ @State completedTasks: DownloadCenterTaskState[] = [];
|
|
|
@State renderVersion: number = 0;
|
|
@State renderVersion: number = 0;
|
|
|
onClose: () => void = () => {};
|
|
onClose: () => void = () => {};
|
|
|
onPauseTask: (taskId: string) => void = (_taskId: string): void => {};
|
|
onPauseTask: (taskId: string) => void = (_taskId: string): void => {};
|
|
@@ -309,12 +352,8 @@ export struct DownloadCenter {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private syncTasksFromProps(): void {
|
|
private syncTasksFromProps(): void {
|
|
|
- this.activeTasks = this.activeTasksProp.map((task: DownloadCenterTask): DownloadCenterTask => {
|
|
|
|
|
- return this.cloneTask(task);
|
|
|
|
|
- });
|
|
|
|
|
- this.completedTasks = this.completedTasksProp.map((task: DownloadCenterTask): DownloadCenterTask => {
|
|
|
|
|
- return this.cloneTask(task);
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ this.activeTasks = this.mergeTaskStates(this.activeTasks, this.activeTasksProp);
|
|
|
|
|
+ this.completedTasks = this.mergeTaskStates(this.completedTasks, this.completedTasksProp);
|
|
|
this.renderVersion = this.tasksVersion;
|
|
this.renderVersion = this.tasksVersion;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -333,7 +372,7 @@ export struct DownloadCenter {
|
|
|
.justifyContent(FlexAlign.Center)
|
|
.justifyContent(FlexAlign.Center)
|
|
|
} else {
|
|
} else {
|
|
|
List({ space: 10 }) {
|
|
List({ space: 10 }) {
|
|
|
- ForEach(this.activeTasks, (task: DownloadCenterTask) => {
|
|
|
|
|
|
|
+ ForEach(this.activeTasks, (task: DownloadCenterTaskState) => {
|
|
|
DownloadCenterTaskRow({
|
|
DownloadCenterTaskRow({
|
|
|
themeColor: this.themeColor,
|
|
themeColor: this.themeColor,
|
|
|
task: task,
|
|
task: task,
|
|
@@ -342,7 +381,7 @@ export struct DownloadCenter {
|
|
|
onPauseTask: this.onPauseTask,
|
|
onPauseTask: this.onPauseTask,
|
|
|
onResumeTask: this.onResumeTask
|
|
onResumeTask: this.onResumeTask
|
|
|
})
|
|
})
|
|
|
- }, (task: DownloadCenterTask): string => {
|
|
|
|
|
|
|
+ }, (task: DownloadCenterTaskState): string => {
|
|
|
return this.getTaskRenderKey(task);
|
|
return this.getTaskRenderKey(task);
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
@@ -363,14 +402,14 @@ export struct DownloadCenter {
|
|
|
.justifyContent(FlexAlign.Center)
|
|
.justifyContent(FlexAlign.Center)
|
|
|
} else {
|
|
} else {
|
|
|
List({ space: 10 }) {
|
|
List({ space: 10 }) {
|
|
|
- ForEach(this.completedTasks, (task: DownloadCenterTask) => {
|
|
|
|
|
|
|
+ ForEach(this.completedTasks, (task: DownloadCenterTaskState) => {
|
|
|
DownloadCenterTaskRow({
|
|
DownloadCenterTaskRow({
|
|
|
themeColor: this.themeColor,
|
|
themeColor: this.themeColor,
|
|
|
task: task,
|
|
task: task,
|
|
|
isDownloading: false,
|
|
isDownloading: false,
|
|
|
refreshVersion: this.renderVersion
|
|
refreshVersion: this.renderVersion
|
|
|
})
|
|
})
|
|
|
- }, (task: DownloadCenterTask): string => {
|
|
|
|
|
|
|
+ }, (task: DownloadCenterTaskState): string => {
|
|
|
return this.getTaskRenderKey(task);
|
|
return this.getTaskRenderKey(task);
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
@@ -404,13 +443,13 @@ export struct DownloadCenter {
|
|
|
|
|
|
|
|
private resolveCurrentDownloadDirectory(): string {
|
|
private resolveCurrentDownloadDirectory(): string {
|
|
|
for (let i = 0; i < this.activeTasks.length; i += 1) {
|
|
for (let i = 0; i < this.activeTasks.length; i += 1) {
|
|
|
- const task: DownloadCenterTask = this.activeTasks[i];
|
|
|
|
|
|
|
+ const task: DownloadCenterTaskState = this.activeTasks[i];
|
|
|
if (StrUtil.isNotEmpty(task.downloadDir)) {
|
|
if (StrUtil.isNotEmpty(task.downloadDir)) {
|
|
|
return task.downloadDir;
|
|
return task.downloadDir;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
for (let i = 0; i < this.completedTasks.length; i += 1) {
|
|
for (let i = 0; i < this.completedTasks.length; i += 1) {
|
|
|
- const task: DownloadCenterTask = this.completedTasks[i];
|
|
|
|
|
|
|
+ const task: DownloadCenterTaskState = this.completedTasks[i];
|
|
|
if (StrUtil.isNotEmpty(task.downloadDir)) {
|
|
if (StrUtil.isNotEmpty(task.downloadDir)) {
|
|
|
return task.downloadDir;
|
|
return task.downloadDir;
|
|
|
}
|
|
}
|
|
@@ -418,29 +457,29 @@ export struct DownloadCenter {
|
|
|
return '';
|
|
return '';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private getTaskRenderKey(task: DownloadCenterTask): string {
|
|
|
|
|
- return task.taskId;
|
|
|
|
|
|
|
+ private mergeTaskStates(currentTasks: DownloadCenterTaskState[], nextTasks: DownloadCenterTask[]): DownloadCenterTaskState[] {
|
|
|
|
|
+ const nextStateMap: Map<string, DownloadCenterTaskState> = new Map<string, DownloadCenterTaskState>();
|
|
|
|
|
+ for (let i = 0; i < currentTasks.length; i += 1) {
|
|
|
|
|
+ const state: DownloadCenterTaskState = currentTasks[i];
|
|
|
|
|
+ nextStateMap.set(state.taskId, state);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const mergedTasks: DownloadCenterTaskState[] = [];
|
|
|
|
|
+ for (let i = 0; i < nextTasks.length; i += 1) {
|
|
|
|
|
+ const nextTask: DownloadCenterTask = nextTasks[i];
|
|
|
|
|
+ const existingState: DownloadCenterTaskState | undefined = nextStateMap.get(nextTask.taskId);
|
|
|
|
|
+ if (existingState) {
|
|
|
|
|
+ existingState.apply(nextTask);
|
|
|
|
|
+ mergedTasks.push(existingState);
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ mergedTasks.push(new DownloadCenterTaskState(nextTask));
|
|
|
|
|
+ }
|
|
|
|
|
+ return mergedTasks;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private cloneTask(task: DownloadCenterTask): DownloadCenterTask {
|
|
|
|
|
- return {
|
|
|
|
|
- taskId: task.taskId,
|
|
|
|
|
- title: task.title,
|
|
|
|
|
- fileName: task.fileName,
|
|
|
|
|
- coverPath: task.coverPath,
|
|
|
|
|
- sizeText: task.sizeText,
|
|
|
|
|
- sourceUrl: task.sourceUrl,
|
|
|
|
|
- targetPath: task.targetPath,
|
|
|
|
|
- downloadDir: task.downloadDir,
|
|
|
|
|
- totalBytes: task.totalBytes,
|
|
|
|
|
- downloadedBytes: task.downloadedBytes,
|
|
|
|
|
- progress: task.progress,
|
|
|
|
|
- speedBytesPerSec: task.speedBytesPerSec,
|
|
|
|
|
- status: task.status,
|
|
|
|
|
- errorMessage: task.errorMessage,
|
|
|
|
|
- createdAt: task.createdAt,
|
|
|
|
|
- finishedAt: task.finishedAt
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ private getTaskRenderKey(task: DownloadCenterTaskState): string {
|
|
|
|
|
+ return task.taskId;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private getCurrentTabIndex(): number {
|
|
private getCurrentTabIndex(): number {
|