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 this.broadCast.off(null, null); 110 broadcastManager.getBroadcast().off(BroadcastConstants.ON_REMOTE_CHANGED, null); 111 mediaObserver.unregisterObserver(this.dataObserver); 112 this.dataObserver.clearSource(); 113 } 114 115 onPageShow() { 116 broadcastManager.getBroadcast().emit(BroadcastConstants.THIRD_ROUTE_PAGE, []); 117 this.onActive(); 118 } 119 120 onPageHide() { 121 this.onInActive(); 122 } 123 124 // Callback when the page is in the foreground 125 onActive() { 126 if (!this.isActive) { 127 Log.info(TAG, 'onActive'); 128 this.isActive = true; 129 } 130 this.loadItem(); 131 } 132 133 // Callback when the page is in the background 134 onInActive() { 135 if (this.isActive) { 136 Log.info(TAG, 'onInActive'); 137 this.isActive = false; 138 } 139 } 140 141 private onUpdateRemoteDevice(res, deviceId): void { 142 Log.debug(TAG, `onUpdateRemoteDevice`); 143 144 if (!this.isActive) { 145 return; 146 } 147 148 if (deviceId != this.deviceId) { 149 Log.debug(TAG, `other device`); 150 return; 151 } 152 153 if (res == Constants.DEVICE_STATE_OFFLINE) { 154 Log.debug(TAG, `device offline route to album main`); 155 router.back({ 156 uri: this.isHorizontal === true ? 'product/pad/view/index' : 'product/phone/view/index', 157 params: { 158 jumpSource: JumpSourceToMain.ALBUM, 159 } 160 }) 161 } else { 162 Log.info(TAG, `ignore typy: ${res}`); 163 return; 164 } 165 } 166 167 onMediaLibDataChange(changeType) { 168 Log.info(TAG, `onMediaLibDataChange type: ${changeType}`); 169 this.isMediaLibDataChanged = true; 170 this.loadItem(); 171 } 172 173 private loadItem() { 174 if (this.isActive && this.isMediaLibDataChanged) { 175 this.albumsDataSource.reloadAlbumItemData().then((isEmpty: boolean) => { 176 this.isEmpty = isEmpty; 177 this.albumsDataSource.notifyDataReload(); 178 }) 179 } else if (this.isActive) { 180 this.albumsDataSource.dataRemove(); 181 } 182 } 183 184 build() { 185 Flex({ 186 direction: FlexDirection.Column, 187 justifyContent: FlexAlign.Start, 188 alignItems: ItemAlign.Start 189 }) { 190 ActionBar({ 191 actionBarProp: $actionBarProp, 192 onMenuClicked: this.onMenuClicked 193 }) 194 195 Stack() { 196 if (this.isEmpty) { 197 NoPhotoIndexComponent({ index: Constants.DISTRIBUTED_ALBUM_PAGE_INDEX }) 198 } else { 199 Grid(this.scroller) { 200 LazyForEach(this.albumsDataSource, (item: LazyItem<AlbumDataItem>) => { 201 if (item && item.get()){ 202 if (item && item.index == 0) { 203 GridItem() { 204 AlbumGridItemTraditionalStyle({ 205 item: item.get(), 206 deviceName: this.title, 207 deviceId: this.deviceId, 208 isBigCard: true 209 }) 210 }.columnStart(0).columnEnd(1) 211 } else { 212 GridItem() { 213 AlbumGridItemTraditionalStyle({ 214 item: item.get(), 215 deviceName: this.title, 216 deviceId: this.deviceId, 217 isBigCard: false 218 }) 219 } 220 } 221 } 222 }, (item: LazyItem<AlbumDataItem>) => item && item.get() ? item.getHashCode() : JSON.stringify(item)) 223 } 224 .columnsTemplate('1fr '.repeat(this.gridColumnsCount)) 225 .padding({ 226 left: $r('app.float.max_padding_start'), 227 right: $r('app.float.max_padding_end'), 228 top: $r('app.float.album_set_page_padding_top') 229 }) 230 .columnsGap($r('app.float.album_set_grid_column_gap')) 231 .rowsGap($r('app.float.album_set_traditional_style_grid_row_gap')) 232 } 233 } 234 .margin({ 235 top: px2vp(this.leftBlank[1]), 236 bottom: px2vp(this.leftBlank[3]) 237 }) 238 } 239 .backgroundColor($r('app.color.default_background_color')) 240 } 241} 242