• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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 */
15import MediaLib from '@ohos.multimedia.mediaLibrary';
16import selectManager from '../manager/SelectManager';
17import mediaModel from '../model/MediaModel';
18import { Log } from '../utils/Log';
19import { MediaConstants } from '../constants/MediaConstants';
20import { MediaDataItem } from './MediaDataItem';
21
22const TAG = "TrashMediaDataItem"
23
24export class TrashMediaDataItem extends MediaDataItem {
25    constructor(selections: string, selectionArgs: Array<string>, index: number) {
26        super(selections, selectionArgs, "", index);
27        this.setSelect(false);
28    }
29
30    async loadFileAsset(): Promise<MediaLib.FileAsset> {
31        let fetchOption: MediaLib.MediaFetchOptions;
32        if (this.status == MediaConstants.UNDEFINED) {
33            fetchOption = {
34                selections: this.selections,
35                selectionArgs: this.selectionArgs,
36                order: `date_added DESC LIMIT ${this.index},1`
37            }
38        } else {
39            fetchOption = {
40                selections: `${MediaLib.FileKey.ID} = ?`,
41                selectionArgs: [this.id.toString()],
42                order: `date_added DESC`
43            }
44        }
45        return (await mediaModel.getAllTrashMediaItem(fetchOption, false)).fileAsset;
46    }
47
48    async onRecover(): Promise<boolean> {
49        try {
50            let fileAsset = await this.loadFileAsset();
51
52            await fileAsset.trash(false);
53            selectManager.deleteSelect(this.uri);
54            this.status = MediaConstants.TRASHED;
55            return true;
56        } catch (err) {
57            Log.error(TAG, `onRecover error: ${JSON.stringify(err)}`);
58            return false;
59        }
60    }
61
62    async onDelete(): Promise<boolean> {
63        try {
64            await mediaModel.deleteOne(this.uri);
65            selectManager.deleteSelect(this.uri);
66            this.status = MediaConstants.TRASHED;
67            return true;
68        } catch (err) {
69            Log.error(TAG, `onDelete error: ${JSON.stringify(err)}`);
70            return false;
71        }
72    }
73}