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 aboutToDisappear(): void { 60 this.swiperController = undefined; 61 } 62 63 build() { 64 Swiper(this.swiperController) { 65 LazyForEach(this.dataSource, (item: LazyItem<MediaDataItem>) => { 66 if (item && item.get()) { 67 PhotoItem({ 68 item: item.get(), 69 pageName: this.photoSwiperTransition 70 }) 71 .zIndex(item && item.index == this.currentIndex ? 2 : 1) 72 } 73 }, (item: LazyItem<MediaDataItem>) => item == null && item.get() ? JSON.stringify(item) : item.getHashCode()) 74 } 75 .itemSpace('8vp') 76 .gesture(PanGesture({ 77 direction: PanDirection.Horizontal 78 })) 79 .index(this.currentIndex) 80 .indicator(false) 81 .loop(false) 82 .onChange((index: number) => { 83 if (AppStorage.Get("isDelete") == 0) { 84 if (this.currentIndex - index == 1) { 85 AppStorage.SetOrCreate("isLeftSwiper", 1); 86 } else if (this.currentIndex - index == -1) { 87 AppStorage.SetOrCreate("isLeftSwiper", 0); 88 } 89 } 90 if (this.duration != 0) { 91 this.onPhotoChanged(index); 92 } 93 }) 94 .disableSwipe(this.canSwipe) 95 } 96}