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 { AlbumSimpleInfo } from '@ohos/common';
17import {
18  AlbumInfo,
19  BroadCastConstants,
20  BrowserDataFactory,
21  DateUtil,
22  Log,
23  MenuContext,
24  ProcessMenuOperation,
25  SelectManager,
26  UserFileManagerAccess
27} from '@ohos/common';
28
29const TAG: string = 'browser_AlbumSetDeleteMenuOperation';
30
31export class AlbumSetDeleteMenuOperation extends ProcessMenuOperation {
32  videoCount: number;
33  photoCount: number;
34  dialogTitle: Resource;
35  clickSetCount: number;
36
37  constructor(menuContext: MenuContext) {
38    super(menuContext);
39  }
40
41  doAction(): void {
42    if (DateUtil.isEmpty(this.menuContext) || DateUtil.isEmpty(this.menuContext.fromSelectMode)) {
43      Log.error(TAG, 'menuContext or fromSelectMode is empty, return');
44      return;
45    }
46    if (this.menuContext.fromSelectMode) {
47      let selectManager: SelectManager = this.menuContext.selectManager;
48      if (DateUtil.isEmpty(selectManager)) {
49        Log.error(TAG, 'selectManager is null, return');
50        return;
51      }
52      this.clickSetCount = selectManager.clickedSet.size;
53      if (this.clickSetCount <= 0) {
54        Log.error(TAG, 'clickSetCount <= 0, return');
55        return;
56      }
57      this.uris = Array.from(selectManager.clickedSet);
58    } else {
59      let albumInfo: AlbumInfo = this.menuContext.albumInfo;
60      if (!DateUtil) {
61        Log.error(TAG, 'albumInfo is null, return');
62        return;
63      }
64      this.uris = [albumInfo.uri];
65    }
66    this.confirmCallback = this.confirmCallback.bind(this);
67    this.cancelCallback = this.cancelCallback.bind(this);
68    this.dataCallback = this.dataCallback.bind(this);
69
70    // api9:获取相册内图片,把相册内图片删完,相册也就删除了
71    // api10:直接删除相册
72    let albumDataImpl = BrowserDataFactory.getFeature(BrowserDataFactory.TYPE_ALBUM);
73    let callback = {
74      callback: (info: AlbumSimpleInfo): void => {
75        this.dataCallback(info);
76      }
77    };
78    albumDataImpl.getData(callback, { uris: this.uris });
79  }
80
81  // Asynchronous callback for getSelection
82  callback(): void {
83    if (this.isCancelled) {
84      return;
85    }
86    if (this.uris == null) {
87      Log.error(TAG, 'Invalid callback data, uris is null!');
88      return;
89    }
90
91    this.processOperation();
92  }
93
94  // Delete a batch of data
95  requestOneBatchOperation(): void {
96    if (this.isCancelled) {
97      return;
98    }
99    this.currentBatch++;
100    let startIndex = (this.currentBatch - 1) * this.BATCH_SIZE;
101    let endIndex = this.currentBatch * this.BATCH_SIZE;
102    let batchUris: string[] = this.uris.slice(startIndex, Math.min(endIndex, this.uris.length));
103    UserFileManagerAccess.getInstance().deleteAlbumByUri(batchUris[0]).then(() => {
104      this.onCompleted()
105    }).catch((error) => {
106      Log.error(TAG, `delete error: ${error}`);
107      this.onError();
108    });
109  }
110
111  // 统计所有选中相册的uris,查询所有待删除的图片和视频个数并显示在dialog
112  private dataCallback(object: AlbumSimpleInfo): void {
113    Log.debug(TAG, `album delete callback coming`);
114    this.videoCount = object.videoCount;
115    this.photoCount = object.count - object.videoCount;
116    this.count = object.uris.length
117
118    Log.debug(TAG, `album delete uris: ${JSON.stringify(this.uris)} count:${this.count}`);
119
120    if (this.clickSetCount == 1) {
121      if (this.videoCount > 0 && this.photoCount > 0) {
122        this.dialogTitle = $r('app.string.recycle_single_album_tips', this.photoCount, this.videoCount);
123      } else if (this.videoCount > 0 && this.photoCount <= 0) {
124        this.dialogTitle = $r('app.string.recycle_single_album_with_videos_tips', this.videoCount);
125      } else if (this.videoCount <= 0 && this.photoCount > 0) {
126        this.dialogTitle = $r('app.string.recycle_single_album_with_photos_tips', this.photoCount);
127      } else if (this.videoCount <= 0 && this.photoCount <= 0) {
128        this.dialogTitle = $r('app.string.recycle_single_empty_album_tips');
129      }
130    } else {
131      if (this.videoCount > 0 && this.photoCount > 0) {
132        this.dialogTitle = $r('app.string.recycle_albums_tips', this.clickSetCount, this.photoCount, this.videoCount);
133      } else if (this.videoCount > 0 && this.photoCount <= 0) {
134        this.dialogTitle = $r('app.string.recycle_albums_with_videos_tips', this.clickSetCount, this.videoCount);
135      } else if (this.videoCount <= 0 && this.photoCount > 0) {
136        this.dialogTitle = $r('app.string.recycle_albums_with_photos_tips', this.clickSetCount, this.photoCount);
137      } else if (this.videoCount <= 0 && this.photoCount <= 0) {
138        this.dialogTitle = $r('app.string.recycle_empty_albums_tips', this.clickSetCount);
139      }
140    }
141
142    this.menuContext.broadCast.emit(BroadCastConstants.SHOW_DELETE_DIALOG,
143      [this.dialogTitle, this.confirmCallback, this.cancelCallback]);
144  }
145
146  private confirmCallback(): void {
147    Log.info(TAG, 'AlbumSet delete confirm')
148    // 1. Variable initialization
149    this.onOperationEnd = this.menuContext.onOperationEnd;
150    // 2. selectManager gets the URI of the data and starts processing deletion in the callback
151    if (this.menuContext.fromSelectMode) {
152      this.menuContext.selectManager.getSelection(this);
153    } else {
154      this.callback();
155    }
156    // 3. onDeleteStart exit selection mode
157    let onOperationStart: Function = this.menuContext.onOperationStart;
158    onOperationStart && onOperationStart();
159
160    this.menuContext.broadCast.emit(BroadCastConstants.DELETE_PROGRESS_DIALOG,
161      [$r('app.string.action_delete'), this.count]);
162  }
163
164  private cancelCallback(): void {
165    Log.info(TAG, 'AlbumSet delete cancel');
166  }
167}