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 { PhotoItem } from './PhotoItem'; 17import { BroadcastConstants } from '@ohos/base/src/main/ets/constants/BroadcastConstants'; 18import { Log } from '@ohos/base/src/main/ets/utils/Log'; 19import { Broadcast } from '@ohos/base/src/main/ets/utils/Broadcast'; 20import { AnimationConstants } from '@ohos/base/src/main/ets/constants/AnimationConstants'; 21import { GroupItemDataSource } from '@ohos/base/src/main/ets/vm/GroupItemDataSource'; 22import { MediaDataItem } from '@ohos/base/src/main/ets/data/MediaDataItem'; 23import { Constants } from '../constants/Constants'; 24import { LazyItem } from '@ohos/base/src/main/ets/vm/ItemDataSource'; 25 26const TAG = "PhotoSwiper" 27 28@Component 29export struct PhotoSwiper { 30 photoSwiperTransition: string 31 private dataSource: GroupItemDataSource; 32 @Consume currentIndex: number; 33 @Consume broadCast: Broadcast; 34 @Consume canSwipe: boolean; 35 @State duration: number = 400; 36 onPhotoChanged: Function; 37 private swiperController: SwiperController; 38 39 aboutToAppear() { 40 this.broadCast.on(BroadcastConstants.ON_DATA_RELOADED, () => { 41 Log.debug(TAG, 'animate to data reloaded start'); 42 animateTo({ 43 duration: AnimationConstants.DELETE_ANIMATE_DURATION, 44 onFinish: () => { 45 } }, () => { 46 if (this.dataSource != null && this.dataSource != undefined) { 47 this.dataSource.dataRemove(); 48 this.broadCast.emit(Constants.DATA_SIZE_CHANGED,[this.dataSource.totalCount()]); 49 } 50 }) 51 }); 52 53 this.broadCast.on(BroadcastConstants.CHANGE_SWIPER_DURATION, (value) => { 54 Log.debug(TAG, `change duration start ${value}`); 55 this.duration = value; 56 }); 57 } 58 59 build() { 60 Swiper(this.swiperController) { 61 LazyForEach(this.dataSource, (item: LazyItem<MediaDataItem>) => { 62 if (item && item.get()) { 63 PhotoItem({ 64 item: item.get(), 65 pageName: this.photoSwiperTransition 66 }) 67 .zIndex(item && item.index == this.currentIndex ? 2 : 1) 68 } 69 }, (item: LazyItem<MediaDataItem>) => item == null && item.get() ? JSON.stringify(item) : item.getHashCode()) 70 } 71 .itemSpace('8vp') 72 .gesture(PanGesture({ 73 direction: PanDirection.Horizontal 74 })) 75 .index(this.currentIndex) 76 .indicator(false) 77 .loop(false) 78 .onChange((index: number) => { 79 if (AppStorage.Get("isDelete") == 0) { 80 if (this.currentIndex - index == 1) { 81 AppStorage.SetOrCreate("isLeftSwiper", 1); 82 } else if (this.currentIndex - index == -1) { 83 AppStorage.SetOrCreate("isLeftSwiper", 0); 84 } 85 } 86 if (this.duration != 0) { 87 this.onPhotoChanged(index); 88 } 89 }) 90 .disableSwipe(this.canSwipe) 91 } 92}