• 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 router from '@system.router';
17import { Log } from '@ohos/base/src/main/ets/utils/Log';
18import { ThirdAlbumGridItem } from '@ohos/third/src/main/ets/components/ThirdAlbumGridItem';
19import { Action } from '../../../common/view/browserOperation/Action';
20import { ActionBar } from '../../../common/view/actionbar/ActionBar';
21import { ActionBarProp } from '../../../common/view/browserOperation/ActionBarProp';
22import { Constants } from '../../../common/model/common/Constants';
23import { Broadcast } from '@ohos/base/src/main/ets/utils/Broadcast';
24import { NoPhotoComponent } from '../../../common/view/NoPhotoComponent';
25import { NoPhotoIndexComponent } from '../../../common/view/NoPhotoIndexComponent';
26import screenManager from '@ohos/base/src/main/ets/manager/ScreenManager';
27import { BroadcastConstants } from '@ohos/base/src/main/ets/constants/BroadcastConstants';
28import broadcastManager from '@ohos/base/src/main/ets/manager/BroadcastManager';
29import { ThirdSelectBarModel } from '../model/ThirdSelectBarModel';
30import { AlbumScrollBar } from '@ohos/base/src/main/ets/components/scrollBar/AlbumScrollBar';
31import { AlbumsDataSource } from '@ohos/base/src/main/ets/vm/AlbumsDataSource';
32import { AlbumDataItem } from '@ohos/base/src/main/ets/data/AlbumDataItem';
33import { MediaConstants } from '@ohos/base/src/main/ets/constants/MediaConstants';
34import { terminateSelfWithResult } from '@ohos/base/src/main/ets/utils/AbilityUtils';
35import { LazyItem } from '@ohos/base/src/main/ets/vm/ItemDataSource';
36import { getResourceString } from '@ohos/base/src/main/ets/utils/ResourceUtils';
37
38const TAG = "ThirdSelectAlbumSetPage"
39// Third Select AlbumSet Page
40@Entry
41@Component
42struct ThirdSelectAlbumSetPage {
43    @State isEmpty: boolean = false;
44    @Provide selectedCount: number = 0;
45    @Provide broadCast: Broadcast = new Broadcast();
46    @Provide('isSelectedMode') @Watch('updateActionBar') isMultiPick: boolean = false;
47    @Provide @Watch('updateActionBar') maxSelectCount: number = Constants.DEFAULT_MAX_THIRD_SELECT_COUNT;
48    @Provide moreMenuList: Array<Action> = new Array<Action>();
49    isFromWallpaper = false; // Whether the current interface is opened from the wallpaper
50    type: string;
51    isFromFa: boolean = false;
52    @State gridRowCount: number = 3;
53    isActive = false;
54    @StorageLink('isSplitMode') isSplitMode: boolean = screenManager.isSplitMode();
55    @StorageLink('leftBlank') leftBlank: [number, number, number, number] = [0, 0, 0, 0];
56    @StorageLink('isSidebar') isSidebar: boolean = screenManager.isSidebar();
57    isFromFaPhoto: boolean = false;
58    isFromThird: boolean = false;
59    private appBroadcast: Broadcast = broadcastManager.getBroadcast();
60    private barModel: ThirdSelectBarModel = new ThirdSelectBarModel();
61    private leftAction: Action = Action.BACK;
62    private title: string | Resource = ""
63    private scroller: Scroller = new Scroller();
64    @Provide isHideScrollBar: boolean = true;
65    @Provide('tabBarShow') isTabBarShow: boolean = false;
66    private albumsDataSource: AlbumsDataSource = new AlbumsDataSource();
67    @State actionBarProp: ActionBarProp = new ActionBarProp();
68    filterMediaType: number = MediaConstants.SELECT_TYPE_ALL;
69    bundleName: string = "";
70    isSelectPhotoGrid: boolean = false;
71    onWindowSizeChangeCallBack = () => this.initGridRowCount();
72
73    updateActionBar(): void {
74        this.actionBarProp = this.barModel.createActionBar(this.leftAction, this.title,
75                                            this.isMultiPick, 0, this.maxSelectCount, this.isSelectPhotoGrid);
76    }
77
78    aboutToAppear(): void {
79        let param = router.getParams();
80        if (param != null) {
81            this.bundleName = new String(param.bundleName).valueOf();
82            this.isMultiPick = new Boolean(param.isMultiPick).valueOf();
83            param.type && (this.type = param.type.toString());
84            if (param.isFromFa != undefined || param.isFromFa != null) {
85                this.isFromFa = new Boolean(param.isFromFa).valueOf();
86            }
87            if (param.isFromFaPhoto != undefined || param.isFromFaPhoto != null) {
88                this.isFromFaPhoto = new Boolean(param.isFromFaPhoto).valueOf();
89            }
90            if (param.isFromThird != undefined || param.isFromThird != null) {
91                this.isFromThird = new Boolean(param.isFromThird).valueOf();
92            }
93            if (param.filterMediaType != undefined || param.filterMediaType != null) {
94                this.filterMediaType = new Number(param.filterMediaType).valueOf();
95            }
96            this.isFromWallpaper = new Boolean(param.isFromWallpaper).valueOf();
97            if (this.isFromWallpaper) {
98                this.maxSelectCount = new Number(param.remainingOfWallpapers).valueOf() || 0
99            } else if (!!param.maxSelectCount && param.maxSelectCount > 0) {
100                let selectCount = new Number(param.maxSelectCount).valueOf();
101                this.maxSelectCount = selectCount > Constants.LIMIT_MAX_THIRD_SELECT_COUNT
102                    ? Constants.LIMIT_MAX_THIRD_SELECT_COUNT
103                    : selectCount;
104            }
105        }
106        Log.info(TAG, `isFromFa: ${this.isFromFa} isFromFaPhoto ${this.isFromFaPhoto}`);
107
108        if (this.isFromFa || this.isFromFaPhoto) {
109            this.albumsDataSource.setSelectType(MediaConstants.SELECT_TYPE_IMAGE)
110            this.albumsDataSource.setBlackList([MediaConstants.ALBUM_ID_VIDEO, MediaConstants.ALBUM_ID_RECYCLE]);
111        } else {
112            this.albumsDataSource.setSelectType(this.filterMediaType);
113            if (this.filterMediaType == MediaConstants.SELECT_TYPE_IMAGE) {
114                this.albumsDataSource.setBlackList([MediaConstants.ALBUM_ID_VIDEO]);
115            }
116            this.albumsDataSource.setBlackList([MediaConstants.ALBUM_ID_RECYCLE]);
117        }
118        this.loadAlbums()
119        this.leftAction = this.isFromFa ? Action.BACK : Action.CANCEL
120        this.title = (this.isFromFa && !this.isFromFaPhoto) ? ActionBarProp.SINGLE_SELECT_ALBUM_TITLE : ActionBarProp.SINGLE_UNSELECT_TITLE
121        this.onMenuClicked = this.onMenuClicked.bind(this);
122        Log.info(TAG, `isMultiPick: ${this.isMultiPick}`);
123        Log.info(TAG, `type: ${this.type}`);
124        Log.info(TAG, `ThirdSelectAlbumSetPage isFromWallpaper: ${this.isFromWallpaper}`);
125        Log.info(TAG, `ThirdSelectAlbumSetPage maxSelectCount: ${this.maxSelectCount}`);
126        Log.info(TAG, `ThirdSelectAlbumSetPage filterMediaType: ${this.filterMediaType}`);
127        this.broadCast.on(Constants.ON_LOADING_FINISHED, (size: number) => {
128            Log.info(TAG, `ON_LOADING_FINISHED size: ${size}`);
129            this.isEmpty = size == 0;
130            this.isHideScrollBar = (size <= (this.gridRowCount * Constants.NUMBER_3 - Constants.NUMBER_1));
131        });
132
133        screenManager.on(screenManager.ON_WIN_SIZE_CHANGED, this.onWindowSizeChangeCallBack);
134
135        this.initGridRowCount();
136        this.updateActionBar();
137        AppStorage.SetOrCreate(Constants.ENTRY_FROM_HAP, Constants.ENTRY_FROM_NONE);
138    }
139
140    aboutToDisappear(): void {
141        this.broadCast.off(null, null);
142        screenManager.off(screenManager.ON_WIN_SIZE_CHANGED, this.onWindowSizeChangeCallBack);
143        this.onWindowSizeChangeCallBack = null;
144    }
145
146    private jumpToAllAlbumGridPage(allAlbumDataItem: AlbumDataItem) {
147        router.push({
148            uri: 'feature/thirdSelect/view/ThirdSelectPhotoGridPage',
149            params: {
150                itemCoverUri: allAlbumDataItem.uri,
151                itemId: allAlbumDataItem.id,
152                itemCount: allAlbumDataItem.count,
153                isMultiPick: this.isMultiPick,
154                isFromWallpaper: this.isFromWallpaper,
155                maxSelectCount: this.maxSelectCount,
156                itemDisplayName: allAlbumDataItem.displayName,
157                isFromFa: this.isFromFa,
158                filterMediaType: this.filterMediaType
159            }
160        });
161    }
162
163     private async loadAlbums() {
164         let displayName = await getResourceString($r('app.string.album_all'));
165         this.albumsDataSource.reloadAlbumItemData().then((isEmpty: boolean) => {
166             this.isEmpty = isEmpty
167             this.albumsDataSource.notifyDataReload()
168            // 从三方拉起,直接跳转到所有相册
169            if (this.isFromThird) {
170                let allAlbumDataItem: AlbumDataItem = this.albumsDataSource.getAlbumDataItemById(MediaConstants.ALBUM_ID_ALL);
171                if (allAlbumDataItem == undefined) {
172                    allAlbumDataItem = new AlbumDataItem(MediaConstants.ALBUM_ID_ALL, 0, displayName, this.filterMediaType, '');
173                }
174                this.jumpToAllAlbumGridPage(allAlbumDataItem);
175            }
176        })
177    }
178
179    private initGridRowCount(): void {
180        let contentWidth = screenManager.getWinWidth();
181        let maxCardWidth = Constants.ALBUM_SET_COVER_SIZE * Constants.GRID_MAX_SIZE_RATIO;
182        this.gridRowCount = Math.ceil((contentWidth - Constants.ALBUM_SET_MARGIN * 2 + Constants.ALBUM_SET_GUTTER)
183        / (maxCardWidth + Constants.ALBUM_SET_GUTTER));
184        Log.info(TAG, `initGridRowCount gridRowCount: ${this.gridRowCount}`);
185    }
186
187    onPageShow() {
188        this.appBroadcast.emit(BroadcastConstants.THIRD_ROUTE_PAGE, []);
189        this.onActive();
190    }
191
192    onPageHide() {
193        this.onInActive();
194    }
195
196    private onActive() {
197        if (!this.isActive) {
198            Log.info(TAG, 'onActive');
199            this.isActive = true;
200        }
201    }
202
203    private onInActive() {
204        if (this.isActive) {
205            Log.info(TAG, 'onInActive');
206            this.isActive = false;
207        }
208    }
209
210    onMenuClicked(action: Action) {
211        Log.info(TAG, `onMenuClicked, action: ${action.actionID}`);
212        switch (action) {
213            case Action.CANCEL:
214                Log.info(TAG, 'click cancel');
215                let abilityResult = {
216                    'resultCode': 0,
217                    'want': {
218                        'parameters': {
219                            'select-item-list': []
220                        }
221                    }
222                };
223                terminateSelfWithResult(abilityResult)
224                break;
225            case Action.BACK:
226                router.back();
227                break;
228            default:
229                break;
230        }
231    }
232
233    build() {
234        Flex({
235            direction: FlexDirection.Column,
236            justifyContent: FlexAlign.Start,
237            alignItems: ItemAlign.Start
238        }) {
239            ActionBar({
240                actionBarProp: $actionBarProp,
241                onMenuClicked: this.onMenuClicked
242            })
243
244            Stack() {
245                if (this.isEmpty) {
246                    NoPhotoIndexComponent({ index: Constants.ALBUM_PAGE_INDEX })
247                }
248                Grid(this.scroller) {
249                    LazyForEach(this.albumsDataSource, (item: LazyItem<AlbumDataItem>) => {
250                        if (item && item.get() && item.get().id != MediaConstants.ALBUM_ID_RECYCLE) {
251                            if ( item.get().index == 0) {
252                                GridItem() {
253                                    ThirdAlbumGridItem({
254                                        item: item.get(),
255                                        isBigCard: true,
256                                        isFromWallpaper: this.isFromWallpaper,
257                                        isFromFa: this.isFromFa,
258                                        isFromFaPhoto: this.isFromFaPhoto,
259                                        filterMediaType: this.filterMediaType,
260                                        bundleName: this.bundleName
261                                    })
262                                }.columnStart(0).columnEnd(1)
263                            } else {
264                                GridItem() {
265                                    ThirdAlbumGridItem({
266                                        item: item.get(),
267                                        isBigCard: false,
268                                        isFromWallpaper: this.isFromWallpaper,
269                                        isFromFa: this.isFromFa,
270                                        isFromFaPhoto: this.isFromFaPhoto,
271                                        filterMediaType: this.filterMediaType,
272                                        bundleName: this.bundleName
273                                    })
274                                }
275                            }
276                        }
277                    }, (item: LazyItem<AlbumDataItem>) => item && item.get() ? item.getHashCode() : JSON.stringify(item))
278                }
279                .columnsTemplate('1fr '.repeat(this.gridRowCount))
280                .padding({
281                    left: $r('app.float.max_padding_start'),
282                    right: $r('app.float.max_padding_end'),
283                    top: $r('app.float.album_set_page_padding_top'),
284                })
285                .columnsGap($r('app.float.album_set_grid_column_gap'))
286                .rowsGap($r('app.float.album_set_grid_row_gap'))
287
288                AlbumScrollBar({ scroller: this.scroller })
289            }
290        }
291        .backgroundColor($r('app.color.default_background_color'))
292        .padding({
293            top: px2vp(this.leftBlank[1]),
294            bottom: px2vp(this.leftBlank[3])
295        })
296    }
297}