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 {
17  Action,
18  ActionBarMode,
19  ActionBarProp,
20  ActionBarSelectionMode,
21  AlbumInfo,
22  Constants,
23  Log,
24  ScreenManager,
25  UiUtil
26} from '@ohos/common';
27import { ActionBar } from '@ohos/common/CommonComponents';
28
29const TAG: string = 'browser_PhotoGridPageActionBar';
30
31@Component
32export struct PhotoGridPageActionBar {
33  @Consume @Watch('updateActionBarProp') isSelectedMode: boolean;
34  @Consume @Watch('updateActionBarProp') isAllSelected: boolean;
35  @StorageLink('isHorizontal') @Watch('updateActionBarProp') isHorizontal: boolean =
36    ScreenManager.getInstance().isHorizontal();
37  @Provide selectedCount: number = 0;
38  @Link @Watch('updateActionBarProp') totalSelectedCount: number;
39  @Consume isEmpty: boolean;
40  title: string = '';
41  albumInfo: AlbumInfo | null = null;
42  isSystemAlbum: boolean = true;
43  isRecycle: boolean = false;
44  onMenuClicked: Function = (): void => {};
45  isDistributedAlbum: boolean = false;
46  @State actionBarProp: ActionBarProp = new ActionBarProp();
47  @StorageLink('statusBarHeight') statusBarHeight: number = 0;
48
49  aboutToAppear(): void {
50    this.updateActionBarProp();
51  }
52
53  updateActionBarProp(): void {
54    this.selectedCount = this.totalSelectedCount;
55    if (this.isHorizontal) {
56      this.actionBarProp = this.createHorizontalActionBar();
57    } else {
58      this.actionBarProp = this.createActionBar();
59    }
60  }
61
62  build() {
63    Column() {
64      ActionBar({
65        actionBarProp: $actionBarProp,
66        onMenuClicked: this.onMenuClicked,
67        isNeedPlaceholder: false
68      })
69    }
70    .padding({
71      top: this.isHorizontal ? Constants.NUMBER_0 : px2vp(this.statusBarHeight)
72    })
73  }
74
75  private createHorizontalActionBar(): ActionBarProp {
76    let menuList: Action[] = [];
77    let actionBarProp: ActionBarProp = new ActionBarProp();
78
79    actionBarProp
80      .setLeftAction(Action.BACK)
81      .setTitle(this.getTitle(this.albumInfo))
82      .setMode(ActionBarMode.STANDARD_MODE);
83    if (this.isSelectedMode) {
84      if (this.isRecycle) {
85        menuList.push(
86          Boolean(this.selectedCount) ? Action.RECOVER : Action.RECOVER_INVALID,
87          Boolean(this.selectedCount) ? Action.DELETE : Action.DELETE_INVALID,
88          this.isAllSelected ? Action.DESELECT_ALL : Action.SELECT_ALL);
89      } else if (this.isDistributedAlbum) {
90        menuList.push(
91          (this.isAllSelected ? Action.DESELECT_ALL : Action.SELECT_ALL),
92          (Boolean(this.selectedCount) ? Action.DOWNLOAD : Action.DOWNLOAD_INVALID));
93      } else {
94        menuList.push(
95          (this.isAllSelected ? Action.DESELECT_ALL : Action.SELECT_ALL),
96          Boolean(this.selectedCount) ? Action.DELETE : Action.DELETE_INVALID, Action.MORE);
97      }
98      actionBarProp
99        .setLeftAction(Action.CANCEL)
100        .setMode(ActionBarMode.SELECTION_MODE)
101        .setSelectionMode(ActionBarSelectionMode.MULTI);
102    } else {
103      if (!this.isSystemAlbum && this.isDistributedAlbum == false) {
104        menuList.push(Action.NEW);
105      }
106      if (this.isRecycle && !this.isEmpty) {
107        menuList.push(Action.CLEAR_RECYCLE);
108        menuList.push(Action.MULTISELECT);
109      }
110      if (!this.isRecycle) {
111        menuList.push(this.isEmpty ? Action.MULTISELECT_INVALID : Action.MULTISELECT);
112      }
113    }
114    actionBarProp.setMenuList(menuList);
115    return actionBarProp;
116  }
117
118  private createActionBar(): ActionBarProp {
119    let menuList: Action[] = [];
120    let actionBarProp: ActionBarProp = new ActionBarProp();
121
122    if (!this.isSystemAlbum && this.isDistributedAlbum == false) {
123      menuList.push(Action.NEW);
124    }
125
126    actionBarProp
127      .setLeftAction(Action.BACK)
128      .setTitle(this.getTitle(this.albumInfo))
129      .setMenuList(menuList)
130      .setMode(ActionBarMode.STANDARD_MODE);
131    Log.info(TAG, `createActionBar, isSelectedMode: ${this.isSelectedMode}`);
132    if (this.isSelectedMode) {
133      menuList = [];
134      actionBarProp
135        .setLeftAction(Action.CANCEL)
136        .setMenuList(menuList)
137        .setMode(ActionBarMode.SELECTION_MODE)
138        .setSelectionMode(ActionBarSelectionMode.MULTI);
139    }
140
141    return actionBarProp;
142  }
143
144  private getTitle(albumInfo: AlbumInfo | null): Resource | string {
145    if (albumInfo) {
146      let res = UiUtil.getDisplayNameResourceByAlbumInfo(albumInfo);
147      if (res != null) {
148        return res;
149      }
150    }
151    return this.title;
152  }
153}