• 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    this.broadCast_ = broadCast;
47    this.albumDataImpl = BrowserDataFactory.getFeature(BrowserDataFactory.TYPE_ALBUM, param);
48
49    if (param && param.deviceId) {
50      this.deviceId = param.deviceId;
51    }
52  }
53
54  initData(): void {
55    TraceControllerUtils.startTrace('AlbumSetPageInitData');
56    Log.debug(TAG, 'initData');
57    this.loadData();
58  }
59
60  loadData(): void {
61    Log.info(TAG, 'load data');
62    if (this.albumDataImpl != null) {
63      let callback: AlbumSetCallback = new AlbumSetCallback(this);
64      this.albumDataImpl.getData(callback,
65        (this.filterMediaType == undefined) ? null : { filterMediaType: this.filterMediaType });
66    }
67  }
68
69  totalCount(): number {
70    let newTotalCount = this.mediaSetList.length;
71    if (this.lastTotalCount != newTotalCount) {
72      Log.info(TAG, `totalCount: ${newTotalCount}`);
73      this.lastTotalCount = newTotalCount;
74    }
75    return newTotalCount;
76  }
77
78  setMultiParameter(isMultiParameter: boolean): void {
79    this.isMultiParameter = isMultiParameter;
80  }
81
82  getData(index: number): AlbumSetDataInfo {
83    Log.info(TAG, `getData index: ${index}, item: ${JSON.stringify(this.mediaSetList[index])}`);
84    if (index < 0 || index >= this.mediaSetList.length) {
85      Log.error(TAG, `index out of the total size, index: ${index} total size: ${this.mediaSetList.length}`);
86      return undefined;
87    }
88    return new AlbumSetDataInfo(this.mediaSetList[index], index);
89  }
90
91  updateAlbumSetData(requestTime: number, mediaSetList: AlbumInfo[]): void {
92    TraceControllerUtils.startTraceWithTaskId('updateAlbumSetData', requestTime);
93    Log.info(TAG, `updateMediaItems size: ${mediaSetList.length}`);
94    this.lastUpdateTime = requestTime;
95    if (this.lastUpdateTime < this.lastChangeTime && this.isActive) {
96      // If there is a new media library change callback,
97      // the query continues and the current data is updated without return.
98      Log.debug(TAG, 'request data expired, request again!');
99      this.loadData();
100    } else {
101      this.hasNewChange = false;
102    }
103    this.mediaSetList = this.excludeBlackList(mediaSetList);
104    this.onDataReloaded();
105    TraceControllerUtils.finishTraceWithTaskId('updateAlbumSetData', requestTime);
106    this.broadCast_.emit(Constants.ON_LOADING_FINISHED, [this.totalCount()]);
107    TraceControllerUtils.finishTrace('AlbumSetPageInitData');
108  }
109
110  excludeBlackList(mediaSetList: AlbumInfo[]): Array<AlbumInfo> {
111    let res: AlbumInfo[];
112    if (0 == this.blackListAlbum.length) {
113      Log.debug(TAG, 'BlackListAlbum: no black list.');
114      res = mediaSetList;
115    } else {
116      Log.debug(TAG, `BlackListAlbum: albums name ${JSON.stringify(this.blackListAlbum)}.`);
117      res = mediaSetList.filter((item) => {
118        return this.blackListAlbum.indexOf(item.uri) < 0; // id 修改为了 uri,保证编译通过,可能发生错误
119      });
120    }
121    res = this.filterAlbumsFunction(res);
122    Log.debug(TAG, `BlackListAlbum: old albums length ${mediaSetList.length}, new albums length ${res.length}.`);
123    return res;
124  }
125
126  onChange(mediaType: string): void {
127    if (this.deviceId == '' || this.deviceId == undefined) {
128      if (mediaType == 'image' || mediaType == 'video' || mediaType == 'album') {
129        super.onChange(mediaType);
130      }
131    } else {
132      if (mediaType == 'remote') {
133        super.onChange(mediaType);
134      }
135    }
136  }
137
138  updateAlbumMediaCount(): void {
139    for (let album of this.mediaSetList) {
140      this.albumDataImpl.getDataCount(null, album);
141    }
142  }
143
144  setFilterMediaType(filterMediaType: string): void {
145    Log.info(TAG, `set filterMediaType: ${filterMediaType}`)
146    this.filterMediaType = filterMediaType;
147  }
148
149  setBlackList(albums: Array<string>): void {
150    this.blackListAlbum = albums;
151    Log.debug(TAG, `BlackListAlbum: set blacklistAlbum ${JSON.stringify(this.blackListAlbum)}.`);
152  }
153
154  setFilterAlbumsFunction(filterAlbumsFunction: Function): void {
155    this.filterAlbumsFunction = filterAlbumsFunction;
156  }
157}