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