• 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 */
15import router from '@system.router';
16import { Log } from '@ohos/base/src/main/ets/utils/Log';
17import { MediaOperationType } from '@ohos/base/src/main/ets/data/MediaOperationType';
18import { MediaOperationActionBar } from './MediaOperationActionBar';
19import { Broadcast } from '@ohos/base/src/main/ets/utils/Broadcast';
20import { AlbumSetNewMenuOperation } from '@ohos/base/src/main/ets/operation/AlbumSetNewMenuOperation';
21import { BroadcastConstants } from '@ohos/base/src/main/ets/constants/BroadcastConstants';
22import { AlbumListCard } from '@ohos/mediaOperation/src/main/ets/components/AlbumListCard';
23import { MenuContext } from '@ohos/base/src/main/ets/operation/MenuContext';
24import { Action } from './Action';
25import { JumpSourceToMain } from '@ohos/base/src/main/ets/data/JumpSourceToMain';
26import { CustomDialogView } from '../dialog/CustomDialogView';
27import screenManager, { ColumnSize } from '@ohos/base/src/main/ets/manager/ScreenManager';
28import broadcastManager from '@ohos/base/src/main/ets/manager/BroadcastManager';
29import { AlbumsDataSource } from '@ohos/base/src/main/ets/vm/AlbumsDataSource'
30import { AlbumDataItem } from '@ohos/base/src/main/ets/data/AlbumDataItem';
31import { MediaConstants } from '@ohos/base/src/main/ets/constants/MediaConstants';
32import { SimpleAlbumDataItem } from '@ohos/base/src/main/ets/data/SimpleAlbumDataItem';
33import { LazyItem } from '@ohos/base/src/main/ets/vm/ItemDataSource';
34
35const TAG = "MediaOperationPage"
36
37@Entry
38@Component
39struct MediaOperationPage {
40    @StorageLink('screenColumns') screenColumns: number = screenManager.getScreenColumns();
41    @StorageLink('leftBlank') leftBlank: [number, number, number, number] = [0, 0, 0, 0];
42    @Provide pageType: string = MediaOperationType.Move;
43    @Provide broadCast: Broadcast = new Broadcast();
44    @Provide moreMenuList: Action[] = new Array<Action>();
45    @Provide loadingFinish: boolean = false;
46    @State listCardWidth: number = 0;
47    isActive: boolean = false; // Whether the page is in the foreground
48    sourceAlbumId: string;
49    private albumInfo: SimpleAlbumDataItem;
50    private appBroadcast: Broadcast = broadcastManager.getBroadcast();
51    private albumsDataSource: AlbumsDataSource = new AlbumsDataSource()
52    private scroller: Scroller = new Scroller();
53
54    aboutToAppear(): void {
55        let blackList = Array.from(MediaConstants.ALBUM_DISABLE_COPY_LIST)
56        let param = router.getParams();
57        if (param) {
58            this.pageType = param.pageType.toString();
59            if (param.albumInfo) {
60                this.albumInfo = JSON.parse(param.albumInfo.toString()) as SimpleAlbumDataItem;
61                this.sourceAlbumId = this.albumInfo.id;
62                blackList.push(this.sourceAlbumId)
63                Log.info(TAG, `router getParams pageType:${this.pageType},Album:${this.sourceAlbumId}`);
64            }
65        }
66        this.albumsDataSource.setBlackList(blackList)
67        this.onActive();
68        this.loadAlbums()
69        this.onMenuClicked = this.onMenuClicked.bind(this);
70
71        this.broadCast.on(BroadcastConstants.MEDIA_OPERATION, (albumInfo: SimpleAlbumDataItem, completedFunc?: Function) => {
72            router.back({
73                uri: '',
74                params: {
75                    pageType: this.pageType,
76                    albumInfo: JSON.stringify(albumInfo)
77                }
78            });
79            completedFunc && completedFunc();
80        });
81        this.updateListCardWidth();
82    }
83
84    private loadAlbums() {
85        this.albumsDataSource.reloadAlbumListItemData().then((isEmpty: boolean) => {
86            this.loadingFinish = true
87            this.albumsDataSource.notifyDataReload()
88        })
89    }
90
91    onMenuClicked(action: Action) {
92        Log.info(TAG, `onMenuClicked, actionID: ${action.actionID}`);
93
94        switch (action) {
95            case Action.CANCEL:
96                router.back({
97                    uri: '',
98                    params: {}
99                });
100                break;
101            case Action.NEW:
102                this.createNewAlbum();
103                break;
104            default:
105                break;
106        }
107    }
108
109    createNewAlbum() {
110        Log.info(TAG, 'createNewAlbum');
111        let menuContext = new MenuContext();
112        this.onOperationStart = this.onOperationStart.bind(this);
113        this.onOperationEnd = this.onOperationEnd.bind(this);
114        menuContext
115            .withOperationStartCallback(this.onOperationStart)
116            .withOperationEndCallback(this.onOperationEnd)
117            .withDataSource(this.albumsDataSource)
118            .withAlbumInfo(this.albumInfo)
119            .withBroadCast(this.broadCast)
120            .withJumpSourceToMain(JumpSourceToMain.ALBUM);
121        let menuOperation = new AlbumSetNewMenuOperation(menuContext);
122        menuOperation.doAction();
123    }
124
125    onOperationStart() {
126    }
127
128    onOperationEnd() {
129    }
130
131    updateListCardWidth(): void {
132        Log.info(TAG, `updateListCardWidth start ${this.screenColumns}`);
133        if (this.screenColumns == ColumnSize.COLUMN_FOUR) {
134            this.listCardWidth = screenManager.getColumnsWidth(ColumnSize.COLUMN_FOUR);
135        } else if (this.screenColumns == ColumnSize.COLUMN_EIGHT) {
136            this.listCardWidth = screenManager.getColumnsWidth(ColumnSize.COLUMN_SIX);
137        } else if (this.screenColumns == ColumnSize.COLUMN_TWELVE) {
138            this.listCardWidth = screenManager.getColumnsWidth(ColumnSize.COLUMN_EIGHT);
139        } else {
140            Log.warn(TAG, 'screenColumns is not init');
141        }
142        Log.info(TAG, `updateListCardWidth ${this.listCardWidth}`);
143    }
144
145    // Callback when the page is in the foreground
146    onActive() {
147        if (!this.isActive) {
148            Log.info(TAG, 'onActive');
149            this.isActive = true;
150        }
151    }
152
153    onBackPress() {
154        Log.info(TAG, 'onBackPress');
155        router.back({
156            uri: '',
157            params: {}
158        });
159        return true;
160    }
161
162    onPageShow() {
163        this.appBroadcast.emit(BroadcastConstants.THIRD_ROUTE_PAGE, []);
164    }
165
166    build() {
167        Column() {
168            MediaOperationActionBar({ onMenuClicked: this.onMenuClicked })
169            List({ scroller: this.scroller }) {
170                ListItem() {
171                    Flex({
172                        direction: FlexDirection.Column,
173                        justifyContent: FlexAlign.Center,
174                        alignItems: ItemAlign.Center
175                    }) {
176                        List() {
177                            LazyForEach(this.albumsDataSource, (item: LazyItem<AlbumDataItem>) => {
178                                ListItem() {
179                                    AlbumListCard({ item: item.get() })
180                                }
181                            }, (item: LazyItem<AlbumDataItem>) => item == null && item.get() ? JSON.stringify(item) : item.getHashCode())
182                        }
183                        .divider({
184                            strokeWidth: 1,
185                            startMargin: $r('app.float.album_list_card_divider_margin_left')
186                        })
187                        .borderRadius($r('sys.float.ohos_id_corner_radius_default_l'))
188                        .backgroundColor($r('sys.color.ohos_id_color_card_bg'))
189                        .padding({
190                            left: $r('app.float.list_card_margin'),
191                            right: $r('app.float.list_card_margin')
192                        })
193                        .width(this.listCardWidth)
194                    }.width('100%')
195                }.width('100%')
196            }
197            .scrollBar(BarState.Auto)
198            .width('100%')
199            .margin({
200                top: $r('app.float.album_let_page_padding_top'),
201                left: $r('app.float.max_padding_start'),
202                right: $r('app.float.max_padding_end'),
203                bottom: px2vp(this.leftBlank[3])
204            })
205            CustomDialogView()
206        }
207        .backgroundColor($r('app.color.default_background_color')) // ux: colorSubBackground
208        .height('100%')
209        .padding({
210            top: px2vp(this.leftBlank[1]),
211            bottom: px2vp(this.leftBlank[3])
212        })
213    }
214
215    pageTransition() {
216        PageTransitionEnter({ type: RouteType.None, duration: 1 })
217            .opacity(0)
218        PageTransitionExit({ type: RouteType.None, duration: 1 })
219            .opacity(0)
220    }
221}