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 { Action } from '../../../common/view/browserOperation/Action'; 19import { ActionBar } from '../../../common/view/actionbar/ActionBar'; 20import { ActionBarProp } from '../../../common/view/browserOperation/ActionBarProp'; 21import { 22 AlbumGridItemTraditionalStyle 23} from '@ohos/distributed/src/main/ets/components/AlbumGridItemTraditionalStyle'; 24import { Constants } from '../../../common/model/common/Constants'; 25import screenManager from '@ohos/base/src/main/ets/manager/ScreenManager'; 26import { Broadcast } from '@ohos/base/src/main/ets/utils/Broadcast'; 27import { NoPhotoIndexComponent } from '../../../common/view/NoPhotoIndexComponent'; 28import { JumpSourceToMain } from '@ohos/base/src/main/ets/data/JumpSourceToMain'; 29import broadcastManager from '@ohos/base/src/main/ets/manager/BroadcastManager'; 30import { BroadcastConstants } from '@ohos/base/src/main/ets/constants/BroadcastConstants'; 31import { CommonObserverCallback } from '@ohos/base/src/main/ets/observer/CommonObserverCallback'; 32import mediaObserver from '@ohos/base/src/main/ets/observer/MediaObserver'; 33import { DistributedAlbumBarModel } from '../model/DistributedAlbumBarModel'; 34import { AlbumsDataSource } from '@ohos/base/src/main/ets/vm/AlbumsDataSource'; 35import { AlbumDataItem } from '@ohos/base/src/main/ets/data/AlbumDataItem'; 36import { PeerDataItem } from '@ohos/base/src/main/ets/data/PeerDataItem'; 37import { LazyItem } from '@ohos/base/src/main/ets/vm/ItemDataSource'; 38 39const TAG = "DistributedAlbumSetPage" 40 41@Entry 42@Component 43struct DistributedAlbumSetPage { 44 @StorageLink('leftBlank') leftBlank: [number, number, number, number] = [0, 0, 0, 0]; 45 @State isEmpty: boolean = false; 46 @State gridColumnsCount: number = 0; 47 @Provide broadCast: Broadcast = new Broadcast(); 48 @Provide isAlbumSetSelectedMode: boolean = false; 49 @StorageLink('isHorizontal') isHorizontal: boolean = screenManager.isHorizontal(); 50 @Provide moreMenuList: Action[] = []; 51 title: any; 52 isActive: boolean = false; // Whether the page is in the foreground 53 scroller: Scroller = new Scroller(); 54 private dataObserver: CommonObserverCallback = new CommonObserverCallback(this); 55 private deviceId: string = ''; 56 private barModel: DistributedAlbumBarModel = new DistributedAlbumBarModel(); 57 private albumsDataSource: AlbumsDataSource = new AlbumsDataSource(); 58 private isMediaLibDataChanged: boolean = true; 59 @State actionBarProp: ActionBarProp = new ActionBarProp(); 60 61 updateActionBar(): void { 62 this.actionBarProp = this.barModel.createActionBar(this.title); 63 } 64 65 onMenuClicked(action: Action, arg: any[]) { 66 Log.info(TAG, `onMenuClicked, action: ${action.actionID}`); 67 switch (action) { 68 case Action.BACK: 69 router.back(); 70 break; 71 default: 72 break; 73 } 74 } 75 76 aboutToAppear(): void { 77 let param = router.getParams(); 78 if (param.item) { 79 Log.debug(TAG, `After router.getParams, param is: ${JSON.stringify(param)}`); 80 let item = JSON.parse(param.item.toString()) as PeerDataItem; 81 this.title = item.deviceName; 82 this.deviceId = item.networkId; 83 this.albumsDataSource.setDeviceId(item.networkId); 84 } else { 85 Log.warn(TAG, `param is error`); 86 } 87 88 this.onMenuClicked = this.onMenuClicked.bind(this); 89 this.broadCast.on(Constants.ON_LOADING_FINISHED, (size: number) => { 90 Log.info(TAG, `ON_LOADING_FINISHED size: ${size}`); 91 this.isEmpty = size == 0; 92 }); 93 94 broadcastManager.getBroadcast().on(BroadcastConstants.ON_REMOTE_CHANGED, 95 this.onUpdateRemoteDevice.bind(this)); 96 97 mediaObserver.registerObserver(this.dataObserver); 98 99 let contentWidth = screenManager.getWinWidth(); 100 let maxCardWidth = Constants.ALBUM_SET_COVER_SIZE * Constants.GRID_MAX_SIZE_RATIO; 101 this.gridColumnsCount = Math.ceil((contentWidth - Constants.ALBUM_SET_MARGIN * 2 + Constants.ALBUM_SET_GUTTER) 102 / (maxCardWidth + Constants.ALBUM_SET_GUTTER)); 103 Log.info(TAG, `the grid count in a line is : ${this.gridColumnsCount}`); 104 105 this.updateActionBar(); 106 } 107 108 aboutToDisappear(): void { 109 broadcastManager.getBroadcast().off(BroadcastConstants.ON_REMOTE_CHANGED, 110 this.onUpdateRemoteDevice.bind(this)); 111 mediaObserver.unregisterObserver(this.dataObserver); 112 } 113 114 onPageShow() { 115 broadcastManager.getBroadcast().emit(BroadcastConstants.THIRD_ROUTE_PAGE, []); 116 this.onActive(); 117 } 118 119 onPageHide() { 120 this.onInActive(); 121 } 122 123 // Callback when the page is in the foreground 124 onActive() { 125 if (!this.isActive) { 126 Log.info(TAG, 'onActive'); 127 this.isActive = true; 128 } 129 this.loadItem(); 130 } 131 132 // Callback when the page is in the background 133 onInActive() { 134 if (this.isActive) { 135 Log.info(TAG, 'onInActive'); 136 this.isActive = false; 137 } 138 } 139 140 private onUpdateRemoteDevice(res, deviceId): void { 141 Log.debug(TAG, `onUpdateRemoteDevice`); 142 143 if (!this.isActive) { 144 return; 145 } 146 147 if (deviceId != this.deviceId) { 148 Log.debug(TAG, `other device`); 149 return; 150 } 151 152 if (res == Constants.DEVICE_STATE_OFFLINE) { 153 Log.debug(TAG, `device offline route to album main`); 154 router.back({ 155 uri: this.isHorizontal === true ? 'product/pad/view/index' : 'product/phone/view/index', 156 params: { 157 jumpSource: JumpSourceToMain.ALBUM, 158 } 159 }) 160 } else { 161 Log.info(TAG, `ignore typy: ${res}`); 162 return; 163 } 164 } 165 166 onMediaLibDataChange(changeType) { 167 Log.info(TAG, `onMediaLibDataChange type: ${changeType}`); 168 this.isMediaLibDataChanged = true; 169 this.loadItem(); 170 } 171 172 private loadItem() { 173 if (this.isActive && this.isMediaLibDataChanged) { 174 this.albumsDataSource.reloadAlbumItemData().then((isEmpty: boolean) => { 175 this.isEmpty = isEmpty; 176 this.albumsDataSource.notifyDataReload(); 177 }) 178 } else if (this.isActive) { 179 this.albumsDataSource.dataRemove(); 180 } 181 } 182 183 build() { 184 Flex({ 185 direction: FlexDirection.Column, 186 justifyContent: FlexAlign.Start, 187 alignItems: ItemAlign.Start 188 }) { 189 ActionBar({ 190 actionBarProp: $actionBarProp, 191 onMenuClicked: this.onMenuClicked 192 }) 193 194 Stack() { 195 if (this.isEmpty) { 196 NoPhotoIndexComponent({ index: Constants.DISTRIBUTED_ALBUM_PAGE_INDEX }) 197 } else { 198 Grid(this.scroller) { 199 LazyForEach(this.albumsDataSource, (item: LazyItem<AlbumDataItem>) => { 200 if (item && item.get()){ 201 if (item && item.index == 0) { 202 GridItem() { 203 AlbumGridItemTraditionalStyle({ 204 item: item.get(), 205 deviceName: this.title, 206 deviceId: this.deviceId, 207 isBigCard: true 208 }) 209 }.columnStart(0).columnEnd(1) 210 } else { 211 GridItem() { 212 AlbumGridItemTraditionalStyle({ 213 item: item.get(), 214 deviceName: this.title, 215 deviceId: this.deviceId, 216 isBigCard: false 217 }) 218 } 219 } 220 } 221 }, (item: LazyItem<AlbumDataItem>) => item && item.get() ? item.getHashCode() : JSON.stringify(item)) 222 } 223 .columnsTemplate('1fr '.repeat(this.gridColumnsCount)) 224 .padding({ 225 left: $r('app.float.max_padding_start'), 226 right: $r('app.float.max_padding_end'), 227 top: $r('app.float.album_set_page_padding_top') 228 }) 229 .columnsGap($r('app.float.album_set_grid_column_gap')) 230 .rowsGap($r('app.float.album_set_traditional_style_grid_row_gap')) 231 } 232 } 233 .margin({ 234 top: px2vp(this.leftBlank[1]), 235 bottom: px2vp(this.leftBlank[3]) 236 }) 237 } 238 .backgroundColor($r('app.color.default_background_color')) 239 } 240} 241