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 type SmartPickerRecommendInfo from './SmartPickerRecommendInfo';
17import { Log } from '@ohos/common/src/main/ets/default/utils/Log';
18import { StringUtil } from '@ohos/common/src/main/ets/default/utils/StringUtil';
19import type { SmartPickerDataInterface } from '../interface/SmartPickerDataInterface';
20import SmartPickerConstants from './SmartPickerConstants';
21import type { AsyncCallback } from '@ohos/common/src/main/ets/default/model/common/AsyncCallback';
22import { SmartPickerDataFactory } from '../interface/SmartPickerDataFactory';
23import type { MediaItem } from '@ohos/common';
24import type { QueryParam } from '@ohos/common/src/main/ets/default/model/browser/BrowserDataImpl';
25import type { RecommendationOptions } from './SmartPickerManager';
26import type common from '@ohos.app.ability.common';
27
28const TAG: string = 'SmartPickerDataAdapter';
29
30export default class SmartPickerDataAdapter {
31  private photosDataInterface: SmartPickerDataInterface = undefined;
32  private uriDataImplMap: Map<string, SmartPickerDataInterface> = new Map();
33  private context: common.Context;
34
35  constructor(context: common.Context) {
36    this.context = context;
37  }
38
39  getTabInfoList(callback: AsyncCallback<SmartPickerRecommendInfo[]>, recommendationOptions: RecommendationOptions, param?: string): void {
40    try {
41      let recommendationType = recommendationOptions.recommendationType;
42      if (recommendationType >= SmartPickerConstants.QR_OR_BAR_CODE && recommendationType <= SmartPickerConstants.PROFILE_PICTURE) {
43        this.photosDataInterface = SmartPickerDataFactory.getFeature(this.context, SmartPickerDataFactory.TYPE_LABEL, this);
44        Log.debug(TAG, 'getTabInfoList this.photosDataInterface:' + this.photosDataInterface);
45        this.photosDataInterface.getTabInfoList(recommendationOptions).then(async (tabInfoList: Array<SmartPickerRecommendInfo>) => {
46          if (tabInfoList !== undefined && tabInfoList.length > 0) {
47            Log.debug(TAG, 'getTabInfoList tabInfoList length:' + tabInfoList.length);
48            this.setUriDataImplMap(tabInfoList, this.photosDataInterface);
49            callback.callback(tabInfoList);
50          } else {
51            Log.error(TAG, 'getTabInfoList tabInfoList is undefined');
52          }
53        });
54      }
55    } catch (err) {
56      Log.error(TAG, 'getTabInfoList err:' + err);
57    }
58  }
59
60  private setUriDataImplMap(tabInfoList: Array<SmartPickerRecommendInfo>, dataImpl: SmartPickerDataInterface): void {
61    if (tabInfoList === undefined || tabInfoList === null || tabInfoList.length <= 0) {
62      Log.error(TAG, 'getTabInsetUriDataImplMap param invalid');
63      return;
64    }
65    tabInfoList.forEach((tabInfo: SmartPickerRecommendInfo) => {
66      if (!StringUtil.isEmpty(tabInfo.getAlbumUri())) {
67        this.uriDataImplMap.set(tabInfo.getAlbumUri(), dataImpl);
68      } else {
69      }
70    });
71  }
72
73  getData(callback: AsyncCallback<MediaItem[]>, param: QueryParam): void {
74    try {
75      let albumUri = param?.albumUri;
76      if (StringUtil.isEmpty(albumUri)) {
77        callback.callback([]);
78        return;
79      }
80      let smartPickerDataInterface = this.uriDataImplMap.get(albumUri);
81      if (smartPickerDataInterface !== undefined && smartPickerDataInterface !== null) {
82        this.uriDataImplMap.get(albumUri).getData(callback, param);
83      } else {
84        callback.callback([]);
85        return;
86      }
87    } catch (err) {
88      Log.error(TAG, 'getData err:' + err);
89    }
90  }
91
92  async getDataCount(albumUri: string, filterMediaType?: string): Promise<number> {
93    let count = 0;
94    if (!StringUtil.isEmpty(albumUri) && this.uriDataImplMap.get(albumUri) !== undefined) {
95      return this.uriDataImplMap.get(albumUri).getDataCount(albumUri, filterMediaType);
96    } else {
97      return new Promise((resolve): void => {
98        resolve(count);
99      });
100    }
101  }
102}
103
104
105