UploadTask.ets 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { VideoItem } from './VideoItem';
  2. /**
  3. * 上传任务状态枚举
  4. */
  5. export enum UploadTaskStatus {
  6. Pending = 0, // 等待中
  7. Uploading = 1, // 上传中
  8. Success = 2, // 成功
  9. Failed = 3, // 失败
  10. Paused = 4 // 已暂停
  11. }
  12. /**
  13. * 上传任务数据源(用于LazyForEach优化)
  14. */
  15. export class UploadTaskDataSource implements IDataSource {
  16. private tasks: VideoItem[] = [];
  17. private listeners: DataChangeListener[] = [];
  18. public totalCount(): number {
  19. return this.tasks.length;
  20. }
  21. public getData(index: number): VideoItem {
  22. return this.tasks[index];
  23. }
  24. public registerDataChangeListener(listener: DataChangeListener): void {
  25. if (this.listeners.indexOf(listener) < 0) {
  26. this.listeners.push(listener);
  27. }
  28. }
  29. public unregisterDataChangeListener(listener: DataChangeListener): void {
  30. const pos = this.listeners.indexOf(listener);
  31. if (pos >= 0) {
  32. this.listeners.splice(pos, 1);
  33. }
  34. }
  35. /**
  36. * 更新数据源
  37. * @param tasks 新的任务列表
  38. */
  39. public updateTasks(tasks: VideoItem[]): void {
  40. this.tasks = tasks;
  41. this.notifyDataReload();
  42. }
  43. /**
  44. * 添加任务
  45. * @param task 任务
  46. */
  47. public addTask(task: VideoItem): void {
  48. this.tasks.push(task);
  49. this.notifyDataAdd(this.tasks.length - 1);
  50. }
  51. /**
  52. * 移除任务
  53. * @param index 索引
  54. */
  55. public removeTask(index: number): void {
  56. if (index >= 0 && index < this.tasks.length) {
  57. this.tasks.splice(index, 1);
  58. this.notifyDataDelete(index);
  59. }
  60. }
  61. /**
  62. * 清空任务
  63. */
  64. public clearTasks(): void {
  65. this.tasks = [];
  66. this.notifyDataReload();
  67. }
  68. /**
  69. * 通知数据重新加载
  70. */
  71. private notifyDataReload(): void {
  72. this.listeners.forEach(listener => {
  73. listener.onDataReloaded();
  74. });
  75. }
  76. /**
  77. * 通知数据添加
  78. * @param index 索引
  79. */
  80. private notifyDataAdd(index: number): void {
  81. this.listeners.forEach(listener => {
  82. listener.onDataAdd(index);
  83. });
  84. }
  85. /**
  86. * 通知数据删除
  87. * @param index 索引
  88. */
  89. private notifyDataDelete(index: number): void {
  90. this.listeners.forEach(listener => {
  91. listener.onDataDelete(index);
  92. });
  93. }
  94. /**
  95. * 通知数据变更
  96. * @param index 索引
  97. */
  98. private notifyDataChange(index: number): void {
  99. this.listeners.forEach(listener => {
  100. listener.onDataChange(index);
  101. });
  102. }
  103. }