• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 router from '@ohos.router';
17import { AlbumSelectGridItemNewStyle, NewAlbumPageActionBar } from '@ohos/browser/BrowserComponents';
18import {
19  Action,
20  AlbumSetDataInfo,
21  AlbumSetDataSource,
22  BroadCast,
23  BroadCastConstants,
24  BroadCastManager,
25  Constants,
26  Log,
27  MediaDataSource,
28  ScreenManager,
29  SelectManager,
30  TraceControllerUtils,
31  UiUtil,
32  UserFileManagerAccess
33} from '@ohos/common';
34import { NoPhotoComponent } from '@ohos/common/CommonComponents';
35
36const TAG: string = 'AlbumSelect';
37
38interface Params {
39  albumName: string;
40  albumUri: string;
41  isNewAlbum: boolean;
42};
43
44@Entry
45@Component
46export struct AlbumSelect {
47  @State isEmpty: boolean = false;
48  @State gridColumnsCount: number = 0;
49  @Provide broadCast: BroadCast = new BroadCast();
50  @Provide('selectedCount') totalSelectedCount: number = 0;
51  @StorageLink('leftBlank') leftBlank: number[]
52    = [0, ScreenManager.getInstance().getStatusBarHeight(), 0, ScreenManager.getInstance().getNaviBarHeight()];
53  private isActive = false;
54  private scroller: Scroller = new Scroller();
55  private albums: AlbumSetDataSource = new AlbumSetDataSource(this.broadCast);
56  private dataSource: MediaDataSource = new MediaDataSource(Constants.DEFAULT_SLIDING_WIN_SIZE);
57  private mSelectManager = new SelectManager();
58  private currentAlbumUri: string = '';
59  private newAlbumName: string = '';
60  private onWindowSizeChangeFunc: Function = (): void => this.onWindowSizeChange();
61  private onLoadingFinshedFunc: Function = (size: number): void => this.onLoadingFinshed(size);
62
63  private onLoadingFinshed(size: number): void {
64    Log.info(TAG, `ON_LOADING_FINISHED size: ${size}`);
65    this.isEmpty = size == 0;
66  }
67
68  private onWindowSizeChange(): void {
69    // 后续phone缩略图支持横竖屏后再放开
70    if (AppStorage.get<string>('deviceType') !== Constants.DEFAULT_DEVICE_TYPE) {
71      // Waiting: 后续phone的宫格页,支持横竖屏后,再放开
72      this.gridColumnsCount = UiUtil.getAlbumGridCount(false);
73    }
74  }
75
76  aboutToAppear(): void {
77    TraceControllerUtils.startTrace('AlbumSetPageAboutToAppear');
78    this.broadCast.on(Constants.ON_LOADING_FINISHED, this.onLoadingFinshedFunc);
79    this.albums = new AlbumSetDataSource(this.broadCast);
80    this.onActive();
81    this.gridColumnsCount = UiUtil.getAlbumGridCount(false);
82    Log.info(TAG, `the grid count in a line is: ${this.gridColumnsCount}`);
83    ScreenManager.getInstance().on(ScreenManager.ON_WIN_SIZE_CHANGED, this.onWindowSizeChangeFunc);
84
85    let param: Params = router.getParams() as Params;
86    if (param) {
87      this.newAlbumName = param.albumName;
88      this.currentAlbumUri = param.albumUri;
89      if (param.isNewAlbum) {
90        AppStorage.SetOrCreate(Constants.APP_KEY_NEW_ALBUM, true);
91      } else {
92        AppStorage.SetOrCreate(Constants.APP_KEY_NEW_ALBUM, false);
93      }
94    }
95    this.albums.setBlackList([this.currentAlbumUri, UserFileManagerAccess.getInstance()
96      .getSystemAlbumUri(UserFileManagerAccess.TRASH_ALBUM_SUB_TYPE)]);
97    Log.info(TAG, `the album uri is: ${this.currentAlbumUri}`);
98    AppStorage.SetOrCreate(Constants.APP_KEY_NEW_ALBUM_TARGET, this.newAlbumName);
99    AppStorage.SetOrCreate(Constants.APP_KEY_NEW_ALBUM_TARGET_URI, this.currentAlbumUri);
100    AppStorage.SetOrCreate(Constants.APP_KEY_NEW_ALBUM_SELECTED, this.mSelectManager);
101    TraceControllerUtils.finishTrace('AlbumSetPageAboutToAppear');
102  }
103
104  aboutToDisappear(): void {
105    ScreenManager.getInstance().off(ScreenManager.ON_WIN_SIZE_CHANGED, this.onWindowSizeChangeFunc);
106    this.broadCast.off(Constants.ON_LOADING_FINISHED, this.onLoadingFinshedFunc);
107    this.albums.setBlackList([]);
108    this.dataSource.releaseBroadCast();
109  }
110
111  onPageShow() {
112    BroadCastManager.getInstance().getBroadCast().emit(BroadCastConstants.THIRD_ROUTE_PAGE, []);
113    this.onActive();
114  }
115
116  onPageHide() {
117    this.onInActive();
118  }
119
120  // Callback when the page is in the foreground
121  onActive() {
122    if (!this.isActive) {
123      Log.info(TAG, 'onActive');
124      this.isActive = true;
125      this.albums && this.albums.onActive();
126      this.dataSource && this.dataSource.onActive();
127      this.dataSource.forceUpdate();
128    }
129  }
130
131  // Callback when the page is in the background
132  onInActive() {
133    if (this.isActive) {
134      Log.info(TAG, 'onInActive');
135      this.isActive = false;
136      this.albums && this.albums.onInActive();
137    }
138  }
139
140  build() {
141    Flex({
142      direction: FlexDirection.Column,
143      justifyContent: FlexAlign.Start,
144      alignItems: ItemAlign.Start
145    }) {
146      NewAlbumPageActionBar({ onMenuClicked: (action: Action): void => this.onMenuClicked(action) })
147
148      Stack() {
149        if (this.isEmpty) {
150          NoPhotoComponent({ title: $r('app.string.title_no_albums') })
151        }
152        Grid(this.scroller) {
153          LazyForEach(this.albums, (item: AlbumSetDataInfo, index?: number) => {
154            GridItem() {
155              AlbumSelectGridItemNewStyle({
156                item: item.data,
157                mSelectManager: this.mSelectManager
158              })
159            }.key('SelectAlbum' + index)
160          }, (item: AlbumSetDataInfo) => 'uri:' + item.data.uri)
161        }
162        .edgeEffect(EdgeEffect.Spring)
163        .scrollBar(BarState.Auto)
164        .columnsTemplate('1fr '.repeat(this.gridColumnsCount))
165        .padding({
166          top: $r('app.float.album_set_page_padding_top'),
167          left: $r('sys.float.ohos_id_card_margin_start'),
168          right: $r('sys.float.ohos_id_card_margin_end'),
169          bottom: $r('sys.float.ohos_id_default_padding_bottom_fixed')
170        })
171        .columnsGap($r('sys.float.ohos_id_card_margin_middle'))
172        .rowsGap($r('sys.float.ohos_id_elements_margin_vertical_l'))
173      }
174    }
175    .backgroundColor($r('app.color.default_background_color'))
176    .padding({
177      top: this.leftBlank[1],
178      bottom: this.leftBlank[3]
179    })
180  }
181
182  private onMenuClicked(action: Action): void {
183    Log.debug(TAG, `onMenuClicked, action: ${action.actionID}`);
184    if (action.actionID === Action.CANCEL.actionID) {
185      Log.info(TAG, 'clear SelectManager data');
186      this.mSelectManager.onModeChange(false);
187      AppStorage.Delete(Constants.APP_KEY_NEW_ALBUM_SELECTED);
188      router.back();
189    }
190    return;
191  }
192}
193