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 72 updateActionBar(): void { 73 this.actionBarProp = this.barModel.createActionBar(this.leftAction, this.title, 74 this.isMultiPick, 0, this.maxSelectCount, this.isSelectPhotoGrid); 75 } 76 77 aboutToAppear(): void { 78 let param = router.getParams(); 79 if (param != null) { 80 this.bundleName = new String(param.bundleName).valueOf(); 81 this.isMultiPick = new Boolean(param.isMultiPick).valueOf(); 82 param.type && (this.type = param.type.toString()); 83 if (param.isFromFa != undefined || param.isFromFa != null) { 84 this.isFromFa = new Boolean(param.isFromFa).valueOf(); 85 } 86 if (param.isFromFaPhoto != undefined || param.isFromFaPhoto != null) { 87 this.isFromFaPhoto = new Boolean(param.isFromFaPhoto).valueOf(); 88 } 89 if (param.isFromThird != undefined || param.isFromThird != null) { 90 this.isFromThird = new Boolean(param.isFromThird).valueOf(); 91 } 92 if (param.filterMediaType != undefined || param.filterMediaType != null) { 93 this.filterMediaType = new Number(param.filterMediaType).valueOf(); 94 } 95 this.isFromWallpaper = new Boolean(param.isFromWallpaper).valueOf(); 96 if (this.isFromWallpaper) { 97 this.maxSelectCount = new Number(param.remainingOfWallpapers).valueOf() || 0 98 } else if (!!param.maxSelectCount && param.maxSelectCount > 0) { 99 let selectCount = new Number(param.maxSelectCount).valueOf(); 100 this.maxSelectCount = selectCount > Constants.LIMIT_MAX_THIRD_SELECT_COUNT 101 ? Constants.LIMIT_MAX_THIRD_SELECT_COUNT 102 : selectCount; 103 } 104 } 105 Log.info(TAG, `isFromFa: ${this.isFromFa} isFromFaPhoto ${this.isFromFaPhoto}`); 106 if (this.isFromFa) { 107 this.albumsDataSource.setSelectType(MediaConstants.SELECT_TYPE_IMAGE) 108 this.albumsDataSource.setBlackList([MediaConstants.ALBUM_ID_VIDEO, MediaConstants.ALBUM_ID_RECYCLE]); 109 } else { 110 this.albumsDataSource.setSelectType(this.filterMediaType); 111 if (this.filterMediaType == MediaConstants.SELECT_TYPE_IMAGE) { 112 this.albumsDataSource.setBlackList([MediaConstants.ALBUM_ID_VIDEO]); 113 } 114 this.albumsDataSource.setBlackList([MediaConstants.ALBUM_ID_RECYCLE]); 115 } 116 this.loadAlbums() 117 this.leftAction = this.isFromFa ? Action.BACK : Action.CANCEL 118 this.title = (this.isFromFa && !this.isFromFaPhoto) ? ActionBarProp.SINGLE_SELECT_ALBUM_TITLE : ActionBarProp.SINGLE_UNSELECT_TITLE 119 this.onMenuClicked = this.onMenuClicked.bind(this); 120 Log.info(TAG, `isMultiPick: ${this.isMultiPick}`); 121 Log.info(TAG, `type: ${this.type}`); 122 Log.info(TAG, `ThirdSelectAlbumSetPage isFromWallpaper: ${this.isFromWallpaper}`); 123 Log.info(TAG, `ThirdSelectAlbumSetPage maxSelectCount: ${this.maxSelectCount}`); 124 Log.info(TAG, `ThirdSelectAlbumSetPage filterMediaType: ${this.filterMediaType}`); 125 this.broadCast.on(Constants.ON_LOADING_FINISHED, (size: number) => { 126 Log.info(TAG, `ON_LOADING_FINISHED size: ${size}`); 127 this.isEmpty = size == 0; 128 this.isHideScrollBar = (size <= (this.gridRowCount * Constants.NUMBER_3 - Constants.NUMBER_1)); 129 }); 130 131 screenManager.on(screenManager.ON_WIN_SIZE_CHANGED, () => { 132 this.initGridRowCount(); 133 }); 134 135 this.initGridRowCount(); 136 this.updateActionBar(); 137 AppStorage.SetOrCreate(Constants.ENTRY_FROM_HAP, Constants.ENTRY_FROM_NONE); 138 } 139 140 private jumpToAllAlbumGridPage(allAlbumDataItem: AlbumDataItem) { 141 router.push({ 142 uri: 'feature/thirdSelect/view/ThirdSelectPhotoGridPage', 143 params: { 144 itemCoverUri: allAlbumDataItem.uri, 145 itemId: allAlbumDataItem.id, 146 itemCount: allAlbumDataItem.count, 147 isMultiPick: this.isMultiPick, 148 isFromWallpaper: this.isFromWallpaper, 149 maxSelectCount: this.maxSelectCount, 150 itemDisplayName: allAlbumDataItem.displayName, 151 isFromFa: this.isFromFa, 152 filterMediaType: this.filterMediaType 153 } 154 }); 155 } 156 157 private async loadAlbums() { 158 let displayName = await getResourceString($r('app.string.album_all')); 159 this.albumsDataSource.reloadAlbumItemData().then((isEmpty: boolean) => { 160 this.isEmpty = isEmpty 161 this.albumsDataSource.notifyDataReload() 162 // 从三方拉起,直接跳转到所有相册 163 if (this.isFromThird) { 164 let allAlbumDataItem: AlbumDataItem = this.albumsDataSource.getAlbumDataItemById(MediaConstants.ALBUM_ID_ALL); 165 if (allAlbumDataItem == undefined) { 166 allAlbumDataItem = new AlbumDataItem(MediaConstants.ALBUM_ID_ALL, 0, displayName, this.filterMediaType, ''); 167 } 168 this.jumpToAllAlbumGridPage(allAlbumDataItem); 169 } 170 }) 171 } 172 173 private initGridRowCount(): void { 174 let contentWidth = screenManager.getWinWidth(); 175 let maxCardWidth = Constants.ALBUM_SET_COVER_SIZE * Constants.GRID_MAX_SIZE_RATIO; 176 this.gridRowCount = Math.ceil((contentWidth - Constants.ALBUM_SET_MARGIN * 2 + Constants.ALBUM_SET_GUTTER) 177 / (maxCardWidth + Constants.ALBUM_SET_GUTTER)); 178 Log.info(TAG, `initGridRowCount gridRowCount: ${this.gridRowCount}`); 179 } 180 181 onPageShow() { 182 this.appBroadcast.emit(BroadcastConstants.THIRD_ROUTE_PAGE, []); 183 this.onActive(); 184 } 185 186 onPageHide() { 187 this.onInActive(); 188 } 189 190 private onActive() { 191 if (!this.isActive) { 192 Log.info(TAG, 'onActive'); 193 this.isActive = true; 194 } 195 } 196 197 private onInActive() { 198 if (this.isActive) { 199 Log.info(TAG, 'onInActive'); 200 this.isActive = false; 201 } 202 } 203 204 onMenuClicked(action: Action) { 205 Log.info(TAG, `onMenuClicked, action: ${action.actionID}`); 206 switch (action) { 207 case Action.CANCEL: 208 Log.info(TAG, 'click cancel'); 209 let abilityResult = { 210 'resultCode': 0, 211 'want': { 212 'parameters': { 213 'select-item-list': [] 214 } 215 } 216 }; 217 terminateSelfWithResult(abilityResult) 218 break; 219 case Action.BACK: 220 router.back(); 221 break; 222 default: 223 break; 224 } 225 } 226 227 build() { 228 Flex({ 229 direction: FlexDirection.Column, 230 justifyContent: FlexAlign.Start, 231 alignItems: ItemAlign.Start 232 }) { 233 ActionBar({ 234 actionBarProp: $actionBarProp, 235 onMenuClicked: this.onMenuClicked 236 }) 237 238 Stack() { 239 if (this.isEmpty) { 240 NoPhotoIndexComponent({ index: Constants.ALBUM_PAGE_INDEX }) 241 } 242 Grid(this.scroller) { 243 LazyForEach(this.albumsDataSource, (item: LazyItem<AlbumDataItem>) => { 244 if (item && item.get() && item.get().id != MediaConstants.ALBUM_ID_RECYCLE) { 245 if ( item.get().index == 0) { 246 GridItem() { 247 ThirdAlbumGridItem({ 248 item: item.get(), 249 isBigCard: true, 250 isFromWallpaper: this.isFromWallpaper, 251 isFromFa: this.isFromFa, 252 isFromFaPhoto: this.isFromFaPhoto, 253 filterMediaType: this.filterMediaType, 254 bundleName: this.bundleName 255 }) 256 }.columnStart(0).columnEnd(1) 257 } else { 258 GridItem() { 259 ThirdAlbumGridItem({ 260 item: item.get(), 261 isBigCard: false, 262 isFromWallpaper: this.isFromWallpaper, 263 isFromFa: this.isFromFa, 264 isFromFaPhoto: this.isFromFaPhoto, 265 filterMediaType: this.filterMediaType, 266 bundleName: this.bundleName 267 }) 268 } 269 } 270 } 271 }, (item: LazyItem<AlbumDataItem>) => item && item.get() ? item.getHashCode() : JSON.stringify(item)) 272 } 273 .columnsTemplate('1fr '.repeat(this.gridRowCount)) 274 .padding({ 275 left: $r('app.float.max_padding_start'), 276 right: $r('app.float.max_padding_end'), 277 top: $r('app.float.album_set_page_padding_top'), 278 }) 279 .columnsGap($r('app.float.album_set_grid_column_gap')) 280 .rowsGap($r('app.float.album_set_grid_row_gap')) 281 282 AlbumScrollBar({ scroller: this.scroller }) 283 } 284 } 285 .backgroundColor($r('app.color.default_background_color')) 286 .padding({ 287 top: px2vp(this.leftBlank[1]), 288 bottom: px2vp(this.leftBlank[3]) 289 }) 290 } 291}