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 { Log } from '@ohos/common';
17import { Trace } from '@ohos/common';
18import { windowManager } from '@ohos/common';
19import { RecentMissionInfo } from '@ohos/common';
20import { RecentsStyleConstants } from '@ohos/recents';
21import { RecentMissionsStage } from '@ohos/recents';
22import { RecentMissionsViewModel } from '@ohos/recents';
23import { RecentMissionsSingleLayout } from '@ohos/recents/component';
24import { RecentMissionsDoubleLayout } from '@ohos/recents/component';
25
26const TAG = 'RecentView';
27
28@Entry
29@Component
30struct RecentView {
31  @StorageLink('recentMissionsList') recentMissionsList: RecentMissionInfo[] = [];
32  @State isClickSubComponent: boolean = false;
33  private mRecentMissionsStage: RecentMissionsStage = new RecentMissionsStage();
34  private mRecentMissionsViewModel?: RecentMissionsViewModel;
35  @State mRecentMissionsRowType: string = '';
36  @State recentLoadedCompleted: boolean = false;
37
38  onPageShow(): void {
39    Log.showInfo(TAG, 'onPageShow');
40    this.isClickSubComponent = false;
41    this.mRecentMissionsStage.onCreate();
42    this.mRecentMissionsViewModel = RecentMissionsViewModel.getInstance();
43    this.mRecentMissionsRowType = this.mRecentMissionsViewModel.getRecentMissionsRowType();
44    this.mRecentMissionsViewModel.getRecentMissionsList();
45    this.closeRecentDockPopup();
46  }
47
48  onPageHide(): void {
49    Log.showInfo(TAG, 'onPageHide');
50    this.recentLoadedCompleted = false;
51    this.isClickSubComponent = false;
52    this.mRecentMissionsStage.onDestroy();
53  }
54
55  onBackPress(): boolean {
56    Log.showInfo(TAG, 'RecentMission EntryView onBackPress');
57    windowManager.hideWindow(windowManager.RECENT_WINDOW_NAME);
58    return true;
59  }
60
61  private closeRecentDockPopup(): void {
62    let num: number = AppStorage.get('sysUiRecentOnClickEvent') as number;
63    if (!num) {
64      num = 0;
65    }
66    AppStorage.setOrCreate('sysUiRecentOnClickEvent', ++num);
67    Log.showDebug(TAG, `closeRecentDockPopup sysUiRecentOnClickEvent closeRecentDockPopup num: ${num}`);
68  }
69
70  build() {
71    Column() {
72      if (this.recentMissionsList && this.mRecentMissionsViewModel && this.mRecentMissionsRowType) {
73        if (this.recentMissionsList.length) {
74          if (this.mRecentMissionsRowType === 'single') {
75            RecentMissionsSingleLayout({ recentMissionsSingleList: $recentMissionsList,
76              isClickSubComponent: $isClickSubComponent});
77          }
78          if (this.mRecentMissionsRowType === 'double') {
79            RecentMissionsDoubleLayout({ recentMissionsDoubleList: $recentMissionsList,
80              isClickSubComponent: $isClickSubComponent});
81          }
82        } else {
83          if (this.recentLoadedCompleted) {
84            Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
85              Text($r('app.string.No_running_apps_recently'))
86                .fontColor(RecentsStyleConstants.DEFAULT_FONT_COLOR)
87                .fontSize(RecentsStyleConstants.DEFAULT_FONT_SIZE)
88            }
89          }
90        }
91      }
92
93      if (this.traceBuildEnd()) {}
94    }
95    .width(RecentsStyleConstants.DEFAULT_LAYOUT_PERCENTAGE)
96    .height(RecentsStyleConstants.DEFAULT_LAYOUT_PERCENTAGE)
97    .backgroundImage(RecentsStyleConstants.DEFAULT_RECENT_BACKGROUND_IMAGE)
98    .onClick(() => {
99      if (!this.isClickSubComponent) {
100        Log.showInfo(TAG, 'click recent missions area');
101        this.mRecentMissionsViewModel?.backView && this.mRecentMissionsViewModel.backView();
102      }
103    })
104  }
105
106  private traceBuildEnd(): boolean {
107    Trace.end(Trace.CORE_METHOD_START_RECENTS);
108    return true;
109  }
110}
111