• 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 */
15
16import { Log } from '../utils/Log';
17import { ItemDataSource } from '../vm/ItemDataSource';
18import { MenuContext } from './MenuContext';
19import { BroadcastConstants } from '../constants/BroadcastConstants';
20import { ProcessMenuOperation } from './ProcessMenuOperation';
21import { MediaDataItem } from '../data/MediaDataItem';
22import { MediaConstants } from '../constants/MediaConstants';
23
24const TAG = "BatchDeleteMenuOperation"
25
26export class BatchDeleteMenuOperation extends ProcessMenuOperation {
27    constructor(menuContext: MenuContext) {
28        super(menuContext);
29    }
30
31    doAction(): void {
32        Log.info(TAG, 'delete doAction');
33        if (this.menuContext == null) {
34            Log.warn(TAG, 'menuContext is null, return');
35            return;
36        }
37
38        let dataSource: ItemDataSource = this.menuContext.dataSource;
39        if (dataSource == null) {
40            this.count = this.menuContext.items.length;
41        } else {
42            this.count = dataSource.getSelectedCount();
43        }
44        if (this.count <= 0) {
45            Log.warn(TAG, 'count <= 0, return');
46            return;
47        }
48
49        this.confirmCallback = this.confirmCallback.bind(this);
50        this.cancelCallback = this.cancelCallback.bind(this);
51
52        let resource: Resource = this.getDeleteMessageResource(dataSource);
53        let deleteResource: Resource = this.menuContext.albumId == MediaConstants.ALBUM_ID_RECYCLE ? $r('app.string.dialog_recycle') : $r('app.string.dialog_delete');
54        this.menuContext.broadCast.emit(BroadcastConstants.SHOW_DELETE_DIALOG, [resource, deleteResource, this.confirmCallback, this.cancelCallback]);
55    }
56
57    getResourceFromBrowser(): Resource {
58        return this.menuContext.albumId == MediaConstants.ALBUM_ID_RECYCLE ? $r('app.string.recycle_single_file_tips') : $r('app.string.delete_single_file_tips')
59    }
60
61    getResourceFromGrid(dataSource: ItemDataSource): Resource {
62        if (dataSource && dataSource.isSelect()) {
63            return this.menuContext.albumId == MediaConstants.ALBUM_ID_RECYCLE ? $r('app.string.recycle_all_files_tips') : $r('app.string.delete_all_files_tips');
64        } else if (this.count == 1) {
65            return this.menuContext.albumId == MediaConstants.ALBUM_ID_RECYCLE ? $r('app.string.recycle_single_file_tips') : $r('app.string.delete_single_file_tips');
66        } else {
67            return this.menuContext.albumId == MediaConstants.ALBUM_ID_RECYCLE ? $r('app.string.recycle_files_tips', this.count) : $r('app.string.delete_files_tips', this.count);
68        }
69    }
70
71    getDeleteMessageResource(dataSource: ItemDataSource): Resource {
72        let resource: Resource;
73        if (this.menuContext.deletePageFromType == BroadcastConstants.DELETE_FROM_BROWSER) {
74            resource = this.getResourceFromBrowser();
75        } else {
76            resource = this.getResourceFromGrid(dataSource);
77        }
78        return resource;
79    }
80
81    confirmCallback(): void {
82        Log.info(TAG, 'Batch delete confirm');
83        AppStorage.SetOrCreate("isDelete", 1);
84
85        // 1. Variable initialization
86        this.onOperationEnd = this.menuContext.onOperationEnd;
87
88        // 2. onDeleteStart exit selection mode
89        let onOperationStart: Function = this.menuContext.onOperationStart;
90        onOperationStart && onOperationStart();
91
92        this.menuContext.broadCast.emit(BroadcastConstants.DELETE_PROGRESS_DIALOG,
93            [$r('app.string.action_delete'), this.count]);
94
95        // 3. selectManager gets the URI of the data and starts processing deletion in the callback
96        let dataSource: ItemDataSource = this.menuContext.dataSource;
97        if (dataSource == null) {
98            this.items = this.menuContext.items;
99        } else {
100            this.items = dataSource.getSelectedItems();
101        }
102        this.processOperation();
103    }
104
105    requestOneBatchOperation() {
106        let item = this.items[this.currentBatch] as MediaDataItem;
107        if (item) {
108            item.onDelete().then(() => {
109                this.currentBatch++;
110                this.menuContext.broadCast.emit(BroadcastConstants.UPDATE_PROGRESS, [this.getExpectProgress(), this.currentBatch]);
111                this.cyclicOperation();
112            })
113        }
114    }
115
116    cancelCallback(): void {
117        Log.info(TAG, 'Batch delete cancel');
118    }
119}