• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { Constants } from '../../../common/model/common/Constants';
20import { ThirdSelectPhotoBrowserActionBar } from '../../../common/view/ThirdSelectPhotoBrowserActionBar';
21import { PhotoBrowserBg } from '@ohos/browser/src/main/ets/components/PhotoBrowserBg';
22import { Constants as PhotoConstants } from '@ohos/browser/src/main/ets/constants/Constants';
23import { Broadcast } from '@ohos/base/src/main/ets/utils/Broadcast';
24import broadcastManager from '@ohos/base/src/main/ets/manager/BroadcastManager';
25import { BroadcastConstants } from '@ohos/base/src/main/ets/constants/BroadcastConstants';
26import { PhotoSwiper } from '@ohos/browser/src/main/ets/components/PhotoSwiper';
27import screenManager from '@ohos/base/src/main/ets/manager/ScreenManager';
28import mMultimodalInputManager from '@ohos/base/src/main/ets/manager/MultimodalInputManager';
29import { GroupItemDataSource } from '@ohos/base/src/main/ets/vm/GroupItemDataSource';
30import { MediaDataItem } from '@ohos/base/src/main/ets/data/MediaDataItem';
31import { MediaConstants } from '@ohos/base/src/main/ets/constants/MediaConstants';
32import { terminateSelfWithResult } from '@ohos/base/src/main/ets/utils/AbilityUtils';
33import { SelectUtil } from '@ohos/base/src/main/ets/utils/SelectUtil';
34import { getResourceString } from '@ohos/base/src/main/ets/utils/ResourceUtils';
35import { showToast } from '@ohos/base/src/main/ets/utils/UiUtil';
36// third selection photoBrowser
37
38const TAG = "ThirdSelectPhotoBrowser"
39
40@Entry
41@Component
42struct ThirdSelectPhotoBrowser {
43    @Provide browserBackgroundColor: Resource = $r('app.color.default_background_color');
44    @Provide('selectedCount') totalSelectedCount: number = 0;
45    @Provide broadCast: Broadcast = new Broadcast();
46    @Provide isSelected: boolean = false;
47    @Provide isShowBar: boolean = true;
48    @Provide isPullingDown: boolean = false;
49    @Provide moreMenuList: Array<Action> = new Array<Action>();
50    @Provide pageFrom: number = Constants.ENTRY_FROM.NORMAL;
51    @Provide canSwipe: boolean = true;
52    isMultiPick = true;
53    thirdSelectTransition: string;
54    controller: SwiperController = new SwiperController();
55    @Provide('transitionIndex') currentIndex: number = 0;
56    isFromFa: boolean = false;
57    bundleName: string = "";
58    isSelectUpperLimited: boolean = false;
59
60    // position
61    thirdSelectPosition: number;
62    private appBroadcast: Broadcast = broadcastManager.getBroadcast();
63    private browserDataSource: GroupItemDataSource = new GroupItemDataSource();
64
65    aboutToAppear(): void {
66        Log.info(TAG, 'photoBrowser aboutToAppear');
67        screenManager.setNavigationBarColor('#FFF1F3F5', '#FF000000');
68        this.browserBackgroundColor = $r('app.color.black');
69        mMultimodalInputManager.registerListener((control: number) => {
70            Log.info(TAG, `key control : ${control} index ${this.currentIndex}`);
71            if (control == 0) {
72                if (this.currentIndex > 0) {
73                    this.onPhotoChanged(this.currentIndex - 1);
74                }
75            } else if (control == 1) {
76                if (this.currentIndex < this.browserDataSource.totalCount() - 1) {
77                    this.onPhotoChanged(this.currentIndex + 1);
78                }
79            } else {
80                this.onBackPress();
81            }
82        });
83        let params = router.getParams();
84        if (params) {
85            this.onPhotoChanged(parseInt(params.position.toString()));
86            this.thirdSelectTransition = params.transition.toString();
87            this.isFromFa = params.isFromFa && true;
88            this.browserDataSource.setSelectType(params.isFromFa ? MediaConstants.SELECT_TYPE_IMAGE : MediaConstants.SELECT_TYPE_ALL);
89            this.browserDataSource.setAlbumId(params.albumId ? params.albumId.toString() : "");
90            this.isMultiPick = params.isMultiPick as boolean;
91            this.bundleName = params.bundleName.toString();
92            this.isSelectUpperLimited = params.isSelectUpperLimited  as boolean;
93        }
94        this.browserDataSource.groupDataItem = AppStorage.Get(Constants.APP_KEY_PHOTO_BROWSER);
95        if (this.isMultiPick == true) {
96            this.totalSelectedCount = this.browserDataSource.getSelectedCount();
97        }
98
99        this.onMenuClicked = this.onMenuClicked.bind(this);
100
101        this.broadCast.on(PhotoConstants.TOGGLE_BAR, () => {
102            this.onToggleBars();
103        });
104        this.broadCast.on(PhotoConstants.PULL_DOWN_END, () => {
105            this.onBackPress();
106        });
107
108        this.broadCast.on(PhotoConstants.DATA_SIZE_CHANGED, (size: number) => {
109            this.onDataSizeChanged(size);
110        });
111        this.broadCast.on(PhotoConstants.DATA_CONTENT_CHANGED, () => {
112            this.onPhotoChanged(this.currentIndex);
113        });
114        this.broadCast.on(PhotoConstants.SET_DISABLE_SWIPE, (value: boolean) => {
115            Log.info(TAG, `set swiper swipe ${value}`);
116            this.canSwipe = value;
117        });
118    }
119
120    aboutToDisappear(): void {
121        screenManager.setNavigationBarColor('#00FFFFFF', '#FF000000');
122        this.broadCast.release();
123        mMultimodalInputManager.unregisterListener();
124    }
125
126    onToggleBars() {
127    }
128
129    onDataSizeChanged(size: number): void {
130        Log.info(TAG, `onDataSizeChanged, size is ${size}`);
131        if (size == 0) {
132            this.onBackPress();
133        }
134    }
135
136    onPhotoChanged(index: number): void {
137        this.currentIndex = index;
138        let currentPhoto = this.getCurrentPhoto();
139        if (currentPhoto == undefined) {
140            Log.warn(TAG, 'onPhotoChanged, item is undefined');
141        } else {
142            this.isSelected = currentPhoto.isSelect;
143            Log.info(TAG, `onPhotoChanged, index: ${index}, currentPhoto: ${currentPhoto.uri}`);
144        }
145    }
146
147    async selectStateChange() {
148        Log.info(TAG, 'change selected.');
149        let currentPhoto = this.getCurrentPhoto();
150        if (currentPhoto == undefined) {
151            return;
152        }
153        if (!this.isSelectUpperLimited || currentPhoto.isSelect) {
154            currentPhoto.setSelect(!currentPhoto.isSelect);
155            this.isSelected = currentPhoto.isSelect;
156            this.totalSelectedCount = this.browserDataSource.getSelectedCount();
157        } else {
158            let toastMsg = await getResourceString($r('app.string.up_to_limit_tips'));
159            showToast(toastMsg);
160        }
161        this.broadCast.emit(BroadcastConstants.SELECT, [this.currentIndex]);
162        Log.info(TAG, `totalSelectedCount: ${this.totalSelectedCount} after state change`);
163    }
164
165    onPageShow() {
166        this.appBroadcast.emit(BroadcastConstants.THIRD_ROUTE_PAGE, []);
167    }
168
169    onPageHide() {
170    }
171
172    onMenuClicked(action: Action) {
173        Log.info(TAG, `onMenuClicked, action: ${action.actionID}`);
174        switch (action) {
175            case Action.BACK:
176                this.onBackPress();
177                return;
178            case Action.MATERIAL_SELECT:
179                Log.info(TAG, 'click UN_SELECTED');
180                this.selectStateChange();
181                return;
182            case Action.SELECTED:
183                Log.info(TAG, 'click SELECTED');
184                this.selectStateChange();
185                return;
186            case Action.OK:
187                Log.info(TAG, 'click OK');
188                this.setPickResult();
189                break;
190            default:
191                break;
192        }
193    }
194
195    getCurrentPhoto(): MediaDataItem {
196        return this.browserDataSource.getDataByIndex(this.currentIndex);
197    }
198
199    onBackPress() {
200        router.back({ params: { index: this.currentIndex } });
201        return true;
202    }
203
204    build() {
205        Stack({ alignContent: Alignment.TopStart }) {
206            PhotoBrowserBg()
207            PhotoSwiper({
208                dataSource: this.browserDataSource,
209                photoSwiperTransition: this.thirdSelectTransition,
210                onPhotoChanged: this.onPhotoChanged.bind(this),
211                swiperController: this.controller
212            })
213            ThirdSelectPhotoBrowserActionBar({
214                isMultiPick: this.isMultiPick,
215                onMenuClicked: this.onMenuClicked
216            }).sharedTransition("thirdSelectPhotoBrowserActionBar", {
217                type: SharedTransitionEffectType.Static,
218                duration: Constants.SHARE_TRANSITION_DURATION,
219                zIndex: 2,
220            })
221        }
222    }
223
224    private setPickResult() {
225        Log.debug(TAG, `setPickResult ${this.isFromFa}`);
226        if (this.isFromFa) {
227            this.appBroadcast.emit(BroadcastConstants.SAVE_FORM_EDITOR_DATA,
228                [AppStorage.Get(Constants.FORM_ITEM_NAME), AppStorage.Get(Constants.FORM_ITEM_ALBUM_ID),
229                AppStorage.Get(Constants.FORM_ITEM_DISPLAY_NAME), this.currentIndex, false]);
230            return;
231        }
232        let uriArray = new Array<string>();
233        if (this.isMultiPick) {
234            uriArray = this.browserDataSource.getSelectedUris();
235            Log.info(TAG, `uri size: ${uriArray}`);
236        } else {
237            let currentPhoto = this.getCurrentPhoto();
238            if (currentPhoto == undefined) {
239                return;
240            }
241            uriArray = [currentPhoto.uri];
242        }
243
244        let abilityResult = {
245            'resultCode': 0,
246            'want': {
247                'parameters': {
248                    'select-item-list': uriArray,
249                }
250            }
251        };
252        Log.info(TAG, `terminateSelfWithResult result: ${JSON.stringify(abilityResult)}`);
253        SelectUtil.grantPermissionForUris(uriArray, this.bundleName);
254        try {
255            Log.info(TAG, `grant permission success.`);
256            globalThis.appContext.terminateSelfWithResult(abilityResult).then((result) => {
257                Log.info(TAG, `terminateSelf result: ${result}`);
258            });
259        } catch (err) {
260            Log.error(TAG, `grant permission error: ${JSON.stringify(err)}`);
261        };
262    }
263
264    pageTransition() {
265        PageTransitionEnter({ type: RouteType.None, duration: PhotoConstants.PAGE_SHOW_ANIMATION_DURATION })
266            .opacity(0)
267        PageTransitionExit({ duration: PhotoConstants.PAGE_SHOW_ANIMATION_DURATION })
268            .opacity(0)
269    }
270}
271