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 { 18 Action, 19 ActionBarProp, 20 AlbumDefine, 21 AlbumSetDataInfo, 22 AlbumSetDataSource, 23 BroadCast, 24 BroadCastConstants, 25 BroadCastManager, 26 CommonObserverCallback, 27 Constants, 28 Log, 29 MediaObserver, 30 NoPhotoIndexComponent, 31 ScreenManager, 32 ThirdSelectManager, 33 UiUtil, 34 UserFileManagerAccess 35} from '@ohos/common'; 36import { ThirdAlbumGridItem } from './ThirdAlbumGridItem'; 37import { ThirdSelectedPageActionBar } from './ThirdSelectedPageActionBar'; 38import { IS_SIDE_BAR, IS_SPLIT_MODE, LEFT_BLANK, SelectParams } from '../utils/ThirdSelectConstants'; 39 40const TAG: string = 'thiSel_ThirdSelectAlbumPageBase'; 41 42// Third Select AlbumSet Page 43@Component 44export struct ThirdSelectAlbumPageBase { 45 @State isEmpty: boolean = false; 46 albums: AlbumSetDataSource; 47 @State totalSelectedCount: number = 0; 48 @Provide broadCast: BroadCast = new BroadCast(); 49 @Provide isSelectedMode: boolean = true; 50 @Provide moreMenuList: Array<Action> = new Array<Action>(); 51 selectManager: ThirdSelectManager; 52 @Provide gridColumnsCount: number = 3; 53 isActive = false; 54 @StorageLink(IS_SPLIT_MODE) isSplitMode: boolean = ScreenManager.getInstance().isSplitMode(); 55 @StorageLink(LEFT_BLANK) leftBlank: [number, number, number, number] 56 = [0, ScreenManager.getInstance().getStatusBarHeight(), 0, ScreenManager.getInstance().getNaviBarHeight()]; 57 @StorageLink(IS_SIDE_BAR) isSidebar: boolean = ScreenManager.getInstance().isSidebar(); 58 dataObserver: CommonObserverCallback = new CommonObserverCallback(this); 59 @State title: string = ''; 60 isFirstEnter: boolean; 61 @Prop @Watch('onPageChanged') pageStatus: boolean; 62 @Provide selectParams: SelectParams = SelectParams.defaultParam(); 63 scroller: Scroller = new Scroller(); 64 private appBroadCast: BroadCast = BroadCastManager.getInstance().getBroadCast(); 65 66 aboutToAppear(): void { 67 this.albums = new AlbumSetDataSource(this.broadCast); 68 let param: any = router.getParams(); 69 this.initSelectParams(param); 70 if (this.selectParams.isFromFa) { 71 this.selectParams.filterMediaType = AlbumDefine.FILTER_MEDIA_TYPE_IMAGE; 72 } 73 if (this.selectParams.filterMediaType == AlbumDefine.FILTER_MEDIA_TYPE_IMAGE) { 74 let userFile: UserFileManagerAccess = UserFileManagerAccess.getInstance(); 75 this.albums.setBlackList([userFile.getSystemAlbumUri(UserFileManagerAccess.TRASH_ALBUM_SUB_TYPE), 76 userFile.getSystemAlbumUri(UserFileManagerAccess.VIDEO_ALBUM_SUB_TYPE), 77 userFile.getSystemAlbumUri(UserFileManagerAccess.FAVORITE_ALBUM_SUB_TYPE)]); 78 } 79 this.albums.setFilterMediaType(this.selectParams.filterMediaType); 80 this.selectManager = AppStorage.Get(Constants.THIRD_SELECT_MANAGER); 81 this.onMenuClicked = this.onMenuClicked.bind(this); 82 Log.debug(TAG, `select params ${JSON.stringify(this.selectParams)}`); 83 this.broadCast.on(Constants.ON_LOADING_FINISHED, (size: number) => { 84 Log.info(TAG, `ON_LOADING_FINISHED size: ${size}`); 85 if (size === 1 && this.albums.mediaSetList[0].albumName === AlbumDefine.ALBUM_ID_RECYCLE) { 86 this.isEmpty = true; 87 } else { 88 this.isEmpty = size == 0; 89 } 90 }); 91 if (this.selectParams.isMultiPick) { 92 this.selectManager.registerCallback('thirdSelectUpdateCount', (newState: number) => { 93 Log.info(TAG, `thirdSelectUpdateCount ${newState}`); 94 this.totalSelectedCount = newState; 95 }); 96 } 97 MediaObserver.getInstance().registerObserver(this.dataObserver); 98 this.initGridRowCount = this.initGridRowCount.bind(this); 99 this.initGridRowCount(); 100 // 后续phone缩略图支持横竖屏后再放开 101 if (AppStorage.Get('deviceType') as string !== Constants.DEFAULT_DEVICE_TYPE) { 102 ScreenManager.getInstance().on(ScreenManager.ON_WIN_SIZE_CHANGED, this.initGridRowCount); 103 } 104 let titleRes = ActionBarProp.SINGLE_TAB_ALBUM_TITLE; 105 UiUtil.getResourceString(titleRes).then((stringResource) => { 106 this.title = stringResource; 107 }) 108 } 109 110 onPageChanged() { 111 if (this.pageStatus) { 112 this.onPageShow(); 113 } else { 114 this.onPageHide(); 115 } 116 } 117 118 onPageShow() { 119 Log.debug(TAG, 'onPageShow'); 120 MediaObserver.getInstance().registerObserver(this.dataObserver); 121 this.appBroadCast.emit(BroadCastConstants.THIRD_ROUTE_PAGE, []); 122 this.onActive(); 123 } 124 125 aboutToDisappear() { 126 this.broadCast.off(null, null); 127 ScreenManager.getInstance().off(ScreenManager.ON_WIN_SIZE_CHANGED, this.initGridRowCount); 128 this.initGridRowCount = null; 129 MediaObserver.getInstance().unregisterObserver(this.dataObserver); 130 this.dataObserver.clearSource(); 131 } 132 133 onMediaLibDataChange(changeType) { 134 Log.info(TAG, `onMediaLibDataChange type: ${changeType}`); 135 this.albums.onChange(changeType); 136 } 137 138 onPageHide() { 139 Log.debug(TAG, 'onPageHide'); 140 this.onInActive(); 141 } 142 143 onActive() { 144 if (!this.isActive) { 145 Log.info(TAG, 'onActive'); 146 this.isActive = true; 147 this.albums && this.albums.onActive(); 148 } 149 } 150 151 onInActive() { 152 if (this.isActive) { 153 Log.info(TAG, 'onInActive'); 154 this.isActive = false; 155 this.albums && this.albums.onInActive(); 156 } 157 } 158 159 onMenuClicked(action: Action) { 160 Log.info(TAG, `onMenuClicked, action: ${action.actionID}`); 161 switch (action.actionID) { 162 case Action.BACK.actionID: 163 router.back(); 164 break; 165 default: 166 break; 167 } 168 } 169 170 @Builder buildItem(item) { 171 ThirdAlbumGridItem({ 172 item: item.data, 173 isFirstEnter: this.isFirstEnter 174 }) 175 } 176 177 build() { 178 Flex({ 179 direction: FlexDirection.Column, 180 justifyContent: FlexAlign.Start, 181 alignItems: ItemAlign.Start 182 }) { 183 ThirdSelectedPageActionBar({ 184 leftAction: Action.BACK, 185 isSelectPhotoGrid: false, 186 title: $title, 187 onMenuClicked: this.onMenuClicked, 188 totalSelectedCount: $totalSelectedCount 189 }) 190 Stack() { 191 if (this.isEmpty) { 192 NoPhotoIndexComponent({ index: Constants.ALBUM_PAGE_INDEX, hasBarSpace: false }) 193 } 194 Grid(this.scroller) { 195 LazyForEach(this.albums, (item: AlbumSetDataInfo, index?: number) => { 196 if (item.data.albumName != AlbumDefine.ALBUM_ID_RECYCLE) { 197 GridItem() { 198 this.buildItem(item) 199 } 200 } 201 }, (item: AlbumSetDataInfo, index: number) => { 202 return item.data.getHashCode() + index; 203 }) 204 } 205 .edgeEffect(EdgeEffect.Spring) 206 .scrollBar(BarState.Auto) 207 .columnsTemplate('1fr '.repeat(this.gridColumnsCount)) 208 .padding({ 209 left: $r('sys.float.ohos_id_card_margin_start'), 210 right: $r('sys.float.ohos_id_card_margin_end'), 211 top: $r('app.float.album_set_page_padding_top'), 212 bottom: $r('sys.float.ohos_id_default_padding_bottom_fixed') 213 }) 214 .columnsGap($r('sys.float.ohos_id_card_margin_middle')) 215 .rowsGap($r('sys.float.ohos_id_elements_margin_vertical_l')) 216 } 217 } 218 .backgroundColor($r('sys.color.ohos_id_color_sub_background')) 219 .padding({ 220 top: this.leftBlank[1], 221 bottom: this.leftBlank[3] 222 }) 223 } 224 225 private initSelectParams(param) { 226 if (param != null) { 227 this.selectParams.bundleName = param.bundleName; 228 this.selectParams.isMultiPick = param.isMultiPick; 229 if (param.isFromFa != undefined || param.isFromFa != null) { 230 this.selectParams.isFromFa = param.isFromFa; 231 } 232 if (param.isFromFaPhoto != undefined || param.isFromFaPhoto != null) { 233 this.selectParams.isFromFaPhoto = param.isFromFaPhoto; 234 } 235 if (param.filterMediaType != undefined || param.filterMediaType != null) { 236 this.selectParams.filterMediaType = param.filterMediaType; 237 } 238 if (param.isFirstEnter != undefined || param.isFirstEnter != null) { 239 this.isFirstEnter = param.isFirstEnter; 240 } 241 this.selectParams.isFromWallpaper = param.isFromWallpaper; 242 if (this.selectParams.isFromWallpaper) { 243 this.selectParams.maxSelectCount = param.remainingOfWallpapers; 244 } else if (!!param.maxSelectCount && param.maxSelectCount > 0) { 245 this.selectParams.maxSelectCount = param.maxSelectCount > Constants.LIMIT_MAX_THIRD_SELECT_COUNT 246 ? Constants.LIMIT_MAX_THIRD_SELECT_COUNT 247 : param.maxSelectCount; 248 } 249 Log.info(TAG, `select param ${JSON.stringify(this.selectParams)}`); 250 } 251 } 252 253 private initGridRowCount(): void { 254 this.gridColumnsCount = UiUtil.getAlbumGridCount(false); 255 Log.info(TAG, `the grid count in a line is: ${this.gridColumnsCount}`); 256 } 257}