1/**
2 * Copyright (c) 2021-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    AppStorage.setOrCreate('recentIdx', 0);
46    this.recentLoadedCompleted = true;
47  }
48
49  onPageHide(): void {
50    Log.showInfo(TAG, 'onPageHide');
51    this.recentLoadedCompleted = false;
52    this.isClickSubComponent = false;
53    this.mRecentMissionsStage.onDestroy();
54  }
55
56  onBackPress(): boolean {
57    Log.showInfo(TAG, 'RecentMission EntryView onBackPress');
58    windowManager.hideWindow(windowManager.RECENT_WINDOW_NAME);
59    return true;
60  }
61
62  build() {
63    Column() {
64      if (this.recentMissionsList && this.mRecentMissionsViewModel && this.mRecentMissionsRowType) {
65        if (this.recentMissionsList.length) {
66          if (this.mRecentMissionsRowType === 'single') {
67            RecentMissionsSingleLayout({ recentMissionsSingleList: $recentMissionsList,
68              isClickSubComponent: $isClickSubComponent});
69          }
70          if (this.mRecentMissionsRowType === 'double') {
71            RecentMissionsDoubleLayout({ recentMissionsDoubleList: $recentMissionsList,
72              isClickSubComponent: $isClickSubComponent});
73          }
74        } else {
75          if (this.recentLoadedCompleted) {
76            Text($r('app.string.No_running_apps_recently'))
77              .fontColor(RecentsStyleConstants.DEFAULT_FONT_COLOR)
78              .fontSize(RecentsStyleConstants.DEFAULT_FONT_SIZE)
79          }
80        }
81      }
82      if (this.traceBuildEnd()) { }
83    }
84    .alignItems(HorizontalAlign.Center)
85    .justifyContent(FlexAlign.Center)
86    .width(RecentsStyleConstants.DEFAULT_LAYOUT_PERCENTAGE)
87    .height(RecentsStyleConstants.DEFAULT_LAYOUT_PERCENTAGE)
88    .backgroundImage(RecentsStyleConstants.DEFAULT_RECENT_BACKGROUND_IMAGE)
89    .onClick(() => {
90      if (!this.isClickSubComponent) {
91        Log.showInfo(TAG, 'click recent missions area');
92        RecentMissionsViewModel.getInstance().backView();
93      }
94    })
95  }
96
97  private traceBuildEnd(): boolean {
98    Trace.end(Trace.CORE_METHOD_START_RECENTS);
99    return true;
100  }
101}