• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { AlbumSetCallback } from './AlbumSetCallback';
17import { TraceControllerUtils } from '../../../utils/TraceControllerUtils';
18import { BrowserDataFactory } from '../../../interface/BrowserDataFactory';
19import type { BrowserDataInterface } from '../../../interface/BrowserDataInterface';
20import { BroadCast } from '../../../utils/BroadCast';
21import { Log } from '../../../utils/Log';
22import { AlbumInfo } from './AlbumInfo';
23import { AbsDataSource } from '../AbsDataSource';
24import { Constants } from '../../common/Constants';
25import { AlbumSetDataInfo } from '../../common/AlbumSetDataInfo';
26import type { InitAlbumParam } from './AlbumDataImpl';
27
28const TAG: string = 'common_AlbumSetDataSource';
29
30export class AlbumSetDataSource extends AbsDataSource {
31  mediaSetList: AlbumInfo[] = [];
32  albumDataImpl: BrowserDataInterface;
33  isMultiParameter: boolean;
34  private broadCast_: BroadCast;
35  private filterMediaType: string = undefined;
36  private blackListAlbum: Array<string> = [];
37  private deviceId = '';
38  private filterAlbumsFunction: Function = (mediaSetList: AlbumInfo[]): AlbumInfo[] => {
39    return mediaSetList;
40  };
41
42  constructor(broadCast: BroadCast, param?: InitAlbumParam) {
43    super();
44
45    Log.debug(TAG, `constructor ${JSON.stringify(param)}`);
46    if (broadCast) {
47      this.broadCast_ = broadCast;
48    }
49    this.albumDataImpl = BrowserDataFactory.getFeature(BrowserDataFactory.TYPE_ALBUM, param);
50
51    if (param && param.deviceId) {
52      this.deviceId = param.deviceId;
53    }
54  }
55
56  initData(): void {
57    TraceControllerUtils.startTrace('AlbumSetPageInitData');
58    Log.debug(TAG, 'initData');
59    this.loadData();
60  }
61
62  loadData(): void {
63    Log.info(TAG, 'load data');
64    if (this.albumDataImpl != null) {
65      let callback: AlbumSetCallback = new AlbumSetCallback(this);
66      this.albumDataImpl.getData(callback,
67        (this.filterMediaType === undefined || this.filterMediaType === null) ?
68          null : { filterMediaType: this.filterMediaType });
69    }
70  }
71
72  totalCount(): number {
73    let newTotalCount = this.mediaSetList.length;
74    if (this.lastTotalCount != newTotalCount) {
75      Log.info(TAG, `totalCount: ${newTotalCount}`);
76      this.lastTotalCount = newTotalCount;
77    }
78    return newTotalCount;
79  }
80
81  setMultiParameter(isMultiParameter: boolean): void {
82    this.isMultiParameter = isMultiParameter;
83  }
84
85  getData(index: number): AlbumSetDataInfo {
86    Log.info(TAG, `getData index: ${index}, item: ${JSON.stringify(this.mediaSetList[index])}`);
87    if (index < 0 || index >= this.mediaSetList.length) {
88      Log.error(TAG, `index out of the total size, index: ${index} total size: ${this.mediaSetList.length}`);
89      return undefined;
90    }
91    return new AlbumSetDataInfo(this.mediaSetList[index], index);
92  }
93
94  updateAlbumSetData(requestTime: number, mediaSetList: AlbumInfo[]): void {
95    TraceControllerUtils.startTraceWithTaskId('updateAlbumSetData', requestTime);
96    Log.info(TAG, `updateMediaItems size: ${mediaSetList.length}`);
97    this.lastUpdateTime = requestTime;
98    if (this.lastUpdateTime < this.lastChangeTime && this.isActive) {
99      // If there is a new media library change callback,
100      // the query continues and the current data is updated without return.
101      Log.debug(TAG, 'request data expired, request again!');
102      this.loadData();
103    } else {
104      this.hasNewChange = false;
105    }
106    this.mediaSetList = this.excludeBlackList(mediaSetList);
107    this.onDataReloaded();
108    TraceControllerUtils.finishTraceWithTaskId('updateAlbumSetData', requestTime);
109    this.broadCast_.emit(Constants.ON_LOADING_FINISHED, [this.totalCount()]);
110    TraceControllerUtils.finishTrace('AlbumSetPageInitData');
111  }
112
113  excludeBlackList(mediaSetList: AlbumInfo[]): Array<AlbumInfo> {
114    let res: AlbumInfo[];
115    if (0 == this.blackListAlbum.length) {
116      Log.debug(TAG, 'BlackListAlbum: no black list.');
117      res = mediaSetList;
118    } else {
119      Log.debug(TAG, `BlackListAlbum: albums name ${JSON.stringify(this.blackListAlbum)}.`);
120      res = mediaSetList.filter((item) => {
121        return this.blackListAlbum.indexOf(item.uri) < 0; // id 修改为了 uri,保证编译通过,可能发生错误
122      });
123    }
124    res = this.filterAlbumsFunction(res);
125    Log.debug(TAG, `BlackListAlbum: old albums length ${mediaSetList.length}, new albums length ${res.length}.`);
126    return res;
127  }
128
129  onChange(mediaType: string): void {
130    if (this.deviceId == '' || this.deviceId == undefined) {
131      if (mediaType == 'image' || mediaType == 'video' || mediaType == 'album') {
132        super.onChange(mediaType);
133      }
134    } else {
135      if (mediaType == 'remote') {
136        super.onChange(mediaType);
137      }
138    }
139  }
140
141  updateAlbumMediaCount(): void {
142    for (let album of this.mediaSetList) {
143      this.albumDataImpl.getDataCount(null, album);
144    }
145  }
146
147  setFilterMediaType(filterMediaType: string): void {
148    Log.info(TAG, `set filterMediaType: ${filterMediaType}`)
149    this.filterMediaType = filterMediaType;
150  }
151
152  setBlackList(albums: Array<string>): void {
153    this.blackListAlbum = albums;
154    Log.debug(TAG, `BlackListAlbum: set blacklistAlbum ${JSON.stringify(this.blackListAlbum)}.`);
155  }
156
157  setFilterAlbumsFunction(filterAlbumsFunction: Function): void {
158    this.filterAlbumsFunction = filterAlbumsFunction;
159  }
160}